From 09981cc8d27de8cda1d8fe93454f56ffd11fdf01 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 6 Apr 2017 16:24:35 -0700 Subject: [PATCH] add new endpoint to parse all rel values from a URL Grabs rel values from both HTTP headers and link tags and merges them together, and resolves any relative values. Also returns the final URL requested and the HTTP response. --- composer.json | 1 + controllers/Rels.php | 93 ++++++++++++++++++++++++++++++++++++++++++++ public/index.php | 2 + 3 files changed, 96 insertions(+) create mode 100644 controllers/Rels.php diff --git a/composer.json b/composer.json index dac355b..0f60c3b 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "controllers/Main.php", "controllers/Parse.php", "controllers/Token.php", + "controllers/Rels.php", "controllers/Certbot.php", "lib/HTTPCurl.php", "lib/HTTPStream.php", diff --git a/controllers/Rels.php b/controllers/Rels.php new file mode 100644 index 0000000..9932513 --- /dev/null +++ b/controllers/Rels.php @@ -0,0 +1,93 @@ +http = new p3k\HTTP(); + } + + private function respond(Response $response, $code, $params, $headers=[]) { + $response->setStatusCode($code); + foreach($headers as $k=>$v) { + $response->headers->set($k, $v); + } + $response->headers->set('Content-Type', 'application/json'); + $opts = JSON_UNESCAPED_SLASHES + JSON_FORCE_OBJECT; + if($this->_pretty) $opts += JSON_PRETTY_PRINT; + $response->setContent(json_encode($params, $opts)."\n"); + return $response; + } + + public function fetch(Request $request, Response $response) { + 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; + } + + if($request->get('max_redirects')) { + $this->http->max_redirects = (int)$request->get('max_redirects'); + } + + if($request->get('pretty')) { + $this->_pretty = true; + } + + $url = $request->get('url'); + + if(!$url) { + return $this->respond($response, 400, [ + 'error' => 'missing_url', + 'error_description' => 'Provide a URL to fetch' + ]); + } + + // 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 = \normalize_url($url); + + $result = $this->http->get($url); + + $html = $result['body']; + $mf2 = mf2\Parse($html, $result['url']); + + $rels = p3k\HTTP::link_rels($result['headers']); + 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; + } + } + + return $this->respond($response, 200, [ + 'url' => $result['url'], + 'code' => $result['code'], + 'rels' => $rels + ]); + } + +} diff --git a/public/index.php b/public/index.php index b0cb02a..d6e4b5d 100644 --- a/public/index.php +++ b/public/index.php @@ -24,6 +24,8 @@ $router->addRoute('GET', '/parse', 'Parse::parse'); $router->addRoute('POST', '/parse', 'Parse::parse'); $router->addRoute('POST', '/token', 'Token::token'); +$router->addRoute('GET', '/rels', 'Rels::fetch'); + $router->addRoute('GET', '/cert', 'Certbot::index'); $router->addRoute('GET', '/cert/auth', 'Certbot::start_auth'); $router->addRoute('GET', '/cert/logout', 'Certbot::logout');