Пример #1
0
 /**
  * Initializes the curl resource
  */
 protected function prepareCurl()
 {
     $this->curl = curl_init();
     $this->complete = Complete::factory(function () {
         if ($this->curl) {
             curl_close($this->curl);
         }
         $this->curl = null;
         $this->complete = null;
     });
     foreach (self::$curlOptions as $option => $value) {
         curl_setopt($this->curl, $option, $value);
     }
     if ($this->method === HttpMethods::POST) {
         curl_setopt($this->curl, CURLOPT_POST, true);
     }
     if (!empty($this->auth)) {
         curl_setopt($this->curl, CURLOPT_USERPWD, sprintf("%s:%s", $this->auth["user"], $this->auth["pass"]));
     }
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 public function start($single = true)
 {
     $this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
     if (!$this->socket) {
         $err = socket_last_error();
         throw new Exceptions\SocketException(socket_strerror($err), $err);
     }
     /** @noinspection PhpUnusedLocalVariableInspection */
     $complete = Complete::factory(function () {
         if ($this->socket) {
             socket_close($this->socket);
         }
     });
     if (!socket_bind($this->socket, $this->host, $this->port) || !socket_listen($this->socket)) {
         $err = socket_last_error();
         throw new Exceptions\SocketException(socket_strerror($err), $err);
     }
     while (true) {
         $this->client = socket_accept($this->socket);
         if (!$this->client) {
             $err = socket_last_error();
             throw new Exceptions\SocketException(socket_strerror($err), $err);
         }
         $input = socket_read($this->client, 2045);
         if (false === $input) {
             $err = socket_last_error();
             throw new Exceptions\SocketException(socket_strerror($err), $err);
         }
         $headersSent = false;
         $parser = $this->getHttpParser();
         $request = $parser->parse($input);
         $response = "";
         try {
             if ($this->callback) {
                 $response = call_user_func($this->callback, $request);
             }
             if (false === $response || null === $response) {
                 $response = $this->getFile($request);
             }
         } catch (Exceptions\HttpStatusError $e) {
             $this->sendResponseHeadersToClient($request, $e->getCode(), $e->getMessage());
             $headersSent = true;
         }
         if (!$headersSent) {
             $this->sendResponseHeadersToClient($request, 200, "OK");
             $this->sendToClient($response);
         }
         if ($single) {
             break;
         }
     }
 }