public function __construct($title, $code, array $errorList, $previous)
 {
     if ($previous instanceof \PDOException) {
         //gets additional information about the database error
         $details = ['state' => $previous->errorInfo[0], 'code' => $previous->errorInfo[1], 'msg' => $previous->errorInfo[2]];
         //retrieves useful information from the error message
         switch ($details['state']) {
             case self::STATE_INTEGRITY_CONSTRAINT_VIOLATION:
                 switch ($details['code']) {
                     case self::CODE_DUPLICATE_UNIQUE_KEY:
                         if (preg_match('|entry \'([\\w\\d-]*)\' for key \'([\\w\\d_]*)\'|', $details['msg'], $pieces)) {
                             $details['entry'] = explode('-', $pieces[1]);
                             $details['key'] = $pieces[2];
                         }
                         break;
                 }
                 break;
         }
         if (isset($errorList['dev'])) {
             $errorList['dev'] = array_merge($details, ['more' => $errorList['dev']]);
         } else {
             $errorList['dev'] = $details;
         }
         $this->details = $details;
     }
     parent::__construct($title, $code, $errorList, $previous);
 }
 /**
  * Constructor
  *
  * @param string $url The not found URL
  * @param array $details The exception details
  */
 public function __construct($url = '', $details = array())
 {
     if (!$url) {
         $url = App::request()->getFullUrl();
     }
     $details['url'] = $url;
     $message = Lang::get('main.http-error-404-message', $details);
     parent::__construct($message, $details);
 }
Exemple #3
0
 /**
  * Check for server connection
  *
  * Checks if the connection already has been established, or tries to
  * establish the connection, if not done yet.
  *
  * @return void
  */
 protected function checkConnection()
 {
     // If the connection could not be established, fsockopen sadly does not
     // only return false (as documented), but also always issues a warning.
     if ($this->connection === null && ($this->connection = @fsockopen($this->options['ip'], $this->options['port'], $errno, $errstr)) === false) {
         // This is a bit hackisch...
         $this->connection = null;
         throw HTTPException::connectionFailure($this->options['ip'], $this->options['port'], $errstr, $errno);
     }
 }
Exemple #4
0
 /**
  * Perform a request to the server and return the result
  *
  * Perform a request to the server and return the result converted into a
  * Response object. If you do not expect a JSON structure, which
  * could be converted in such a response object, set the forth parameter to
  * true, and you get a response object retuerned, containing the raw body.
  *
  * @param string $method
  * @param string $path
  * @param string $data
  * @param bool $raw
  * @return Response
  * @throws HTTPException
  */
 public function request($method, $path, $data = null, $raw = false)
 {
     $basicAuth = '';
     if ($this->options['username']) {
         $basicAuth .= "{$this->options['username']}:{$this->options['password']}@";
     }
     // TODO SSL support?
     $httpFilePointer = @fopen('http://' . $basicAuth . $this->options['host'] . ':' . $this->options['port'] . $path, 'r', false, stream_context_create(array('http' => array('method' => $method, 'content' => $data, 'ignore_errors' => true, 'max_redirects' => 0, 'user_agent' => 'Doctrine CouchDB ODM $Revision$', 'timeout' => $this->options['timeout'], 'header' => 'Content-type: application/json'))));
     // Check if connection has been established successfully
     if ($httpFilePointer === false) {
         $error = error_get_last();
         throw HTTPException::connectionFailure($this->options['ip'], $this->options['port'], $error['message'], 0);
     }
     // Read request body
     $body = '';
     while (!feof($httpFilePointer)) {
         $body .= fgets($httpFilePointer);
     }
     $metaData = stream_get_meta_data($httpFilePointer);
     // The structure of this array differs depending on PHP compiled with
     // --enable-curlwrappers or not. Both cases are normally required.
     $rawHeaders = isset($metaData['wrapper_data']['headers']) ? $metaData['wrapper_data']['headers'] : $metaData['wrapper_data'];
     $headers = array();
     foreach ($rawHeaders as $lineContent) {
         // Extract header values
         if (preg_match('(^HTTP/(?P<version>\\d+\\.\\d+)\\s+(?P<status>\\d+))S', $lineContent, $match)) {
             $headers['version'] = $match['version'];
             $headers['status'] = (int) $match['status'];
         } else {
             list($key, $value) = explode(':', $lineContent, 2);
             $headers[strtolower($key)] = ltrim($value);
         }
     }
     if (empty($headers['status'])) {
         throw HTTPException::readFailure($this->options['ip'], $this->options['port'], 'Received an empty response or not status code', 0);
     }
     // Create repsonse object from couch db response
     if ($headers['status'] >= 400) {
         return new ErrorResponse($headers['status'], $headers, $body);
     }
     return new Response($headers['status'], $headers, $body, $raw);
 }
 /**
  * Check for server connection
  *
  * Checks if the connection already has been established, or tries to
  * establish the connection, if not done yet.
  *
  * @return void
  * @throws HTTPException
  */
 protected function checkConnection()
 {
     // Setting Connection scheme according ssl support
     if ($this->options['ssl']) {
         if (!extension_loaded('openssl')) {
             // no openssl extension loaded.
             // This is a bit hackisch...
             $this->connection = null;
             throw HTTPException::connectionFailure($this->options['ip'], $this->options['port'], "ssl activated without openssl extension loaded", 0);
         }
         $host = 'ssl://' . $this->options['host'];
     } else {
         $host = $this->options['ip'];
     }
     // If the connection could not be established, fsockopen sadly does not
     // only return false (as documented), but also always issues a warning.
     if ($this->connection === null && ($this->connection = @fsockopen($host, $this->options['port'], $errno, $errstr)) === false) {
         // This is a bit hackisch...
         $this->connection = null;
         throw HTTPException::connectionFailure($this->options['ip'], $this->options['port'], $errstr, $errno);
     }
 }
 public function __construct($message = 'Client sent too many requests.')
 {
     parent::__construct("Too Many Requests", 429, $message);
 }
 public function __construct($message = 'Internal server error for unknown reasons.')
 {
     parent::__construct("Internal Server Error", 500, $message);
 }
 public function __construct($message = 'Resource not found.')
 {
     parent::__construct("Not Found", 404, $message);
 }
Exemple #9
0
 public function __construct($url)
 {
     $msg = 'The URL ' . htmlize($url) . ' caused too many redirections.';
     parent::__construct($msg);
 }
 public function __construct($message = 'Unauthorized resource or insufficient permission level.')
 {
     parent::__construct("Unauthorized", 401, $message);
 }
 public function __construct($message = 'Unexpected Content-Type header.')
 {
     parent::__construct("Unsupported Media Type", 415, $message);
 }
 public function __construct($message = 'Bandwidth limit exceeded.')
 {
     parent::__construct("Bandwidth Limit Exceeded", 509, $message);
 }
 public function __construct($message = 'The client took too long to respond.')
 {
     parent::__construct("Request Timeout", 408, $message);
 }
Exemple #14
0
 public function __construct($message, $error_code)
 {
     parent::__construct(500, $message, $error_code);
 }
 public function __construct($message = 'Insufficient storage.')
 {
     parent::__construct("Insufficient Storage", 507, $message);
 }
 public function __construct($message = 'Unexpected route, invalid value or missing parameter in the request.')
 {
     parent::__construct("Bad Request", 400, $message);
 }
Exemple #17
0
 /**
  * @return string
  */
 public function normalString()
 {
     return parent::__toString();
 }
 /**
  * Constructor
  *
  * @param string $message Error message
  * @param int $code Error code
  */
 public function __construct($message, $code)
 {
     parent::__construct($message, $code);
     $this->message = $message;
     $this->code = $code;
 }
 /**
  * Perform a request to the server and return the result
  *
  * Perform a request to the server and return the result converted into a
  * Response object. If you do not expect a JSON structure, which
  * could be converted in such a response object, set the forth parameter to
  * true, and you get a response object returned, containing the raw body.
  *
  * @param string $method
  * @param string $path
  * @param string $data
  * @param bool $raw
  * @param array $headers
  * @return Response
  * @throws HTTPException
  */
 public function request($method, $path, $data = null, $raw = false, array $headers = array())
 {
     $fullPath = $path;
     if ($this->options['path']) {
         $fullPath = '/' . $this->options['path'] . $path;
     }
     $this->checkConnection($method, $fullPath, $data, $headers);
     // Read request body.
     $body = '';
     while (!feof($this->httpFilePointer)) {
         $body .= fgets($this->httpFilePointer);
     }
     $headers = $this->getStreamHeaders();
     if (empty($headers['status'])) {
         throw HTTPException::readFailure($this->options['ip'], $this->options['port'], 'Received an empty response or not status code', 0);
     }
     // Create response object from couch db response.
     if ($headers['status'] >= 400) {
         return new ErrorResponse($headers['status'], $headers, $body);
     }
     return new Response($headers['status'], $headers, $body, $raw);
 }
 function __construct($message, $code = null, $previous = null)
 {
     parent::__construct(new APIResponse([status_code => 501, body => $message]), $code, $previous);
 }
 /**
  * Reads multipart data from sourceConnection and streams it to the
  * targetConnection.Returns the body of the request or the status code in
  * case there is no body.
  *
  * @param $method
  * @param $path
  * @param $streamEnd
  * @param array $requestHeaders
  * @return mixed|string
  * @throws \Exception
  * @throws \HTTPException
  */
 protected function sendStream($method, $path, $streamEnd, $requestHeaders = array())
 {
     $dataStream = $this->sourceConnection;
     // Read the json doc. Use _attachments field to find the total
     // Content-Length and create the request header with initial doc data.
     // At present CouchDB can't handle chunked data and needs
     // Content-Length header.
     $str = '';
     $jsonFlag = 0;
     $attachmentCount = 0;
     $totalAttachmentLength = 0;
     $streamLine = $this->getNextLineFromSourceConnection();
     while ($jsonFlag == 0 || $jsonFlag == 1 && trim($streamLine) == '') {
         $str .= $streamLine;
         if (strpos($streamLine, 'Content-Type: application/json') !== false) {
             $jsonFlag = 1;
         }
         $streamLine = $this->getNextLineFromSourceConnection();
     }
     $docBoundaryLength = strlen(explode('=', $requestHeaders['Content-Type'], 2)[1]) + 2;
     $json = json_decode($streamLine, true);
     foreach ($json['_attachments'] as $docName => $metaData) {
         // Quotes and a "/r/n"
         $totalAttachmentLength += strlen('Content-Disposition: attachment; filename=') + strlen($docName) + 4;
         $totalAttachmentLength += strlen('Content-Type: ') + strlen($metaData['content_type']) + 2;
         $totalAttachmentLength += strlen('Content-Length: ');
         if (isset($metaData['encoding'])) {
             $totalAttachmentLength += $metaData['encoded_length'] + strlen($metaData['encoded_length']) + 2;
             $totalAttachmentLength += strlen('Content-Encoding: ') + strlen($metaData['encoding']) + 2;
         } else {
             $totalAttachmentLength += $metaData['length'] + strlen($metaData['length']) + 2;
         }
         $totalAttachmentLength += 2;
         $attachmentCount++;
     }
     // Add Content-Length to the headers.
     $requestHeaders['Content-Length'] = strlen($str) + strlen($streamLine) + $totalAttachmentLength + $attachmentCount * (2 + $docBoundaryLength) + $docBoundaryLength + 2;
     if ($this->targetConnection == null) {
         $this->targetConnection = $this->targetClient->getConnection($method, $path, null, $requestHeaders);
     }
     // Write the initial body data.
     fwrite($this->targetConnection, $str);
     // Write the rest of the data including attachments line by line or in
     // chunks.
     while (!feof($dataStream) && ($streamEnd === null || strpos($streamLine, $streamEnd) === false)) {
         $totalSent = 0;
         $length = strlen($streamLine);
         while ($totalSent != $length) {
             $sent = fwrite($this->targetConnection, substr($streamLine, $totalSent));
             if ($sent === false) {
                 throw new \HTTPException('Stream write error.');
             } else {
                 $totalSent += $sent;
             }
         }
         // Use maxLength while reading the data as there may be no newlines
         // in the binary and compressed attachments, or the lines may be
         // very long.
         $streamLine = $this->getNextLineFromSourceConnection(100000);
     }
     // Read response headers
     $rawHeaders = '';
     $headers = array('connection' => $this->targetClient->getOptions()['keep-alive'] ? 'Keep-Alive' : 'Close');
     // Remove leading newlines, should not occur at all, actually.
     while (($line = fgets($this->targetConnection)) !== false && ($lineContent = rtrim($line)) === '') {
     }
     // Throw exception, if connection has been aborted by the server, and
     // leave handling to the user for now.
     if ($line === false) {
         // sendStream can't be called in recursion as the source stream can be
         // read only once.
         $error = error_get_last();
         throw HTTPException::connectionFailure($this->targetClient->getOptions()['ip'], $this->targetClient->getOptions()['port'], $error['message'], 0);
     }
     do {
         // Also store raw headers for later logging
         $rawHeaders .= $lineContent . "\n";
         // Extract header values
         if (preg_match('(^HTTP/(?P<version>\\d+\\.\\d+)\\s+(?P<status>\\d+))S', $lineContent, $match)) {
             $headers['version'] = $match['version'];
             $headers['status'] = (int) $match['status'];
         } else {
             list($key, $value) = explode(':', $lineContent, 2);
             $headers[strtolower($key)] = ltrim($value);
         }
     } while (($line = fgets($this->targetConnection)) !== false && ($lineContent = rtrim($line)) !== '');
     // Read response body
     $body = '';
     // HTTP 1.1 supports chunked transfer encoding, if the according
     // header is not set, just read the specified amount of bytes.
     $bytesToRead = (int) (isset($headers['content-length']) ? $headers['content-length'] : 0);
     // Read body only as specified by chunk sizes, everything else
     // are just footnotes, which are not relevant for us.
     while ($bytesToRead > 0) {
         $body .= $read = fgets($this->targetConnection, $bytesToRead + 1);
         $bytesToRead -= strlen($read);
     }
     // Reset the connection if the server asks for it.
     if ($headers['connection'] !== 'Keep-Alive') {
         fclose($this->targetConnection);
         $this->targetConnection = null;
     }
     // Handle some response state as special cases
     switch ($headers['status']) {
         case 301:
         case 302:
         case 303:
         case 307:
             // Temporary redirect.
             // sendStream can't be called in recursion as the source stream can be
             // read only once.
             throw HTTPException::fromResponse($path, new Response($headers['status'], $headers, $body));
     }
     return $body != '' ? json_decode($body, true) : array("status" => $headers['status']);
 }
 public function __construct($message = NULL)
 {
     parent::__construct($message, 404);
 }
 public function __construct($message = 'Resource conflict. Could be server side or client side.')
 {
     parent::__construct("Conflict", 409, $message);
 }
Exemple #24
0
 public function __construct($code, $message, $error_code = null)
 {
     parent::__construct($code, $message, $error_code);
 }
 public function __construct($message = 'Service unavailable. Might be due to traffic load.')
 {
     parent::__construct("Service Unavailable", 503, $message);
 }
 public function __construct($message = 'Resource is permanently gone.')
 {
     parent::__construct("Gone", 410, $message);
 }
Exemple #27
0
 public function __construct($message)
 {
     parent::__construct(404, $message, null);
 }
 public function __construct($message = 'The used method (HTTP verb) is not allowed for this resource.')
 {
     parent::__construct("Method Not Allowed", 405, $message);
 }
 public function __construct($message = "I’m a teapot", $code = 418, Exception $previous = null)
 {
     parent::__construct($message, $code, $previous);
 }
Exemple #30
0
 public function __construct($path, $method)
 {
     $this->_path = $path;
     $this->_method = $method;
     parent::__construct($path, $method, "404");
 }