From 50fb954e66e43e431a6dfed92d0f3638056f205e Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 11 Jun 2019 19:40:16 -0100 Subject: request on host-meta Signed-off-by: Maxence Lange --- lib/Exceptions/HostMetaException.php | 40 +++++++++++++++++++++++++++++++ lib/Service/CurlService.php | 46 ++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 lib/Exceptions/HostMetaException.php diff --git a/lib/Exceptions/HostMetaException.php b/lib/Exceptions/HostMetaException.php new file mode 100644 index 00000000..9c58ede4 --- /dev/null +++ b/lib/Exceptions/HostMetaException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Exceptions; + + +use Exception; + + +class HostMetaException extends Exception { + +} + diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index cbea8819..ccf315cd 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -37,6 +37,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; use Exception; use OCA\Social\AP; +use OCA\Social\Exceptions\HostMetaException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RedundancyLimitException; @@ -121,7 +122,13 @@ class CurlService { throw new InvalidResourceException(); } - $request = new Request('/.well-known/webfinger'); + try { + $path = $this->hostMeta($host); + } catch (HostMetaException $e) { + $path = '/.well-known/webfinger'; + } + + $request = new Request($path); $request->addData('resource', 'acct:' . $account); $request->setAddress($host); @@ -138,6 +145,32 @@ class CurlService { } + /** + * @param string $host + * + * @return string + * @throws HostMetaException + */ + public function hostMeta(string &$host): string { + $request = new Request('/.well-known/host-meta'); + $request->setAddress($host); + + try { + $result = $this->request($request); + } catch (Exception $e) { + $this->miscService->log( + 'hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0 + ); + throw new HostMetaException($e->getMessage()); + } + + $url = $this->get('Link.@attributes.template', $result, ''); + $host = parse_url($url, PHP_URL_HOST); + + return parse_url($url, PHP_URL_PATH); + } + + /** * @param string $account * @@ -252,9 +285,16 @@ class CurlService { } $this->miscService->log( - '[>>] request: ' . json_encode($request) . ' - result: ' . $result, 1 + '[>>] request: ' . json_encode($request) . ' - content-type: ' + . $request->getContentType() . ' - result: ' . $result, 1 ); + if (strpos($request->getContentType(), 'application/xrd') === 0) { + $xml = simplexml_load_string($result); + $result = json_encode($xml, JSON_UNESCAPED_SLASHES); + $this->miscService->log('XRD conversion to JSON: ' . $result, 1); + } + $result = json_decode((string)$result, true); if (is_array($result)) { return $result; @@ -427,6 +467,8 @@ class CurlService { $this->parseRequestResultCurl($curl, $request); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + $request->setContentType(($contentType === null) ? '' : $contentType); $request->setResultCode($code); $this->parseRequestResultCode301($code, $request); -- cgit v1.2.3