mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb: fix headers
This commit is contained in:
parent
f83bc9528d
commit
8a77d4482c
@ -33,3 +33,9 @@ pub fn (app mut App) text() {
|
||||
app.vweb.text('hello world')
|
||||
}
|
||||
|
||||
pub fn (app mut App) cookie() {
|
||||
app.vweb.text('Headers:')
|
||||
app.vweb.set_cookie('cookie', 'test')
|
||||
app.vweb.text(app.vweb.headers)
|
||||
app.vweb.text('Text: hello world')
|
||||
}
|
@ -13,7 +13,6 @@ const (
|
||||
|
||||
struct Request {
|
||||
pub:
|
||||
headers2 []string
|
||||
headers map[string]string
|
||||
method string
|
||||
// cookies map[string]string
|
||||
@ -98,6 +97,21 @@ pub fn (req mut Request) add_header(key, val string) {
|
||||
// req.h = h
|
||||
}
|
||||
|
||||
pub fn parse_headers(lines []string) map[string]string {
|
||||
mut headers := map[string]string
|
||||
for i, line in lines {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
words := line.split(': ')
|
||||
if words.len != 2 {
|
||||
continue
|
||||
}
|
||||
headers[words[0]] = words[1]
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
pub fn (req &Request) do() ?Response {
|
||||
if req.typ == 'POST' {
|
||||
// req.headers << 'Content-Type: application/x-www-form-urlencoded'
|
||||
|
@ -19,7 +19,6 @@ fn test_http_get() {
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
fn test_http_get_from_vlang_utc_now() {
|
||||
/*
|
||||
urls := ['http://vlang.io/utc_now', 'https://vlang.io/utc_now']
|
||||
|
157
vlib/vweb/vweb.v
157
vlib/vweb/vweb.v
@ -10,6 +10,22 @@ import (
|
||||
|
||||
const (
|
||||
methods_with_form = ['POST', 'PUT', 'PATCH']
|
||||
HEADER_SERVER = 'Server: VWeb\r\n' // TODO add to the headers
|
||||
HTTP_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found'
|
||||
HTTP_500 = 'HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n500 Internal Server Error'
|
||||
mime_types = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.gif': 'image/gif',
|
||||
'.htm': 'text/html; charset=utf-8',
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.js': 'application/javascript',
|
||||
'.wasm': 'application/wasm',
|
||||
'.pdf': 'application/pdf',
|
||||
'.png': 'image/png',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.xml': 'text/xml; charset=utf-8'
|
||||
}
|
||||
)
|
||||
|
||||
struct Context {
|
||||
@ -21,56 +37,52 @@ pub:
|
||||
form map[string]string
|
||||
// TODO Response
|
||||
mut:
|
||||
headers []string // response headers
|
||||
}
|
||||
|
||||
pub fn (ctx Context) text(s string) {
|
||||
h := ctx.headers.join('\n')
|
||||
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/plain\n$h\n$s')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) json(s string) {
|
||||
h := ctx.headers.join('\n')
|
||||
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: application/json\n$h\n$s')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) redirect(url string) {
|
||||
h := ctx.headers.join('\n')
|
||||
ctx.conn.write('HTTP/1.1 302 Found\nLocation: $url\n$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) {
|
||||
ctx.set_header('Set-Cookie', '$key=$val')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) get_cookie(key string) string {
|
||||
for h in ctx.req.headers2 {
|
||||
if h.starts_with('Cookie:') || 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) {
|
||||
// ctx.resp.headers[key] = val
|
||||
ctx.headers << '$key: $val'
|
||||
headers string // response headers
|
||||
}
|
||||
|
||||
pub fn (ctx Context) html(html string) {
|
||||
h := ctx.headers.join('\n')
|
||||
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: text/html\n$h\n\n$html')
|
||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n$ctx.headers\r\n\r\n$html')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) text(s string) {
|
||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n$ctx.headers\r\n\r\n $s')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) json(s string) {
|
||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n$ctx.headers\r\n\r\n$s')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) redirect(url string) {
|
||||
ctx.conn.write('HTTP/1.1 302 Found\r\nLocation: $url\r\n\r\n$ctx.headers')
|
||||
}
|
||||
|
||||
pub fn (ctx Context) not_found(s string) {
|
||||
ctx.conn.write(HTTP_404)
|
||||
}
|
||||
|
||||
pub fn (ctx mut Context) set_cookie(key, val string) { // TODO support directives, escape cookie value (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
|
||||
ctx.add_header('Set-Cookie', '$key=$val')
|
||||
}
|
||||
|
||||
pub fn (ctx mut Context) get_cookie(key string) ?string { // TODO refactor
|
||||
cookie_header := ctx.get_header('Cookie')
|
||||
cookie := if cookie_header.contains(';') {
|
||||
cookie_header.find_between('$key=', ';')
|
||||
} else {
|
||||
cookie_header
|
||||
}
|
||||
if cookie != '' {
|
||||
return cookie
|
||||
}
|
||||
return error('Cookie not found')
|
||||
}
|
||||
|
||||
fn (ctx mut Context) add_header(key, val string) {
|
||||
ctx.headers = ctx.headers + if ctx.headers == '' { '$key: val' } else { '\r\n$key: val' }
|
||||
}
|
||||
|
||||
fn (ctx mut Context) get_header(key string) string {
|
||||
return ctx.headers.find_between('\r\n$key: ', '\r\n')
|
||||
}
|
||||
|
||||
pub fn run<T>(port int) {
|
||||
@ -86,32 +98,20 @@ pub fn run<T>(port int) {
|
||||
// TODO move this to handle_conn<T>(conn, app)
|
||||
s := conn.read_line()
|
||||
if s == '' {
|
||||
conn.write('HTTP/1.1 500 Not Found \nContent-Type: text/plain \n\n500')
|
||||
conn.write(HTTP_500)
|
||||
conn.close()
|
||||
continue
|
||||
}
|
||||
// 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
|
||||
*/
|
||||
return
|
||||
}
|
||||
// Parse the first line
|
||||
// "GET / HTTP/1.1"
|
||||
first_line := s.all_before('\n')
|
||||
vals := first_line.split(' ')
|
||||
if vals.len < 2 {
|
||||
println('no vals for http')
|
||||
conn.write(HTTP_500)
|
||||
conn.close()
|
||||
return
|
||||
}
|
||||
mut action := vals[1].right(1).all_before('/')
|
||||
if action.contains('?') {
|
||||
action = action.all_before('?')
|
||||
@ -120,8 +120,7 @@ pub fn run<T>(port int) {
|
||||
action = 'index'
|
||||
}
|
||||
req := http.Request{
|
||||
headers: map[string]string
|
||||
headers2: headers
|
||||
headers: http.parse_headers(s.split_into_lines())
|
||||
ws_func: 0
|
||||
user_ptr: 0
|
||||
method: vals[0]
|
||||
@ -158,7 +157,7 @@ pub fn run<T>(port int) {
|
||||
|
||||
// Call the right action
|
||||
app.$action() or {
|
||||
conn.write('HTTP/1.1 404 Not Found \nContent-Type: text/plain \n\n404 not found')
|
||||
conn.write(HTTP_404)
|
||||
}
|
||||
conn.close()
|
||||
}
|
||||
@ -194,21 +193,6 @@ fn (ctx mut Context) parse_form(s string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
const (
|
||||
mime_types = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.gif': 'image/gif',
|
||||
'.htm': 'text/html; charset=utf-8',
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.js': 'application/javascript',
|
||||
'.wasm': 'application/wasm',
|
||||
'.pdf': 'application/pdf',
|
||||
'.png': 'image/png',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.xml': 'text/xml; charset=utf-8'
|
||||
}
|
||||
)
|
||||
|
||||
fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) {
|
||||
files := os.ls(directory_path)
|
||||
@ -227,8 +211,7 @@ fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) {
|
||||
}
|
||||
}
|
||||
|
||||
// todo: os.is_dir is broken now
|
||||
// so we expect that file is dir it has no extension
|
||||
// todo: os.is_dir is broken now so we expect that file is dir it has no extension
|
||||
if flag {
|
||||
ctx.scan_static_directory(directory_path + '/' + file, mount_path + '/' + file)
|
||||
} else {
|
||||
@ -247,7 +230,7 @@ pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
||||
|
||||
if static_file != '' {
|
||||
data := os.read_file(static_file) or { return false }
|
||||
ctx.conn.write('HTTP/1.1 200 OK\nContent-Type: $mime_type\n\n$data')
|
||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: $mime_type\r\n\r\n$data')
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
Loading…
Reference in New Issue
Block a user