/** * 解析请求 * @param $request Swoole\Request * @return unknown_type */ function parseRequest($request) { $url_info = parse_url($request->meta['uri']); $request->meta['request_time'] = time(); $request->meta['path'] = $url_info['path']; if (isset($url_info['fragment'])) { $request->meta['fragment'] = $url_info['fragment']; } if (isset($url_info['query'])) { parse_str($url_info['query'], $request->get); } //POST请求,有http body if ($request->meta['method'] === 'POST') { $this->parser->parseBody($request); } //解析Cookies if (!empty($request->head['Cookie'])) { $this->parser->parseCookie($request); } }
function parseHeader($data) { $parts = explode("\r\n\r\n", $data, 2); // parts[0] = HTTP头; // parts[1] = HTTP主体,GET请求没有body $headerLines = explode("\r\n", $parts[0]); // HTTP协议头,方法,路径,协议[RFC-2616 5.1] list($status['method'], $status['uri'], $status['protocol']) = explode(' ', $headerLines[0], 3); //错误的HTTP请求 if (empty($status['method']) or empty($status['uri']) or empty($status['protocol'])) { return false; } unset($headerLines[0]); //解析Header $this->respHeader = \Swoole\Http\Parser::parseHeaderLine($headerLines); $this->status = $status; if (isset($parts[1])) { $this->buffer = $parts[1]; } return true; }
private function parseHeader($data) { $parts = explode("\r\n\r\n", $data, 2); $headerLines = explode("\r\n", $parts[0]); list($this->rspHeaders['method'], $this->rspHeaders['uri'], $this->rspHeaders['protocol']) = explode(' ', $headerLines[0], 3); $this->respHeader = \Swoole\Http\Parser::parseHeaderLine($headerLines); if (isset($parts[1])) { $this->content = $parts[1]; } //print_r($this ->respHeader); \SysLog::info(__METHOD__ . " header == " . print_r($this->respHeader, true), __CLASS__); }
/** * 握手 * @param $headerBuffer * @return bool */ function doHandShake($headerBuffer) { $header = Swoole\Http\Parser::parseHeader($headerBuffer); if (!isset($header['Sec-WebSocket-Accept'])) { $this->disconnect(); return false; } if ($header['Sec-WebSocket-Accept'] != base64_encode(pack('H*', sha1($this->key . self::GUID)))) { $this->disconnect(); return false; } $this->handshake = true; $this->header = $header; return true; }