Skip to content

Commit

Permalink
Improve User Info handling
Browse files Browse the repository at this point in the history
- Component formatting simplified
- User Info are now rawurlencoded
- Fix Http::createFromServer to work with HTTP_AUTHORIZATION
  • Loading branch information
nyamsprod committed Oct 15, 2015
1 parent 592f4f4 commit d9d99c6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All Notable changes to `League\Url` will be documented in this file

### Fixed

- [BUG FIX] `User` and `Pass` encoding
- [BUG FIX] `Http::createFromServer` handling userinfo when not using `mod_php` 'HTTP_AUTHORIZATION'
- [BUG FIX] `UriParser` handling URI strings with invalid scheme
- [BUG FIX] `QueryParser` handling numeric index [issue #25](https://github.com/thephpleague/uri/issues/25)
- [BUG FIX] `DataPath` mimetype syntax validation [issue #21](https://github.com/thephpleague/uri/issues/21)
Expand Down
2 changes: 1 addition & 1 deletion src/Components/DataPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected static function format($mimetype, $parameters, $isBinaryData, $data)
$parameters .= ';'.static::BINARY_PARAMETER;
}

return static::encodePath($mimetype.$parameters.','.$data);
return static::encode($mimetype.$parameters.','.$data);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Components/HierarchicalPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function __toString()
}
$path = $front_delimiter.implode(static::$separator, array_map([$this, 'encode'], $this->data));

return $this->encodePath($path);
return $this->encode($path);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ protected function validate($path)
*/
public function __toString()
{
return $this->encodePath(static::encode($this->data));
return static::encode($this->data);
}
}
15 changes: 0 additions & 15 deletions src/Components/PathTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,6 @@ public function withTypecode($type)
return $this->modify($path.$extension);
}

/**
* Normalize path encoding
*
* @param string $path the path to normalize
*
* @param string
*
*/
protected static function encodePath($path)
{
return preg_replace_callback(',(?<encode>%[0-9A-F]{2}),i', function (array $matches) {
return strtoupper($matches['encode']);
}, $path);
}

/**
* Encode a segment or the entire path string
*
Expand Down
14 changes: 12 additions & 2 deletions src/Schemes/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,19 @@ protected static function fetchServerScheme(array $server)
*/
protected static function fetchServerUserInfo(array $server)
{
$server += ['PHP_AUTH_USER' => null, 'PHP_AUTH_PW' => null];
$server += ['PHP_AUTH_USER' => null, 'PHP_AUTH_PW' => null, 'HTTP_AUTHORIZATION' => null];
$parser = new UriParser();
if (!empty($server['HTTP_AUTHORIZATION'])
&& 0 === strpos(strtolower($server['HTTP_AUTHORIZATION']), 'basic')
) {
$res = explode(':', base64_decode(substr($server['HTTP_AUTHORIZATION'], 6)), 2);
$login = array_shift($res);
$pass = array_shift($res);

return $parser->buildUserInfo(rawurlencode($login), rawurlencode($pass));
}

return (new UriParser())->buildUserInfo($server['PHP_AUTH_USER'], $server['PHP_AUTH_PW']);
return $parser->buildUserInfo(rawurlencode($server['PHP_AUTH_USER']), rawurlencode($server['PHP_AUTH_PW']));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Types/ImmutableComponentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ protected static function encode($value)
return preg_quote($value, '/');
}, static::$characters_set));

return preg_replace_callback(
$str = preg_replace_callback(
'/(?:[^'.$reservedChars.']+|%(?![A-Fa-f0-9]{2}))/',
function (array $matches) {
return rawurlencode($matches[0]);
},
$value
);

return preg_replace_callback(',(?<encode>%[0-9A-F]{2}),i', function (array $matches) {
return strtoupper($matches['encode']);
}, $str);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Types/ImmutablePropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function filterPropertyValue($value)
(is_object($value) ? get_class($value) : gettype($value))
));
}

if ('' === $value) {
return null;
}
Expand Down
12 changes: 12 additions & 0 deletions test/Schemes/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public function validServerArray()
'HTTP_HOST' => 'localhost:23',
],
],
'with user info and HTTP AUTHORIZATION' => [
'https://foo:bar@localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTP_AUTHORIZATION' => 'basic '.base64_encode('foo:bar'),
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'HTTP_HOST' => 'localhost:23',
],
],
'without request uri' => [
'https://127.0.0.1:23/toto?foo=bar',
[
Expand Down

0 comments on commit d9d99c6

Please sign in to comment.