2
namespace GuzzleHttp\Message;
5
* Request and response parser used by Guzzle
10
* Parse an HTTP request message into an associative array of parts.
12
* @param string $message HTTP request to parse
14
* @return array|bool Returns false if the message is invalid
16
public function parseRequest($message)
18
if (!($parts = $this->parseMessage($message))) {
22
// Parse the protocol and protocol version
23
if (isset($parts['start_line'][2])) {
24
$startParts = explode('/', $parts['start_line'][2]);
25
$protocol = strtoupper($startParts[0]);
26
$version = isset($startParts[1]) ? $startParts[1] : '1.1';
33
'method' => strtoupper($parts['start_line'][0]),
34
'protocol' => $protocol,
35
'protocol_version' => $version,
36
'headers' => $parts['headers'],
37
'body' => $parts['body']
40
$parsed['request_url'] = $this->getUrlPartsFromMessage(
41
(isset($parts['start_line'][1]) ? $parts['start_line'][1] : ''), $parsed);
47
* Parse an HTTP response message into an associative array of parts.
49
* @param string $message HTTP response to parse
51
* @return array|bool Returns false if the message is invalid
53
public function parseResponse($message)
55
if (!($parts = $this->parseMessage($message))) {
59
list($protocol, $version) = explode('/', trim($parts['start_line'][0]));
62
'protocol' => $protocol,
63
'protocol_version' => $version,
64
'code' => $parts['start_line'][1],
65
'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
66
'headers' => $parts['headers'],
67
'body' => $parts['body']
72
* Parse a message into parts
74
* @param string $message Message to parse
78
private function parseMessage($message)
88
// Iterate over each line in the message, accounting for line endings
89
$lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
90
for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
94
// If two line breaks were encountered, then this is the end of body
96
if ($i < $totalLines - 1) {
97
$body = implode('', array_slice($lines, $i + 2));
102
// Parse message headers
104
$startLine = explode(' ', $line, 3);
105
} elseif (strpos($line, ':')) {
106
$parts = explode(':', $line, 2);
107
$key = trim($parts[0]);
108
$value = isset($parts[1]) ? trim($parts[1]) : '';
109
if (!isset($headers[$key])) {
110
$headers[$key] = $value;
111
} elseif (!is_array($headers[$key])) {
112
$headers[$key] = [$headers[$key], $value];
114
$headers[$key][] = $value;
120
'start_line' => $startLine,
121
'headers' => $headers,
127
* Create URL parts from HTTP message parts
129
* @param string $requestUrl Associated URL
130
* @param array $parts HTTP message parts
134
private function getUrlPartsFromMessage($requestUrl, array $parts)
136
// Parse the URL information from the message
137
$urlParts = ['path' => $requestUrl, 'scheme' => 'http'];
139
// Check for the Host header
140
if (isset($parts['headers']['Host'])) {
141
$urlParts['host'] = $parts['headers']['Host'];
142
} elseif (isset($parts['headers']['host'])) {
143
$urlParts['host'] = $parts['headers']['host'];
145
$urlParts['host'] = null;
148
if (false === strpos($urlParts['host'], ':')) {
149
$urlParts['port'] = '';
151
$hostParts = explode(':', $urlParts['host']);
152
$urlParts['host'] = trim($hostParts[0]);
153
$urlParts['port'] = (int) trim($hostParts[1]);
154
if ($urlParts['port'] == 443) {
155
$urlParts['scheme'] = 'https';
159
// Check if a query is present
160
$path = $urlParts['path'];
161
$qpos = strpos($path, '?');
163
$urlParts['query'] = substr($path, $qpos + 1);
164
$urlParts['path'] = substr($path, 0, $qpos);
166
$urlParts['query'] = '';