From 9f74be4cf6a90eee84d98c541f134a2f51708dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Tue, 5 Jan 2021 01:30:27 +0100 Subject: [PATCH] vweb: route to index dynamically (#7782) --- vlib/vweb/tests/vweb_test.v | 2 +- vlib/vweb/tests/vweb_test_server.v | 7 ++-- vlib/vweb/vweb.v | 56 ++++++++++++++++++------------ 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index b68c8d1464..9fac04b8db 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -60,7 +60,7 @@ fn assert_common_headers(received string) { } fn test_a_simple_tcp_client_can_connect_to_the_vweb_server() { - received := simple_tcp_client({}) or { + received := simple_tcp_client(path: '/') or { assert err == '' return } diff --git a/vlib/vweb/tests/vweb_test_server.v b/vlib/vweb/tests/vweb_test_server.v index cb6d048582..b41d37c999 100644 --- a/vlib/vweb/tests/vweb_test_server.v +++ b/vlib/vweb/tests/vweb_test_server.v @@ -44,13 +44,12 @@ pub fn (mut app App) init_once() { eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.') } -pub fn (mut app App) index() { - app.text('Welcome to VWeb') +pub fn (mut app App) index() vweb.Result { + return app.text('Welcome to VWeb') } pub fn (mut app App) simple() vweb.Result { - app.text('A simple result') - return vweb.Result{} + return app.text('A simple result') } pub fn (mut app App) html_page() vweb.Result { diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index e93179e374..04295a7a40 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -367,19 +367,14 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { // mut url_words := vals[1][1..].split('/').filter(it != '') x := vals[1][1..].split('/') mut url_words := x.filter(it != '') - if url_words.len == 0 { - app.index() - return - } else { - // Parse URL query - if url_words.last().contains('?') { - words := url_words.last().after('?').split('&') - tmp_query := words.map(it.split('=')) - url_words[url_words.len - 1] = url_words.last().all_before('?') - for data in tmp_query { - if data.len == 2 { - app.query[data[0]] = data[1] - } + // Parse URL query + if url_words.len > 0 && url_words.last().contains('?') { + words := url_words.last().after('?').split('&') + tmp_query := words.map(it.split('=')) + url_words[url_words.len - 1] = url_words.last().all_before('?') + for data in tmp_query { + if data.len == 2 { + app.query[data[0]] = data[1] } } } @@ -420,15 +415,24 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { route_words_a = attrs.filter(it.to_lower() != 'get').map(it[1..].split('/')) } if attrs.len == 0 || (attrs.len == 1 && route_words_a.len == 0) { - // No routing for this method. If it matches, call it and finish matching - // since such methods have a priority. - // For example URL `/register` matches route `/:user`, but `fn register()` - // should be called first. - if (req_method_str == '' && - url_words[0] == method.name && url_words.len == 1) || - (req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1) { + if url_words.len > 0 { + // No routing for this method. If it matches, call it and finish matching + // since such methods have a priority. + // For example URL `/register` matches route `/:user`, but `fn register()` + // should be called first. + if (req_method_str == '' && + url_words[0] == method.name && url_words.len == 1) || + (req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1) { + $if debug { + println('easy match method=$method.name') + } + app.$method(vars) + return + } + } else if method.name == 'index' { + // handle / to .index() $if debug { - println('easy match method=$method.name') + println('route to .index()') } app.$method(vars) return @@ -436,8 +440,10 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { } else { mut req_method := []string{} if route_words_a.len > 0 { - for route_words in route_words_a { - if route_words[0] in methods_without_first && route_words.len == 1 { + for route_words_ in route_words_a { + // cannot move to line initialize line because of C error with map(it.filter(it != '')) + route_words := route_words_.filter(it != '') + if route_words.len == 1 && route_words[0] in methods_without_first { req_method << route_words[0] } if url_words.len == route_words.len || @@ -451,6 +457,10 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { mut matching := false mut unknown := false mut variables := []string{cap: route_words.len} + if route_words.len == 0 && url_words.len == 0 { + // index route + matching = true + } for i in 0 .. route_words.len { if url_words.len == i { variables << ''