修改路由匹配正则表达式,添加更多测试用例
This commit is contained in:
+9
-7
@@ -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
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
|
||||
@@ -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}')
|
||||
|
||||
Reference in New Issue
Block a user