mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: preserve the port value if given in HTTP::setGetVar (#5280). BUGFIX: allow username only input rather than user:pass combo.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101793 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
deaa1d675e
commit
9bb41119e7
@ -112,29 +112,31 @@ class HTTP {
|
|||||||
if(isset($parts['query'])) parse_str($parts['query'], $params);
|
if(isset($parts['query'])) parse_str($parts['query'], $params);
|
||||||
$params[$varname] = $varvalue;
|
$params[$varname] = $varvalue;
|
||||||
|
|
||||||
// Recompile Uri
|
// Generate URI segments and formatting
|
||||||
$newUri = $parts['scheme'] . '://' . (
|
$scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
|
||||||
isset($parts['user']) && $parts['user'] != '' && isset($parts['pass'])
|
$user = (isset($parts['user']) && $parts['user'] != '') ? $parts['user'] : '';
|
||||||
? $parts['user'] . ':' . $parts['pass'] . '@'
|
|
||||||
: ''
|
if($user != '') {
|
||||||
) .
|
// format in either user:pass@host.com or user@host.com
|
||||||
$parts['host'] . (
|
$user .= (isset($parts['pass']) && $parts['pass'] != '') ? ':' . $parts['pass'] . '@' : '@';
|
||||||
isset($parts['path']) && $parts['path'] != ''
|
}
|
||||||
? $parts['path']
|
|
||||||
: ''
|
|
||||||
) . (
|
|
||||||
($params)
|
|
||||||
// XHTML compliant by default
|
|
||||||
? '?' . http_build_query($params, null, '&')
|
|
||||||
: ''
|
|
||||||
) . (
|
|
||||||
isset($parts['fragment']) && $parts['fragment'] != ''
|
|
||||||
? '#' . $parts['fragment']
|
|
||||||
: ''
|
|
||||||
);
|
|
||||||
|
|
||||||
|
$host = (isset($parts['host'])) ? $parts['host'] : '';
|
||||||
|
$port = (isset($parts['port']) && $parts['port'] != '') ? ':'.$parts['port'] : '';
|
||||||
|
$path = (isset($parts['path']) && $parts['path'] != '') ? $parts['path'] : '';
|
||||||
|
|
||||||
|
// handle URL params which are existing / new
|
||||||
|
$params = ($params) ? '?' . http_build_query($params, null, '&') : '';
|
||||||
|
|
||||||
|
// keep fragments (anchors) intact.
|
||||||
|
$fragment = (isset($parts['fragment']) && $parts['fragment'] != '') ? '#'.$parts['fragment'] : '';
|
||||||
|
|
||||||
|
// Recompile URI segments
|
||||||
|
$newUri = $scheme . '://' . $user . $host . $port . $path . $params . $fragment;
|
||||||
|
|
||||||
if($isRelative) return Director::makeRelative($newUri);
|
if($isRelative) return Director::makeRelative($newUri);
|
||||||
else return $newUri;
|
|
||||||
|
return $newUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function RAW_setGetVar($varname, $varvalue, $currentURL = null) {
|
static function RAW_setGetVar($varname, $varvalue, $currentURL = null) {
|
||||||
|
@ -83,13 +83,30 @@ class HTTPTest extends SapphireTest {
|
|||||||
HTTP::setGetVar('foo', 'new', 'http://test.com/?foo=&foo=old'),
|
HTTP::setGetVar('foo', 'new', 'http://test.com/?foo=&foo=old'),
|
||||||
'Absolute URL and empty query param'
|
'Absolute URL and empty query param'
|
||||||
);
|
);
|
||||||
|
// http_build_query() escapes angular brackets, they should be correctly urldecoded by the browser client
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
// http_build_query() escapes angular brackets, they should be correctly urldecoded by the browser client
|
|
||||||
'http://test.com/?foo%5Btest%5D=one&foo%5Btest%5D=two',
|
'http://test.com/?foo%5Btest%5D=one&foo%5Btest%5D=two',
|
||||||
HTTP::setGetVar('foo[test]', 'two', 'http://test.com/?foo[test]=one'),
|
HTTP::setGetVar('foo[test]', 'two', 'http://test.com/?foo[test]=one'),
|
||||||
'Absolute URL and PHP array query string notation'
|
'Absolute URL and PHP array query string notation'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$urls = array(
|
||||||
|
'http://www.test.com:8080',
|
||||||
|
'http://test.com:3000/',
|
||||||
|
'http://test.com:3030/baz/',
|
||||||
|
'http://baz:foo@test.com',
|
||||||
|
'http://baz@test.com/',
|
||||||
|
'http://baz:foo@test.com:8080',
|
||||||
|
'http://baz@test.com:8080'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($urls as $testURL) {
|
||||||
|
$this->assertEquals(
|
||||||
|
$testURL .'?foo=bar',
|
||||||
|
HTTP::setGetVar('foo', 'bar', $testURL),
|
||||||
|
'Absolute URL and Port Number'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user