Skip to content

Commit

Permalink
Separate encode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 3, 2016
1 parent f5aae3a commit 8dd104d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/Driver/Encoder/Http1Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ protected function encodeHeaders(array $headers)
foreach ($values as $value) {
switch (strtolower($name)) {
case 'host':
$data = sprintf("%s: %s\r\n%s", $name, Message\encode($value), $data);
$data = sprintf("%s: %s\r\n%s", $name, Message\encodeHeader($value), $data);
break;

default:
$data .= sprintf("%s: %s\r\n", $name, Message\encode($value));
$data .= sprintf("%s: %s\r\n", $name, Message\encodeHeader($value));
}
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/Message/BasicRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ public function getRequestTarget()
}

return $this->uri;

$target = encode($this->uri->getPath(), true);

if ('' === $target) {
$target = '/';
}

$query = $this->uri->getQueryValues();

if (empty($query)) {
return $target;
}

$encoded = [];

foreach ($query as $name => $values) {
foreach ($values as $value) {
if ('' === $value) {
$encoded[] = encode($name);
} else {
$encoded[] = sprintf('%s=%s', encode($name), encode($value));
}
}
}

return sprintf('%s?%s', $target, implode('&', $encoded));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Message/BasicUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ public function __toString()
}
}

$uri .= encode($this->path);
$uri .= encodePath($this->path);

$query = $this->encodeQuery();
if ($query) {
$uri = sprintf('%s?%s', $uri, $query);
}

if ($this->fragment) {
$uri = sprintf('%s#%s', $uri, encode($this->fragment));
$uri = sprintf('%s#%s', $uri, encodeValue($this->fragment));
}

return $uri;
Expand Down Expand Up @@ -514,9 +514,9 @@ protected function encodeAuthority()

if ('' !== $this->user) {
if ('' !== $this->password) {
$authority = sprintf('%s:%s@%s', encode($this->user), encode($this->password), $authority);
$authority = sprintf('%s:%s@%s', encodeValue($this->user), encodeValue($this->password), $authority);
} else {
$authority = sprintf('%s@%s', encode($this->user), $authority);
$authority = sprintf('%s@%s', encodeValue($this->user), $authority);
}
}

Expand All @@ -542,9 +542,9 @@ protected function encodeQuery()
foreach ($this->query as $name => $values) {
foreach ($values as $value) {
if ('' === $value) {
$query[] = encode($name);
$query[] = encodeValue($name);
} else {
$query[] = sprintf('%s=%s', encode($name), encode($value));
$query[] = sprintf('%s=%s', encodeValue($name), encodeValue($value));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Cookie/BasicCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __toString()
*/
public function toHeader()
{
return Message\encode($this->name) . '=' . Message\encode($this->value);
return Message\encodeValue($this->name) . '=' . Message\encodeValue($this->value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Cookie/SetCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function toHeader()
}

if ('' !== $this->path) {
$line .= '; Path=' . Message\encode($this->path, true);
$line .= '; Path=' . Message\encodePath($this->path);
}

if ('' !== $this->domain) {
Expand Down
58 changes: 46 additions & 12 deletions src/Message/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,58 @@

if (!function_exists(__NAMESPACE__ . '\encode')) {
/**
* Escapes all reserved chars.
* Escapes URI value.
*
* @param string $string
* @param bool $isPath
* @param string $value
*
* @return string
*/
function encode($string, $isPath = false)
function encodeValue($value)
{
if ($isPath) {
$regex = '/(?:[^A-Za-z0-9_\-\.~\/:%]+|%(?![A-Fa-f0-9]{2}))/';
} else {
$regex = '/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/% ]+|%(?![A-Fa-f0-9]{2}))/';
}
return preg_replace_callback(
'/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/%]+|%(?![A-Fa-f0-9]{2}))/',
function (array $matches) {
return rawurlencode($matches[0]);
},
$value
);
}

return preg_replace_callback($regex, function (array $matches) {
return rawurlencode($matches[0]);
}, $string);
/**
* Escapes path.
*
* @param string $path
*
* @return string
*/
function encodePath($path)
{
return preg_replace_callback(
'/(?:[^A-Za-z0-9_\-\.~\/:%]+|%(?![A-Fa-f0-9]{2}))/',
function (array $matches) {
return rawurlencode($matches[0]);
},
$path
);
}


/**
* Escapes header value.
*
* @param string $header
*
* @return string
*/
function encodeHeader($header)
{
return preg_replace_callback(
'/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/% ]+|%(?![A-Fa-f0-9]{2}))/',
function (array $matches) {
return rawurlencode($matches[0]);
},
$header
);
}

/**
Expand Down

0 comments on commit 8dd104d

Please sign in to comment.