1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: run vfmt, add it to v test-cleancode

This commit is contained in:
Delyan Angelov
2020-12-27 11:38:12 +02:00
parent 1b9d514275
commit d563261e58
8 changed files with 94 additions and 181 deletions

View File

@@ -20,20 +20,20 @@ pub const (
http_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\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 = {
'.css': 'text/css; charset=utf-8'
'.gif': 'image/gif'
'.htm': 'text/html; charset=utf-8'
'.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'
'.jpg': 'image/jpeg'
'.js': 'application/javascript'
'.json': 'application/json'
'.md': 'text/markdown; charset=utf-8'
'.pdf': 'application/pdf'
'.png': 'image/png'
'.svg': 'image/svg+xml'
'.txt': 'text/plain; charset=utf-8'
'.md': 'text/markdown; charset=utf-8'
'.pdf': 'application/pdf'
'.png': 'image/png'
'.svg': 'image/svg+xml'
'.txt': 'text/plain; charset=utf-8'
'.wasm': 'application/wasm'
'.xml': 'text/xml; charset=utf-8'
'.xml': 'text/xml; charset=utf-8'
}
max_http_post_size = 1024 * 1024
default_port = 8080
@@ -46,8 +46,8 @@ mut:
content_type string = 'text/plain'
status string = '200 OK'
pub:
req http.Request
conn net.TcpConn
req http.Request
conn net.TcpConn
// TODO Response
pub mut:
form map[string]string
@@ -117,7 +117,9 @@ pub fn (mut ctx Context) redirect(url string) Result {
return Result{}
}
ctx.done = true
send_string(ctx.conn, 'HTTP/1.1 302 Found\r\nLocation: ${url}${ctx.headers}\r\n${headers_close}') or { return Result{} }
send_string(ctx.conn, 'HTTP/1.1 302 Found\r\nLocation: $url$ctx.headers\r\n$headers_close') or {
return Result{}
}
return Result{}
}
@@ -126,8 +128,8 @@ pub fn (mut ctx Context) not_found() Result {
return Result{}
}
ctx.done = true
send_string(ctx.conn, http_404) or {}
return vweb.Result{}
send_string(ctx.conn, http_404) or { }
return Result{}
}
pub fn (mut ctx Context) set_cookie(cookie Cookie) {
@@ -212,11 +214,11 @@ pub fn run_app<T>(mut app T, port int) {
for {
mut conn := l.accept() or { panic('accept() failed') }
handle_conn<T>(mut conn, mut app)
//app.vweb.page_gen_time = time.ticks() - t
//eprintln('handle conn() took ${time.ticks()-t}ms')
//message := readall(conn)
//println(message)
/*
// app.vweb.page_gen_time = time.ticks() - t
// eprintln('handle conn() took ${time.ticks()-t}ms')
// message := readall(conn)
// println(message)
/*
if message.len > max_http_post_size {
println('message.len = $message.len > max_http_post_size')
conn.send_string(http_500) or {}
@@ -238,13 +240,13 @@ pub fn run_app<T>(mut app T, port int) {
fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
conn.set_read_timeout(1 * time.second)
defer { conn.close() or {} }
//fn handle_conn<T>(conn net.Socket, app_ T) T {
//mut app := app_
//first_line := strip(lines[0])
defer {
conn.close() or { }
}
// fn handle_conn<T>(conn net.Socket, app_ T) T {
// mut app := app_
// first_line := strip(lines[0])
mut reader := io.new_buffered_reader(reader: io.make_reader(conn))
page_gen_start := time.ticks()
first_line := reader.read_line() or {
println('Failed first_line')
@@ -259,36 +261,34 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
vals := first_line.split(' ')
if vals.len < 2 {
println('no vals for http')
send_string(conn, http_500) or {}
send_string(conn, http_500) or { }
return
}
mut headers := []string{}
mut body := ''
mut in_headers := true
mut len := 0
//for line in lines[1..] {
for _ in 0..100 {
//println(j)
line := reader.read_line() or {
// for line in lines[1..] {
for _ in 0 .. 100 {
// println(j)
line := reader.read_line() or {
println('Failed read_line')
break
}
sline := strip(line)
if sline == '' {
//if in_headers {
// End of headers, no body => exit
if len == 0 {
break
}
// if in_headers {
// End of headers, no body => exit
if len == 0 {
break
}
//} //else {
// End of body
//break
// End of body
// break
//}
// read body
read_body := io.read_all(reader: reader) or { []byte{} }
body += read_body.bytestr()
break
}
if in_headers {
@@ -343,7 +343,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
mime_type := app.vweb.static_mime_types[static_file_name]
if static_file != '' && mime_type != '' {
data := os.read_file(static_file) or {
send_string(conn, http_404) or {}
send_string(conn, http_404) or { }
return
}
app.vweb.send_response_to_client(mime_type, data)
@@ -491,7 +491,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
}
if action == '' {
// site not found
send_string(conn, http_404) or {}
send_string(conn, http_404) or { }
return
}
$for method in T.methods {
@@ -526,12 +526,8 @@ fn (mut ctx Context) parse_form(s string) {
if keyval.len != 2 {
continue
}
key := urllib.query_unescape(keyval[0]) or {
continue
}
val := urllib.query_unescape(keyval[1]) or {
continue
}
key := urllib.query_unescape(keyval[0]) or { continue }
val := urllib.query_unescape(keyval[1]) or { continue }
$if debug {
println('http form "$key" => "$val"')
}
@@ -543,9 +539,7 @@ fn (mut ctx Context) parse_form(s string) {
}
fn (mut ctx Context) scan_static_directory(directory_path string, mount_path string) {
files := os.ls(directory_path) or {
panic(err)
}
files := os.ls(directory_path) or { panic(err) }
if files.len > 0 {
for file in files {
full_path := directory_path + '/' + file
@@ -556,8 +550,7 @@ fn (mut ctx Context) scan_static_directory(directory_path string, mount_path str
// Rudimentary guard against adding files not in mime_types.
// Use serve_static directly to add non-standard mime types.
if ext in mime_types {
ctx.serve_static(mount_path + '/' + file, full_path,
mime_types[ext])
ctx.serve_static(mount_path + '/' + file, full_path, mime_types[ext])
}
}
}
@@ -639,7 +632,6 @@ fn filter(s string) string {
pub type RawHtml = string
fn send_string(conn net.TcpConn, s string) ? {
conn.write(s.bytes())?
conn.write(s.bytes()) ?
}