Skip to content

Commit fea6e47

Browse files
committed
Status line should be the first line/item in setHeadersFromString()/setHeadersFromArray().
1 parent ae497df commit fea6e47

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

src/Builder/ResponseBuilder.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function getResponse()
4848
*/
4949
public function setHeadersFromArray(array $headers)
5050
{
51+
$status = array_shift($headers);
52+
$this->setStatus($status);
53+
5154
foreach ($headers as $headerLine) {
5255
$headerLine = trim($headerLine);
5356
if ('' === $headerLine) {
@@ -89,44 +92,56 @@ public function setHeadersFromString($headers)
8992
return $this;
9093
}
9194

95+
/**
96+
* Set response status from a status string.
97+
*
98+
* @param string $statusLine Response status as a string.
99+
*
100+
* @return $this
101+
*
102+
* @ throws \UnexpectedValueException For invalid header values.
103+
* @throws \InvalidArgumentException For invalid status line.
104+
*/
105+
public function setStatus($statusLine)
106+
{
107+
$parts = explode(' ', $statusLine, 3);
108+
if (count($parts) < 2 || strpos(strtolower($parts[0]), 'http/') !== 0) {
109+
throw new \InvalidArgumentException(
110+
sprintf('"%s" is not a valid HTTP status line', $statusLine)
111+
);
112+
}
113+
114+
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
115+
$this->response = $this->response
116+
->withStatus((int) $parts[1], $reasonPhrase)
117+
->withProtocolVersion(substr($parts[0], 5));
118+
119+
return $this;
120+
}
121+
92122
/**
93123
* Add header represented by a string.
94124
*
95125
* @param string $headerLine Response header as a string.
96126
*
97127
* @return $this
98128
*
99-
* @throws \UnexpectedValueException For invalid header values.
100129
* @throws \InvalidArgumentException For invalid header names or values.
101130
*/
102131
public function addHeader($headerLine)
103132
{
104-
if (strpos(strtolower($headerLine), 'http/') === 0) {
105-
$parts = explode(' ', $headerLine, 3);
106-
if (count($parts) < 2) {
107-
throw new \UnexpectedValueException(
108-
sprintf('"%s" is not a valid HTTP status line', $headerLine)
109-
);
110-
}
111-
112-
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
113-
$this->response = $this->response
114-
->withStatus((int) $parts[1], $reasonPhrase)
115-
->withProtocolVersion(substr($parts[0], 5));
133+
$parts = explode(':', $headerLine, 2);
134+
if (count($parts) !== 2) {
135+
throw new \InvalidArgumentException(
136+
sprintf('"%s" is not a valid HTTP header line', $headerLine)
137+
);
138+
}
139+
$name = trim(urldecode($parts[0]));
140+
$value = trim(urldecode($parts[1]));
141+
if ($this->response->hasHeader($name)) {
142+
$this->response = $this->response->withAddedHeader($name, $value);
116143
} else {
117-
$parts = explode(':', $headerLine, 2);
118-
if (count($parts) !== 2) {
119-
throw new \UnexpectedValueException(
120-
sprintf('"%s" is not a valid HTTP header line', $headerLine)
121-
);
122-
}
123-
$name = trim(urldecode($parts[0]));
124-
$value = trim(urldecode($parts[1]));
125-
if ($this->response->hasHeader($name)) {
126-
$this->response = $this->response->withAddedHeader($name, $value);
127-
} else {
128-
$this->response = $this->response->withHeader($name, $value);
129-
}
144+
$this->response = $this->response->withHeader($name, $value);
130145
}
131146

132147
return $this;

0 commit comments

Comments
 (0)