Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: remove CURLRequest headers sharing from $_SERVER #5249

Merged
merged 21 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d1edaee
fix: reset unneeded headers to prevent request error
kenjis Oct 19, 2021
245dade
fix: reset unneeded configs
kenjis Oct 19, 2021
fa3620b
test: add test for CURLRequest unshared headers
kenjis Oct 19, 2021
4b51e01
test: fix test method names
kenjis Oct 19, 2021
e307ef5
fix: remove populateHeaders() not to share request headers form brows…
kenjis Oct 28, 2021
9cbe0f2
fix: make $unsharedHeaders protected
kenjis Oct 29, 2021
c9bac5f
fix: reset $unsharedHeaders after sending request
kenjis Oct 29, 2021
d39543e
docs: improve changelogs/v4.1.5.rst format
kenjis Oct 29, 2021
9a6a0d3
docs: add upgrading and changelog
kenjis Oct 29, 2021
613b980
fix: remove $unsharedHeaders and add $defaultOptions
kenjis Oct 29, 2021
8b08642
docs: fix changelogs/v4.1.5.rst
kenjis Oct 29, 2021
182c252
docs: fix upgrade_415.rst
kenjis Oct 29, 2021
aed89d9
fix: add config $CURLRequestShareOptions for the CURLRequest
kenjis Oct 29, 2021
436013e
test: add tests for $CURLRequestShareOptions = false
kenjis Oct 29, 2021
9e88ec4
test: add tests for sharing options
kenjis Oct 29, 2021
a6c06cc
refactor: remove unnecessary second param
kenjis Oct 29, 2021
f53a1da
refactor: move Config\App::$CURLRequestShareOptions to Config\CURLReq…
kenjis Oct 30, 2021
772a408
fix: change from private to protected
kenjis Oct 30, 2021
3901496
docs: add PHPDoc comments
kenjis Oct 30, 2021
8a5973b
config: add curlrequest.shareOptions
kenjis Oct 30, 2021
88963ba
docs: add about CURLRequest $shareOptions
kenjis Oct 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/Config/CURLRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class CURLRequest extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* CURLRequest Share Options
* --------------------------------------------------------------------------
*
* Whether share options between requests or not.
*
* If true, all the options won't be reset between requests.
* It may cause an error request with unnecessary headers.
*
* @var bool
*/
public $shareOptions = true;
}
6 changes: 6 additions & 0 deletions env
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@
#--------------------------------------------------------------------

# logger.threshold = 4

#--------------------------------------------------------------------
# CURLRequest
#--------------------------------------------------------------------

# curlrequest.shareOptions = true
71 changes: 56 additions & 15 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;
use Config\CURLRequest as ConfigCURLRequest;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -42,7 +43,14 @@ class CURLRequest extends Request
*
* @var array
*/
protected $config = [
protected $config;

/**
* The default setting values
*
* @var array
*/
protected $defaultConfig = [
'timeout' => 0.0,
'connect_timeout' => 150,
'debug' => false,
Expand Down Expand Up @@ -72,6 +80,23 @@ class CURLRequest extends Request
*/
protected $delay = 0.0;

/**
* The default options from the constructor. Applied to all requests.
*
* @var array
*/
private $defaultOptions;

/**
* Whether share options between requests or not.
*
* If true, all the options won't be reset between requests.
* It may cause an error request with unnecessary headers.
*
* @var bool
*/
private $shareOptions;

/**
* Takes an array of options to set the following possible class properties:
*
Expand All @@ -92,9 +117,15 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response

parent::__construct($config);

$this->response = $response;
$this->baseURI = $uri->useRawQueryString();
$this->response = $response;
$this->baseURI = $uri->useRawQueryString();
$this->defaultOptions = $options;

/** @var ConfigCURLRequest|null $configCURLRequest */
$configCURLRequest = config('CURLRequest');
$this->shareOptions = $configCURLRequest->shareOptions ?? true;

$this->config = $this->defaultConfig;
$this->parseOptions($options);
}

Expand All @@ -114,9 +145,29 @@ public function request($method, string $url, array $options = []): ResponseInte

$this->send($method, $url);

if ($this->shareOptions === false) {
$this->resetOptions();
}

return $this->response;
}

/**
* Reset all options to default.
*/
protected function resetOptions()
{
// Reset headers
$this->headers = [];
$this->headerMap = [];

// Reset configs
$this->config = $this->defaultConfig;

// Set the default options for next request
$this->parseOptions($this->defaultOptions);
}

/**
* Convenience method for sending a GET request.
*/
Expand Down Expand Up @@ -350,27 +401,17 @@ public function send(string $method, string $url)
}

/**
* Takes all headers current part of this request and adds them
* to the cURL request.
* Adds $this->headers to the cURL request.
*/
protected function applyRequestHeaders(array $curlOptions = []): array
{
if (empty($this->headers)) {
$this->populateHeaders();
// Otherwise, it will corrupt the request
$this->removeHeader('Host');
$this->removeHeader('Accept-Encoding');
}

$headers = $this->headers();

if (empty($headers)) {
return $curlOptions;
}

$set = [];

foreach (array_keys($headers) as $name) {
foreach (array_keys($this->headers) as $name) {
$set[] = $name . ': ' . $this->getHeaderLine($name);
}

Expand Down
Loading