/**
  * Receive a response header from curl
  *
  * @param resource $curl   Curl handle
  * @param string   $header Received header
  *
  * @return int
  */
 public function receiveResponseHeader($curl, $header)
 {
     static $normalize = array("\r", "\n");
     $length = strlen($header);
     $header = str_replace($normalize, '', $header);
     if (strpos($header, 'HTTP/') === 0) {
         $startLine = explode(' ', $header, 3);
         $code = $startLine[1];
         $status = isset($startLine[2]) ? $startLine[2] : '';
         // Only download the body of the response to the specified response
         // body when a successful response is received.
         if ($code >= 200 && $code < 300) {
             $body = $this->request->getResponseBody();
         } else {
             $body = EntityBody::factory();
         }
         $response = new Response($code, null, $body);
         $response->setStatus($code, $status);
         $this->request->startResponse($response);
         $this->request->dispatch('request.receive.status_line', array('request' => $this, 'line' => $header, 'status_code' => $code, 'reason_phrase' => $status));
     } elseif ($pos = strpos($header, ':')) {
         $this->request->getResponse()->addHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
     }
     return $length;
 }
Ejemplo n.º 2
0
 public function dispatch($request, $connection)
 {
     $response = new Response(200);
     $path = $request->getPath();
     if ($this->match($path)) {
         echo "Found match for path: {$path}" . PHP_EOL;
         if (is_callable($this->routes[$path])) {
             call_user_func_array($this->routes[$path], array($request, $response, $connection));
         } else {
             throw new \Exception('Invalid route definition.');
         }
         return;
     }
     if (is_null($this->fileHandler)) {
         throw new \Exception('No file handler configured');
     }
     if (false === $this->fileHandler->canHandleRequest($request)) {
         $response->setStatus(404);
         $response->setBody('File not found');
         $connection->send($response);
         $connection->close();
         return;
     }
     $this->fileHandler->handleRequest($request, $response, $connection);
 }
Ejemplo n.º 3
0
 /**
  * @param Guzzle\Http\Message\RequestInterface
  * @return Guzzle\Http\Message\Response
  */
 public function handshake(RequestInterface $request)
 {
     $body = $this->sign($request->getHeader('Sec-WebSocket-Key1', true), $request->getHeader('Sec-WebSocket-Key2', true), (string) $request->getBody());
     $headers = array('Upgrade' => 'WebSocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Origin' => $request->getHeader('Origin', true), 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host', true) . $request->getPath());
     $response = new Response(101, $headers, $body);
     $response->setStatus(101, 'WebSocket Protocol Handshake');
     return $response;
 }
Ejemplo n.º 4
0
 /**
  * @param  \Guzzle\Http\Message\RequestInterface $request
  * @return \Guzzle\Http\Message\Response
  * @throws \UnderflowException If there hasn't been enough data received
  */
 public function handshake(RequestInterface $request)
 {
     $body = substr($request->getBody(), 0, 8);
     if (8 !== strlen($body)) {
         throw new \UnderflowException("Not enough data received to issue challenge response");
     }
     $challenge = $this->sign((string) $request->getHeader('Sec-WebSocket-Key1'), (string) $request->getHeader('Sec-WebSocket-Key2'), $body);
     $headers = array('Upgrade' => 'WebSocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Origin' => (string) $request->getHeader('Origin'), 'Sec-WebSocket-Location' => 'ws://' . (string) $request->getHeader('Host') . $request->getPath());
     $response = new Response(101, $headers, $challenge);
     $response->setStatus(101, 'WebSocket Protocol Handshake');
     return $response;
 }