}
/**
- * @throws HTTPException\InternalServerErrorException
+ * {@inheritDoc}
*/
- protected function request(string $method, string $url, array $opts = []): IHTTPResult
+ public function request(string $method, string $url, array $opts = []): IHTTPResult
{
$this->profiler->startRecording('network');
$this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
$conf = [];
- if (!empty($opts['cookiejar'])) {
- $jar = new FileCookieJar($opts['cookiejar']);
+ if (!empty($opts[HTTPClientOptions::COOKIEJAR])) {
+ $jar = new FileCookieJar($opts[HTTPClientOptions::COOKIEJAR]);
$conf[RequestOptions::COOKIES] = $jar;
}
- $header = [];
+ $headers = [];
- if (!empty($opts['accept_content'])) {
- $header['Accept'] = $opts['accept_content'];
+ if (!empty($opts[HTTPClientOptions::ACCEPT_CONTENT])) {
+ $headers['Accept'] = $opts[HTTPClientOptions::ACCEPT_CONTENT];
}
- if (!empty($opts['header'])) {
- $header = array_merge($opts['header'], $header);
+ if (!empty($opts[HTTPClientOptions::LEGACY_HEADER])) {
+ $this->logger->notice('Wrong option \'headers\' used.');
+ $headers = array_merge($opts[HTTPClientOptions::LEGACY_HEADER], $headers);
}
- if (!empty($opts['headers'])) {
- $this->logger->notice('Wrong option \'headers\' used.');
- $header = array_merge($opts['headers'], $header);
+ if (!empty($opts[HTTPClientOptions::HEADERS])) {
+ $headers = array_merge($opts[HTTPClientOptions::HEADERS], $headers);
+ }
+
+ $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
+
+ if (!empty($opts[HTTPClientOptions::TIMEOUT])) {
+ $conf[RequestOptions::TIMEOUT] = $opts[HTTPClientOptions::TIMEOUT];
}
- $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
+ if (!empty($opts[HTTPClientOptions::BODY])) {
+ $conf[RequestOptions::BODY] = $opts[HTTPClientOptions::BODY];
+ }
- if (!empty($opts['timeout'])) {
- $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
+ if (!empty($opts[HTTPClientOptions::AUTH])) {
+ $conf[RequestOptions::AUTH] = $opts[HTTPClientOptions::AUTH];
}
$conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
- if (!empty($opts['content_length']) &&
- $response->getHeaderLine('Content-Length') > $opts['content_length']) {
+ if (!empty($opts[HTTPClientOptions::CONTENT_LENGTH]) &&
+ (int)$response->getHeaderLine('Content-Length') > $opts[HTTPClientOptions::CONTENT_LENGTH]) {
throw new TransferException('The file is too big!');
}
};
try {
- switch ($method) {
- case 'get':
- $response = $this->client->get($url, $conf);
- break;
- case 'head':
- $response = $this->client->head($url, $conf);
- break;
- default:
- throw new TransferException('Invalid method');
- }
+ $this->logger->debug('http request config.', ['url' => $url, 'method' => $method, 'options' => $conf]);
+
+ $response = $this->client->request($method, $url, $conf);
return new GuzzleResponse($response, $url);
} catch (TransferException $exception) {
if ($exception instanceof RequestException &&
$exception->hasResponse()) {
return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
} else {
- return new CurlResult($url, '', ['http_code' => $exception->getCode()], $exception->getCode(), '');
+ return new CurlResult($url, '', ['http_code' => 500], $exception->getCode(), '');
}
- } catch (InvalidArgumentException $argumentException) {
+ } catch (InvalidArgumentException | \InvalidArgumentException $argumentException) {
$this->logger->info('Invalid Argument for HTTP call.', ['url' => $url, 'method' => $method, 'exception' => $argumentException]);
- return new CurlResult($url, '', ['http_code' => $argumentException->getCode()], $argumentException->getCode(), $argumentException->getMessage());
+ return new CurlResult($url, '', ['http_code' => 500], $argumentException->getCode(), $argumentException->getMessage());
} finally {
$this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
$this->profiler->stopRecording();
}
/** {@inheritDoc}
- *
- * @throws HTTPException\InternalServerErrorException
*/
public function head(string $url, array $opts = []): IHTTPResult
{
{
$opts = [];
- $opts[RequestOptions::JSON] = $params;
+ $opts[HTTPClientOptions::BODY] = $params;
if (!empty($headers)) {
- $opts['headers'] = $headers;
+ $opts[HTTPClientOptions::HEADERS] = $headers;
}
if (!empty($timeout)) {
- $opts[RequestOptions::TIMEOUT] = $timeout;
+ $opts[HTTPClientOptions::TIMEOUT] = $timeout;
}
return $this->request('post', $url, $opts);
$url = trim($url, "'");
- // Designate a temporary file that will store cookies during the session.
- // Some websites test the browser for cookie support, so this enhances results.
- $this->resolver->setCookieJar(tempnam(get_temppath() , 'url_resolver-'));
-
$urlResult = $this->resolver->resolveURL($url);
if ($urlResult->didErrorOccur()) {
- throw new TransferException($urlResult->getErrorMessageString());
+ throw new TransferException($urlResult->getErrorMessageString(), $urlResult->getHTTPStatusCode());
}
return $urlResult->getURL();