Skip to content

Commit 6bf0b4b

Browse files
committed
refactor!: remove $upper functionality in Request::getMethod()
1 parent 22906b3 commit 6bf0b4b

17 files changed

+76
-94
lines changed

app/Config/Feature.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,17 @@ class Feature extends BaseConfig
1818
* Use filter execution order in 4.4 or before.
1919
*/
2020
public bool $oldFilterOrder = false;
21+
22+
/**
23+
* Use lowercase HTTP method names like "get", "post" in Config\Filters::$methods.
24+
*
25+
* But the HTTP method is case-sensitive. So using lowercase is wrong.
26+
* We should disable this and use uppercase names like "GET", "POST", etc.
27+
*
28+
* The method token is case-sensitive because it might be used as a gateway
29+
* to object-based systems with case-sensitive method names. By convention,
30+
* standardized methods are defined in all-uppercase US-ASCII letters.
31+
* https://www.rfc-editor.org/rfc/rfc9110#name-overview
32+
*/
33+
public bool $lowerCaseFilterMethods = true;
2134
}

system/CodeIgniter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ public function storePreviousURL($uri)
10271027
public function spoofRequestMethod()
10281028
{
10291029
// Only works with POSTED forms
1030-
if (strtolower($this->request->getMethod()) !== 'post') {
1030+
if ($this->request->getMethod() !== 'POST') {
10311031
return;
10321032
}
10331033

@@ -1038,7 +1038,7 @@ public function spoofRequestMethod()
10381038
}
10391039

10401040
// Only allows PUT, PATCH, DELETE
1041-
if (in_array(strtoupper($method), ['PUT', 'PATCH', 'DELETE'], true)) {
1041+
if (in_array($method, ['PUT', 'PATCH', 'DELETE'], true)) {
10421042
$this->request = $this->request->setMethod($method);
10431043
}
10441044
}

system/Filters/Filters.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,11 @@ protected function processMethods()
494494
return;
495495
}
496496

497-
// Request method won't be set for CLI-based requests
498-
$method = strtolower($this->request->getMethod()) ?? 'cli';
497+
$method = $this->request->getMethod();
498+
499+
if (config(Feature::class)->lowerCaseFilterMethods) {
500+
$method = strtolower($method);
501+
}
499502

500503
if (array_key_exists($method, $this->config->methods)) {
501504
if (config(Feature::class)->oldFilterOrder) {

system/HTTP/CLIRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CLIRequest extends Request
5656
*
5757
* @var string
5858
*/
59-
protected $method = 'cli';
59+
protected $method = 'CLI';
6060

6161
/**
6262
* Constructor

system/HTTP/CURLRequest.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
130130
* Sends an HTTP request to the specified $url. If this is a relative
131131
* URL, it will be merged with $this->baseURI to form a complete URL.
132132
*
133-
* @param string $method
133+
* @param string $method HTTP method
134134
*/
135135
public function request($method, string $url, array $options = []): ResponseInterface
136136
{
@@ -177,55 +177,55 @@ protected function resetOptions()
177177
*/
178178
public function get(string $url, array $options = []): ResponseInterface
179179
{
180-
return $this->request('get', $url, $options);
180+
return $this->request('GET', $url, $options);
181181
}
182182

183183
/**
184184
* Convenience method for sending a DELETE request.
185185
*/
186186
public function delete(string $url, array $options = []): ResponseInterface
187187
{
188-
return $this->request('delete', $url, $options);
188+
return $this->request('DELETE', $url, $options);
189189
}
190190

191191
/**
192192
* Convenience method for sending a HEAD request.
193193
*/
194194
public function head(string $url, array $options = []): ResponseInterface
195195
{
196-
return $this->request('head', $url, $options);
196+
return $this->request('HEAD', $url, $options);
197197
}
198198

199199
/**
200200
* Convenience method for sending an OPTIONS request.
201201
*/
202202
public function options(string $url, array $options = []): ResponseInterface
203203
{
204-
return $this->request('options', $url, $options);
204+
return $this->request('OPTIONS', $url, $options);
205205
}
206206

207207
/**
208208
* Convenience method for sending a PATCH request.
209209
*/
210210
public function patch(string $url, array $options = []): ResponseInterface
211211
{
212-
return $this->request('patch', $url, $options);
212+
return $this->request('PATCH', $url, $options);
213213
}
214214

215215
/**
216216
* Convenience method for sending a POST request.
217217
*/
218218
public function post(string $url, array $options = []): ResponseInterface
219219
{
220-
return $this->request('post', $url, $options);
220+
return $this->request('POST', $url, $options);
221221
}
222222

223223
/**
224224
* Convenience method for sending a PUT request.
225225
*/
226226
public function put(string $url, array $options = []): ResponseInterface
227227
{
228-
return $this->request('put', $url, $options);
228+
return $this->request('PUT', $url, $options);
229229
}
230230

231231
/**
@@ -339,17 +339,6 @@ protected function prepareURL(string $url): string
339339
);
340340
}
341341

342-
/**
343-
* Get the request method. Overrides the Request class' method
344-
* since users expect a different answer here.
345-
*
346-
* @param bool|false $upper Whether to return in upper or lower case.
347-
*/
348-
public function getMethod(bool $upper = false): string
349-
{
350-
return ($upper) ? strtoupper($this->method) : strtolower($this->method);
351-
}
352-
353342
/**
354343
* Fires the actual cURL request.
355344
*
@@ -446,8 +435,6 @@ protected function applyRequestHeaders(array $curlOptions = []): array
446435
*/
447436
protected function applyMethod(string $method, array $curlOptions): array
448437
{
449-
$method = strtoupper($method);
450-
451438
$this->method = $method;
452439
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
453440

system/HTTP/IncomingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function is(string $type): bool
406406
$httpMethods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS'];
407407

408408
if (in_array($valueUpper, $httpMethods, true)) {
409-
return strtoupper($this->getMethod()) === $valueUpper;
409+
return $this->getMethod() === $valueUpper;
410410
}
411411

412412
if ($valueUpper === 'JSON') {

system/HTTP/OutgoingRequest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ private function getHostFromUri(URI $uri): string
6666
}
6767

6868
/**
69-
* Get the request method.
69+
* Retrieves the HTTP method of the request.
7070
*
71-
* @param bool $upper Whether to return in upper or lower case.
72-
*
73-
* @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent
71+
* @return string Returns the request method.
7472
*/
75-
public function getMethod(bool $upper = false): string
73+
public function getMethod(): string
7674
{
77-
return ($upper) ? strtoupper($this->method) : strtolower($this->method);
75+
return $this->method;
7876
}
7977

8078
/**

system/HTTP/OutgoingRequestInterface.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@
2121
interface OutgoingRequestInterface extends MessageInterface
2222
{
2323
/**
24-
* Get the request method.
25-
* An extension of PSR-7's getMethod to allow casing.
24+
* Retrieves the HTTP method of the request.
2625
*
27-
* @param bool $upper Whether to return in upper or lower case.
28-
*
29-
* @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent
26+
* @return string Returns the request method.
3027
*/
31-
public function getMethod(bool $upper = false): string;
28+
public function getMethod(): string;
3229

3330
/**
3431
* Return an instance with the provided HTTP method.

system/HTTP/Request.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,6 @@ public function __construct($config = null) // @phpstan-ignore-line
4949
}
5050
}
5151

52-
/**
53-
* Get the request method.
54-
*
55-
* @param bool $upper Whether to return in upper or lower case.
56-
*
57-
* @deprecated 4.0.5 The $upper functionality will be removed and this will revert to its PSR-7 equivalent
58-
*
59-
* @codeCoverageIgnore
60-
*/
61-
public function getMethod(bool $upper = false): string
62-
{
63-
return ($upper) ? strtoupper($this->method) : strtolower($this->method);
64-
}
65-
6652
/**
6753
* Sets the request method. Used when spoofing the request.
6854
*

tests/system/CodeIgniterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ public function testSpoofRequestMethodCanUsePUT(): void
733733
$this->codeigniter->run();
734734
ob_get_clean();
735735

736-
$this->assertSame('put', Services::incomingrequest()->getMethod());
736+
$this->assertSame('PUT', Services::incomingrequest()->getMethod());
737737
}
738738

739739
public function testSpoofRequestMethodCannotUseGET(): void
@@ -758,7 +758,7 @@ public function testSpoofRequestMethodCannotUseGET(): void
758758
$this->codeigniter->run();
759759
ob_get_clean();
760760

761-
$this->assertSame('post', Services::incomingrequest()->getMethod());
761+
$this->assertSame('POST', Services::incomingrequest()->getMethod());
762762
}
763763

764764
/**

tests/system/HTTP/CLIRequestTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,7 @@ public function testGetIPAddressDefault(): void
525525
public function testMethodReturnsRightStuff(): void
526526
{
527527
// Defaults method to CLI now.
528-
$this->assertSame('cli', $this->request->getMethod());
529-
$this->assertSame('CLI', $this->request->getMethod(true));
528+
$this->assertSame('CLI', $this->request->getMethod());
530529
}
531530

532531
public function testMethodIsCliReturnsAlwaysTrue(): void

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testGetSetsCorrectMethod(): void
103103
{
104104
$this->request->get('http://example.com');
105105

106-
$this->assertSame('get', $this->request->getMethod());
106+
$this->assertSame('GET', $this->request->getMethod());
107107

108108
$options = $this->request->curl_options;
109109

@@ -115,7 +115,7 @@ public function testDeleteSetsCorrectMethod(): void
115115
{
116116
$this->request->delete('http://example.com');
117117

118-
$this->assertSame('delete', $this->request->getMethod());
118+
$this->assertSame('DELETE', $this->request->getMethod());
119119

120120
$options = $this->request->curl_options;
121121

@@ -127,7 +127,7 @@ public function testHeadSetsCorrectMethod(): void
127127
{
128128
$this->request->head('http://example.com');
129129

130-
$this->assertSame('head', $this->request->getMethod());
130+
$this->assertSame('HEAD', $this->request->getMethod());
131131

132132
$options = $this->request->curl_options;
133133

@@ -139,7 +139,7 @@ public function testOptionsSetsCorrectMethod(): void
139139
{
140140
$this->request->options('http://example.com');
141141

142-
$this->assertSame('options', $this->request->getMethod());
142+
$this->assertSame('OPTIONS', $this->request->getMethod());
143143

144144
$options = $this->request->curl_options;
145145

@@ -281,7 +281,7 @@ public function testPatchSetsCorrectMethod(): void
281281
{
282282
$this->request->patch('http://example.com');
283283

284-
$this->assertSame('patch', $this->request->getMethod());
284+
$this->assertSame('PATCH', $this->request->getMethod());
285285

286286
$options = $this->request->curl_options;
287287

@@ -293,7 +293,7 @@ public function testPostSetsCorrectMethod(): void
293293
{
294294
$this->request->post('http://example.com');
295295

296-
$this->assertSame('post', $this->request->getMethod());
296+
$this->assertSame('POST', $this->request->getMethod());
297297

298298
$options = $this->request->curl_options;
299299

@@ -305,7 +305,7 @@ public function testPutSetsCorrectMethod(): void
305305
{
306306
$this->request->put('http://example.com');
307307

308-
$this->assertSame('put', $this->request->getMethod());
308+
$this->assertSame('PUT', $this->request->getMethod());
309309

310310
$options = $this->request->curl_options;
311311

@@ -322,19 +322,19 @@ public function testCustomMethodSetsCorrectMethod(): void
322322
$options = $this->request->curl_options;
323323

324324
$this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options);
325-
$this->assertSame('CUSTOM', $options[CURLOPT_CUSTOMREQUEST]);
325+
$this->assertSame('custom', $options[CURLOPT_CUSTOMREQUEST]);
326326
}
327327

328328
public function testRequestMethodGetsSanitized(): void
329329
{
330330
$this->request->request('<script>Custom</script>', 'http://example.com');
331331

332-
$this->assertSame('custom', $this->request->getMethod());
332+
$this->assertSame('Custom', $this->request->getMethod());
333333

334334
$options = $this->request->curl_options;
335335

336336
$this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options);
337-
$this->assertSame('CUSTOM', $options[CURLOPT_CUSTOMREQUEST]);
337+
$this->assertSame('Custom', $options[CURLOPT_CUSTOMREQUEST]);
338338
}
339339

340340
public function testRequestSetsBasicCurlOptions(): void
@@ -951,7 +951,7 @@ public function testPostFormEncoded(): void
951951
'form_params' => $params,
952952
]);
953953

954-
$this->assertSame('post', $this->request->getMethod());
954+
$this->assertSame('POST', $this->request->getMethod());
955955

956956
$options = $this->request->curl_options;
957957

@@ -974,7 +974,7 @@ public function testPostFormMultipart(): void
974974
'multipart' => $params,
975975
]);
976976

977-
$this->assertSame('post', $this->request->getMethod());
977+
$this->assertSame('POST', $this->request->getMethod());
978978

979979
$options = $this->request->curl_options;
980980

@@ -1022,7 +1022,7 @@ public function testJSONData(): void
10221022
'json' => $params,
10231023
]);
10241024

1025-
$this->assertSame('post', $this->request->getMethod());
1025+
$this->assertSame('POST', $this->request->getMethod());
10261026

10271027
$expected = json_encode($params);
10281028
$this->assertSame(

0 commit comments

Comments
 (0)