]> git.mxchange.org Git - friendica.git/blob - src/Network/HTTPClient/Client/HttpClient.php
Merge pull request #11457 from annando/performance
[friendica.git] / src / Network / HTTPClient / Client / HttpClient.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Network\HTTPClient\Client;
23
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;
40
41 /**
42  * Performs HTTP requests to a given URL
43  */
44 class HttpClient implements ICanSendHttpRequests
45 {
46         /** @var LoggerInterface */
47         private $logger;
48         /** @var Profiler */
49         private $profiler;
50         /** @var Client */
51         private $client;
52         /** @var URLResolver */
53         private $resolver;
54
55         public function __construct(LoggerInterface $logger, Profiler $profiler, Client $client, URLResolver $resolver)
56         {
57                 $this->logger   = $logger;
58                 $this->profiler = $profiler;
59                 $this->client   = $client;
60                 $this->resolver = $resolver;
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
67         {
68                 $this->profiler->startRecording('network');
69                 $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
70
71                 if (Network::isLocalLink($url)) {
72                         $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
73                 }
74
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));
79                 }
80
81                 $parts2     = [];
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);
87                         } else {
88                                 $parts2[] = $part;
89                         }
90                 }
91                 $parts['path'] = implode('/', $parts2);
92                 $url           = Network::unparseURL($parts);
93
94                 if (Network::isUrlBlocked($url)) {
95                         $this->logger->info('Domain is blocked.', ['url' => $url]);
96                         $this->profiler->stopRecording();
97                         return CurlResult::createErrorCurl($url);
98                 }
99
100                 $conf = [];
101
102                 if (!empty($opts[HttpClientOptions::COOKIEJAR])) {
103                         $jar                           = new FileCookieJar($opts[HttpClientOptions::COOKIEJAR]);
104                         $conf[RequestOptions::COOKIES] = $jar;
105                 }
106
107                 $headers = [];
108
109                 if (!empty($opts[HttpClientOptions::ACCEPT_CONTENT])) {
110                         $headers['Accept'] = $opts[HttpClientOptions::ACCEPT_CONTENT];
111                 }
112
113                 if (!empty($opts[HttpClientOptions::LEGACY_HEADER])) {
114                         $this->logger->notice('Wrong option \'headers\' used.');
115                         $headers = array_merge($opts[HttpClientOptions::LEGACY_HEADER], $headers);
116                 }
117
118                 if (!empty($opts[HttpClientOptions::HEADERS])) {
119                         $headers = array_merge($opts[HttpClientOptions::HEADERS], $headers);
120                 }
121
122                 $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
123
124                 if (!empty($opts[HttpClientOptions::TIMEOUT])) {
125                         $conf[RequestOptions::TIMEOUT] = $opts[HttpClientOptions::TIMEOUT];
126                 }
127
128                 if (!empty($opts[HttpClientOptions::BODY])) {
129                         $conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
130                 }
131
132                 if (!empty($opts[HttpClientOptions::AUTH])) {
133                         $conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH];
134                 }
135
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!');
140                         }
141                 };
142
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;
146                 }
147
148                 try {
149                         $this->logger->debug('http request config.', ['url' => $url, 'method' => $method, 'options' => $conf]);
150
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(), '');
157                         } else {
158                                 return new CurlResult($url, '', ['http_code' => 500], $exception->getCode(), '');
159                         }
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());
163                 } finally {
164                         $this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
165                         $this->profiler->stopRecording();
166                 }
167         }
168
169         /** {@inheritDoc}
170          */
171         public function head(string $url, array $opts = []): ICanHandleHttpResponses
172         {
173                 return $this->request('head', $url, $opts);
174         }
175
176         /**
177          * {@inheritDoc}
178          */
179         public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
180         {
181                 // In case there is no
182                 $opts[HttpClientOptions::ACCEPT_CONTENT] = $opts[HttpClientOptions::ACCEPT_CONTENT] ?? $accept_content;
183
184                 return $this->request('get', $url, $opts);
185         }
186
187         /**
188          * {@inheritDoc}
189          */
190         public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
191         {
192                 $opts = [];
193
194                 $opts[HttpClientOptions::BODY] = $params;
195
196                 if (!empty($headers)) {
197                         $opts[HttpClientOptions::HEADERS] = $headers;
198                 }
199
200                 if (!empty($timeout)) {
201                         $opts[HttpClientOptions::TIMEOUT] = $timeout;
202                 }
203
204                 return $this->request('post', $url, $opts);
205         }
206
207         /**
208          * {@inheritDoc}
209          */
210         public function finalUrl(string $url): string
211         {
212                 $this->profiler->startRecording('network');
213
214                 if (Network::isLocalLink($url)) {
215                         $this->logger->debug('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
216                 }
217
218                 if (Network::isUrlBlocked($url)) {
219                         $this->logger->info('Domain is blocked.', ['url' => $url]);
220                         return $url;
221                 }
222
223                 if (Network::isRedirectBlocked($url)) {
224                         $this->logger->info('Domain should not be redirected.', ['url' => $url]);
225                         return $url;
226                 }
227
228                 $url = Network::stripTrackingQueryParams($url);
229
230                 $url = trim($url, "'");
231
232                 $urlResult = $this->resolver->resolveURL($url);
233
234                 if ($urlResult->didErrorOccur()) {
235                         throw new TransferException($urlResult->getErrorMessageString(), $urlResult->getHTTPStatusCode());
236                 }
237
238                 return $urlResult->getURL();
239         }
240
241         /**
242          * {@inheritDoc}
243          */
244         public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string
245         {
246                 $ret = $this->fetchFull($url, $accept_content, $timeout, $cookiejar);
247
248                 return $ret->getBody();
249         }
250
251         /**
252          * {@inheritDoc}
253          */
254         public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses
255         {
256                 return $this->get(
257                         $url,
258                         $accept_content,
259                         [
260                                 HttpClientOptions::TIMEOUT   => $timeout,
261                                 HttpClientOptions::COOKIEJAR => $cookiejar
262                         ]
263                 );
264         }
265 }