From d1da1e9bc1743591a36536865f583ee8b1af91bd Mon Sep 17 00:00:00 2001 From: yangshangbo <42314632+yangshangbo@users.noreply.github.com> Date: Tue, 15 Apr 2025 14:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B7=AF=E7=94=B1=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hono/app.v | 16 +++++++++------- hono/response.v | 2 +- hono/test.v | 2 ++ src/http_server.v | 6 ++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hono/app.v b/hono/app.v index efa96d0..95328b3 100644 --- a/hono/app.v +++ b/hono/app.v @@ -75,22 +75,24 @@ fn match_path_with_regex(real_path string, reg_path string) (bool, regex.RE) { mut one_more_star_reg := regex.regex_opt(r'\*{2,}') or { panic(err) } repl_on_e_more_star_fn := fn (re regex.RE, in_txt string, start int, end int) string { if in_txt.ends_with('*') { - return r'(\w+/){0,}\w+' + return r'([^#\?]/){0,}[^#\?]+' } - return r'(\w+//){0,}\w+' + return r'([^#\?]+//){0,}[^#\?]+' } - mut replaced_path := one_more_star_reg.replace_by_fn(reg_path, repl_on_e_more_star_fn) - replaced_path = replaced_path.replace('*', r'\w+') - mut pamam_reg := regex.regex_opt(r':\w+') or { panic(err) } + mut replaced_reg_path := reg_path.replace('?', r'\?') + replaced_reg_path = replaced_reg_path.replace('+', r'\+') + mut replaced_path := one_more_star_reg.replace_by_fn(replaced_reg_path, repl_on_e_more_star_fn) + replaced_path = replaced_path.replace('*', r'[^#\?]+') + mut pamam_reg := regex.regex_opt(r':[^/]+') or { panic(err) } repl_fn := fn (re regex.RE, in_txt string, start int, end int) string { match_str := in_txt[start..end] - return '(?P<${match_str[1..]}>\\w+)' + return '(?P<${match_str[1..]}>[^/]+)' } replaced_path = pamam_reg.replace_by_fn(replaced_path, repl_fn) replaced_path = replaced_path + '$' mut reg := regex.regex_opt(replaced_path) or { panic(err) } end := time.now() - println('Regex compile time: ${end - start}') + println('Regex time: ${end - start}') return reg.matches_string(real_path), reg } diff --git a/hono/response.v b/hono/response.v index 6754e1e..d55d8c1 100644 --- a/hono/response.v +++ b/hono/response.v @@ -20,7 +20,7 @@ pub fn Response.text(body string) http.Response { return http.Response{ header: http.new_header(http.HeaderConfig{ key: http.CommonHeader.content_type - value: 'plain/text' + value: 'text/plain' }) status_code: 200 body: body diff --git a/hono/test.v b/hono/test.v index 9a8a4e6..991f6a6 100644 --- a/hono/test.v +++ b/hono/test.v @@ -9,5 +9,7 @@ fn init() { is_match3, reg := match_path_with_regex('/users/123/posts/456/comments', '/users/**') println('reg: ${reg.query}') assert is_match3 == true + is_match4, _ := match_path_with_regex('/article+&9/杨/why-!important#999', '/article+&*/:杨/why-!important#999') + assert is_match4 == true println('all tests passed') } diff --git a/src/http_server.v b/src/http_server.v index 446cc30..3792c56 100644 --- a/src/http_server.v +++ b/src/http_server.v @@ -3,6 +3,12 @@ import hono fn main() { mut hono_app := hono.Hono{} + hono_app.get('/article/:id', fn (req hono.Request) http.Response { + println('Request url: ${req.url}') + println('Query map: ${req.query}') + println('Param map: ${req.param}') + return hono.Response.text('Hello, GET request! id = ${req.param['id']}, q = ${req.param['q']}') + }) hono_app.get('/api/**/:folder/user', fn (req hono.Request) http.Response { println('Request url: ${req.url}') println('Query map: ${req.query}')