diff --git a/cmd/tools/vtest-cleancode.v b/cmd/tools/vtest-cleancode.v index a4304213e7..3a6266560d 100644 --- a/cmd/tools/vtest-cleancode.v +++ b/cmd/tools/vtest-cleancode.v @@ -66,6 +66,7 @@ const ( 'vlib/semver/', 'vlib/strings/', 'vlib/time/', + 'vlib/vweb/', ] ) diff --git a/cmd/tools/vtest-fmt.v b/cmd/tools/vtest-fmt.v index 0ac0e32152..303433ee08 100644 --- a/cmd/tools/vtest-fmt.v +++ b/cmd/tools/vtest-fmt.v @@ -8,8 +8,6 @@ const ( known_failing_exceptions = [ 'vlib/crypto/aes/const.v', /* multiple narrow columns of []string turned to 1 long single column, otherwise works */ - 'vlib/vweb/vweb.v', - /* $for method in T.methods { => $for method in T(methods) { , `return // xx` => parse expr error */ 'vlib/v/gen/js/tests/life.v', /* error: unexpected `,`, expecting ), on JS.setInterval(fn () { show(game) game = step(game) }, 500) */ 'vlib/builtin/js/builtin.v', @@ -17,11 +15,10 @@ const ( 'vlib/builtin/js/jsfns_node.js.v', 'vlib/builtin/js/jsfns.js.v', 'vlib/builtin/js/jsfns_browser.js.v', - 'vlib/builtin/bare/linuxsys_bare.v', /* error: expr(): bad token `asm`, on `asm {}` */ - 'vlib/picoev/picoev.v', + 'vlib/builtin/bare/linuxsys_bare.v', /* the fn args are removed, then `cb fn (picohttpparser.Request, mut picohttpparser.Response)` can not be reparsed */ - 'vlib/os/os.v' /* os.v - `a := [ c'/bin/sh', c'-c', byteptr(cmd.str), 0 ]` */, + 'vlib/picoev/picoev.v', ] ) diff --git a/vlib/vweb/assets/assets.v b/vlib/vweb/assets/assets.v index 8e340fb54c..2b4d783837 100644 --- a/vlib/vweb/assets/assets.v +++ b/vlib/vweb/assets/assets.v @@ -77,16 +77,12 @@ fn (am AssetManager) combine(asset_type string, to_file bool) string { if to_file { return out_file } - cached := os.read_file(out_file) or { - return '' - } + cached := os.read_file(out_file) or { return '' } return cached } // rebuild for asset in am.get_assets(asset_type) { - data := os.read_file(asset.file_path) or { - return '' - } + data := os.read_file(asset.file_path) or { return '' } out += data } if am.minify { @@ -100,13 +96,9 @@ fn (am AssetManager) combine(asset_type string, to_file bool) string { return out } if !os.is_dir(am.cache_dir) { - os.mkdir(am.cache_dir) or { - panic(err) - } - } - mut file := os.create(out_file) or { - panic(err) + os.mkdir(am.cache_dir) or { panic(err) } } + mut file := os.create(out_file) or { panic(err) } file.write(out.bytes()) file.close() return out_file diff --git a/vlib/vweb/assets/assets_test.v b/vlib/vweb/assets/assets_test.v index 86e7aedac7..6a63d30002 100644 --- a/vlib/vweb/assets/assets_test.v +++ b/vlib/vweb/assets/assets_test.v @@ -20,15 +20,12 @@ fn cache_dir(test_name string) string { fn get_test_file_path(file string) string { path := os.join_path(base_cache_dir(), file) - - if ! os.is_dir(base_cache_dir()) { + if !os.is_dir(base_cache_dir()) { os.mkdir_all(base_cache_dir()) } - - if ! os.exists(path) { + if !os.exists(path) { os.write_file(path, get_test_file_contents(file)) } - return path } @@ -40,7 +37,6 @@ fn get_test_file_contents(file string) string { 'test2.css' { '.two {\n\tcolor: #996633;\n}\n' } else { 'wibble\n' } } - return contents } @@ -80,23 +76,19 @@ fn test_combine_css() { mut am := assets.new_manager() am.cache_dir = cache_dir('test_combine_css') clean_cache_dir(am.cache_dir) - am.add_css(get_test_file_path('test1.css')) am.add_css(get_test_file_path('test2.css')) - // TODO: How do I test non-minified, is there a "here doc" format that keeps formatting? am.minify = true expected := '.one { color: #336699; } .two { color: #996633; } ' actual := am.combine_css(false) assert actual == expected assert actual.contains(expected) - // Test cache path doesn't change when input files and minify setting do not. path1 := am.combine_css(true) clean_cache_dir(am.cache_dir) path2 := am.combine_css(true) assert path1 == path2 - clean_cache_dir(am.cache_dir) } @@ -104,10 +96,8 @@ fn test_combine_js() { mut am := assets.new_manager() am.cache_dir = cache_dir('test_combine_js') clean_cache_dir(am.cache_dir) - am.add_js(get_test_file_path('test1.js')) am.add_js(get_test_file_path('test2.js')) - expected1 := '{"one": 1}' expected2 := '{"two": 2}' expected := expected1 + '\n' + expected2 + '\n' @@ -116,20 +106,17 @@ fn test_combine_js() { assert actual.contains(expected) assert actual.contains(expected1) assert actual.contains(expected2) - am.minify = true clean_cache_dir(am.cache_dir) expected3 := expected1 + ' ' + expected2 + ' ' actual2 := am.combine_js(false) assert actual2 == expected3 assert actual2.contains(expected3) - // Test cache path doesn't change when input files and minify setting do not. path1 := am.combine_js(true) clean_cache_dir(am.cache_dir) path2 := am.combine_js(true) assert path1 == path2 - clean_cache_dir(am.cache_dir) } @@ -141,29 +128,24 @@ fn test_include_css() { actual := am.include_css(false) assert actual == expected assert actual.contains(expected) - // Two lines of output. file2 := get_test_file_path('test2.css') am.add_css(file2) am.cache_dir = cache_dir('test_include_css') clean_cache_dir(am.cache_dir) - expected2 := expected + '\n' actual2 := am.include_css(false) assert actual2 == expected2 assert actual2.contains(expected2) - // Combined output. clean_cache_dir(am.cache_dir) actual3 := am.include_css(true) assert actual3.contains(expected2) == false - assert actual3.starts_with('\n' actual2 := am.include_js(false) assert actual2 == expected2 assert actual2.contains(expected2) - // Combined output. clean_cache_dir(am.cache_dir) actual3 := am.include_js(true) assert actual3.contains(expected2) == false - assert actual3.starts_with('' { + } else if line == '' { state = .html } if line.contains('@include ') && false { // TODO - pos := line.index('@include ') or { - continue - } + pos := line.index('@include ') or { continue } file_name := line[pos + 9..] file_path := os.join_path('templates', '${file_name}.html') mut file_content := os.read_file(file_path) or { @@ -90,29 +84,23 @@ _ = footer file_content = file_content.replace("\'", '"') lines2 := file_content.split_into_lines() for l in lines2 { - lines.insert(i+1, l) + lines.insert(i + 1, l) } continue - //s.writeln(file_content) + // s.writeln(file_content) } else if line.contains('@js ') { - pos := line.index('@js') or { - continue - } + pos := line.index('@js') or { continue } s.write('') } else if line.contains('@css ') { - pos := line.index('@css') or { - continue - } + pos := line.index('@css') or { continue } s.write('') } else if line.contains('@if ') { s.writeln(str_end) - pos := line.index('@if') or { - continue - } + pos := line.index('@if') or { continue } s.writeln('if ' + line[pos + 4..] + '{') s.writeln(str_start) } else if line.contains('@end') { @@ -125,16 +113,14 @@ _ = footer s.writeln(str_start) } else if line.contains('@for') { s.writeln(str_end) - pos := line.index('@for') or { - continue - } + pos := line.index('@for') or { continue } s.writeln('for ' + line[pos + 4..] + '{') s.writeln(str_start) } else if state == .html && line.contains('span.') && line.ends_with('{') { // `span.header {` => `` class := line.find_between('span.', '{').trim_space() s.writeln('') - in_span = true + in_span = true } else if state == .html && line.contains('.') && line.ends_with('{') { // `.header {` => `
` class := line.find_between('.', '{').trim_space() @@ -162,4 +148,3 @@ _ = footer s.writeln('// === end of vweb html template ===') return s.str() } - diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index ba176c5835..daf3753dd0 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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(mut app T, port int) { for { mut conn := l.accept() or { panic('accept() failed') } handle_conn(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(mut app T, port int) { fn handle_conn(mut conn net.TcpConn, mut app T) { conn.set_read_timeout(1 * time.second) - defer { conn.close() or {} } - //fn handle_conn(conn net.Socket, app_ T) T { - //mut app := app_ - //first_line := strip(lines[0]) - + defer { + conn.close() or { } + } + // fn handle_conn(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(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(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(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()) ? }