示例#1
0
 /**
  * @param StreamSocket $socket
  *
  * @return \Generator
  */
 public function handleClient(StreamSocket $socket)
 {
     $this->prepareSocket($socket);
     $parser = new RequestParser();
     $header = $body = '';
     while (false === strpos($header, "\r\n\r\n")) {
         $header .= (yield $socket->read(1));
     }
     $request = $parser->parseHeader($header);
     $serverAddress = explode(':', $socket->getLocalName());
     $request->setServerPort(array_pop($serverAddress));
     $request->setServerName(implode('', $serverAddress));
     $remoteAddress = explode(':', $socket->getRemoteName());
     $request->setRemotePort(array_pop($remoteAddress));
     $request->setRemoteAddr(implode('', $remoteAddress));
     if ($request->getMethod() == 'POST') {
         if ($length = $request->getHeader('Content-Length')) {
             while (strlen($body) < $length) {
                 $body .= (yield $socket->read(4096));
             }
         } else {
             while ('' != ($part = (yield $socket->read(4096)))) {
                 $body .= $part;
             }
         }
     }
     $request->setBody($body);
     $response = $this->handleRequest($request, new ProtectedStreamSocket($socket));
     // Connection=keep-alive is not supported at this time
     $response->setHeader('Connection', 'close');
     $length = strlen($response);
     $bytesTransferred = 0;
     while ($bytesTransferred < $length) {
         $bytes = (yield @$socket->write(substr($response, $bytesTransferred, 4096)));
         if (false == $bytes) {
             break;
         }
         $bytesTransferred += $bytes;
     }
     (yield $socket->close());
 }
示例#2
0
 public function testParser()
 {
     $parser = new RequestParser();
     $request = $parser->parseHeader("GET /foo/bar?abc=5#1234 HTTP/1.1\n" . "Connection: keep-alive\n" . "Host: localhost:8000\n" . "\n");
 }