From f7135979f53a65f9181db7e2c75c518a55d0f046 Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Fri, 1 Jan 2021 21:57:45 +0100 Subject: [PATCH] vweb: fixed req type without path (#7792) --- vlib/vweb/vweb.v | 64 ++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 79901f2e9f..92ab7a88d4 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -389,14 +389,44 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { $if method.return_type is Result { attrs := method.attrs route_words_a = [][]string{} + // Get methods + // Get is default + mut req_method_str := '$req.method' + if req.method == .post { + if 'post' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'post').map(it[1..].split('/')) + } + } else if req.method == .put { + if 'put' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'put').map(it[1..].split('/')) + } + } else if req.method == .patch { + if 'patch' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'patch').map(it[1..].split('/')) + } + } else if req.method == .delete { + if 'delete' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'delete').map(it[1..].split('/')) + } + } else if req.method == .head { + if 'head' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'head').map(it[1..].split('/')) + } + } else if req.method == .options { + if 'options' in attrs { + route_words_a = attrs.filter(it.to_lower() != 'options').map(it[1..].split('/')) + } + } else { + route_words_a = attrs.filter(it.to_lower() != 'get').map(it[1..].split('/')) + } if attrs.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 == .get && + if (req_method_str == '' && url_words[0] == method.name && url_words.len == 1) || - (req.method == .post && url_words[0] + '_post' == method.name) { + (req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1) { $if debug { println('easy match method=$method.name') } @@ -404,36 +434,6 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { return } } else { - // Get methods - // Get is default - mut req_method_str := '$req.method' - if req.method == .post { - if 'post' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'post').map(it[1..].split('/')) - } - } else if req.method == .put { - if 'put' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'put').map(it[1..].split('/')) - } - } else if req.method == .patch { - if 'patch' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'patch').map(it[1..].split('/')) - } - } else if req.method == .delete { - if 'delete' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'delete').map(it[1..].split('/')) - } - } else if req.method == .head { - if 'head' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'head').map(it[1..].split('/')) - } - } else if req.method == .options { - if 'options' in attrs { - route_words_a = attrs.filter(it.to_lower() != 'options').map(it[1..].split('/')) - } - } else { - route_words_a = attrs.filter(it.to_lower() != 'get').map(it[1..].split('/')) - } mut req_method := []string{} if route_words_a.len > 0 { for route_words in route_words_a {