diff --git a/src/main/php/xp/web/HttpProtocol.class.php b/src/main/php/xp/web/HttpProtocol.class.php deleted file mode 100755 index 78ec2989..00000000 --- a/src/main/php/xp/web/HttpProtocol.class.php +++ /dev/null @@ -1,147 +0,0 @@ -application= $application; - $this->logging= $logging; - } - - /** - * Sends an error - * - * @param web.Request $response - * @param web.Response $response - * @param web.Error $error - * @return void - */ - private function sendError($request, $response, $error) { - if ($response->flushed()) { - $error->printStackTrace(); - } else { - $loader= ClassLoader::getDefault(); - $message= Status::message($error->status()); - - $response->answer($error->status(), $message); - foreach (['web/error-'.$this->application->environment()->profile().'.html', 'web/error.html'] as $variant) { - if (!$loader->providesResource($variant)) continue; - $response->send(sprintf( - $loader->getResource($variant), - $error->status(), - htmlspecialchars($message), - htmlspecialchars($error->getMessage()), - htmlspecialchars($error->toString()) - )); - break; - } - } - $this->logging->__invoke($request, $response, $error->toString()); - } - - /** - * Initialize Protocol - * - * @return bool - */ - public function initialize() { - $this->close= (bool)getenv('NO_KEEPALIVE'); - return true; - } - - /** - * Handle client connect - * - * @param peer.Socket $socket - */ - public function handleConnect($socket) { - // Intentionally empty - } - - /** - * Handle client disconnect - * - * @param peer.Socket $socket - */ - public function handleDisconnect($socket) { - $socket->close(); - } - - /** - * Handle client data - * - * @param peer.Socket $socket - * @return void - */ - public function handleData($socket) { - gc_enable(); - $input= new Input($socket); - - // Ignore malformed requests - if (null === $input->method()) { - $socket->close(); - return; - } - - $request= new Request($input); - $response= new Response(new Output($socket)); - $response->header('Date', gmdate('D, d M Y H:i:s T')); - $response->header('Host', $request->header('Host')); - - // Honour a "Connection: close" by the client - if ($this->close || 0 === strncasecmp('close', $request->header('Connection'), 5)) { - $response->header('Connection', 'close'); - $close= true; - } else { - $close= false; - } - - try { - $this->application->service($request, $response); - $this->logging->__invoke($request, $response); - } catch (Error $e) { - $this->sendError($request, $response, $e); - } catch (\Throwable $e) { // PHP7 - $this->sendError($request, $response, new InternalServerError($e)); - } catch (\Exception $e) { // PHP5 - $this->sendError($request, $response, new InternalServerError($e)); - } finally { - $response->end(); - $close ? $socket->close() : $request->consume(); - - gc_collect_cycles(); - gc_disable(); - clearstatcache(); - \xp::gc(); - } - } - - /** - * Handle I/O error - * - * @param peer.Socket $socket - * @param lang.XPException $e - */ - public function handleError($socket, $e) { - // $e->printStackTrace(); - $socket->close(); - } -} diff --git a/src/main/php/xp/web/Input.class.php b/src/main/php/xp/web/Input.class.php deleted file mode 100755 index da772ae4..00000000 --- a/src/main/php/xp/web/Input.class.php +++ /dev/null @@ -1,87 +0,0 @@ -socket= $socket; - $this->buffer= ''; - - if (null === ($message= $this->readLine())) return; - sscanf($message, '%s %s HTTP/%d.%d', $this->method, $this->uri, $major, $minor); - } - - /** @return string */ - public function readLine() { - if (null === $this->buffer) return null; // EOF - - while (false === ($p= strpos($this->buffer, "\r\n"))) { - $chunk= $this->socket->readBinary(); - if ('' === $chunk) { - $return= $this->buffer; - $this->buffer= null; - return $return; - } - $this->buffer.= $chunk; - } - - $return= substr($this->buffer, 0, $p); - $this->buffer= substr($this->buffer, $p + 2); - return $return; - } - - /** @return string */ - public function scheme() { return $this->socket instanceof CryptoSocket ? 'https' : 'http'; } - - /** @return string */ - public function method() { return $this->method; } - - /** @return sring */ - public function uri() { return $this->uri; } - - /** @return iterable */ - public function headers() { - yield 'Remote-Addr' => $this->socket->remoteEndpoint()->getHost(); - while ($line= $this->readLine()) { - sscanf($line, "%[^:]: %[^\r]", $name, $value); - yield $name => $value; - } - } - - /** - * Reads a given number of bytes - * - * @param int $length Pass -1 to read all - * @return string - */ - public function read($length= -1) { - if (-1 === $length) { - $data= $this->buffer; - while (!$this->socket->eof()) { - $data.= $this->socket->readBinary(); - } - $this->buffer= null; - } else if (strlen($this->buffer) >= $length) { - $data= substr($this->buffer, 0, $length); - $this->buffer= substr($this->buffer, $length); - } else { - $data= $this->buffer; - $eof= false; - while (strlen($data) < $length) { - $data.= $this->socket->readBinary($length - strlen($data)); - if ($eof= $this->socket->eof()) break; - } - $this->buffer= $eof ? null : ''; - } - return $data; - } -} \ No newline at end of file diff --git a/src/main/php/xp/web/Output.class.php b/src/main/php/xp/web/Output.class.php deleted file mode 100755 index 712e0856..00000000 --- a/src/main/php/xp/web/Output.class.php +++ /dev/null @@ -1,23 +0,0 @@ -socket= $socket; - } - - public function begin($status, $message, $headers) { - $this->socket->write(sprintf("HTTP/1.1 %d %s\r\n", $status, $message)); - foreach ($headers as $name => $header) { - foreach ($header as $value) { - $this->socket->write($name.': '.$value."\r\n"); - } - } - $this->socket->write("\r\n"); - } - - public function write($bytes) { - $this->socket->write($bytes); - } -} \ No newline at end of file