svn: /pear/packages/HTML_QuickForm2/trunk/ HTML/QuickForm2/Controller/Action/Jump.php tests/QuickForm2/Controller/Action/JumpTest.php
| From: | Alexey Borzov | Date: | Thu, 23 Feb 2012 12:27:59 +0000 |
| Subject: | svn: /pear/packages/HTML_QuickForm2/trunk/ HTML/QuickForm2/Controller/Action/Jump.php tests/QuickForm2/Controller/Action/JumpTest.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-64400@lists.php.net to get a copy of this message | ||
avb Thu, 23 Feb 2012 12:27:59 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=323441
Log:
Use host detection algorithm from Symfony, defaulting to $_SERVER['HTTP_HOST'] (see bug
#19216)
Bug: http://pear.php.net/bugs/19216 (unknown)
Changed paths:
U pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Controller/Action/Jump.php
U pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Controller/Action/JumpTest.php
Modified: pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Controller/Action/Jump.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Controller/Action/Jump.php 2012-02-23
01:26:46 UTC (rev 323440)
+++ pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Controller/Action/Jump.php 2012-02-23
12:27:59 UTC (rev 323441)
@@ -61,6 +61,12 @@
implements HTML_QuickForm2_Controller_Action
{
/**
+ * Whether to trust $_SERVER entries coming from proxies
+ * @var bool
+ */
+ protected $trustProxy = false;
+
+ /**
* Splits (part of) the URI into path and query components
*
* @param string $uri String of the form 'foo?bar'
@@ -119,6 +125,19 @@
}
/**
+ * Constructor, sets $trustProxy flag
+ *
+ * @param bool $trustProxy Whether to trust $_SERVER entries
+ * (specifically HTTP_X_FORWARDED_HOST) coming from proxies.
+ * Should be set to true if application is hosted behind a
+ * reverse proxy that you manage.
+ */
+ public function __construct($trustProxy = false)
+ {
+ $this->trustProxy = (bool)$trustProxy;
+ }
+
+ /**
* Resolves relative URL using current page's URL as base
*
* The method follows procedure described in section 4 of RFC 1808 and
@@ -129,7 +148,7 @@
*
* @return string Absolute URL
*/
- protected static function resolveRelativeURL($url)
+ protected function resolveRelativeURL($url)
{
$https = !empty($_SERVER['HTTPS']) && ('off' !=
strtolower($_SERVER['HTTPS']));
$scheme = ($https? 'https:': 'http:');
@@ -137,9 +156,22 @@
return $scheme . $url;
} else {
- $host = $scheme . '//' . $_SERVER['SERVER_NAME'] .
- (($https && 443 == $_SERVER['SERVER_PORT'] ||
- !$https && 80 == $_SERVER['SERVER_PORT'])? '':
':' . $_SERVER['SERVER_PORT']);
+ if ($this->trustProxy &&
!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
+ $parts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
+ $host = trim(end($parts));
+ } else {
+ $host = '';
+ foreach (array('HTTP_HOST', 'SERVER_NAME',
'SERVER_ADDR') as $key) {
+ if (!empty($_SERVER[$key])) {
+ $host = $_SERVER[$key];
+ break;
+ }
+ }
+ }
+ $host = $scheme . '//' . preg_replace('/:\d+$/', '',
$host)
+ . (($https && 443 == $_SERVER['SERVER_PORT']
+ || !$https && 80 == $_SERVER['SERVER_PORT'])
+ ? '' : ':' . $_SERVER['SERVER_PORT']);
if ('' == $url) {
return $host . $_SERVER['REQUEST_URI'];
@@ -175,7 +207,7 @@
$action = $page->getForm()->getAttribute('action');
// Bug #13087: RFC 2616 requires an absolute URI in Location header
if (!preg_match('@^([a-z][a-z0-9.+-]*):@i', $action)) {
- $action = self::resolveRelativeURL($action);
+ $action = $this->resolveRelativeURL($action);
}
if (!$page->getController()->propagateId()) {
Modified: pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Controller/Action/JumpTest.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Controller/Action/JumpTest.php 2012-02-23
01:26:46 UTC (rev 323440)
+++ pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Controller/Action/JumpTest.php 2012-02-23
12:27:59 UTC (rev 323441)
@@ -6,7 +6,7 @@
*
* LICENSE:
*
- * Copyright (c) 2006-2011, Alexey Borzov <avb@php.net>,
+ * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
* Bertrand Mansion <golgote@mamasam.com>
* All rights reserved.
*
@@ -71,7 +71,7 @@
->will($this->returnArgument(0));
// see RFC 3986, section 5.4
- $_SERVER['SERVER_NAME'] = 'a';
+ $_SERVER['HTTP_HOST'] = 'a';
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['REQUEST_URI'] = '/b/c/d;p?q';
}
@@ -232,5 +232,74 @@
$this->assertNotRegexp('/^https:/i',
$mockPage->handle('jump'));
}
+
+ /**
+ * Use HTTP_HOST as the default, falling back to SERVER_NAME (and SERVER_ADDR)
+ *
+ * @link http://pear.php.net/bugs/bug.php?id=19216
+ */
+ public function testBug19216()
+ {
+ $controller = new HTML_QuickForm2_Controller('bug19216');
+ $mockPage = $this->getMock(
+ 'HTML_QuickForm2_Controller_Page', array('populateForm'),
+ array(new HTML_QuickForm2('testhost'))
+ );
+ $controller->addPage($mockPage);
+ $controller->addHandler('jump', $this->mockJump);
+ $mockPage->getForm()->setAttribute('action', '/foo');
+
+ $_SERVER['HTTP_HOST'] = 'example.org';
+ $_SERVER['SERVER_NAME'] = 'example.com';
+ $_SERVER['SERVER_ADDR'] = '1.2.3.4';
+ $this->assertStringStartsWith('http://example.org/foo?',
$mockPage->handle('jump'));
+
+ $_SERVER['HTTP_HOST'] = '';
+ $this->assertStringStartsWith('http://example.com/foo?',
$mockPage->handle('jump'));
+
+ $_SERVER['SERVER_NAME'] = '';
+ $this->assertStringStartsWith('http://1.2.3.4/foo?',
$mockPage->handle('jump'));
+ }
+
+ public function testHttpHostWithPortNumber()
+ {
+ $controller = new HTML_QuickForm2_Controller('weirdhost');
+ $mockPage = $this->getMock(
+ 'HTML_QuickForm2_Controller_Page', array('populateForm'),
+ array(new HTML_QuickForm2('weirdhost'))
+ );
+ $controller->addPage($mockPage);
+ $controller->addHandler('jump', $this->mockJump);
+ $mockPage->getForm()->setAttribute('action', '/foo');
+
+ $_SERVER['HTTP_HOST'] = 'example.org:80';
+ $this->assertStringStartsWith('http://example.org/foo?',
$mockPage->handle('jump'));
+ }
+
+ public function testHttpXForwardedHost()
+ {
+ $_SERVER['HTTP_X_FORWARDED_HOST'] = 'example.org, example.com';
+ $_SERVER['HTTP_HOST'] = 'localhost';
+
+ $controller = new HTML_QuickForm2_Controller('forwarded');
+ $mockPage = $this->getMock(
+ 'HTML_QuickForm2_Controller_Page', array('populateForm'),
+ array(new HTML_QuickForm2('forwarded'))
+ );
+ $controller->addPage($mockPage);
+ $controller->addHandler('jump', $this->mockJump);
+ $mockPage->getForm()->setAttribute('action', '/foo');
+
+ $this->assertStringStartsWith('http://localhost/foo?',
$mockPage->handle('jump'));
+
+ $trustingJump = $this->getMock(
+ 'HTML_QuickForm2_Controller_Action_Jump', array('doRedirect'),
array(true)
+ );
+ $trustingJump->expects($this->atLeastOnce())->method('doRedirect')
+ ->will($this->returnArgument(0));
+ $controller->addHandler('jump', $trustingJump);
+
+ $this->assertStringStartsWith('http://example.com/foo?',
$mockPage->handle('jump'));
+ }
}
?>
\ No newline at end of file