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
@ -8,11 +8,11 @@ const (
|
|||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
pub mut:
|
pub mut:
|
||||||
vweb vweb.Context // TODO embed
|
vweb vweb.Context // TODO embed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
vweb.run<App>(Port)
|
vweb.run<App>(Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (app mut App) init() {
|
pub fn (app mut App) init() {
|
||||||
@ -20,16 +20,22 @@ pub fn (app mut App) init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (app mut App) json_endpoint() {
|
pub fn (app mut App) json_endpoint() {
|
||||||
app.vweb.json('{"a": 3}')
|
app.vweb.json('{"a": 3}')
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub fn (app mut App) index() {
|
pub fn (app mut App) index() {
|
||||||
$vweb.html()
|
$vweb.html()
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub fn (app mut App) text() {
|
pub fn (app mut App) text() {
|
||||||
app.vweb.text('hello world')
|
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 {
|
struct Request {
|
||||||
pub:
|
pub:
|
||||||
headers2 []string
|
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
method string
|
method string
|
||||||
// cookies map[string]string
|
// cookies map[string]string
|
||||||
@ -98,6 +97,21 @@ pub fn (req mut Request) add_header(key, val string) {
|
|||||||
// req.h = h
|
// 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 {
|
pub fn (req &Request) do() ?Response {
|
||||||
if req.typ == 'POST' {
|
if req.typ == 'POST' {
|
||||||
// req.headers << 'Content-Type: application/x-www-form-urlencoded'
|
// 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() {
|
fn test_http_get_from_vlang_utc_now() {
|
||||||
/*
|
/*
|
||||||
urls := ['http://vlang.io/utc_now', 'https://vlang.io/utc_now']
|
urls := ['http://vlang.io/utc_now', 'https://vlang.io/utc_now']
|
||||||
|
215
vlib/vweb/vweb.v
215
vlib/vweb/vweb.v
@ -10,68 +10,80 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
methods_with_form = ['POST', 'PUT', 'PATCH']
|
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 {
|
struct Context {
|
||||||
static_files map[string]string
|
static_files map[string]string
|
||||||
static_mime_types map[string]string
|
static_mime_types map[string]string
|
||||||
pub:
|
pub:
|
||||||
req http.Request
|
req http.Request
|
||||||
conn net.Socket
|
conn net.Socket
|
||||||
form map[string]string
|
form map[string]string
|
||||||
// TODO Response
|
// TODO Response
|
||||||
mut:
|
mut:
|
||||||
headers []string // response headers
|
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'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) html(html string) {
|
pub fn (ctx Context) html(html string) {
|
||||||
h := ctx.headers.join('\n')
|
ctx.conn.write('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\nContent-Type: text/html\n$h\n\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) {
|
pub fn run<T>(port int) {
|
||||||
println('Running vweb app on http://localhost:$port ...')
|
println('Running vweb app on http://localhost:$port ...')
|
||||||
@ -80,48 +92,35 @@ pub fn run<T>(port int) {
|
|||||||
app.init()
|
app.init()
|
||||||
for {
|
for {
|
||||||
conn := l.accept() or {
|
conn := l.accept() or {
|
||||||
panic('accept() failed')
|
panic('accept() failed')
|
||||||
}
|
}
|
||||||
//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()
|
s := conn.read_line()
|
||||||
if s == '' {
|
if s == '' {
|
||||||
conn.write('HTTP/1.1 500 Not Found \nContent-Type: text/plain \n\n500')
|
conn.write(HTTP_500)
|
||||||
conn.close()
|
conn.close()
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
// 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
|
// 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(' ')
|
||||||
mut action := vals[1].right(1).all_before('/')
|
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('?') {
|
if action.contains('?') {
|
||||||
action = action.all_before('?')
|
action = action.all_before('?')
|
||||||
}
|
}
|
||||||
if action == '' {
|
if action == '' {
|
||||||
action = 'index'
|
action = 'index'
|
||||||
}
|
}
|
||||||
req := http.Request{
|
req := http.Request{
|
||||||
headers: map[string]string
|
headers: http.parse_headers(s.split_into_lines())
|
||||||
headers2: headers
|
|
||||||
ws_func: 0
|
ws_func: 0
|
||||||
user_ptr: 0
|
user_ptr: 0
|
||||||
method: vals[0]
|
method: vals[0]
|
||||||
@ -154,15 +153,15 @@ pub fn run<T>(port int) {
|
|||||||
// if app.vweb.handle_static() {
|
// if app.vweb.handle_static() {
|
||||||
// conn.close()
|
// conn.close()
|
||||||
// continue
|
// continue
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Call the right action
|
// Call the right action
|
||||||
app.$action() or {
|
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()
|
conn.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn foobar<T>() {
|
pub fn foobar<T>() {
|
||||||
@ -193,27 +192,12 @@ fn (ctx mut Context) parse_form(s string) {
|
|||||||
ctx.form[key] = val
|
ctx.form[key] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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) {
|
fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) {
|
||||||
files := os.ls(directory_path)
|
files := os.ls(directory_path)
|
||||||
if files.len > 0 {
|
if files.len > 0 {
|
||||||
for file in files {
|
for file in files {
|
||||||
mut ext := ''
|
mut ext := ''
|
||||||
mut i := file.len
|
mut i := file.len
|
||||||
mut flag := true
|
mut flag := true
|
||||||
@ -227,33 +211,32 @@ fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: os.is_dir is broken now
|
// todo: os.is_dir is broken now so we expect that file is dir it has no extension
|
||||||
// so we expect that file is dir it has no extension
|
|
||||||
if flag {
|
if flag {
|
||||||
ctx.scan_static_directory(directory_path + '/' + file, mount_path + '/' + file)
|
ctx.scan_static_directory(directory_path + '/' + file, mount_path + '/' + file)
|
||||||
} else {
|
} else {
|
||||||
ctx.static_files[mount_path + '/' + file] = directory_path + '/' + file
|
ctx.static_files[mount_path + '/' + file] = directory_path + '/' + file
|
||||||
ctx.static_mime_types[mount_path + '/' + file] = mime_types[ext]
|
ctx.static_mime_types[mount_path + '/' + file] = mime_types[ext]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
||||||
ctx.scan_static_directory(directory_path, '')
|
ctx.scan_static_directory(directory_path, '')
|
||||||
|
|
||||||
static_file := ctx.static_files[ctx.req.url]
|
static_file := ctx.static_files[ctx.req.url]
|
||||||
mime_type := ctx.static_mime_types[ctx.req.url]
|
mime_type := ctx.static_mime_types[ctx.req.url]
|
||||||
|
|
||||||
if static_file != '' {
|
if static_file != '' {
|
||||||
data := os.read_file(static_file) or { return false }
|
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 true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx mut Context) serve_static(url, file_path, mime_type string) {
|
pub fn (ctx mut Context) serve_static(url, file_path, mime_type string) {
|
||||||
ctx.static_files[url] = file_path
|
ctx.static_files[url] = file_path
|
||||||
ctx.static_mime_types[url] = mime_type
|
ctx.static_mime_types[url] = mime_type
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user