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:
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,23 +82,28 @@ 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{}
|
||||||
for i, line in lines {
|
for i, line in lines {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
words := line.split(':')
|
words := line.split(':')
|
||||||
if words.len != 2 {
|
if words.len != 2 {
|
||||||
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"
|
||||||
@ -128,12 +117,12 @@ pub fn run<T>(port int) {
|
|||||||
action = 'index'
|
action = 'index'
|
||||||
}
|
}
|
||||||
req := http.Request{
|
req := http.Request{
|
||||||
headers: map[string]string{}
|
headers: map[string]string{}
|
||||||
headers2: headers
|
headers2: headers
|
||||||
ws_func: 0
|
ws_func: 0
|
||||||
user_ptr: 0
|
user_ptr: 0
|
||||||
method: vals[0]
|
method: vals[0]
|
||||||
url: vals[1]
|
url: vals[1]
|
||||||
}
|
}
|
||||||
println('vweb action = "$action"')
|
println('vweb action = "$action"')
|
||||||
//mut app := T{
|
//mut app := T{
|
||||||
@ -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
Block a user