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

vweb: 404

This commit is contained in:
Alexander Medvednikov 2019-08-02 04:04:48 +02:00
parent c145a5cd7d
commit f81562ee7e
2 changed files with 27 additions and 2 deletions

View File

@ -207,5 +207,11 @@ fn (p mut Parser) comptime_method_call(typ Type) {
} }
p.check(.lpar) p.check(.lpar)
p.check(.rpar) p.check(.rpar)
if p.tok == .key_orelse {
p.check(.key_orelse)
p.genln('else {')
p.check(.lcbr)
p.statements()
}
} }

View File

@ -45,6 +45,10 @@ $h
') ')
} }
pub fn (ctx Context) not_found(s string) {
ctx.conn.write('HTTP/1.1 404 Not Found')
}
pub fn (ctx mut Context) set_cookie(key, val string) { pub fn (ctx mut Context) set_cookie(key, val string) {
ctx.set_header('Set-Cookie', '$key=$val') ctx.set_header('Set-Cookie', '$key=$val')
} }
@ -117,6 +121,12 @@ pub fn run<T>(port int) {
if action.contains('?') { if action.contains('?') {
action = action.all_before('?') action = action.all_before('?')
} }
if action == 'favicon.ico' {
println('favicon.ico')
conn.write('HTTP/1.1 404 Not Found')
conn.close()
continue
}
if action == '' { if action == '' {
action = 'index' action = 'index'
} }
@ -144,7 +154,8 @@ pub fn run<T>(port int) {
println('vweb action = "$action"') println('vweb action = "$action"')
if vals.len < 2 { if vals.len < 2 {
println('no vals for http') println('no vals for http')
return conn.close()
continue
} }
// Serve a static file if it's one // Serve a static file if it's one
@ -154,7 +165,13 @@ pub fn run<T>(port int) {
// } // }
// Call the right action // Call the right action
app.$action() app.$action() or {
conn.write('HTTP/1.1 404 Not Found
Content-Type: text/plain
404 not found
')
}
conn.close() conn.close()
} }
} }
@ -170,7 +187,9 @@ fn (ctx mut Context) parse_form(s string) {
str_form = str_form.replace('+', ' ') str_form = str_form.replace('+', ' ')
words := str_form.split('&') words := str_form.split('&')
for word in words { for word in words {
println('parse form keyval="$word"')
keyval := word.split('=') keyval := word.split('=')
if keyval.len != 2 { continue }
key := keyval[0] key := keyval[0]
val := keyval[1] val := keyval[1]
//println('http form $key => $val') //println('http form $key => $val')