3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Network\HTTPClient\Client;
24 use Friendica\Core\System;
25 use Friendica\Network\HTTPClient\Response\CurlResult;
26 use Friendica\Network\HTTPClient\Response\GuzzleResponse;
27 use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
28 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
29 use Friendica\Util\Network;
30 use Friendica\Util\Profiler;
31 use GuzzleHttp\Client;
32 use GuzzleHttp\Cookie\FileCookieJar;
33 use GuzzleHttp\Exception\RequestException;
34 use GuzzleHttp\Exception\TransferException;
35 use GuzzleHttp\RequestOptions;
36 use mattwright\URLResolver;
37 use Psr\Http\Message\ResponseInterface;
38 use Psr\Log\InvalidArgumentException;
39 use Psr\Log\LoggerInterface;
42 * Performs HTTP requests to a given URL
44 class HttpClient implements ICanSendHttpRequests
46 /** @var LoggerInterface */
52 /** @var URLResolver */
55 public function __construct(LoggerInterface $logger, Profiler $profiler, Client $client, URLResolver $resolver)
57 $this->logger = $logger;
58 $this->profiler = $profiler;
59 $this->client = $client;
60 $this->resolver = $resolver;
66 public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
68 $this->profiler->startRecording('network');
69 $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
71 if (Network::isLocalLink($url)) {
72 $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
75 if (strlen($url) > 1000) {
76 $this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
77 $this->profiler->stopRecording();
78 return CurlResult::createErrorCurl(substr($url, 0, 200));
82 $parts = parse_url($url);
83 $path_parts = explode('/', $parts['path'] ?? '');
84 foreach ($path_parts as $part) {
85 if (strlen($part) <> mb_strlen($part)) {
86 $parts2[] = rawurlencode($part);
91 $parts['path'] = implode('/', $parts2);
92 $url = Network::unparseURL($parts);
94 if (Network::isUrlBlocked($url)) {
95 $this->logger->info('Domain is blocked.', ['url' => $url]);
96 $this->profiler->stopRecording();
97 return CurlResult::createErrorCurl($url);
102 if (!empty($opts[HttpClientOptions::COOKIEJAR])) {
103 $jar = new FileCookieJar($opts[HttpClientOptions::COOKIEJAR]);
104 $conf[RequestOptions::COOKIES] = $jar;
109 if (!empty($opts[HttpClientOptions::ACCEPT_CONTENT])) {
110 $headers['Accept'] = $opts[HttpClientOptions::ACCEPT_CONTENT];
113 if (!empty($opts[HttpClientOptions::LEGACY_HEADER])) {
114 $this->logger->notice('Wrong option \'headers\' used.');
115 $headers = array_merge($opts[HttpClientOptions::LEGACY_HEADER], $headers);
118 if (!empty($opts[HttpClientOptions::HEADERS])) {
119 $headers = array_merge($opts[HttpClientOptions::HEADERS], $headers);
122 $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
124 if (!empty($opts[HttpClientOptions::TIMEOUT])) {
125 $conf[RequestOptions::TIMEOUT] = $opts[HttpClientOptions::TIMEOUT];
128 if (!empty($opts[HttpClientOptions::BODY])) {
129 $conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
132 if (!empty($opts[HttpClientOptions::AUTH])) {
133 $conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH];
136 $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
137 if (!empty($opts[HttpClientOptions::CONTENT_LENGTH]) &&
138 (int)$response->getHeaderLine('Content-Length') > $opts[HttpClientOptions::CONTENT_LENGTH]) {
139 throw new TransferException('The file is too big!');
143 if (empty($conf[HttpClientOptions::HEADERS]['Accept']) && in_array($method, ['get', 'head'])) {
144 $this->logger->info('Accept header was missing, using default.', ['url' => $url, 'callstack' => System::callstack()]);
145 $conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT;
149 $this->logger->debug('http request config.', ['url' => $url, 'method' => $method, 'options' => $conf]);
151 $response = $this->client->request($method, $url, $conf);
152 return new GuzzleResponse($response, $url);
153 } catch (TransferException $exception) {
154 if ($exception instanceof RequestException &&
155 $exception->hasResponse()) {
156 return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
158 return new CurlResult($url, '', ['http_code' => 500], $exception->getCode(), '');
160 } catch (InvalidArgumentException | \InvalidArgumentException $argumentException) {
161 $this->logger->info('Invalid Argument for HTTP call.', ['url' => $url, 'method' => $method, 'exception' => $argumentException]);
162 return new CurlResult($url, '', ['http_code' => 500], $argumentException->getCode(), $argumentException->getMessage());
164 $this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
165 $this->profiler->stopRecording();
171 public function head(string $url, array $opts = []): ICanHandleHttpResponses
173 return $this->request('head', $url, $opts);
179 public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
181 // In case there is no
182 $opts[HttpClientOptions::ACCEPT_CONTENT] = $opts[HttpClientOptions::ACCEPT_CONTENT] ?? $accept_content;
184 return $this->request('get', $url, $opts);
190 public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
194 $opts[HttpClientOptions::BODY] = $params;
196 if (!empty($headers)) {
197 $opts[HttpClientOptions::HEADERS] = $headers;
200 if (!empty($timeout)) {
201 $opts[HttpClientOptions::TIMEOUT] = $timeout;
204 return $this->request('post', $url, $opts);
210 public function finalUrl(string $url): string
212 $this->profiler->startRecording('network');
214 if (Network::isLocalLink($url)) {
215 $this->logger->debug('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
218 if (Network::isUrlBlocked($url)) {
219 $this->logger->info('Domain is blocked.', ['url' => $url]);
223 if (Network::isRedirectBlocked($url)) {
224 $this->logger->info('Domain should not be redirected.', ['url' => $url]);
228 $url = Network::stripTrackingQueryParams($url);
230 $url = trim($url, "'");
232 $urlResult = $this->resolver->resolveURL($url);
234 if ($urlResult->didErrorOccur()) {
235 throw new TransferException($urlResult->getErrorMessageString(), $urlResult->getHTTPStatusCode());
238 return $urlResult->getURL();
244 public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string
246 $ret = $this->fetchFull($url, $accept_content, $timeout, $cookiejar);
248 return $ret->getBody();
254 public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses
260 HttpClientOptions::TIMEOUT => $timeout,
261 HttpClientOptions::COOKIEJAR => $cookiejar