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:
parent
cc834dd7a7
commit
329485d4b6
@ -27,8 +27,7 @@ pub fn (app & App) json_endpoint() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (app mut App) index() {
|
pub fn (app mut App) index() {
|
||||||
app.cnt ++
|
app.cnt++
|
||||||
|
|
||||||
$vweb.html()
|
$vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,6 +312,19 @@ pub fn (s Socket) read_line() string {
|
|||||||
return res
|
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 {
|
pub fn (s Socket) get_port() int {
|
||||||
mut addr := C.sockaddr_in {}
|
mut addr := C.sockaddr_in {}
|
||||||
size := 16 // sizeof(sockaddr_in)
|
size := 16 // sizeof(sockaddr_in)
|
||||||
|
@ -44,7 +44,7 @@ mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) html(html string) {
|
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) }
|
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 := if cookie_header.contains(';') {
|
||||||
cookie_header.find_between('$key=', ';')
|
cookie_header.find_between('$key=', ';')
|
||||||
} else {
|
} else {
|
||||||
cookie_header
|
cookie_header.find_between('$key=', '\r')
|
||||||
|
//cookie_header
|
||||||
}
|
}
|
||||||
if cookie != '' {
|
if cookie != '' {
|
||||||
return cookie
|
return cookie
|
||||||
@ -90,7 +91,7 @@ fn (ctx mut Context) add_header(key, val string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (ctx &Context) get_header(key string) 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) {
|
//pub fn run<T>(port int) {
|
||||||
@ -105,15 +106,15 @@ pub fn run<T>(app mut T, port int) {
|
|||||||
}
|
}
|
||||||
//foobar<T>()
|
//foobar<T>()
|
||||||
// TODO move this to handle_conn<T>(conn, app)
|
// TODO move this to handle_conn<T>(conn, app)
|
||||||
s := conn.read_line()
|
first_line:= conn.read_line()
|
||||||
if s == '' {
|
if first_line == '' {
|
||||||
conn.write(HTTP_500) or {}
|
conn.write(HTTP_500) or {}
|
||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Parse the first line
|
// Parse the first line
|
||||||
// "GET / HTTP/1.1"
|
// "GET / HTTP/1.1"
|
||||||
first_line := s.all_before('\n')
|
//first_line := s.all_before('\n')
|
||||||
vals := first_line.split(' ')
|
vals := first_line.split(' ')
|
||||||
if vals.len < 2 {
|
if vals.len < 2 {
|
||||||
println('no vals for http')
|
println('no vals for http')
|
||||||
@ -121,6 +122,15 @@ pub fn run<T>(app mut T, port int) {
|
|||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
return
|
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('/')
|
mut action := vals[1][1..].all_before('/')
|
||||||
if action.contains('?') {
|
if action.contains('?') {
|
||||||
action = action.all_before('?')
|
action = action.all_before('?')
|
||||||
@ -129,13 +139,15 @@ pub fn run<T>(app mut T, port int) {
|
|||||||
action = 'index'
|
action = 'index'
|
||||||
}
|
}
|
||||||
req := http.Request{
|
req := http.Request{
|
||||||
headers: http.parse_headers(s.split_into_lines())
|
headers: http.parse_headers(headers) //s.split_into_lines())
|
||||||
ws_func: 0
|
ws_func: 0
|
||||||
user_ptr: 0
|
user_ptr: 0
|
||||||
method: vals[0]
|
method: vals[0]
|
||||||
url: vals[1]
|
url: vals[1]
|
||||||
}
|
}
|
||||||
$if debug {
|
$if debug {
|
||||||
|
println('req.headers = ')
|
||||||
|
println(req.headers)
|
||||||
println('vweb action = "$action"')
|
println('vweb action = "$action"')
|
||||||
}
|
}
|
||||||
//mut app := T{
|
//mut app := T{
|
||||||
|
Loading…
Reference in New Issue
Block a user