@@ -246,12 +246,8 @@ public function match(ServerRequestInterface $request) : RouteInterface
246246 $ allowedMethods = [];
247247
248248 foreach ($ this ->routes as $ route ) {
249- $ routeHost = $ route ->getHost ();
250- if (isset ($ routeHost )) {
251- $ routeHost = $ this ->hosts [$ routeHost ] ?? $ routeHost ;
252- if ($ routeHost <> $ requestHost ) {
253- continue ;
254- }
249+ if (!$ this ->compareHosts ($ route ->getHost (), $ requestHost )) {
250+ continue ;
255251 }
256252
257253 // https://github.com/sunrise-php/http-router/issues/50
@@ -300,9 +296,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
300296 try {
301297 return $ this ->handle ($ request );
302298 } catch (MethodNotAllowedException | RouteNotFoundException $ e ) {
303- return $ handler -> handle (
304- $ request -> withAttribute ( self :: ATTR_NAME_FOR_ROUTING_ERROR , $ e )
305- );
299+ $ request = $ request -> withAttribute ( self :: ATTR_NAME_FOR_ROUTING_ERROR , $ e );
300+
301+ return $ handler -> handle ( $ request );
306302 }
307303 }
308304
@@ -319,4 +315,34 @@ public function load(LoaderInterface ...$loaders) : void
319315 $ this ->addRoute (...$ loader ->load ()->all ());
320316 }
321317 }
318+
319+ /**
320+ * Compares the given route host and the given request host
321+ *
322+ * Returns `true` if the route host is `null`
323+ * or if the route host is equal to the request host,
324+ * otherwise returns `false`.
325+ *
326+ * @param null|string $routeHost
327+ * @param string $requestHost
328+ *
329+ * @return bool
330+ */
331+ private function compareHosts (?string $ routeHost , string $ requestHost ) : bool
332+ {
333+ if (null === $ routeHost ) {
334+ return true ;
335+ }
336+
337+ // trying to resolve the route host....
338+ if (isset ($ this ->hosts [$ routeHost ])) {
339+ $ routeHost = $ this ->hosts [$ routeHost ];
340+ }
341+
342+ if ($ requestHost === $ routeHost ) {
343+ return true ;
344+ }
345+
346+ return false ;
347+ }
322348}
0 commit comments