Skip to content

Commit ae497df

Browse files
committed
Single header line parsing extracted to a separate method in ResponseBuilder.
1 parent a97bd29 commit ae497df

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

src/Builder/ResponseBuilder.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,13 @@ public function getResponse()
4848
*/
4949
public function setHeadersFromArray(array $headers)
5050
{
51-
$statusLine = trim(array_shift($headers));
52-
$parts = explode(' ', $statusLine, 3);
53-
if (count($parts) < 2 || substr(strtolower($parts[0]), 0, 5) !== 'http/') {
54-
throw new \UnexpectedValueException(
55-
sprintf('"%s" is not a valid HTTP status line', $statusLine)
56-
);
57-
}
58-
59-
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
60-
$this->response = $this->response
61-
->withStatus((int) $parts[1], $reasonPhrase)
62-
->withProtocolVersion(substr($parts[0], 5));
63-
6451
foreach ($headers as $headerLine) {
6552
$headerLine = trim($headerLine);
6653
if ('' === $headerLine) {
6754
continue;
6855
}
6956

70-
$parts = explode(':', $headerLine, 2);
71-
if (count($parts) !== 2) {
72-
throw new \UnexpectedValueException(
73-
sprintf('"%s" is not a valid HTTP header line', $headerLine)
74-
);
75-
}
76-
$name = trim(urldecode($parts[0]));
77-
$value = trim(urldecode($parts[1]));
78-
if ($this->response->hasHeader($name)) {
79-
$this->response = $this->response->withAddedHeader($name, $value);
80-
} else {
81-
$this->response = $this->response->withHeader($name, $value);
82-
}
57+
$this->addHeader($headerLine);
8358
}
8459

8560
return $this;
@@ -113,4 +88,47 @@ public function setHeadersFromString($headers)
11388

11489
return $this;
11590
}
91+
92+
/**
93+
* Add header represented by a string.
94+
*
95+
* @param string $headerLine Response header as a string.
96+
*
97+
* @return $this
98+
*
99+
* @throws \UnexpectedValueException For invalid header values.
100+
* @throws \InvalidArgumentException For invalid header names or values.
101+
*/
102+
public function addHeader($headerLine)
103+
{
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));
116+
} 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+
}
130+
}
131+
132+
return $this;
133+
}
116134
}

0 commit comments

Comments
 (0)