summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-06-11 19:40:16 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-06-21 10:59:53 -0100
commit50fb954e66e43e431a6dfed92d0f3638056f205e (patch)
treead6239b1b47c859a46701f19458ce371d0c56475
parent20a4cb0b6f547fc85b3e05cd5a1de247d33ad918 (diff)
request on host-meta
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r--lib/Exceptions/HostMetaException.php40
-rw-r--r--lib/Service/CurlService.php46
2 files changed, 84 insertions, 2 deletions
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 @@
+<?php
+declare(strict_types=1);
+
+
+/**
+ * Nextcloud - Social Support
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+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);
@@ -139,6 +146,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
*
* @return Person
@@ -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);