]> git.mxchange.org Git - friendica.git/blob - src/Network/HTTPClient.php
Adapt tests
[friendica.git] / src / Network / HTTPClient.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU APGL 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;
23
24 use Friendica\Core\System;
25 use Friendica\Util\Network;
26 use Friendica\Util\Profiler;
27 use GuzzleHttp\Client;
28 use GuzzleHttp\Cookie\FileCookieJar;
29 use GuzzleHttp\Exception\RequestException;
30 use GuzzleHttp\Exception\TransferException;
31 use GuzzleHttp\RequestOptions;
32 use mattwright\URLResolver;
33 use Psr\Http\Message\ResponseInterface;
34 use Psr\Log\LoggerInterface;
35
36 /**
37  * Performs HTTP requests to a given URL
38  */
39 class HTTPClient implements IHTTPClient
40 {
41         /** @var LoggerInterface */
42         private $logger;
43         /** @var Profiler */
44         private $profiler;
45         /** @var Client */
46         private $client;
47         /** @var URLResolver */
48         private $resolver;
49
50         public function __construct(LoggerInterface $logger, Profiler $profiler, Client $client, URLResolver $resolver)
51         {
52                 $this->logger   = $logger;
53                 $this->profiler = $profiler;
54                 $this->client   = $client;
55                 $this->resolver = $resolver;
56         }
57
58         /**
59          * @throws HTTPException\InternalServerErrorException
60          */
61         protected function request(string $method, string $url, array $opts = []): IHTTPResult
62         {
63                 $this->profiler->startRecording('network');
64                 $this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
65
66                 if (Network::isLocalLink($url)) {
67                         $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
68                 }
69
70                 if (strlen($url) > 1000) {
71                         $this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
72                         $this->profiler->stopRecording();
73                         return CurlResult::createErrorCurl(substr($url, 0, 200));
74                 }
75
76                 $parts2     = [];
77                 $parts      = parse_url($url);
78                 $path_parts = explode('/', $parts['path'] ?? '');
79                 foreach ($path_parts as $part) {
80                         if (strlen($part) <> mb_strlen($part)) {
81                                 $parts2[] = rawurlencode($part);
82                         } else {
83                                 $parts2[] = $part;
84                         }
85                 }
86                 $parts['path'] = implode('/', $parts2);
87                 $url           = Network::unparseURL($parts);
88
89                 if (Network::isUrlBlocked($url)) {
90                         $this->logger->info('Domain is blocked.', ['url' => $url]);
91                         $this->profiler->stopRecording();
92                         return CurlResult::createErrorCurl($url);
93                 }
94
95                 $conf = [];
96
97                 if (!empty($opts['cookiejar'])) {
98                         $jar                           = new FileCookieJar($opts['cookiejar']);
99                         $conf[RequestOptions::COOKIES] = $jar;
100                 }
101
102                 $header = [];
103
104                 if (!empty($opts['accept_content'])) {
105                         array_push($header, 'Accept: ' . $opts['accept_content']);
106                 }
107
108                 if (!empty($opts['header'])) {
109                         $header = array_merge($opts['header'], $header);
110                 }
111
112                 if (!empty($opts['headers'])) {
113                         $this->logger->notice('Wrong option \'headers\' used.');
114                         $header = array_merge($opts['headers'], $header);
115                 }
116
117                 $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
118
119                 if (!empty($opts['timeout'])) {
120                         $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
121                 }
122
123                 $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
124                         if (!empty($opts['content_length']) &&
125                                 $response->getHeaderLine('Content-Length') > $opts['content_length']) {
126                                 throw new TransferException('The file is too big!');
127                         }
128                 };
129
130                 try {
131                         switch ($method) {
132                                 case 'get':
133                                         $response = $this->client->get($url, $conf);
134                                         break;
135                                 case 'head':
136                                         $response = $this->client->head($url, $conf);
137                                         break;
138                                 default:
139                                         throw new TransferException('Invalid method');
140                         }
141                         return new GuzzleResponse($response, $url);
142                 } catch (TransferException $exception) {
143                         if ($exception instanceof RequestException &&
144                                 $exception->hasResponse()) {
145                                 return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
146                         } else {
147                                 return new CurlResult($url, '', ['http_code' => $exception->getCode()], $exception->getCode(), '');
148                         }
149                 } finally {
150                         $this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
151                         $this->profiler->stopRecording();
152                 }
153         }
154
155         /** {@inheritDoc}
156          *
157          * @throws HTTPException\InternalServerErrorException
158          */
159         public function head(string $url, array $opts = []): IHTTPResult
160         {
161                 return $this->request('head', $url, $opts);
162         }
163
164         /**
165          * {@inheritDoc}
166          */
167         public function get(string $url, array $opts = []): IHTTPResult
168         {
169                 return $this->request('get', $url, $opts);
170         }
171
172         /**
173          * {@inheritDoc}
174          */
175         public function post(string $url, $params, array $headers = [], int $timeout = 0): IHTTPResult
176         {
177                 $opts = [];
178
179                 $opts[RequestOptions::JSON] = $params;
180
181                 if (!empty($headers)) {
182                         $opts['headers'] = $headers;
183                 }
184
185                 if (!empty($timeout)) {
186                         $opts[RequestOptions::TIMEOUT] = $timeout;
187                 }
188
189                 return $this->request('post', $url, $opts);
190         }
191
192         /**
193          * {@inheritDoc}
194          */
195         public function finalUrl(string $url)
196         {
197                 $this->profiler->startRecording('network');
198
199                 if (Network::isLocalLink($url)) {
200                         $this->logger->debug('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
201                 }
202
203                 if (Network::isUrlBlocked($url)) {
204                         $this->logger->info('Domain is blocked.', ['url' => $url]);
205                         return $url;
206                 }
207
208                 if (Network::isRedirectBlocked($url)) {
209                         $this->logger->info('Domain should not be redirected.', ['url' => $url]);
210                         return $url;
211                 }
212
213                 $url = Network::stripTrackingQueryParams($url);
214
215                 $url = trim($url, "'");
216
217                 // Designate a temporary file that will store cookies during the session.
218                 // Some websites test the browser for cookie support, so this enhances results.
219                 $this->resolver->setCookieJar(tempnam(get_temppath() , 'url_resolver-'));
220
221                 $urlResult = $this->resolver->resolveURL($url);
222
223                 if ($urlResult->didErrorOccur()) {
224                         throw new TransferException($urlResult->getErrorMessageString());
225                 }
226
227                 return $urlResult->getURL();
228         }
229
230         /**
231          * {@inheritDoc}
232          */
233         public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '')
234         {
235                 $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar);
236
237                 return $ret->getBody();
238         }
239
240         /**
241          * {@inheritDoc}
242          */
243         public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '')
244         {
245                 return $this->get(
246                         $url,
247                         [
248                                 'timeout'        => $timeout,
249                                 'accept_content' => $accept_content,
250                                 'cookiejar'      => $cookiejar
251                         ]
252                 );
253         }
254 }