vweb: parse request headers

pull/1408/head
Alexander Medvednikov 2019-08-01 18:36:36 +02:00
parent 3580179602
commit b57752e67e
2 changed files with 33 additions and 2 deletions

View File

@ -6,8 +6,8 @@ module http
struct Request {
pub:
// headers []string
headers map_string
headers2 []string
headers map[string]string
method string
// cookies map[string]string
h string

View File

@ -50,8 +50,18 @@ pub fn (ctx mut Context) set_cookie(key, val string) {
}
pub fn (ctx Context) get_cookie(key string) string {
for h in ctx.req.headers2 {
if h.starts_with('Cookie:') {
cookie := h.right(7)
return cookie.find_between('$key=', ';')
}
}
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) {
@ -61,8 +71,10 @@ fn (ctx mut Context) set_header(key, val string) {
pub fn (ctx Context) html(html string) {
//tmpl := os.read_file(path) or {return}
h := ctx.headers.join('\n')
ctx.conn.write('HTTP/1.1 200 OK
Content-Type: text/html
$h
$html
')
@ -79,6 +91,24 @@ pub fn run<T>(port int) {
}
// TODO move this to handle_conn<T>(conn, app)
s := conn.read_line()
// 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
}
headers << line
/*
key := words[0]
val := words[1]
headers[key] = val
*/
}
// Parse the first line
// "GET / HTTP/1.1"
first_line := s.all_before('\n')
@ -92,6 +122,7 @@ pub fn run<T>(port int) {
}
req := http.Request{
headers: map[string]string{}
headers2: headers
ws_func: 0
user_ptr: 0
method: vals[0]