mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Merge pull request #69 from f/master
Radix-tree already has url param parsing with state-machine implementation which is faster than regex parsing. We don't need regex parsing anymore.
This commit is contained in:
		
						commit
						0a1abe9a42
					
				
					 3 changed files with 13 additions and 35 deletions
				
			
		|  | @ -30,7 +30,7 @@ class Kemal::Handler < HTTP::Handler | ||||||
|     lookup = @tree.find radix_path(context.request.override_method as String, context.request.path) |     lookup = @tree.find radix_path(context.request.override_method as String, context.request.path) | ||||||
|     if lookup.found? |     if lookup.found? | ||||||
|       route = lookup.payload as Route |       route = lookup.payload as Route | ||||||
|       if route.match?(context.request) |       context.request.url_params = lookup.params | ||||||
|       begin |       begin | ||||||
|         context.response.content_type = "text/html" |         context.response.content_type = "text/html" | ||||||
|         body = route.handler.call(context).to_s |         body = route.handler.call(context).to_s | ||||||
|  | @ -41,7 +41,6 @@ class Kemal::Handler < HTTP::Handler | ||||||
|         return render_500(context, ex.to_s) |         return render_500(context, ex.to_s) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|     end |  | ||||||
|     # Render 404 unless a route matches |     # Render 404 unless a route matches | ||||||
|     return render_404(context) |     return render_404(context) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  | @ -35,14 +35,11 @@ class Kemal::ParamParser | ||||||
|     parse_part(@request.query) |     parse_part(@request.query) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Ditto: This needs memoization without the huge AllParamTypes union :| |  | ||||||
|   def parse_url_params |   def parse_url_params | ||||||
|     if @request.url_params |     params = @request.url_params | ||||||
|       url_params = @request.url_params.not_nil! |     if params | ||||||
|       name_table = url_params.regex.name_table |       params.not_nil!.each do |key, value| | ||||||
|       url_params.size.times do |i| |         @params[key] = value | ||||||
|         name = (name_table.fetch(i + 1) { i + 1 }) as String |  | ||||||
|         @params[name] = url_params[i + 1] |  | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  | @ -6,17 +6,6 @@ class Kemal::Route | ||||||
|   getter method |   getter method | ||||||
| 
 | 
 | ||||||
|   def initialize(@method, @path, &@handler : HTTP::Server::Context -> _) |   def initialize(@method, @path, &@handler : HTTP::Server::Context -> _) | ||||||
|     @compiled_regex = pattern_to_regex(@path) |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   def match?(request) |  | ||||||
|     self.class.check_for_method_override!(request) |  | ||||||
|     return nil unless request.override_method == @method |  | ||||||
|     return true if request.path.not_nil!.includes?(':') && request.path.not_nil! == @path |  | ||||||
|     request.path.not_nil!.match(@compiled_regex) do |url_params| |  | ||||||
|       request.url_params = url_params |  | ||||||
|       return true |  | ||||||
|     end |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Checks if request params contain _method param to override request incoming method |   # Checks if request params contain _method param to override request incoming method | ||||||
|  | @ -36,11 +25,4 @@ class Kemal::Route | ||||||
|     override_method = override_method.upcase |     override_method = override_method.upcase | ||||||
|     return (override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE") |     return (override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE") | ||||||
|   end |   end | ||||||
| 
 |  | ||||||
|   private def pattern_to_regex(pattern) |  | ||||||
|     pattern = pattern.gsub(/\:(?<param>\w+)/) do |_, match| |  | ||||||
|       "(?<#{match["param"]}>.*)" |  | ||||||
|     end |  | ||||||
|     Regex.new "^#{pattern}/?$" |  | ||||||
|   end |  | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue