diff --git a/composer.json b/composer.json index 39f5f63..1eed498 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "p3k\\XRay\\": "lib/XRay" }, "files": [ - "lib/helpers.php" + "lib/helpers.php", + "lib/XRay.php" ] }, "require-dev": { diff --git a/controllers/Rels.php b/controllers/Rels.php index b4866b8..6dd5853 100644 --- a/controllers/Rels.php +++ b/controllers/Rels.php @@ -24,13 +24,15 @@ class Rels { } public function fetch(Request $request, Response $response) { + $opts = []; + if($request->get('timeout')) { // We might make 2 HTTP requests, so each request gets half the desired timeout - $this->http->timeout = $request->get('timeout') / 2; + $opts['timeout'] = $request->get('timeout') / 2; } if($request->get('max_redirects')) { - $this->http->max_redirects = (int)$request->get('max_redirects'); + $opts['max_redirects'] = (int)$request->get('max_redirects'); } if($request->get('pretty')) { @@ -46,51 +48,11 @@ class Rels { ]); } - // Attempt some basic URL validation - $scheme = parse_url($url, PHP_URL_SCHEME); - if(!in_array($scheme, ['http','https'])) { - return $this->respond($response, 400, [ - 'error' => 'invalid_url', - 'error_description' => 'Only http and https URLs are supported' - ]); - } - - $host = parse_url($url, PHP_URL_HOST); - if(!$host) { - return $this->respond($response, 400, [ - 'error' => 'invalid_url', - 'error_description' => 'The URL provided was not valid' - ]); - } - - $url = p3k\XRay\normalize_url($url); - - $result = $this->http->get($url); - - $html = $result['body']; - $mf2 = mf2\Parse($html, $result['url']); - - $rels = $result['rels']; - if(isset($mf2['rels'])) { - $rels = array_merge($rels, $mf2['rels']); - } - - // Resolve all relative URLs - foreach($rels as $rel=>$values) { - foreach($values as $i=>$value) { - $value = \mf2\resolveUrl($result['url'], $value); - $rels[$rel][$i] = $value; - } - } - - if(count($rels) == 0) - $rels = new StdClass; + $xray = new p3k\XRay(); + $xray->http = $this->http; + $res = $xray->rels($url, $opts); - return $this->respond($response, 200, [ - 'url' => $result['url'], - 'code' => $result['code'], - 'rels' => $rels - ]); + return $this->respond($response, !empty($res['error']) ? 400 : 200, $res); } } diff --git a/lib/XRay.php b/lib/XRay.php new file mode 100644 index 0000000..73cca7a --- /dev/null +++ b/lib/XRay.php @@ -0,0 +1,22 @@ +http = new HTTP(); + } + + public function rels($url, $opts=[]) { + $rels = new XRay\Rels($this->http); + return $rels->parse($url, $opts); + } + + public function parse($url, $opts=[]) { + $parser = new XRay\Parser($this->http); + return $parser->parse($url, $opts); + } + +} + diff --git a/lib/XRay/Rels.php b/lib/XRay/Rels.php new file mode 100644 index 0000000..b12301e --- /dev/null +++ b/lib/XRay/Rels.php @@ -0,0 +1,63 @@ +http = $http; + } + + public function parse($url, $opts=[]) { + if(isset($opts['timeout'])) + $this->http->set_timeout($opts['timeout']); + if(isset($opts['max_redirects'])) + $this->http->set_max_redirects($opts['max_redirects']); + + $scheme = parse_url($url, PHP_URL_SCHEME); + if(!in_array($scheme, ['http','https'])) { + return [ + 'error' => 'invalid_url', + 'error_description' => 'Only http and https URLs are supported' + ]; + } + + $host = parse_url($url, PHP_URL_HOST); + if(!$host) { + return [ + 'error' => 'invalid_url', + 'error_description' => 'The URL provided was not valid' + ]; + } + + $url = normalize_url($url); + + $result = $this->http->get($url); + + $html = $result['body']; + $mf2 = \mf2\Parse($html, $result['url']); + + $rels = $result['rels']; + if(isset($mf2['rels'])) { + $rels = array_merge($rels, $mf2['rels']); + } + + // Resolve all relative URLs + foreach($rels as $rel=>$values) { + foreach($values as $i=>$value) { + $value = \mf2\resolveUrl($result['url'], $value); + $rels[$rel][$i] = $value; + } + } + + if(count($rels) == 0) + $rels = new \StdClass; + + return [ + 'url' => $result['url'], + 'code' => $result['code'], + 'rels' => $rels + ]; + } + +}