diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index b06b947..4af38ab 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -1,6 +1,5 @@ getHeaders($message); + $headers = HTTP::getHeaders($message); if (isset($headers['Sec-Websocket-Version'])) { // HyBi if ($headers['Sec-Websocket-Version'] == '8') { @@ -170,38 +170,4 @@ class WebSocket implements ProtocolInterface { return $this->_version[$version]; } - - /** - * @param string - * @return array - * @todo Consider strtolower all the header keys...right now PHP Changes Sec-WebSocket-X to Sec-Websocket-X...this could change - */ - protected function getHeaders($http_message) { - return function_exists('http_parse_headers') ? http_parse_headers($http_message) : $this->http_parse_headers($http_message); - } - - /** - * This is a fallback method for http_parse_headers as not all php installs have the HTTP module present - * @internal - */ - protected function http_parse_headers($http_message) { - $retVal = array(); - $fields = explode("br", preg_replace("%(<|/\>|>)%", "", nl2br($http_message))); - - foreach ($fields as $field) { - if (preg_match('%^(GET|POST|PUT|DELETE|PATCH)(\s)(.*)%', $field, $matchReq)) { - $retVal["Request Method"] = $matchReq[1]; - $retVal["Request Url"] = $matchReq[3]; - } elseif (preg_match('/([^:]+): (.+)/m', $field, $match) ) { - $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1]))); - if (isset($retVal[$match[1]])) { - $retVal[$match[1]] = array($retVal[$match[1]], $match[2]); - } else { - $retVal[$match[1]] = trim($match[2]); - } - } - } - - return $retVal; - } } \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket/Util/HTTP.php b/lib/Ratchet/Protocol/WebSocket/Util/HTTP.php new file mode 100644 index 0000000..8e52f61 --- /dev/null +++ b/lib/Ratchet/Protocol/WebSocket/Util/HTTP.php @@ -0,0 +1,39 @@ +|>)%", "", nl2br($http_message))); + + foreach ($fields as $field) { + if (preg_match('%^(GET|POST|PUT|DELETE|PATCH)(\s)(.*)%', $field, $matchReq)) { + $retVal["Request Method"] = $matchReq[1]; + $retVal["Request Url"] = $matchReq[3]; + } elseif (preg_match('/([^:]+): (.+)/m', $field, $match) ) { + $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1]))); + if (isset($retVal[$match[1]])) { + $retVal[$match[1]] = array($retVal[$match[1]], $match[2]); + } else { + $retVal[$match[1]] = trim($match[2]); + } + } + } + + return $retVal; + } +} \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php index c677574..37f9621 100644 --- a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php +++ b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php @@ -1,5 +1,6 @@ sign($headers['Sec-Websocket-Key']); return array(