3
namespace GuzzleHttp\Message;
6
* Request and response parser used by Guzzle
11
* Parse an HTTP request message into an associative array of parts.
13
* @param string $message HTTP request to parse
15
* @return array|bool Returns false if the message is invalid
17
public function parseRequest($message)
19
if (!($parts = $this->parseMessage($message))) {
23
// Parse the protocol and protocol version
24
if (isset($parts['start_line'][2])) {
25
$startParts = explode('/', $parts['start_line'][2]);
26
$protocol = strtoupper($startParts[0]);
27
$version = isset($startParts[1]) ? $startParts[1] : '1.1';
34
'method' => strtoupper($parts['start_line'][0]),
35
'protocol' => $protocol,
36
'protocol_version' => $version,
37
'headers' => $parts['headers'],
38
'body' => $parts['body']
41
$parsed['request_url'] = $this->getUrlPartsFromMessage(
42
(isset($parts['start_line'][1]) ? $parts['start_line'][1] : ''), $parsed);
48
* Parse an HTTP response message into an associative array of parts.
50
* @param string $message HTTP response to parse
52
* @return array|bool Returns false if the message is invalid
54
public function parseResponse($message)
56
if (!($parts = $this->parseMessage($message))) {
60
list($protocol, $version) = explode('/', trim($parts['start_line'][0]));
63
'protocol' => $protocol,
64
'protocol_version' => $version,
65
'code' => $parts['start_line'][1],
66
'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
67
'headers' => $parts['headers'],
68
'body' => $parts['body']
73
* Parse a message into parts
75
* @param string $message Message to parse
79
private function parseMessage($message)
89
// Iterate over each line in the message, accounting for line endings
90
$lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
91
for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
95
// If two line breaks were encountered, then this is the end of body
97
if ($i < $totalLines - 1) {
98
$body = implode('', array_slice($lines, $i + 2));
103
// Parse message headers
105
$startLine = explode(' ', $line, 3);
106
} elseif (strpos($line, ':')) {
107
$parts = explode(':', $line, 2);
108
$key = trim($parts[0]);
109
$value = isset($parts[1]) ? trim($parts[1]) : '';
110
if (!isset($headers[$key])) {
111
$headers[$key] = $value;
112
} elseif (!is_array($headers[$key])) {
113
$headers[$key] = [$headers[$key], $value];
115
$headers[$key][] = $value;
121
'start_line' => $startLine,
122
'headers' => $headers,
128
* Create URL parts from HTTP message parts
130
* @param string $requestUrl Associated URL
131
* @param array $parts HTTP message parts
135
private function getUrlPartsFromMessage($requestUrl, array $parts)
137
// Parse the URL information from the message
138
$urlParts = ['path' => $requestUrl, 'scheme' => 'http'];
140
// Check for the Host header
141
if (isset($parts['headers']['Host'])) {
142
$urlParts['host'] = $parts['headers']['Host'];
143
} elseif (isset($parts['headers']['host'])) {
144
$urlParts['host'] = $parts['headers']['host'];
146
$urlParts['host'] = null;
149
if (false === strpos($urlParts['host'], ':')) {
150
$urlParts['port'] = '';
152
$hostParts = explode(':', $urlParts['host']);
153
$urlParts['host'] = trim($hostParts[0]);
154
$urlParts['port'] = (int) trim($hostParts[1]);
155
if ($urlParts['port'] == 443) {
156
$urlParts['scheme'] = 'https';
160
// Check if a query is present
161
$path = $urlParts['path'];
162
$qpos = strpos($path, '?');
164
$urlParts['query'] = substr($path, $qpos + 1);
165
$urlParts['path'] = substr($path, 0, $qpos);
167
$urlParts['query'] = '';