Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Improving parser
Browse files Browse the repository at this point in the history
- Adding the ParserException Exception class to isolate Parser Exception
 the ParserException extends InvalidArgumentException so there are two ways
to catch the thrown error

- Improve URI parsing by removing all regular expressions.
  • Loading branch information
nyamsprod committed Nov 2, 2016
1 parent 936f07c commit 0b30458
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ matrix:
- php: hhvm
env: COLLECT_COVERAGE=false VALIDATE_CODING_STYLE=false
fast_finish: true
allow_failures:
- php: hhvm

cache:
directories:
Expand Down
96 changes: 48 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ This is a drop-in replacement to PHP's `parse_url` function, with the following
use League\Uri\Parser;

$parser = new Parser();
var_dump($parser('http://[email protected]/'));
var_export($parser('http://[email protected]/'));
//returns the following array
//[
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'foo.com',
// 'port' => null,
// 'path' => '',
// 'query' => '@bar.com/',
// 'fragment' => null,
//];

var_dump(parse_url('http://[email protected]/'));
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'foo.com',
// 'port' => null,
// 'path' => '',
// 'query' => '@bar.com/',
// 'fragment' => null,
//);

var_export(parse_url('http://[email protected]/'));
//returns the following array
//[
// 'scheme' => 'http',
// 'host' => 'bar.com',
// 'user' => 'foo.com?',
// 'path' => '/',
//];
//array(
// 'scheme' => 'http',
// 'host' => 'bar.com',
// 'user' => 'foo.com?',
// 'path' => '/',
//);
```

- The `Parser::__invoke` method always returns all URI components.
Expand All @@ -66,26 +66,26 @@ var_dump(parse_url('http://[email protected]/'));
use League\Uri\Parser;

$parser = new Parser();
var_dump($parser('http://www.example.com/'));
var_export($parser('http://www.example.com/'));
//returns the following array
//[
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'www.example.com',
// 'port' => null,
// 'path' => '/',
// 'query' => null,
// 'fragment' => null,
//];

var_dump(parse_url('http://www.example.com/'));
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'www.example.com',
// 'port' => null,
// 'path' => '/',
// 'query' => null,
// 'fragment' => null,
//);

var_export(parse_url('http://www.example.com/'));
//returns the following array
//[
// 'scheme' => 'http',
// 'host' => 'www.example.com',
// 'path' => '/',
//];
//array(
// 'scheme' => 'http',
// 'host' => 'www.example.com',
// 'path' => '/',
//);
```

- Accessing individual component is simple without needing extra parameters:
Expand Down Expand Up @@ -140,18 +140,18 @@ use League\Uri\Parser;

$uri = 'http:www.example.com';
$parser = new Parser();
var_dump($parser($uri));
var_export($parser($uri));
//returns the following array
//[
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => null,
// 'port' => null,
// 'path' => 'www.example.com',
// 'query' => null,
// 'fragment' => null,
//];
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => null,
// 'port' => null,
// 'path' => 'www.example.com',
// 'query' => null,
// 'fragment' => null,
//);
```

**This invalid HTTP URI is successfully parsed.**
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/ParserBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ParserBench
{
private $uri = 'ftp://cnn.example.com&[email protected]/top_story.htm';
private $uri = 'https://cnn.example.com&[email protected]/top_story.htm';

/**
* Baseline - comparison with parse_url
Expand Down
6 changes: 4 additions & 2 deletions src/HostValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait HostValidation
{
protected static $starting_label_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

protected static $local_link_prefix = '1111111010';

/**
* validate the host component
*
Expand All @@ -51,7 +53,7 @@ protected function filterHost($host)
return $host;
}

throw new InvalidArgumentException(sprintf('The submitted host `%s` is invalid', $host));
throw ParserException::createFromInvalidHost($host);
}

/**
Expand Down Expand Up @@ -91,7 +93,7 @@ protected function isValidHostnameIpv6($ipv6)

$res = array_reduce(str_split(unpack('A16', inet_pton($ipv6))[1]), $reducer, '');

return substr($res, 0, 10) === self::LOCAL_LINK_PREFIX;
return substr($res, 0, 10) === self::$local_link_prefix;
}

/**
Expand Down
Loading

0 comments on commit 0b30458

Please sign in to comment.