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