13
13
14
14
use CodeIgniter \HTTP \Exceptions \HTTPException ;
15
15
use Config \App ;
16
+ use Config \CURLRequest as ConfigCURLRequest ;
16
17
use InvalidArgumentException ;
17
18
18
19
/**
@@ -42,7 +43,14 @@ class CURLRequest extends Request
42
43
*
43
44
* @var array
44
45
*/
45
- protected $ config = [
46
+ protected $ config ;
47
+
48
+ /**
49
+ * The default setting values
50
+ *
51
+ * @var array
52
+ */
53
+ protected $ defaultConfig = [
46
54
'timeout ' => 0.0 ,
47
55
'connect_timeout ' => 150 ,
48
56
'debug ' => false ,
@@ -72,6 +80,23 @@ class CURLRequest extends Request
72
80
*/
73
81
protected $ delay = 0.0 ;
74
82
83
+ /**
84
+ * The default options from the constructor. Applied to all requests.
85
+ *
86
+ * @var array
87
+ */
88
+ private $ defaultOptions ;
89
+
90
+ /**
91
+ * Whether share options between requests or not.
92
+ *
93
+ * If true, all the options won't be reset between requests.
94
+ * It may cause an error request with unnecessary headers.
95
+ *
96
+ * @var bool
97
+ */
98
+ private $ shareOptions ;
99
+
75
100
/**
76
101
* Takes an array of options to set the following possible class properties:
77
102
*
@@ -92,9 +117,15 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
92
117
93
118
parent ::__construct ($ config );
94
119
95
- $ this ->response = $ response ;
96
- $ this ->baseURI = $ uri ->useRawQueryString ();
120
+ $ this ->response = $ response ;
121
+ $ this ->baseURI = $ uri ->useRawQueryString ();
122
+ $ this ->defaultOptions = $ options ;
123
+
124
+ /** @var ConfigCURLRequest|null $configCURLRequest */
125
+ $ configCURLRequest = config ('CURLRequest ' );
126
+ $ this ->shareOptions = $ configCURLRequest ->shareOptions ?? true ;
97
127
128
+ $ this ->config = $ this ->defaultConfig ;
98
129
$ this ->parseOptions ($ options );
99
130
}
100
131
@@ -114,9 +145,29 @@ public function request($method, string $url, array $options = []): ResponseInte
114
145
115
146
$ this ->send ($ method , $ url );
116
147
148
+ if ($ this ->shareOptions === false ) {
149
+ $ this ->resetOptions ();
150
+ }
151
+
117
152
return $ this ->response ;
118
153
}
119
154
155
+ /**
156
+ * Reset all options to default.
157
+ */
158
+ protected function resetOptions ()
159
+ {
160
+ // Reset headers
161
+ $ this ->headers = [];
162
+ $ this ->headerMap = [];
163
+
164
+ // Reset configs
165
+ $ this ->config = $ this ->defaultConfig ;
166
+
167
+ // Set the default options for next request
168
+ $ this ->parseOptions ($ this ->defaultOptions );
169
+ }
170
+
120
171
/**
121
172
* Convenience method for sending a GET request.
122
173
*/
@@ -350,27 +401,17 @@ public function send(string $method, string $url)
350
401
}
351
402
352
403
/**
353
- * Takes all headers current part of this request and adds them
354
- * to the cURL request.
404
+ * Adds $this->headers to the cURL request.
355
405
*/
356
406
protected function applyRequestHeaders (array $ curlOptions = []): array
357
407
{
358
408
if (empty ($ this ->headers )) {
359
- $ this ->populateHeaders ();
360
- // Otherwise, it will corrupt the request
361
- $ this ->removeHeader ('Host ' );
362
- $ this ->removeHeader ('Accept-Encoding ' );
363
- }
364
-
365
- $ headers = $ this ->headers ();
366
-
367
- if (empty ($ headers )) {
368
409
return $ curlOptions ;
369
410
}
370
411
371
412
$ set = [];
372
413
373
- foreach (array_keys ($ headers ) as $ name ) {
414
+ foreach (array_keys ($ this -> headers ) as $ name ) {
374
415
$ set [] = $ name . ': ' . $ this ->getHeaderLine ($ name );
375
416
}
376
417
0 commit comments