-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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: CURLRequest - clear response headers between requests #7398
fix: CURLRequest - clear response headers between requests #7398
Conversation
I believe that for each request, a new response instance should be created, and not passed as a dependency. |
I can still understand sharing request options, but I don't believe there is a use case for sharing and overriding response objects. |
The idea was to allow the use of Response classes other than the default one.
Existing headers are now always removed before collecting the new response headers. |
Tests were failed. Please rebase. |
I agree.
I don't understand why do we need to pass a Response object. I think it is not correct to share the same Response object between requests. This is not a bit clean, but I think better: --- a/system/HTTP/CURLRequest.php
+++ b/system/HTTP/CURLRequest.php
@@ -137,7 +137,12 @@ class CURLRequest extends OutgoingRequest
$this->resetOptions();
}
- return $this->response;
+ $response = $this->response;
+
+ // Set new Response for the next request.
+ $this->response = new Response(config('App'));
+
+ return $response;
}
/** |
If possible, I think this (and deprecate --- a/system/HTTP/CURLRequest.php
+++ b/system/HTTP/CURLRequest.php
@@ -125,6 +125,8 @@ class CURLRequest extends OutgoingRequest
*/
public function request($method, string $url, array $options = []): ResponseInterface
{
+ $this->response = new Response(config('App'));
+
$this->parseOptions($options);
$url = $this->prepareURL($url); |
Returning a new response class for every CURL request would mean we tightly couple the response with the CodeIgniter For me, that would be a regression in extensibility. Of course, "resetting" headers would be a compromise, but there is something we get in return. The question is, do we want to offer fewer options and introduce a BC break? |
@michalsn I don't know the real situation, but it seems to me that 90% of CI developers will use the classes that are available. But for example, I want to give the code. And I have not seen angry comments about the impossibility of replacing the class. CodeIgniter4/system/HTTP/IncomingRequest.php Lines 904 to 911 in 354dc67
CodeIgniter4/system/HTTP/Files/FileCollection.php Lines 164 to 187 in 354dc67
|
We should always avoid BC breaks especially in patch versions. Then how about something like this: --- a/system/HTTP/CURLRequest.php
+++ b/system/HTTP/CURLRequest.php
@@ -125,6 +125,8 @@ class CURLRequest extends OutgoingRequest
*/
public function request($method, string $url, array $options = []): ResponseInterface
{
+ $this->response = clone $this->responseOrig;
+
$this->parseOptions($options);
$url = $this->prepareURL($url); |
I don't see a use case for changing the Response class. |
@iRedds I don't know if anybody is using other response classes than the default one, but ~5 years ago, I would take advantage of this approach in one project. The argument that framework already has places where things are tightly coupled doesn't speak to me. Just because we don't give everywhere a choice about what class we will use doesn't mean we should "level down" and take that choice away from the places where we have it now. @kenjis I see the added value in replacing the default class in some cases. A long time ago I had a project where I would extend the Response class to simplify handling the response headers. I was consuming a really specific API. I like your suggestion with The bottom line is that I allow the thought that keeping the support for the custom Response class is just my fixation. Since dropping the support would involve a BC break, I would love to hear from other members. @codeigniter4/core-team any thoughts on this one? |
@michalsn I can offer two alternatives.
public function __construct(protected CURLFactoryInterface $factory, protected array $options = []){}
new CURLRequest(new CURLFactory(), $options)
// Now we can get a new Response object.
$this->factory->response();
protected function buildResponse(): ResponseInterface
{
return new Response(config('App'));
} |
@michalsn Okay, you had the real use cases. So removing injection of ResponseInterface is degrade and a breaking change. We should use Anyway, it is unacceptable to share only one Response object; if you send a request twice, the first response changes. It is a terrible bug. |
1c44da9
to
1f3ce7a
Compare
<?php
namespace App\Controllers;
use CodeIgniter\HTTP\CURLRequest;
use CodeIgniter\HTTP\URI;
use Config\App;
class Home extends BaseController
{
public function index()
{
$client = new CURLRequest(new App(), new URI());
$client->get('https://codeigniter.com/');
}
}
|
Good point. Fixed. |
Description
Fixes #7394
Checklist: