Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/2710' into develop
Browse files Browse the repository at this point in the history
Forward port #2710
  • Loading branch information
weierophinney committed Oct 10, 2012
2 parents 811b561 + 1d1c995 commit 5945314
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
60 changes: 58 additions & 2 deletions library/Zend/Mvc/Router/Http/Segment.php
Expand Up @@ -24,6 +24,38 @@
*/
class Segment implements RouteInterface
{
/**
* Map of allowed special chars in path segments.
*
* http://tools.ietf.org/html/rfc3986#appendix-A
* segement = *pchar
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*
* @var array
*/
private static $urlencodeCorrectionMap = array(
'%21' => "!", // sub-delims
'%24' => "$", // sub-delims
'%26' => "&", // sub-delims
'%27' => "'", // sub-delims
'%28' => "(", // sub-delims
'%29' => ")", // sub-delims
'%2A' => "*", // sub-delims
// '%2B' => "+", // sub-delims - special value for php/urlencode
'%2C' => ",", // sub-delims
// '%2D' => "-", // unreserved - not touched by urlencode
// '%2E' => ".", // unreserved - not touched by urlencode
'%3A' => ":", // pchar
'%3B' => ";", // sub-delims
'%3D' => "=", // sub-delims
'%40' => "@", // pchar
// '%5F' => "_", // unreserved - not touched by urlencode
'%7E' => "~", // unreserved
);

/**
* Parts of the route.
*
Expand Down Expand Up @@ -264,7 +296,7 @@ protected function buildPath(array $parts, array $mergedParams, $isOptional, $ha
$skip = false;
}

$path .= urlencode($mergedParams[$part[1]]);
$path .= $this->encode($mergedParams[$part[1]]);

$this->assembledParams[] = $part[1];
break;
Expand Down Expand Up @@ -330,7 +362,7 @@ public function match(Request $request, $pathOffset = null)

foreach ($this->paramMap as $index => $name) {
if (isset($matches[$index]) && $matches[$index] !== '') {
$params[$name] = urldecode($matches[$index]);
$params[$name] = $this->decode($matches[$index]);
}
}

Expand Down Expand Up @@ -367,4 +399,28 @@ public function getAssembledParams()
{
return $this->assembledParams;
}

/**
* Encode a path segment.
*
* @param string $value
* @return string
*/
private function encode($value)
{
$encoded = urlencode($value);
$encoded = strtr($encoded, self::$urlencodeCorrectionMap);
return $encoded;
}

/**
* Decode a path segment.
*
* @param string $value
* @return string
*/
private function decode($value)
{
return urldecode($value);
}
}
6 changes: 6 additions & 0 deletions tests/ZendTest/Mvc/Router/Http/SegmentTest.php
Expand Up @@ -153,6 +153,12 @@ public static function routeProvider()
null,
array('foo' => 'foo bar')
),
'urlencode-flaws-corrected' => array(
new Segment('/:foo'),
"/!$&'()*,-.:;=@_~",
null,
array('foo' => "!$&'()*,-.:;=@_~")
),
'empty-matches-are-replaced-with-defaults' => array(
new Segment('/foo[/:bar]/baz-:baz', array(), array('bar' => 'bar')),
'/foo/baz-baz',
Expand Down

0 comments on commit 5945314

Please sign in to comment.