From b57752e67e1ad0d4abbfce78f479eb7fc2ff883a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 1 Aug 2019 18:36:36 +0200 Subject: [PATCH] vweb: parse request headers --- vlib/http/http.v | 4 ++-- vlib/vweb/vweb.v | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/vlib/http/http.v b/vlib/http/http.v index e2a29a8ccd..ba72e01c85 100644 --- a/vlib/http/http.v +++ b/vlib/http/http.v @@ -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 diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 27f01e77a4..87548d2b49 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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(port int) { } // TODO move this to handle_conn(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(port int) { } req := http.Request{ headers: map[string]string{} + headers2: headers ws_func: 0 user_ptr: 0 method: vals[0]