Skip to content

Commit 1685e81

Browse files
[Bug][Swoole] Disable unintentional swoole header formatting by config value (#710)
* Implemented config octane.swoole.header_name_formatting to deactivate swoole header formatting. on-behalf-of: @e-solutions-GmbH <[email protected]> * Update SwooleClient.php * Update SwooleClient.php * Update SwooleClientTest.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 226a7e6 commit 1685e81

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/Swoole/SwooleClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ public function serveStaticFile(Request $request, RequestContext $context): void
119119
$octaneConfig = $context->octaneConfig ?? [];
120120

121121
if (! empty($octaneConfig['static_file_headers'])) {
122+
$formatHeaders = config('octane.swoole.format_headers', true);
123+
122124
foreach ($octaneConfig['static_file_headers'] as $pattern => $headers) {
123125
if ($request->is($pattern)) {
124126
foreach ($headers as $name => $value) {
125-
$swooleResponse->header($name, $value);
127+
$swooleResponse->header($name, $value, $formatHeaders);
126128
}
127129
}
128130
}
@@ -159,9 +161,11 @@ public function sendResponseHeaders(Response $response, SwooleResponse $swooleRe
159161
unset($headers['Set-Cookie']);
160162
}
161163

164+
$formatHeaders = config('octane.swoole.format_headers', true);
165+
162166
foreach ($headers as $name => $values) {
163167
foreach ($values as $value) {
164-
$swooleResponse->header($name, $value);
168+
$swooleResponse->header($name, $value, $formatHeaders);
165169
}
166170
}
167171

tests/SwooleClientTest.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Octane\Tests;
44

5+
use Config;
56
use Exception;
67
use Illuminate\Http\Request;
78
use Illuminate\Http\Response;
@@ -140,7 +141,7 @@ public function test_static_file_headers_can_be_sent(): void
140141
]);
141142

142143
$swooleResponse->shouldReceive('status')->once()->with(200);
143-
$swooleResponse->shouldReceive('header')->once()->with('X-Test-Header', 'Valid');
144+
$swooleResponse->shouldReceive('header')->once()->with('X-Test-Header', 'Valid', true);
144145
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/plain');
145146
$swooleResponse->shouldReceive('sendfile')->once()->with(realpath(__DIR__.'/public/foo.txt'));
146147

@@ -185,9 +186,9 @@ public function test_respond_method_sends_response_to_swoole(): void
185186
$swooleResponse = Mockery::mock('Swoole\Http\Response');
186187

187188
$swooleResponse->shouldReceive('status')->once()->with(200);
188-
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
189-
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
190-
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
189+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', true);
190+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', true);
191+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), true);
191192
$swooleResponse->shouldReceive('cookie')->once()->with('new', 'value', 0, '/', '', false, true, 'lax');
192193
$swooleResponse->shouldReceive('cookie')->once()->with('cleared', 'deleted', Mockery::type('int'), '/', '', false, true, 'lax');
193194
$swooleResponse->shouldReceive('write')->with('Hello World');
@@ -210,9 +211,9 @@ public function test_respond_method_send_streamed_response_to_swoole(): void
210211
$swooleResponse = Mockery::mock('Swoole\Http\Response');
211212

212213
$swooleResponse->shouldReceive('status')->once()->with(200);
213-
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
214-
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
215-
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
214+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', true);
215+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', true);
216+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), true);
216217
$swooleResponse->shouldReceive('write')->once()->with('Hello World');
217218
$swooleResponse->shouldReceive('end')->once();
218219

@@ -231,9 +232,9 @@ public function test_respond_method_with_laravel_specific_status_code_sends_resp
231232
$swooleResponse = Mockery::mock('Swoole\Http\Response');
232233

233234
$swooleResponse->shouldReceive('status')->once()->with(419, 'Page Expired');
234-
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
235-
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
236-
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
235+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', true);
236+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', true);
237+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), true);
237238
$swooleResponse->shouldReceive('write')->with('Hello World');
238239
$swooleResponse->shouldReceive('end')->once();
239240

@@ -290,9 +291,9 @@ public function test_respond_method_send_not_chunked_response_to_swoole(): void
290291
$swooleResponse = Mockery::mock(SwooleResponse::class);
291292

292293
$swooleResponse->shouldReceive('status')->once()->with(200);
293-
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
294-
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
295-
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
294+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', true);
295+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', true);
296+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), true);
296297
$swooleResponse->shouldReceive('write')->never();
297298
$swooleResponse->shouldReceive('end')->once()->with('Hello World');
298299

@@ -311,9 +312,9 @@ public function test_respond_method_send_chunked_response_to_swoole(): void
311312
$swooleResponse = Mockery::mock('Swoole\Http\Response');
312313

313314
$swooleResponse->shouldReceive('status')->once()->with(200);
314-
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
315-
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
316-
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
315+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', true);
316+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', true);
317+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), true);
317318
$swooleResponse->shouldReceive('write')->once()->with('Hello ');
318319
$swooleResponse->shouldReceive('write')->once()->with('World');
319320
$swooleResponse->shouldReceive('end')->once();
@@ -324,4 +325,28 @@ public function test_respond_method_send_chunked_response_to_swoole(): void
324325
'swooleResponse' => $swooleResponse,
325326
]), new OctaneResponse($response));
326327
}
328+
329+
/** @doesNotPerformAssertions @test */
330+
public function test_respond_method_preserves_header_formatting_if_configured(): void
331+
{
332+
$this->createApplication();
333+
334+
$client = new SwooleClient;
335+
336+
Config::set('octane.swoole.format_headers', false);
337+
338+
$swooleResponse = Mockery::mock('Swoole\Http\Response');
339+
340+
$swooleResponse->shouldReceive('status')->once()->with(200);
341+
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private', false);
342+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html', false);
343+
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'), false);
344+
$swooleResponse->shouldReceive('end')->once();
345+
346+
$response = new Response(null, 200, ['Content-Type' => 'text/html']);
347+
348+
$client->respond(new RequestContext([
349+
'swooleResponse' => $swooleResponse,
350+
]), new OctaneResponse($response));
351+
}
327352
}

0 commit comments

Comments
 (0)