diff --git a/composer.json b/composer.json index 8985ca1..c0afae7 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "autoload": { "psr-4": { - "XRay\\": "lib/XRay" + "p3k\\XRay\\": "lib/XRay" }, "files": [ "lib/helpers.php" diff --git a/composer.lock b/composer.lock index 3353338..ca69224 100644 --- a/composer.lock +++ b/composer.lock @@ -256,16 +256,16 @@ }, { "name": "p3k/http", - "version": "0.1.1", + "version": "0.1.4", "source": { "type": "git", "url": "https://github.com/aaronpk/p3k-http.git", - "reference": "7409b0a44f190b053d694304e716de7ce5b3568b" + "reference": "136aac6f7ecd6d6e16e8ff9286b43110680c49ab" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/7409b0a44f190b053d694304e716de7ce5b3568b", - "reference": "7409b0a44f190b053d694304e716de7ce5b3568b", + "url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/136aac6f7ecd6d6e16e8ff9286b43110680c49ab", + "reference": "136aac6f7ecd6d6e16e8ff9286b43110680c49ab", "shasum": "" }, "require": { @@ -290,7 +290,7 @@ ], "description": "A simple wrapper API around the PHP curl functions", "homepage": "https://github.com/aaronpk/p3k-http", - "time": "2017-04-28T18:51:28+00:00" + "time": "2017-04-28T19:46:12+00:00" }, { "name": "p3k/timezone", diff --git a/controllers/Certbot.php b/controllers/Certbot.php index 8fd2738..96afb1c 100644 --- a/controllers/Certbot.php +++ b/controllers/Certbot.php @@ -13,7 +13,7 @@ class Certbot { $state = mt_rand(10000,99999); $_SESSION['state'] = $state; - $response->setContent(view('certbot', [ + $response->setContent(p3k\XRay\view('certbot', [ 'title' => 'X-Ray', 'state' => $state ])); @@ -109,7 +109,7 @@ class Certbot { 'challenge' => $challenge ]), 0, 600); - $response->setContent(view('certbot', [ + $response->setContent(p3k\XRay\view('certbot', [ 'title' => 'X-Ray', 'challenge' => $challenge, 'token' => $token, diff --git a/controllers/Main.php b/controllers/Main.php index 6f78ba2..62204d5 100644 --- a/controllers/Main.php +++ b/controllers/Main.php @@ -5,7 +5,7 @@ use Symfony\Component\HttpFoundation\Response; class Main { public function index(Request $request, Response $response) { - $response->setContent(view('index', [ + $response->setContent(p3k\XRay\view('index', [ 'title' => 'X-Ray' ])); return $response; diff --git a/controllers/Parse.php b/controllers/Parse.php index b29933f..d0be707 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -2,7 +2,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use XRay\Formats; +use p3k\XRay\Formats; class Parse { @@ -92,7 +92,7 @@ class Parse { ]); } - $url = \normalize_url($url); + $url = p3k\XRay\normalize_url($url); // Check if this is a Twitter URL and if they've provided API credentials, use the API if(preg_match('/https?:\/\/(?:mobile\.twitter\.com|twitter\.com|twtr\.io)\/(?:[a-z0-9_\/!#]+statuse?s?\/([0-9]+)|([a-zA-Z0-9_]+))/i', $url, $match)) { @@ -103,10 +103,14 @@ class Parse { return $this->parseGitHubURL($request, $response, $url); } - if(!should_follow_redirects($url)) + // Special-case appspot.com URLs to not follow redirects. + // https://cloud.google.com/appengine/docs/php/urlfetch/ + if(!p3k\XRay\should_follow_redirects($url)) { + $this->http->set_max_redirects(0); $this->http->set_transport(new p3k\HTTP\Stream()); - else + } else { $this->http->set_transport(new p3k\HTTP\Curl()); + } // Now fetch the URL and check for any curl errors // Don't cache the response if a token is used to fetch it diff --git a/controllers/Rels.php b/controllers/Rels.php index 8cdcef5..b4866b8 100644 --- a/controllers/Rels.php +++ b/controllers/Rels.php @@ -63,7 +63,7 @@ class Rels { ]); } - $url = \normalize_url($url); + $url = p3k\XRay\normalize_url($url); $result = $this->http->get($url); diff --git a/lib/XRay/Formats/GitHub.php b/lib/XRay/Formats/GitHub.php index cc1fb5d..766356d 100644 --- a/lib/XRay/Formats/GitHub.php +++ b/lib/XRay/Formats/GitHub.php @@ -1,5 +1,5 @@ 0) { // There is an author h-card on this page // Now look for the first h-* object other than an h-card and use that as the object @@ -496,7 +496,7 @@ class Mf2 { $found = false; foreach($item['properties']['url'] as $url) { if(self::isURL($url)) { - $url = self::normalize_url($url); + $url = \p3k\XRay\normalize_url($url); if($url == $authorURL) { $data['url'] = $url; $found = true; @@ -723,25 +723,4 @@ class Mf2 { } return \mf2\Parse($result['body'], $url); } - - private static function normalize_url($url) { - $parts = parse_url($url); - if(empty($parts['path'])) - $parts['path'] = '/'; - $parts['host'] = strtolower($parts['host']); - return self::build_url($parts); - } - - private static function build_url($parsed_url) { - $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; - $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; - $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; - $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; - $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; - $pass = ($user || $pass) ? "$pass@" : ''; - $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; - $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; - $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; - return "$scheme$user$pass$host$port$path$query$fragment"; - } } diff --git a/lib/XRay/Formats/Twitter.php b/lib/XRay/Formats/Twitter.php index 5bc1fe8..246d4d2 100644 --- a/lib/XRay/Formats/Twitter.php +++ b/lib/XRay/Formats/Twitter.php @@ -1,5 +1,5 @@ assertEquals('http://example.com/', $result); } public function testAddsSlashToBareDomain() { $url = 'http://example.com'; - $result = normalize_url($url); + $result = p3k\XRay\normalize_url($url); $this->assertEquals('http://example.com/', $result); } + public function testDoesNotModify() { + $url = 'https://example.com/'; + $result = p3k\XRay\normalize_url($url); + $this->assertEquals('https://example.com/', $result); + } + }