@@ -30,6 +30,7 @@ local new_tab = base.new_tab
3030local tonumber = tonumber
3131local ipairs = ipairs
3232local ffi = require (" ffi" )
33+ local C = ffi .C
3334local ffi_cast = ffi .cast
3435local ffi_cdef = ffi .cdef
3536local insert_tab = table.insert
101102
102103
103104ffi_cdef [[
105+ int memcmp(const void *s1, const void *s2, size_t n);
106+
104107 void *radix_tree_new();
105108 int radix_tree_destroy(void *t);
106109 int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
126129local _M = { _VERSION = 1.7 }
127130
128131
132+ local function has_suffix (s , suffix )
133+ if type (s ) ~= " string" or type (suffix ) ~= " string" then
134+ return false
135+ end
136+ if # s < # suffix then
137+ return false
138+ end
139+ local rc = C .memcmp (ffi .cast (" char *" , s ) + # s - # suffix , suffix , # suffix )
140+ return rc == 0
141+ end
142+
143+
129144-- only work under lua51 or luajit
130145local function setmt__gc (t , mt )
131146 local prox = newproxy (true )
@@ -258,9 +273,7 @@ function pre_insert_route(self, path, route)
258273 local is_wildcard = false
259274 if h and h :sub (1 , 1 ) == ' *' then
260275 is_wildcard = true
261- h = h :sub (2 ):reverse ()
262- else
263- h = h :reverse ()
276+ h = h :sub (2 )
264277 end
265278
266279 insert_tab (route_opts .hosts , is_wildcard )
@@ -272,9 +285,7 @@ function pre_insert_route(self, path, route)
272285 local host = hosts
273286 if host :sub (1 , 1 ) == ' *' then
274287 is_wildcard = true
275- host = host :sub (2 ):reverse ()
276- else
277- host = host :reverse ()
288+ host = host :sub (2 )
278289 end
279290
280291 route_opts .hosts = {is_wildcard , host }
@@ -383,20 +394,11 @@ end
383394
384395
385396local function match_host (route_host_is_wildcard , route_host , request_host )
386- if type (request_host ) ~= " string" or # route_host > # request_host then
387- return false
388- end
389-
390397 if not route_host_is_wildcard then
391398 return route_host == request_host
392399 end
393400
394- local i = request_host :find (route_host , 1 , true )
395- if i ~= 1 then
396- return false
397- end
398-
399- return true
401+ return has_suffix (request_host , route_host )
400402end
401403
402404
@@ -564,20 +566,16 @@ local function match_route_opts(route, opts, ...)
564566 if route .hosts then
565567 local matched = false
566568
567- if opts .host and not opts .host_reversed then
568- opts .host_reversed = opts .host :reverse ()
569- end
570-
571569 local hosts = route .hosts
572- local reverse_host = opts .host_reversed
573- if reverse_host then
570+ local host = opts .host
571+ if host then
574572 for i = 1 , # hosts , 2 do
575- if match_host (hosts [i ], hosts [i + 1 ], reverse_host ) then
573+ if match_host (hosts [i ], hosts [i + 1 ], host ) then
576574 if opts_matched_exists then
577575 if hosts [i ] then
578- opts .matched ._host = ( hosts [i + 1 ] .. " * " ): reverse ()
576+ opts .matched ._host = " * " .. hosts [i + 1 ]
579577 else
580- opts .matched ._host = hosts [ i + 1 ]: reverse ()
578+ opts .matched ._host = opts . host
581579 end
582580 end
583581 matched = true
0 commit comments