vweb: fix V panic: array index out of range: 1/0
parent
72a7eb6e35
commit
d526cfc205
|
@ -24,28 +24,17 @@ pub:
|
||||||
|
|
||||||
pub fn (ctx Context) text(s string) {
|
pub fn (ctx Context) text(s string) {
|
||||||
h := ctx.headers.join('\n')
|
h := ctx.headers.join('\n')
|
||||||
ctx.conn.write('HTTP/1.1 200 OK
|
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/plain\n$h\n$s')
|
||||||
Content-Type: text/plain
|
|
||||||
$h
|
|
||||||
$s
|
|
||||||
')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) json(s string) {
|
pub fn (ctx Context) json(s string) {
|
||||||
h := ctx.headers.join('\n')
|
h := ctx.headers.join('\n')
|
||||||
ctx.conn.write('HTTP/1.1 200 OK
|
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: application/json\n$h\n$s')
|
||||||
Content-Type: application/json
|
|
||||||
$h
|
|
||||||
$s
|
|
||||||
')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) redirect(url string) {
|
pub fn (ctx Context) redirect(url string) {
|
||||||
h := ctx.headers.join('\n')
|
h := ctx.headers.join('\n')
|
||||||
ctx.conn.write('HTTP/1.1 302 Found
|
ctx.conn.write('HTTP/1.1 302 Found\nLocation: $url\n$h')
|
||||||
Location: $url
|
|
||||||
$h
|
|
||||||
')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) not_found(s string) {
|
pub fn (ctx Context) not_found(s string) {
|
||||||
|
@ -64,11 +53,11 @@ pub fn (ctx Context) get_cookie(key string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
/*
|
/*
|
||||||
cookie := ctx.req.headers['Cookie']
|
cookie := ctx.req.headers['Cookie']
|
||||||
println('get cookie $key : "$cookie"')
|
println('get cookie $key : "$cookie"')
|
||||||
return cookie.find_between('$key=', ';')
|
return cookie.find_between('$key=', ';')
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (ctx mut Context) set_header(key, val string) {
|
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) {
|
pub fn (ctx Context) html(html string) {
|
||||||
h := ctx.headers.join('\n')
|
h := ctx.headers.join('\n')
|
||||||
ctx.conn.write('HTTP/1.1 200 OK
|
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/html\n$h\n\n$html')
|
||||||
Content-Type: text/html
|
|
||||||
$h
|
|
||||||
|
|
||||||
$html
|
|
||||||
')
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +82,11 @@ pub fn run<T>(port int) {
|
||||||
}
|
}
|
||||||
// TODO move this to handle_conn<T>(conn, app)
|
// TODO move this to handle_conn<T>(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
|
// Parse request headers
|
||||||
lines := s.split_into_lines()
|
lines := s.split_into_lines()
|
||||||
mut headers := []string //map[string]string{}
|
mut headers := []string //map[string]string{}
|
||||||
|
@ -110,11 +99,11 @@ pub fn run<T>(port int) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
headers << line
|
headers << line
|
||||||
/*
|
/*
|
||||||
key := words[0]
|
key := words[0]
|
||||||
val := words[1]
|
val := words[1]
|
||||||
headers[key] = val
|
headers[key] = val
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
// Parse the first line
|
// Parse the first line
|
||||||
// "GET / HTTP/1.1"
|
// "GET / HTTP/1.1"
|
||||||
|
@ -162,11 +151,7 @@ pub fn run<T>(port int) {
|
||||||
|
|
||||||
// Call the right action
|
// Call the right action
|
||||||
app.$action() or {
|
app.$action() or {
|
||||||
conn.write('HTTP/1.1 404 Not Found
|
conn.write('HTTP/1.1 404 Not Found \nContent-Type: text/plain \n\n404 not found')
|
||||||
Content-Type: text/plain
|
|
||||||
|
|
||||||
404 not found
|
|
||||||
')
|
|
||||||
}
|
}
|
||||||
conn.close()
|
conn.close()
|
||||||
}
|
}
|
||||||
|
@ -246,11 +231,7 @@ pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
||||||
|
|
||||||
if static_file != '' {
|
if static_file != '' {
|
||||||
data := os.read_file(static_file) or { return false }
|
data := os.read_file(static_file) or { return false }
|
||||||
ctx.conn.write('HTTP/1.1 200 OK
|
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: $mime_type\n\n$data')
|
||||||
Content-Type: $mime_type
|
|
||||||
|
|
||||||
$data
|
|
||||||
')
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
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_files[url] = file_path
|
||||||
ctx.static_mime_types[url] = mime_type
|
ctx.static_mime_types[url] = mime_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue