Skip to content

Commit

Permalink
* fixed handling of URLs with more than a domain-name (fixed opposite…
Browse files Browse the repository at this point in the history
… of parse_url())

git-svn-id: http://svn.php.net/repository/pear/packages/Net_IDNA2/trunk@305340 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
neufeind committed Nov 14, 2010
1 parent 95cf2e7 commit 81c62f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
36 changes: 35 additions & 1 deletion Net/IDNA2.php
Expand Up @@ -2452,7 +2452,7 @@ public function decode($input, $one_time_encoding = false)
if (isset($parsed['scheme'])) { if (isset($parsed['scheme'])) {
$parsed['scheme'] .= (strtolower($parsed['scheme']) == 'mailto') ? ':' : '://'; $parsed['scheme'] .= (strtolower($parsed['scheme']) == 'mailto') ? ':' : '://';
} }
$return = join('', $parsed); $return = $this->_unparse_url($parsed);
} else { // parse_url seems to have failed, try without it } else { // parse_url seems to have failed, try without it
$arr = explode('.', $input); $arr = explode('.', $input);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
Expand Down Expand Up @@ -2483,6 +2483,40 @@ public function decode($input, $one_time_encoding = false)




// {{{ private // {{{ private
/**
* Opposite function to parse_url()
*
* Inspired by code from comments of php.net-documentation for parse_url()
*
* @array string parts as returned by parse_url()
* @return string
* @access private
*/
private function _unparse_url($parts_arr) {
if (!empty($parts_arr['scheme'])) {
$ret_url = $parts_arr['scheme'];
}
if(!empty($parts_arr['user'])) {
$ret_url .= $parts_arr['user'];
if (!empty($parts_arr['pass'])) {
$ret_url .= ':' . $parts_arr['pass'];
}
$ret_url .= '@';
}
$ret_url .= $parts_arr['host'];
if (!empty($parts_arr['port'])) {
$ret_url .= ':' . $parts_arr['port'];
}
$ret_url .= $parts_arr['path'];
if (!empty($parts_arr['query'])) {
$ret_url .= '?' . $parts_arr['query'];
}
if (!empty($parts_arr['fragment'])) {
$ret_url .= '#' . $parts_arr['fragment'];
}
return $ret_url;
}

/** /**
* The actual encoding algorithm. * The actual encoding algorithm.
* *
Expand Down
6 changes: 3 additions & 3 deletions tests/Net_IDNA2Test.php
Expand Up @@ -9,11 +9,11 @@ public function setUp() {
} }


public function testShouldDecodePortNumbersFragmentsAndUrisCorrectly() { public function testShouldDecodePortNumbersFragmentsAndUrisCorrectly() {
$result = $this->idn->decode('http://www.xn--ml-6kctd8d6a.org:8080/test.php?arg=1#fragment');

// not sure where this testcase came from, but it's wrong // not sure where this testcase came from, but it's wrong
// $result = $this->idn->decode('http://www.xn--ml-6kctd8d6a.org:8080/test.php?arg1=1&arg2=2#fragment');
// $this->assertSame("http://www.ехаmрlе.org:8080/test.php?arg=1#fragment", $result); // $this->assertSame("http://www.ехаmрlе.org:8080/test.php?arg=1#fragment", $result);
$this->assertSame("http://www.example.org:8080/test.php?arg=1#fragment", $result); $result = $this->idn->decode('http://xn--tst-qla.example.com:8080/test.php?arg1=1&arg2=2#fragment');
$this->assertSame("http://täst.example.com:8080/test.php?arg1=1&arg2=2#fragment", $result);
} }


public function testEncodingForGermanEszettUsingIDNA2003() { public function testEncodingForGermanEszettUsingIDNA2003() {
Expand Down

0 comments on commit 81c62f2

Please sign in to comment.