From e9902401a592cc5d4eb7210301b183d6e76b8b4b Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 25 Aug 2021 21:09:14 +0200 Subject: [PATCH] Fixing HTTPClient::post() and introduce HTTPRequestOptions --- src/Network/HTTPClient.php | 44 ++++++++++++++++-------------- src/Network/HTTPRequestOptions.php | 40 +++++++++++++++++++++++++++ src/Network/IHTTPClient.php | 10 +++++-- 3 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/Network/HTTPRequestOptions.php diff --git a/src/Network/HTTPClient.php b/src/Network/HTTPClient.php index eabd5d450..e982d19af 100644 --- a/src/Network/HTTPClient.php +++ b/src/Network/HTTPClient.php @@ -57,7 +57,7 @@ class HTTPClient implements IHTTPClient } /** - * @throws HTTPException\InternalServerErrorException + * {@inheritDoc} */ public function request(string $method, string $url, array $opts = []): IHTTPResult { @@ -95,35 +95,39 @@ class HTTPClient implements IHTTPClient $conf = []; - if (!empty($opts['cookiejar'])) { - $jar = new FileCookieJar($opts['cookiejar']); + if (!empty($opts[HTTPRequestOptions::COOKIEJAR])) { + $jar = new FileCookieJar($opts[HTTPRequestOptions::COOKIEJAR]); $conf[RequestOptions::COOKIES] = $jar; } - $header = []; + $headers = []; - if (!empty($opts['accept_content'])) { - $header['Accept'] = $opts['accept_content']; + if (!empty($opts[HTTPRequestOptions::ACCEPT_CONTENT])) { + $headers['Accept'] = $opts[HTTPRequestOptions::ACCEPT_CONTENT]; } - if (!empty($opts['header'])) { - $header = array_merge($opts['header'], $header); - } - - if (!empty($opts['headers'])) { + if (!empty($opts[HTTPRequestOptions::LEGACY_HEADER])) { $this->logger->notice('Wrong option \'headers\' used.'); - $header = array_merge($opts['headers'], $header); + $headers = array_merge($opts[HTTPRequestOptions::LEGACY_HEADER], $headers); } - $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header); + if (!empty($opts[HTTPRequestOptions::HEADERS])) { + $headers = array_merge($opts[HTTPRequestOptions::HEADERS], $headers); + } - if (!empty($opts['timeout'])) { - $conf[RequestOptions::TIMEOUT] = $opts['timeout']; + $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers); + + if (!empty($opts[HTTPRequestOptions::TIMEOUT])) { + $conf[RequestOptions::TIMEOUT] = $opts[HTTPRequestOptions::TIMEOUT]; + } + + if (!empty($opts[HTTPRequestOptions::BODY])) { + $conf[RequestOptions::BODY] = $opts[HTTPRequestOptions::BODY]; } $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) { - if (!empty($opts['content_length']) && - $response->getHeaderLine('Content-Length') > $opts['content_length']) { + if (!empty($opts[HTTPRequestOptions::CONTENT_LENGTH]) && + (int)$response->getHeaderLine('Content-Length') > $opts[HTTPRequestOptions::CONTENT_LENGTH]) { throw new TransferException('The file is too big!'); } }; @@ -160,8 +164,6 @@ class HTTPClient implements IHTTPClient } /** {@inheritDoc} - * - * @throws HTTPException\InternalServerErrorException */ public function head(string $url, array $opts = []): IHTTPResult { @@ -183,10 +185,10 @@ class HTTPClient implements IHTTPClient { $opts = []; - $opts[RequestOptions::JSON] = $params; + $opts[RequestOptions::BODY] = $params; if (!empty($headers)) { - $opts['headers'] = $headers; + $opts[RequestOptions::HEADERS] = $headers; } if (!empty($timeout)) { diff --git a/src/Network/HTTPRequestOptions.php b/src/Network/HTTPRequestOptions.php new file mode 100644 index 000000000..7cfee892a --- /dev/null +++ b/src/Network/HTTPRequestOptions.php @@ -0,0 +1,40 @@ + (mixed) setting the body for sending data + * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value + * 'timeout' => int Timeout in seconds, default system config value or 60 seconds + * 'cookiejar' => path to cookie jar file + * 'header' => header array + * 'content_length' => int maximum File content length * * @return IHTTPResult */