Browse Source

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.
pull/39/head
Aaron Parecki 7 years ago
parent
commit
09981cc8d2
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 96 additions and 0 deletions
  1. +1
    -0
      composer.json
  2. +93
    -0
      controllers/Rels.php
  3. +2
    -0
      public/index.php

+ 1
- 0
composer.json View File

@ -14,6 +14,7 @@
"controllers/Main.php", "controllers/Main.php",
"controllers/Parse.php", "controllers/Parse.php",
"controllers/Token.php", "controllers/Token.php",
"controllers/Rels.php",
"controllers/Certbot.php", "controllers/Certbot.php",
"lib/HTTPCurl.php", "lib/HTTPCurl.php",
"lib/HTTPStream.php", "lib/HTTPStream.php",

+ 93
- 0
controllers/Rels.php View File

@ -0,0 +1,93 @@
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Rels {
public $http;
private $_pretty = false;
public function __construct() {
$this->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
]);
}
}

+ 2
- 0
public/index.php View File

@ -24,6 +24,8 @@ $router->addRoute('GET', '/parse', 'Parse::parse');
$router->addRoute('POST', '/parse', 'Parse::parse'); $router->addRoute('POST', '/parse', 'Parse::parse');
$router->addRoute('POST', '/token', 'Token::token'); $router->addRoute('POST', '/token', 'Token::token');
$router->addRoute('GET', '/rels', 'Rels::fetch');
$router->addRoute('GET', '/cert', 'Certbot::index'); $router->addRoute('GET', '/cert', 'Certbot::index');
$router->addRoute('GET', '/cert/auth', 'Certbot::start_auth'); $router->addRoute('GET', '/cert/auth', 'Certbot::start_auth');
$router->addRoute('GET', '/cert/logout', 'Certbot::logout'); $router->addRoute('GET', '/cert/logout', 'Certbot::logout');

Loading…
Cancel
Save