From d526cfc205dfd31a0cf4d585e43a6efcf7255135 Mon Sep 17 00:00:00 2001 From: Carlos Esquerdo Bernat Date: Sun, 11 Aug 2019 14:07:22 +0200 Subject: [PATCH] vweb: fix V panic: array index out of range: 1/0 --- vlib/vweb/vweb.v | 87 ++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 54 deletions(-) diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 8ee1a57881..a006e79592 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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) { @@ -64,11 +53,11 @@ pub fn (ctx Context) get_cookie(key string) string { } } return '' -/* + /* cookie := ctx.req.headers['Cookie'] println('get cookie $key : "$cookie"') return cookie.find_between('$key=', ';') -*/ + */ } fn (ctx mut Context) set_header(key, val 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') } @@ -97,24 +81,29 @@ pub fn run(port int) { panic('accept() failed') } // TODO move this to handle_conn(conn, app) - s := conn.read_line() + 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{} for i, line in lines { - if i == 0 { - continue - } - words := line.split(':') - if words.len != 2 { - continue - } + if i == 0 { + continue + } + words := line.split(':') + if words.len != 2 { + continue + } headers << line -/* - key := words[0] - val := words[1] - headers[key] = val -*/ + /* + key := words[0] + val := words[1] + headers[key] = val + */ } // Parse the first line // "GET / HTTP/1.1" @@ -128,12 +117,12 @@ pub fn run(port int) { action = 'index' } req := http.Request{ - headers: map[string]string{} + headers: map[string]string{} headers2: headers - ws_func: 0 - user_ptr: 0 - method: vals[0] - url: vals[1] + ws_func: 0 + user_ptr: 0 + method: vals[0] + url: vals[1] } println('vweb action = "$action"') //mut app := T{ @@ -162,12 +151,8 @@ pub fn run(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 @@ -259,6 +240,4 @@ $data 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 -} - - +}