This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -79,12 +79,23 @@ protected function detectHost()
79
79
80
80
if (isset ($ _SERVER ['HTTP_HOST ' ]) && !empty ($ _SERVER ['HTTP_HOST ' ])) {
81
81
// Detect if the port is set in SERVER_PORT and included in HTTP_HOST
82
- if (isset ($ _SERVER ['SERVER_PORT ' ])) {
83
- $ portStr = ': ' . $ _SERVER ['SERVER_PORT ' ];
84
- if (substr ($ _SERVER ['HTTP_HOST ' ], 0 -strlen ($ portStr ), strlen ($ portStr )) == $ portStr ) {
85
- $ this ->setHost (substr ($ _SERVER ['HTTP_HOST ' ], 0 , 0 -strlen ($ portStr )));
82
+ if (isset ($ _SERVER ['SERVER_PORT ' ])
83
+ && preg_match ('/^(?P<host>.*?):(?P<port>\d+)$/ ' , $ _SERVER ['HTTP_HOST ' ], $ matches )
84
+ ) {
85
+ // If they are the same, set the host to just the hostname
86
+ // portion of the Host header.
87
+ if ((int ) $ matches ['port ' ] === (int ) $ _SERVER ['SERVER_PORT ' ]) {
88
+ $ this ->setHost ($ matches ['host ' ]);
86
89
return ;
87
90
}
91
+
92
+ // At this point, we have a SERVER_PORT that differs from the
93
+ // Host header, indicating we likely have a port-forwarding
94
+ // situation. As such, we'll set the host and port from the
95
+ // matched values.
96
+ $ this ->setPort ((int ) $ matches ['port ' ]);
97
+ $ this ->setHost ($ matches ['host ' ]);
98
+ return ;
88
99
}
89
100
90
101
$ this ->setHost ($ _SERVER ['HTTP_HOST ' ]);
Original file line number Diff line number Diff line change @@ -219,4 +219,12 @@ public function testCanUseXForwardedPortIfProvided()
219
219
$ url ->setUseProxy (true );
220
220
$ this ->assertEquals ('http://www.secondhost.org:8888 ' , $ url ->__invoke ());
221
221
}
222
+
223
+ public function testUsesHostHeaderWhenPortForwardingDetected ()
224
+ {
225
+ $ _SERVER ['HTTP_HOST ' ] = 'localhost:10088 ' ;
226
+ $ _SERVER ['SERVER_PORT ' ] = 10081 ;
227
+ $ url = new Helper \ServerUrl ();
228
+ $ this ->assertEquals ('http://localhost:10088 ' , $ url ->__invoke ());
229
+ }
222
230
}
You can’t perform that action at this time.
0 commit comments