fix: isolate Function Responses from leftover output buffers and arg_separator.outputUnverified
A streamed Response leaving a buffer open lost its body and swallowed the output of later Invocations, and one closing every buffer triggered a notice. The query string is now always built with "&".
581465d parent: d541338 modified
src/HttpConverter.php +8 -2 | @@ -32,7 +32,8 @@ final class HttpConverter | ||
| 32 | 32 | { |
| 33 | 33 | $method = strtoupper(self::string($event['method'] ?? $event['httpMethod'] ?? null) ?? 'GET'); |
| 34 | 34 | $path = '/'.ltrim(self::string($event['path'] ?? null) ?? '', '/'); |
| 35 | - $queryString = http_build_query(self::stringMap($event['queryStringParameters'] ?? null)); | |
| 35 | + // An explicit separator, as arg_separator.output may be "&". | |
| 36 | + $queryString = http_build_query(self::stringMap($event['queryStringParameters'] ?? null), '', '&'); | |
| 36 | 37 | parse_str($queryString, $query); |
| 37 | 38 | |
| 38 | 39 | $body = self::string($event['body'] ?? null) ?? ''; |
| @@ -82,6 +83,7 @@ final class HttpConverter | ||
| 82 | 83 | public static function toFunctionResponse(Response $response): array |
| 83 | 84 | { |
| 84 | 85 | $body = ''; |
| 86 | + $level = ob_get_level(); | |
| 85 | 87 | // A callback handler keeps the chunks even when a streamed callback calls ob_flush(). |
| 86 | 88 | ob_start(static function (string $chunk) use (&$body): string { |
| 87 | 89 | $body .= $chunk; |
| @@ -91,7 +93,11 @@ final class HttpConverter | ||
| 91 | 93 | try { |
| 92 | 94 | $response->sendContent(); |
| 93 | 95 | } finally { |
| 94 | - ob_end_flush(); | |
| 96 | + // Buffers the Application left open would swallow the output of later Invocations, | |
| 97 | + // and ours may already be gone if it closed every buffer. | |
| 98 | + while (ob_get_level() > $level) { | |
| 99 | + ob_end_flush(); | |
| 100 | + } | |
| 95 | 101 | } |
| 96 | 102 | |
| 97 | 103 | $values = $response->headers->allPreserveCaseWithoutCookies(); |
| @@ -32,7 +32,8 @@ final class HttpConverter | |||
| 32 | { | 32 | { |
| 33 | $method = strtoupper(self::string($event['method'] ?? $event['httpMethod'] ?? null) ?? 'GET'); | 33 | $method = strtoupper(self::string($event['method'] ?? $event['httpMethod'] ?? null) ?? 'GET'); |
| 34 | $path = '/'.ltrim(self::string($event['path'] ?? null) ?? '', '/'); | 34 | $path = '/'.ltrim(self::string($event['path'] ?? null) ?? '', '/'); |
| 35 | - $queryString = http_build_query(self::stringMap($event['queryStringParameters'] ?? null)); | 35 | + // An explicit separator, as arg_separator.output may be "&". |
| 36 | + $queryString = http_build_query(self::stringMap($event['queryStringParameters'] ?? null), '', '&'); | ||
| 36 | parse_str($queryString, $query); | 37 | parse_str($queryString, $query); |
| 37 | 38 | ||
| 38 | $body = self::string($event['body'] ?? null) ?? ''; | 39 | $body = self::string($event['body'] ?? null) ?? ''; |
| @@ -82,6 +83,7 @@ final class HttpConverter | |||
| 82 | public static function toFunctionResponse(Response $response): array | 83 | public static function toFunctionResponse(Response $response): array |
| 83 | { | 84 | { |
| 84 | $body = ''; | 85 | $body = ''; |
| 86 | + $level = ob_get_level(); | ||
| 85 | // A callback handler keeps the chunks even when a streamed callback calls ob_flush(). | 87 | // A callback handler keeps the chunks even when a streamed callback calls ob_flush(). |
| 86 | ob_start(static function (string $chunk) use (&$body): string { | 88 | ob_start(static function (string $chunk) use (&$body): string { |
| 87 | $body .= $chunk; | 89 | $body .= $chunk; |
| @@ -91,7 +93,11 @@ final class HttpConverter | |||
| 91 | try { | 93 | try { |
| 92 | $response->sendContent(); | 94 | $response->sendContent(); |
| 93 | } finally { | 95 | } finally { |
| 94 | - ob_end_flush(); | 96 | + // Buffers the Application left open would swallow the output of later Invocations, |
| 97 | + // and ours may already be gone if it closed every buffer. | ||
| 98 | + while (ob_get_level() > $level) { | ||
| 99 | + ob_end_flush(); | ||
| 100 | + } | ||
| 95 | } | 101 | } |
| 96 | 102 | ||
| 97 | $values = $response->headers->allPreserveCaseWithoutCookies(); | 103 | $values = $response->headers->allPreserveCaseWithoutCookies(); |
modified
tests/HttpConverterTest.php +25 -0 | @@ -72,6 +72,18 @@ final class HttpConverterTest extends TestCase | ||
| 72 | 72 | self::assertSame(['filter' => ['status' => 'open'], 'tags' => ['php']], $request->query->all()); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | + public function testQueryStringIgnoresTheConfiguredArgSeparator(): void | |
| 76 | + { | |
| 77 | + $separator = ini_set('arg_separator.output', '&'); | |
| 78 | + try { | |
| 79 | + $request = HttpConverter::toRequest(['queryStringParameters' => ['a' => '1', 'b' => '2']]); | |
| 80 | + } finally { | |
| 81 | + ini_set('arg_separator.output', (string) $separator); | |
| 82 | + } | |
| 83 | + | |
| 84 | + self::assertSame(['a' => '1', 'b' => '2'], $request->query->all()); | |
| 85 | + } | |
| 86 | + | |
| 75 | 87 | public function testBase64BodyIsDecoded(): void |
| 76 | 88 | { |
| 77 | 89 | $binary = random_bytes(64)."\xff\xfe"; |
| @@ -236,6 +248,19 @@ final class HttpConverterTest extends TestCase | ||
| 236 | 248 | self::assertSame('chunk 1, chunk 2', HttpConverter::toFunctionResponse($response)['body']); |
| 237 | 249 | } |
| 238 | 250 | |
| 251 | + public function testOutputBuffersLeftOpenByTheResponseAreClosed(): void | |
| 252 | + { | |
| 253 | + $level = ob_get_level(); | |
| 254 | + | |
| 255 | + $body = HttpConverter::toFunctionResponse(new StreamedResponse(static function (): void { | |
| 256 | + ob_start(); | |
| 257 | + echo 'kept'; | |
| 258 | + }))['body']; | |
| 259 | + | |
| 260 | + self::assertSame($level, ob_get_level()); | |
| 261 | + self::assertSame('kept', $body); | |
| 262 | + } | |
| 263 | + | |
| 239 | 264 | public function testBinaryFileResponseIsBuffered(): void |
| 240 | 265 | { |
| 241 | 266 | $file = tempnam(sys_get_temp_dir(), 'scaleway-runtime'); |
| @@ -72,6 +72,18 @@ final class HttpConverterTest extends TestCase | |||
| 72 | self::assertSame(['filter' => ['status' => 'open'], 'tags' => ['php']], $request->query->all()); | 72 | self::assertSame(['filter' => ['status' => 'open'], 'tags' => ['php']], $request->query->all()); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | + public function testQueryStringIgnoresTheConfiguredArgSeparator(): void | ||
| 76 | + { | ||
| 77 | + $separator = ini_set('arg_separator.output', '&'); | ||
| 78 | + try { | ||
| 79 | + $request = HttpConverter::toRequest(['queryStringParameters' => ['a' => '1', 'b' => '2']]); | ||
| 80 | + } finally { | ||
| 81 | + ini_set('arg_separator.output', (string) $separator); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + self::assertSame(['a' => '1', 'b' => '2'], $request->query->all()); | ||
| 85 | + } | ||
| 86 | + | ||
| 75 | public function testBase64BodyIsDecoded(): void | 87 | public function testBase64BodyIsDecoded(): void |
| 76 | { | 88 | { |
| 77 | $binary = random_bytes(64)."\xff\xfe"; | 89 | $binary = random_bytes(64)."\xff\xfe"; |
| @@ -236,6 +248,19 @@ final class HttpConverterTest extends TestCase | |||
| 236 | self::assertSame('chunk 1, chunk 2', HttpConverter::toFunctionResponse($response)['body']); | 248 | self::assertSame('chunk 1, chunk 2', HttpConverter::toFunctionResponse($response)['body']); |
| 237 | } | 249 | } |
| 238 | 250 | ||
| 251 | + public function testOutputBuffersLeftOpenByTheResponseAreClosed(): void | ||
| 252 | + { | ||
| 253 | + $level = ob_get_level(); | ||
| 254 | + | ||
| 255 | + $body = HttpConverter::toFunctionResponse(new StreamedResponse(static function (): void { | ||
| 256 | + ob_start(); | ||
| 257 | + echo 'kept'; | ||
| 258 | + }))['body']; | ||
| 259 | + | ||
| 260 | + self::assertSame($level, ob_get_level()); | ||
| 261 | + self::assertSame('kept', $body); | ||
| 262 | + } | ||
| 263 | + | ||
| 239 | public function testBinaryFileResponseIsBuffered(): void | 264 | public function testBinaryFileResponseIsBuffered(): void |
| 240 | { | 265 | { |
| 241 | $file = tempnam(sys_get_temp_dir(), 'scaleway-runtime'); | 266 | $file = tempnam(sys_get_temp_dir(), 'scaleway-runtime'); |