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

vweb: fix headers and cookies

This commit is contained in:
Alexander Medvednikov 2019-12-07 00:44:22 +03:00
parent cc834dd7a7
commit 329485d4b6
3 changed files with 33 additions and 9 deletions

View File

@ -27,8 +27,7 @@ pub fn (app & App) json_endpoint() {
}
pub fn (app mut App) index() {
app.cnt ++
app.cnt++
$vweb.html()
}

View File

@ -312,6 +312,19 @@ pub fn (s Socket) read_line() string {
return res
}
// TODO
pub fn (s Socket) read_all() string {
mut buf := [MAX_READ]byte // where C.recv will store the network data
mut res := '' // The final result, including the ending \n.
for {
n := C.recv(s.sockfd, buf, MAX_READ-1, 0)
if n == -1 { return res }
if n == 0 { return res }
res += tos_clone(buf)
}
return res
}
pub fn (s Socket) get_port() int {
mut addr := C.sockaddr_in {}
size := 16 // sizeof(sockaddr_in)

View File

@ -44,7 +44,7 @@ mut:
}
pub fn (ctx Context) html(html string) {
//println('$html HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n$ctx.headers\r\n\r\n$html')
//println('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n$ctx.headers\r\n\r\n$html')
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n$ctx.headers\r\n\r\n$html') or { panic(err) }
}
@ -74,7 +74,8 @@ pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
cookie := if cookie_header.contains(';') {
cookie_header.find_between('$key=', ';')
} else {
cookie_header
cookie_header.find_between('$key=', '\r')
//cookie_header
}
if cookie != '' {
return cookie
@ -90,7 +91,7 @@ fn (ctx mut Context) add_header(key, val string) {
}
fn (ctx &Context) get_header(key string) string {
return ctx.headers.find_between('\r\n$key: ', '\r\n')
return ctx.req.headers[key]
}
//pub fn run<T>(port int) {
@ -105,15 +106,15 @@ pub fn run<T>(app mut T, port int) {
}
//foobar<T>()
// TODO move this to handle_conn<T>(conn, app)
s := conn.read_line()
if s == '' {
first_line:= conn.read_line()
if first_line == '' {
conn.write(HTTP_500) or {}
conn.close() or {}
return
}
// Parse the first line
// "GET / HTTP/1.1"
first_line := s.all_before('\n')
//first_line := s.all_before('\n')
vals := first_line.split(' ')
if vals.len < 2 {
println('no vals for http')
@ -121,6 +122,15 @@ pub fn run<T>(app mut T, port int) {
conn.close() or {}
return
}
mut headers := []string
for _ in 0..30 {
header := conn.read_line()
headers << header
//println('header="$header" len = ' + header.len.str())
if header.len <= 2 {
break
}
}
mut action := vals[1][1..].all_before('/')
if action.contains('?') {
action = action.all_before('?')
@ -129,13 +139,15 @@ pub fn run<T>(app mut T, port int) {
action = 'index'
}
req := http.Request{
headers: http.parse_headers(s.split_into_lines())
headers: http.parse_headers(headers) //s.split_into_lines())
ws_func: 0
user_ptr: 0
method: vals[0]
url: vals[1]
}
$if debug {
println('req.headers = ')
println(req.headers)
println('vweb action = "$action"')
}
//mut app := T{