vweb: continue after bad http client connection; performance fixes
* Enable compiling vweb with -prod (by supressing 'declared and not used' warning about 'reset') . * Fix http responses (now wrk is happy and shows no errors) by adding a Content-Length header. * Fix -g compilation for urllib.v . * vweb: println action= only in debug mode. * vweb: max request headers counting fix. * Make vweb.html get a 'ctx mut Context' param, just like the other methods. * vweb: simplify add_header. * Use a string builder for the most common html case so that the response http text can be send in one go. * vweb: reduce _STR/string interpolation usage in the most common html response case. * vweb: refactor common http response formatting into Context.send_response_to_client/2 method.pull/3051/head
parent
cfeec92826
commit
13769f440f
|
@ -22,7 +22,7 @@ pub fn (app mut App) init() {
|
||||||
app.vweb.handle_static('.')
|
app.vweb.handle_static('.')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (app & App) json_endpoint() {
|
pub fn (app mut App) json_endpoint() {
|
||||||
app.vweb.json('{"a": 3}')
|
app.vweb.json('{"a": 3}')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -178,7 +178,7 @@ fn (p mut Parser) comp_time() {
|
||||||
p.genln('/////////////////// tmpl end')
|
p.genln('/////////////////// tmpl end')
|
||||||
receiver := p.cur_fn.args[0]
|
receiver := p.cur_fn.args[0]
|
||||||
dot := if receiver.is_mut || receiver.ptr || receiver.typ.ends_with('*') { '->' } else { '.' }
|
dot := if receiver.is_mut || receiver.ptr || receiver.typ.ends_with('*') { '->' } else { '.' }
|
||||||
p.genln('vweb__Context_html($receiver.name /*!*/$dot vweb, tmpl_res)')
|
p.genln('vweb__Context_html( & $receiver.name /*!*/$dot vweb, tmpl_res)')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
p.error('bad comptime expr')
|
p.error('bad comptime expr')
|
||||||
|
|
|
@ -627,7 +627,7 @@ fn (p mut Parser) check_unused_and_mut_vars() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if !var.is_used && !p.pref.is_repl && !var.is_arg &&
|
if !var.is_used && !p.pref.is_repl && !var.is_arg &&
|
||||||
!p.pref.translated && var.name != 'tmpl_res'
|
!p.pref.translated && var.name != 'tmpl_res' && p.mod != 'vweb'
|
||||||
{
|
{
|
||||||
p.production_error_with_token_index('`$var.name` declared and not used', var.token_idx )
|
p.production_error_with_token_index('`$var.name` declared and not used', var.token_idx )
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,13 +202,23 @@ pub fn dial(address string, port int) ?Socket {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// send string data to socket
|
// send data to socket (when you have a memory buffer)
|
||||||
pub fn (s Socket) send(buf byteptr, len int) ?int {
|
pub fn (s Socket) send(buf byteptr, len int) ?int {
|
||||||
res := C.send(s.sockfd, buf, len, MSG_NOSIGNAL)
|
mut dptr := buf
|
||||||
if res < 0 {
|
mut dlen := len
|
||||||
return error('net.send: failed with $res')
|
for {
|
||||||
|
sbytes := C.send(s.sockfd, dptr, dlen, MSG_NOSIGNAL)
|
||||||
|
if sbytes < 0 { return error('net.send: failed with $sbytes') }
|
||||||
|
dlen -= sbytes
|
||||||
|
if dlen <= 0 { break }
|
||||||
|
dptr += sbytes
|
||||||
}
|
}
|
||||||
return res
|
return len
|
||||||
|
}
|
||||||
|
|
||||||
|
// send string data to socket (when you have a v string)
|
||||||
|
pub fn (s Socket) send_string(sdata string) ?int {
|
||||||
|
return s.send( sdata.str, sdata.len )
|
||||||
}
|
}
|
||||||
|
|
||||||
// receive string data from socket
|
// receive string data from socket
|
||||||
|
|
|
@ -518,8 +518,8 @@ fn parse_url(rawurl string, via_request bool) ?URL {
|
||||||
// RFC 3986, §3.3:
|
// RFC 3986, §3.3:
|
||||||
// In addition, a URI reference (Section 4.1) may be a relative-path reference,
|
// In addition, a URI reference (Section 4.1) may be a relative-path reference,
|
||||||
// in which case the first path segment cannot contain a colon (':') character.
|
// in which case the first path segment cannot contain a colon (':') character.
|
||||||
colon := rest.index(':') or { -1 }
|
colon := rest.index(':') or { return error('there should be a : in the URL') }
|
||||||
slash := rest.index('/') or { -1 }
|
slash := rest.index('/') or { return error('there should be a / in the URL') }
|
||||||
if colon >= 0 && (slash < 0 || colon < slash) {
|
if colon >= 0 && (slash < 0 || colon < slash) {
|
||||||
// First path segment has colon. Not allowed in relative URL.
|
// First path segment has colon. Not allowed in relative URL.
|
||||||
return error(error_msg('parse_url: first path segment in URL cannot contain colon', ''))
|
return error(error_msg('parse_url: first path segment in URL cannot contain colon', ''))
|
||||||
|
@ -553,7 +553,7 @@ struct ParseAuthorityRes {
|
||||||
fn parse_authority(authority string) ?ParseAuthorityRes {
|
fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||||
i := authority.last_index('@')
|
i := authority.last_index('@')
|
||||||
mut host := ''
|
mut host := ''
|
||||||
mut user := user('')
|
mut zuser := user('')
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
h := parse_host(authority) or {
|
h := parse_host(authority) or {
|
||||||
return error(err)
|
return error(err)
|
||||||
|
@ -566,7 +566,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||||
host = h
|
host = h
|
||||||
}
|
}
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return ParseAuthorityRes{host: host, user: user}
|
return ParseAuthorityRes{host: host, user: zuser}
|
||||||
}
|
}
|
||||||
mut userinfo := authority[..i]
|
mut userinfo := authority[..i]
|
||||||
if !valid_userinfo(userinfo) {
|
if !valid_userinfo(userinfo) {
|
||||||
|
@ -577,7 +577,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||||
return error(err)
|
return error(err)
|
||||||
}
|
}
|
||||||
userinfo = u
|
userinfo = u
|
||||||
user = user(userinfo)
|
zuser = user(userinfo)
|
||||||
} else {
|
} else {
|
||||||
mut username, mut password := split(userinfo, `:`, true)
|
mut username, mut password := split(userinfo, `:`, true)
|
||||||
u := unescape(username, .encode_user_password) or {
|
u := unescape(username, .encode_user_password) or {
|
||||||
|
@ -588,10 +588,10 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||||
return error(err)
|
return error(err)
|
||||||
}
|
}
|
||||||
password = p
|
password = p
|
||||||
user = user_password(username, password)
|
zuser = user_password(username, password)
|
||||||
}
|
}
|
||||||
return ParseAuthorityRes{
|
return ParseAuthorityRes{
|
||||||
user: user
|
user: zuser
|
||||||
host: host
|
host: host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,13 @@ mut:
|
||||||
buf []byte
|
buf []byte
|
||||||
pub:
|
pub:
|
||||||
len int
|
len int
|
||||||
|
initial_size int = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_builder(initial_size int) Builder {
|
pub fn new_builder(initial_size int) Builder {
|
||||||
return Builder {
|
return Builder {
|
||||||
buf: make(0, initial_size, 1)
|
buf: make(0, initial_size, 1)
|
||||||
|
initial_size: initial_size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +50,6 @@ pub fn (b mut Builder) str() string {
|
||||||
|
|
||||||
pub fn (b mut Builder) free() {
|
pub fn (b mut Builder) free() {
|
||||||
unsafe{ free(b.buf.data) }
|
unsafe{ free(b.buf.data) }
|
||||||
b.buf = make(0, 1, 1)
|
b.buf = make(0, b.initial_size, 1)
|
||||||
b.len = 0
|
b.len = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,13 @@ mut:
|
||||||
buf []byte
|
buf []byte
|
||||||
pub:
|
pub:
|
||||||
len int
|
len int
|
||||||
|
initial_size int = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_builder(initial_size int) Builder {
|
pub fn new_builder(initial_size int) Builder {
|
||||||
return Builder {
|
return Builder {
|
||||||
buf: make(0, initial_size, sizeof(byte))
|
buf: make(0, initial_size, sizeof(byte))
|
||||||
|
initial_size: initial_size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,4 +46,6 @@ pub fn (b mut Builder) cut(n int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (b mut Builder) free() {
|
pub fn (b mut Builder) free() {
|
||||||
|
b.buf = make(0, b.initial_size, 1)
|
||||||
|
b.len = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,16 @@ import (
|
||||||
net
|
net
|
||||||
http
|
http
|
||||||
net.urllib
|
net.urllib
|
||||||
|
strings
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
HEADER_SERVER = 'Server: VWeb\r\n'
|
||||||
HTTP_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found'
|
HEADER_CONNECTION_CLOSE = 'Connection: close\r\n'
|
||||||
HTTP_500 = 'HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n500 Internal Server Error'
|
HEADERS_CLOSE = '${HEADER_SERVER}${HEADER_CONNECTION_CLOSE}\r\n'
|
||||||
|
HTTP_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n${HEADERS_CLOSE}404 Not Found'
|
||||||
|
HTTP_500 = 'HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n${HEADERS_CLOSE}500 Internal Server Error'
|
||||||
mime_types = {
|
mime_types = {
|
||||||
'.css': 'text/css; charset=utf-8',
|
'.css': 'text/css; charset=utf-8',
|
||||||
'.gif': 'image/gif',
|
'.gif': 'image/gif',
|
||||||
|
@ -44,37 +47,49 @@ mut:
|
||||||
done bool
|
done bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) html(html string) {
|
fn (ctx mut Context) send_response_to_client(mimetype string, res string) bool {
|
||||||
if ctx.done { return }
|
if ctx.done { return false }
|
||||||
//println('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n$ctx.headers\r\n\r\n$html')
|
ctx.done = true
|
||||||
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) }
|
mut sb := strings.new_builder(1024)
|
||||||
|
sb.write('HTTP/1.1 200 OK\r\nContent-Type: ') sb.write(mimetype)
|
||||||
|
sb.write('\r\nContent-Length: ') sb.write(res.len.str())
|
||||||
|
sb.write(ctx.headers)
|
||||||
|
sb.write('\r\n')
|
||||||
|
sb.write(HEADERS_CLOSE)
|
||||||
|
sb.write(res)
|
||||||
|
ctx.conn.send_string(sb.str()) or { return false }
|
||||||
|
sb.free()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (ctx mut Context) html(s string) {
|
||||||
|
ctx.send_response_to_client('text/html', s)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx mut Context) text(s string) {
|
pub fn (ctx mut Context) text(s string) {
|
||||||
|
ctx.send_response_to_client('text/plain', s)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (ctx mut Context) json(s string) {
|
||||||
|
ctx.send_response_to_client('application/json', s)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (ctx mut Context) redirect(url string) {
|
||||||
if ctx.done { return }
|
if ctx.done { return }
|
||||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n' +
|
|
||||||
'$ctx.headers\r\n$s') or { panic(err) }
|
|
||||||
ctx.done = true
|
ctx.done = true
|
||||||
|
ctx.conn.send_string('HTTP/1.1 302 Found\r\nLocation: ${url}${ctx.headers}\r\n${HEADERS_CLOSE}') or { return }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) json(s string) {
|
pub fn (ctx mut Context) not_found(s string) {
|
||||||
if ctx.done { return }
|
if ctx.done { return }
|
||||||
ctx.conn.write('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n$ctx.headers\r\n\r\n$s') or { panic(err) }
|
ctx.done = true
|
||||||
|
ctx.conn.send_string(HTTP_404) or { return }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx Context) redirect(url string) {
|
pub fn (ctx mut Context) set_cookie(key, val string) {
|
||||||
if ctx.done { return }
|
// TODO support directives, escape cookie value (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
|
||||||
ctx.conn.write('HTTP/1.1 302 Found\r\nLocation: $url\r\n$ctx.headers\r\n\r\n') or { panic(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (ctx Context) not_found(s string) {
|
|
||||||
if ctx.done { return }
|
|
||||||
ctx.conn.write(HTTP_404) or { panic(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
//println('Set-Cookie $key=$val')
|
//println('Set-Cookie $key=$val')
|
||||||
ctx.add_header('Set-Cookie', '$key=$val; Secure; HttpOnly')
|
ctx.add_header('Set-Cookie', '${key}=${val}; Secure; HttpOnly')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
|
pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
|
||||||
|
@ -92,8 +107,7 @@ pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
|
||||||
|
|
||||||
fn (ctx mut Context) add_header(key, val string) {
|
fn (ctx mut Context) add_header(key, val string) {
|
||||||
//println('add_header($key, $val)')
|
//println('add_header($key, $val)')
|
||||||
ctx.headers = ctx.headers +
|
ctx.headers = ctx.headers + '\r\n$key: $val'
|
||||||
if ctx.headers == '' { '$key: $val' } else { '\r\n$key: $val' }
|
|
||||||
//println(ctx.headers)
|
//println(ctx.headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,16 +122,14 @@ pub fn run<T>(app mut T, port int) {
|
||||||
//mut app := T{}
|
//mut app := T{}
|
||||||
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)
|
||||||
first_line:= conn.read_line()
|
first_line:= conn.read_line()
|
||||||
if first_line == '' {
|
if first_line == '' {
|
||||||
conn.write(HTTP_500) or {}
|
conn.send_string(HTTP_500) or {}
|
||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
// Parse the first line
|
// Parse the first line
|
||||||
// "GET / HTTP/1.1"
|
// "GET / HTTP/1.1"
|
||||||
|
@ -125,18 +137,17 @@ pub fn run<T>(app mut T, port int) {
|
||||||
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')
|
||||||
conn.write(HTTP_500) or {}
|
conn.send_string(HTTP_500) or {}
|
||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
mut headers := []string
|
mut headers := []string
|
||||||
for _ in 0..30 {
|
for {
|
||||||
|
if headers.len >= 30 { break }
|
||||||
header := conn.read_line()
|
header := conn.read_line()
|
||||||
headers << header
|
headers << header
|
||||||
//println('header="$header" len = ' + header.len.str())
|
//println('header="$header" len = ' + header.len.str())
|
||||||
if header.len <= 2 {
|
if header.len <= 2 { break }
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mut action := vals[1][1..].all_before('/')
|
mut action := vals[1][1..].all_before('/')
|
||||||
if action.contains('?') {
|
if action.contains('?') {
|
||||||
|
@ -196,9 +207,11 @@ pub fn run<T>(app mut T, port int) {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Call the right action
|
// Call the right action
|
||||||
println('action=$action')
|
$if debug {
|
||||||
|
println('action=$action')
|
||||||
|
}
|
||||||
app.$action() or {
|
app.$action() or {
|
||||||
conn.write(HTTP_404) or {}
|
conn.send_string(HTTP_404) or {}
|
||||||
}
|
}
|
||||||
conn.close() or {}
|
conn.close() or {}
|
||||||
reset := 'reset'
|
reset := 'reset'
|
||||||
|
@ -267,6 +280,7 @@ fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
||||||
|
if ctx.done { return false }
|
||||||
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]
|
||||||
|
@ -274,8 +288,7 @@ pub fn (ctx mut Context) handle_static(directory_path string) bool {
|
||||||
|
|
||||||
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\r\nContent-Type: $mime_type\r\n\r\n$data') or { panic(err) }
|
return ctx.send_response_to_client(mime_type, data)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue