8000 Fixed broken logic in Request::getHttpHost(). It was ignoring HTTP_HO… · corra/symfony@f019541 · GitHub
[go: up one dir, main page]

Skip to content

Commit f019541

Browse files
committed
Fixed broken logic in Request::getHttpHost(). It was ignoring HTTP_HOST completely.
1 parent 0b9f3d8 commit f019541

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,18 @@ public function getPort()
488488
*/
489489
public function getHttpHost()
490490
{
491-
$host = $this->getHost();
492491
$scheme = $this->getScheme();
493-
$name = $this->server->get('SERVER_NAME');
492+
$host = $this->getHost();
493+
if(!$host) {
494+
$host = $this->server->get('SERVER_NAME');
495+
}
494496
$port = $this->getPort();
495497

496498
if (('http' == $scheme && $port == 80) || ('https' == $scheme && $port == 443)) {
497-
return $name;
499+
return $host;
498500
}
499501

500-
return $name.':'.$port;
502+
return $host.':'.$port;
501503
}
502504

503505
/**

tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function testGetUri()
183183
// http://hostname:8080/index.php/path/info?query=string
184184

185185
$server['HTTP_HOST'] = 'hostname:8080';
186-
$server['SERVER_NAME'] = 'hostname';
186+
$server['SERVER_NAME'] = 'servername';
187187
$server['SERVER_PORT'] = '8080';
188188

189189
$server['QUERY_STRING'] = 'query=string';
@@ -202,7 +202,7 @@ public function testGetUri()
202202

203203
// Use std port number
204204
$server['HTTP_HOST'] = 'hostname';
205-
$server['SERVER_NAME'] = 'hostname';
205+
$server['SERVER_NAME'] = 'servername';
206206
$server['SERVER_PORT'] = '80';
207207

208208
$request->initialize(array(), array(), array(), array(), array(), $server);
@@ -211,20 +211,20 @@ public function testGetUri()
211211

212212
// Without HOST HEADER
213213
unset($server['HTTP_HOST']);
214-
$server['SERVER_NAME'] = 'hostname';
214+
$server['SERVER_NAME'] = 'servername';
215215
$server['SERVER_PORT'] = '80';
216216

217217
$request->initialize(array(), array(), array(), array(), array(), $server);
218218

219-
$this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
219+
$this->assertEquals('http://servername/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
220220

221221
// Request with URL REWRITING (hide index.php)
222222
// RewriteCond %{REQUEST_FILENAME} !-f
223223
// RewriteRule ^(.*)$ index.php [QSA,L]
224224
// http://hostname:8080/path/info?query=string
225225
$server = array();
226226
$server['HTTP_HOST'] = 'hostname:8080';
227-
$server['SERVER_NAME'] = 'hostname';
227+
$server['SERVER_NAME'] = 'servername';
228228
$server['SERVER_PORT'] = '8080';
229229

230230
$server['REDIRECT_QUERY_STRING'] = 'query=string';
@@ -242,7 +242,7 @@ public function testGetUri()
242242
// Use std port number
243243
// http://hostname/path/info?query=string
244244
$server['HTTP_HOST'] = 'hostname';
245-
$server['SERVER_NAME'] = 'hostname';
245+
$server['SERVER_NAME'] = 'servername';
246246
$server['SERVER_PORT'] = '80';
247247

248248
$request->initialize(array(), array(), array(), array(), array(), $server);
@@ -251,12 +251,12 @@ public function testGetUri()
251251

252252
// Without HOST HEADER
253253
unset($server['HTTP_HOST']);
254-
$server['SERVER_NAME'] = 'hostname';
254+
$server['SERVER_NAME'] = 'servername';
255255
$server['SERVER_PORT'] = '80';
256256

257257
$request->initialize(array(), array(), array(), array(), array(), $server);
258258

259-
$this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
259+
$this->assertEquals('http://servername/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
260260
}
261261

262262
/**
@@ -282,7 +282,7 @@ public function testGetUriForPath()
282282
// http://hostname:8080/index.php/path/info?query=string
283283

284284
$server['HTTP_HOST'] = 'hostname:8080';
285-
$server['SERVER_NAME'] = 'hostname';
285+
$server['SERVER_NAME'] = 'servername';
286286
$server['SERVER_PORT'] = '8080';
287287

288288
$server['QUERY_STRING'] = 'query=string';
@@ -301,7 +301,7 @@ public function testGetUriForPath()
301301

302302
// Use std port number
303303
$server['HTTP_HOST'] = 'hostname';
304-
$server['SERVER_NAME'] = 'hostname';
304+
$server['SERVER_NAME'] = 'servername';
305305
$server['SERVER_PORT'] = '80';
306306

307307
$request->initialize(array(), array(), array(), array(), array(), $server);
@@ -310,20 +310,20 @@ public function testGetUriForPath()
310310

311311
// Without HOST HEADER
312312
unset($server['HTTP_HOST']);
313-
$server['SERVER_NAME'] = 'hostname';
313+
$server['SERVER_NAME'] = 'servername';
314314
$server['SERVER_PORT'] = '80';
315315

316316
$request->initialize(array(), array(), array(), array(), array(), $server);
317317

318-
$this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
318+
$this->assertEquals('http://servername/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
319319

320320
// Request with URL REWRITING (hide index.php)
321321
// RewriteCond %{REQUEST_FILENAME} !-f
322322
// RewriteRule ^(.*)$ index.php [QSA,L]
323323
// http://hostname:8080/path/info?query=string
324324
$server = array();
325325
$server['HTTP_HOST'] = 'hostname:8080';
326-
$server['SERVER_NAME'] = 'hostname';
326+
$server['SERVER_NAME'] = 'servername';
327327
$server['SERVER_PORT'] = '8080';
328328

329329
$server['REDIRECT_QUERY_STRING'] = 'query=string';
@@ -341,7 +341,7 @@ public function testGetUriForPath()
341341
// Use std port number
342342
// http://hostname/path/info?query=string
343343
$server['HTTP_HOST'] = 'hostname';
344-
$server['SERVER_NAME'] = 'hostname';
344+
$server['SERVER_NAME'] = 'servername';
345345
$server['SERVER_PORT'] = '80';
346346

347347
$request->initialize(array(), array(), array(), array(), array(), $server);
@@ -350,13 +350,13 @@ public function testGetUriForPath()
350350

351351
// Without HOST HEADER
352352
unset($server['HTTP_HOST']);
353-
$server['SERVER_NAME'] = 'hostname';
353+
$server['SERVER_NAME'] = 'servername';
354354
$server['SERVER_PORT'] = '80';
355355

356356
$request->initialize(array(), array(), array(), array(), array(), $server);
357357

358-
$this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
359-
$this->assertEquals('hostname', $request->getHttpHost());
358+
$this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
359+
$this->assertEquals('servername', $request->getHttpHost());
360360
}
361361

362362
/**

0 commit comments

Comments
 (0)
0