1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: add set_status() (#6087)

This commit is contained in:
Sandro Martini 2020-08-09 18:05:06 +02:00 committed by GitHub
parent 2dd90de993
commit c7fae4dd6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,6 +42,7 @@ mut:
static_files map[string]string
static_mime_types map[string]string
content_type string = 'text/plain'
status string = '200 OK'
pub:
req http.Request
conn net.Socket
@ -71,8 +72,9 @@ fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool {
ctx.done = true
mut sb := strings.new_builder(1024)
defer { sb.free() }
sb.write('HTTP/1.1 200 OK\r\nContent-Type: ') sb.write(mimetype)
sb.write('\r\nContent-Length: ') sb.write(res.len.str())
sb.write('HTTP/1.1 ${ctx.status}')
sb.write('\r\nContent-Type: ${mimetype}')
sb.write('\r\nContent-Length: ${res.len}')
sb.write(ctx.headers)
sb.write('\r\n')
sb.write(headers_close)
@ -164,6 +166,14 @@ pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
return error('Cookie not found')
}
pub fn (mut ctx Context) set_status(code int, desc string) {
if code < 100 || code > 599 {
ctx.status = '500 Internal Server Error'
} else {
ctx.status = '$code $desc'
}
}
pub fn (mut ctx Context) add_header(key, val string) {
//println('add_header($key, $val)')
ctx.headers = ctx.headers + '\r\n$key: $val'