public function __construct(HttpMessage $response = null)
 {
     if ($response && $response->getType() != HTTP_MSG_RESPONSE) {
         throw new Kwf_Exception("invalid response type");
     }
     if ($response && ($response->getResponseCode() == 301 || $response->getResponseCode() == 302)) {
         //workaround for strange pecl_http bug that breaks requests that redirect to the same url again
         //and for some reason then there is only response message containing in the body the second response message (including http headers)
         if (preg_match('#^HTTP/1\\.. [0-9]{3} #', $response->getBody())) {
             $r = HttpMessage::factory($response->getBody());
             if ($r->getType() == HTTP_MSG_RESPONSE) {
                 $response = $r;
             }
         }
     }
     $this->_response = $response;
 }
示例#2
0
 /**
  * Perform the HTTP request.
  *
  * @param      \phpcouch\http\HttpRequest HTTP Request object
  *
  * @return     \phpcouch\http\HttpResponse  The response from the server
  *
  * @author     Simon Thulbourn <*****@*****.**>
  * @since      1.0.0
  */
 public function sendRequest(\phpcouch\http\HttpRequest $request)
 {
     $internalRequest = new \HttpRequest($request->getDestination(), self::$httpMethods[$request->getMethod()]);
     // additional headers
     foreach ($request->getHeaders() as $key => $values) {
         foreach ($values as $value) {
             $this->headers[$key] = $value;
         }
     }
     if (!isset($this->headers['Content-Type'])) {
         $this->headers['Content-Type'] = 'application/json';
     }
     if (null !== ($payload = $request->getContent())) {
         if (is_resource($payload)) {
             // This adapter has no real stream support as of now
             $payload = stream_get_contents($payload, -1, 0);
         }
         if ('PUT' == $request->getMethod()) {
             $internalRequest->setPutData($payload);
         } elseif ('POST' == $request->getMethod()) {
             $internalRequest->setBody($payload);
             $this->headers['Content-Length'] = strlen($payload);
         }
     }
     $internalRequest->addHeaders($this->headers);
     $message = new \HttpMessage($internalRequest->send());
     $response = new HttpResponse();
     $response->setStatusCode($message->getResponseCode());
     if (!isset($response)) {
         throw new TransportException('Could not read HTTP response status line');
     }
     foreach ($message->getHeaders() as $key => $value) {
         $response->setHeader($key, $value);
     }
     $response->setContent($message->getBody());
     if ($message->getResponseCode() >= 400) {
         if ($message->getResponseCode() % 500 < 100) {
             // a 5xx response
             throw new HttpServerErrorException($message->getResponseStatus(), $message->getResponseCode(), $response);
         } else {
             // a 4xx response
             throw new HttpClientErrorException($message->getResponseStatus(), $message->getResponseCode(), $response);
         }
     }
     return $response;
 }
示例#3
0
文件: Peclhttp.php 项目: horde/horde
 /**
  * Constructor.
  *
  * @param string $uri
  * @param HttpMessage $message
  */
 public function __construct($uri, HttpMessage $message)
 {
     try {
         $parent = $message->getParentMessage();
         $location = $parent->getHeader('Location');
         $this->uri = $location;
     } catch (HttpRuntimeException $e) {
         $this->uri = $uri;
     }
     $this->httpVersion = $message->getHttpVersion();
     $this->code = $message->getResponseCode();
     $this->_message = $message;
     $this->_headers = new Horde_Support_CaseInsensitiveArray($message->getHeaders());
     $this->headers = array_change_key_case($this->_headers->getArrayCopy());
 }
示例#4
0
 /**
  * Constructor.
  *
  * @param string $uri
  * @param HttpMessage $message
  */
 public function __construct($uri, HttpMessage $message)
 {
     try {
         $parent = $message->getParentMessage();
         $location = $parent->getHeader('Location');
         $this->uri = $location;
     } catch (HttpRuntimeException $e) {
         $this->uri = $uri;
     }
     $this->httpVersion = $message->getHttpVersion();
     $this->code = $message->getResponseCode();
     $this->_message = $message;
     foreach ($message->getHeaders() as $k => $v) {
         $this->headers[strtolower($k)] = $v;
     }
 }
示例#5
0
 /**
  * {@inheritdoc}
  * @see Scalr\Service\OpenStack\Client.ClientResponseInterface::hasError()
  */
 public function hasError()
 {
     if (!isset($this->errorData)) {
         $this->errorData = false;
         $code = $this->message->getResponseCode();
         if ($code < 200 || $code > 299) {
             $this->errorData = new ErrorData();
             if ($this->format == AppFormat::APP_JSON) {
                 $d = @json_decode($this->getContent());
                 if ($d === null) {
                     $this->errorData->code = $code;
                     $this->errorData->message = strip_tags($this->getContent());
                     $this->errorData->details = '';
                 } else {
                     list(, $v) = each($d);
                     if (is_object($v)) {
                         $this->errorData->code = $v->code;
                         $this->errorData->message = $v->message;
                         $this->errorData->details = isset($v->details) ? (string) $v->details : '';
                     } else {
                         //QuantumError
                         $this->errorData->code = $code;
                         $this->errorData->message = (string) $v;
                         $this->errorData->details = '';
                     }
                 }
             } else {
                 if ($this->format == AppFormat::APP_XML) {
                     $d = simplexml_load_string($this->getContent());
                     $this->errorData->code = $code;
                     $this->errorData->message = isset($d->message) ? (string) $d->message : '';
                     $this->errorData->details = isset($d->details) ? (string) $d->details : '';
                 } else {
                     throw new \InvalidArgumentException(sprintf('Unexpected application format "%s" in class %s', (string) $this->format, get_class($this)));
                 }
             }
             throw new RestClientException($this->errorData);
         }
     }
     return $this->errorData;
 }
 /**
  * {@inheritdoc}
  * @see Scalr\Service\Aws\Client.ClientResponseInterface::getResponseCode()
  */
 public function getResponseCode()
 {
     return $this->message->getResponseCode();
 }
示例#7
0
function response_dump(HttpMessage $res)
{
    printf("Code: %d\n", $res->getResponseCode());
    printf("Body: %s\n", $res->getBody());
}