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

vweb: fix V panic: array index out of range: 1/0

This commit is contained in:
Carlos Esquerdo Bernat 2019-08-11 14:07:22 +02:00 committed by Alexander Medvednikov
parent 72a7eb6e35
commit d526cfc205

View File

@ -24,28 +24,17 @@ pub:
pub fn (ctx Context) text(s string) {
h := ctx.headers.join('\n')
ctx.conn.write('HTTP/1.1 200 OK
Content-Type: text/plain
$h
$s
')
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/plain\n$h\n$s')
}
pub fn (ctx Context) json(s string) {
h := ctx.headers.join('\n')
ctx.conn.write('HTTP/1.1 200 OK
Content-Type: application/json
$h
$s
')
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: application/json\n$h\n$s')
}
pub fn (ctx Context) redirect(url string) {
h := ctx.headers.join('\n')
ctx.conn.write('HTTP/1.1 302 Found
Location: $url
$h
')
ctx.conn.write('HTTP/1.1 302 Found\nLocation: $url\n$h')
}
pub fn (ctx Context) not_found(s string) {
@ -78,12 +67,7 @@ fn (ctx mut Context) set_header(key, val string) {
pub fn (ctx Context) html(html string) {
h := ctx.headers.join('\n')
ctx.conn.write('HTTP/1.1 200 OK
Content-Type: text/html
$h
$html
')
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/html\n$h\n\n$html')
}
@ -98,6 +82,11 @@ pub fn run<T>(port int) {
}
// TODO move this to handle_conn<T>(conn, app)
s := conn.read_line()
if s == '' {
conn.write('HTTP/1.1 500 Not Found \nContent-Type: text/plain \n\n500')
conn.close()
continue
}
// Parse request headers
lines := s.split_into_lines()
mut headers := []string //map[string]string{}
@ -162,11 +151,7 @@ pub fn run<T>(port int) {
// Call the right action
app.$action() or {
conn.write('HTTP/1.1 404 Not Found
Content-Type: text/plain
404 not found
')
conn.write('HTTP/1.1 404 Not Found \nContent-Type: text/plain \n\n404 not found')
}
conn.close()
}
@ -246,11 +231,7 @@ pub fn (ctx mut Context) handle_static(directory_path string) bool {
if static_file != '' {
data := os.read_file(static_file) or { return false }
ctx.conn.write('HTTP/1.1 200 OK
Content-Type: $mime_type
$data
')
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: $mime_type\n\n$data')
return true
}
return false
@ -260,5 +241,3 @@ pub fn (ctx mut Context) serve_static(url, file_path, mime_type string) {
ctx.static_files[url] = file_path
ctx.static_mime_types[url] = mime_type
}