Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Http/Adapter/FPM/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ protected function sendStatus(int $statusCode): void
* Output Header
*
* @param string $key
* @param string $value
* @param string|array<string> $value
* @return void
*/
public function sendHeader(string $key, string $value): void
public function sendHeader(string $key, mixed $value): void
{
\header($key.': '.$value);
if (\is_array($value)) {
foreach ($value as $v) {
\header($key.': '.$v, false);
}
} else {
\header($key.': '.$value);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Adapter/Swoole/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ protected function sendStatus(int $statusCode): void
* Send Header
*
* @param string $key
* @param string $value
* @param string|array<string> $value
* @return void
*/
public function sendHeader(string $key, string $value): void
public function sendHeader(string $key, mixed $value): void
{
$this->swoole->header($key, $value);
}
Expand Down
32 changes: 26 additions & 6 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ abstract class Response
protected bool $sent = false;

/**
* @var array
* @var array<string, string|array<string>>
*/
protected array $headers = [];

Expand Down Expand Up @@ -365,7 +365,15 @@ public function enablePayload(): static
*/
public function addHeader(string $key, string $value): static
{
$this->headers[$key] = $value;
if (\array_key_exists($key, $this->headers)) {
if (\is_array($this->headers[$key])) {
$this->headers[$key][] = $value;
} else {
$this->headers[$key] = [$this->headers[$key], $value];
}
} else {
$this->headers[$key] = $value;
}

return $this;
}
Expand All @@ -391,7 +399,7 @@ public function removeHeader(string $key): static
*
* Return array of all response headers
*
* @return array
* @return array<string, array<string, string>>
*/
public function getHeaders(): array
{
Expand Down Expand Up @@ -483,7 +491,19 @@ public function send(string $body = ''): void
if (!$this->disablePayload) {
$length = strlen($body);

$this->size = $this->size + strlen(implode("\n", $this->headers)) + $length;
$headersSize = 0;
foreach ($this->headers as $name => $values) {
if (\is_array($values)) {
foreach ($values as $value) {
$headersSize += \strlen($name . ': ' . $value);
}
$headersSize += (\count($values) - 1) * 2; // linebreaks
} else {
$headersSize += \strlen($name . ': ' . $values);
}
}
$headersSize += (\count($this->headers) - 1) * 2; // linebreaks
$this->size = $this->size + $headersSize + $length;

if (array_key_exists(
$this->contentType,
Expand Down Expand Up @@ -599,10 +619,10 @@ abstract protected function sendStatus(int $statusCode): void;
* Output Header
*
* @param string $key
* @param string $value
* @param string|array<string> $value
* @return void
*/
abstract public function sendHeader(string $key, string $value): void;
abstract public function sendHeader(string $key, mixed $value): void;

/**
* Send Cookie
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public function testCookie()
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
}

public function testSetCookie()
{
$response = $this->client->call(Client::METHOD_GET, '/set-cookie');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('value1', $response['cookies']['key1']);
$this->assertEquals('value2', $response['cookies']['key2']);
}
}
27 changes: 26 additions & 1 deletion tests/e2e/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,29 @@ public function call(string $method, string $path = '', array $headers = [], arr
$responseType = '';
$responseBody = '';

$cookies = [];

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders, &$cookies) {
$len = strlen($header);
$header = explode(':', $header, 2);

if (count($header) < 2) { // ignore invalid headers
return $len;
}

if (strtolower(trim($header[0])) == 'set-cookie') {
$parsed = $this->parseCookie((string)trim($header[1]));
$name = array_key_first($parsed);
$cookies[$name] = $parsed[$name];
}

$responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);

return $len;
Expand All @@ -99,6 +107,23 @@ public function call(string $method, string $path = '', array $headers = [], arr
return [
'headers' => $responseHeaders,
'body' => $responseBody,
'cookies' => $cookies,
];
}

/**
* Parse Cookie String
*
* @param string $cookie
* @return array
*/
public function parseCookie(string $cookie): array
{
$cookies = [];

parse_str(strtr($cookie, ['&' => '%26', '+' => '%2B', ';' => '&']), $cookies);

return $cookies;
}

}
9 changes: 9 additions & 0 deletions tests/e2e/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
$response->send($request->getHeaders()['cookie'] ?? '');
});

Http::get('/set-cookie')
->inject('request')
->inject('response')
->action(function (Request $request, Response $response) {
$response->addHeader('Set-Cookie', 'key1=value1');
$response->addHeader('Set-Cookie', 'key2=value2');
$response->send('OK');
});

Http::get('/chunked')
->inject('response')
->action(function (Response $response) {
Expand Down