Ejemplo n.º 1
0
 /**
  * Method to test getStatusCode().
  *
  * @return void
  *
  * @covers \Windwalker\Http\Response::getStatusCode
  * @covers \Windwalker\Http\Response::withStatus
  */
 public function testWithAndGetStatusCode()
 {
     $this->assertEquals(200, $this->instance->getStatusCode());
     $res = $this->instance->withStatus(403);
     $this->assertNotSame($res, $this->instance);
     $this->assertEquals(403, $res->getStatusCode());
     $res = $res->withStatus(500, 'Unknown error');
     $this->assertEquals(500, $res->getStatusCode());
     $this->assertEquals('Unknown error', $res->getReasonPhrase());
 }
Ejemplo n.º 2
0
 /**
  * header
  *
  * @param string  $string
  * @param bool    $replace
  * @param integer $code
  *
  * @return  static
  */
 public function header($string, $replace = true, $code = null)
 {
     if (strpos($string, ':') !== false) {
         list($header, $value) = explode(': ', $string, 2);
         if ($replace) {
             $this->message = $this->message->withHeader($header, $value);
         } else {
             $this->message = $this->message->withAddedHeader($header, $value);
         }
     } elseif (strpos($string, 'HTTP') === 0) {
         $this->status = $string;
     } else {
         $this->others[] = $string;
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * Constructor.
  *
  * @param  string|UriInterface  $uri      The redirect uri.
  * @param  int                  $status   The status code.
  * @param  array                $headers  The custom headers.
  */
 public function __construct($uri, $status = 303, array $headers = array())
 {
     if ($uri instanceof UriInterface || $uri instanceof \Windwalker\Uri\UriInterface) {
         $uri = (string) $uri;
     }
     if (!is_string($uri)) {
         throw new \InvalidArgumentException(sprintf('Invalid URI type, string or UriInterface required, %s provided.', gettype($uri)));
     }
     $headers['location'] = array($uri);
     parent::__construct(new Stream('php://temp', 'r'), $status, $headers);
 }
Ejemplo n.º 4
0
 /**
  * Constructor.
  *
  * @param  int     $status   The status code.
  * @param  array   $headers  The custom headers.
  */
 public function __construct($status = 204, array $headers = array())
 {
     $body = new Stream('php://memory', Stream::MODE_READ_ONLY_FROM_BEGIN);
     parent::__construct($body, $status, $headers);
 }
 /**
  * Constructor.
  *
  * @param  string  $body     The body data.
  * @param  int     $status   The status code.
  * @param  array   $headers  The custom headers.
  */
 public function __construct($body = '', $status = 200, array $headers = array())
 {
     parent::__construct($this->handleBody($body), $status, $this->addContentTypeToHeader($headers, $this->type . '; charset=utf-8'));
 }
Ejemplo n.º 6
0
 /**
  * Method to get a response object from a server response.
  *
  * @param   array   $headers  The response headers as an array.
  * @param   string  $body     The response body as a string.
  *
  * @return  Response
  *
  * @since   2.1
  * @throws  \UnexpectedValueException
  */
 protected function getResponse(array $headers, $body)
 {
     // Create the response object.
     $return = new Response();
     // Set the body for the response.
     $return->getBody()->write($body);
     $return->getBody()->rewind();
     // Get the response code from the first offset of the response headers.
     preg_match('/[0-9]{3}/', array_shift($headers), $matches);
     $code = $matches[0];
     if (is_numeric($code)) {
         $return = $return->withStatus($code);
     } else {
         throw new \UnexpectedValueException('No HTTP response code found.');
     }
     // Add the response headers to the response object.
     foreach ($headers as $header) {
         $pos = strpos($header, ':');
         $return = $return->withHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
     }
     return $return;
 }
Ejemplo n.º 7
0
 /**
  * Method to get a response object from a server response.
  *
  * @param   string  $content  The complete server response, including headers
  *                            as a string if the response has no errors.
  * @param   array   $info     The cURL request information.
  *
  * @return  Response
  *
  * @since   2.0
  * @throws  \UnexpectedValueException
  */
 protected function getResponse($content, $info)
 {
     // Create the response object.
     $return = new Response();
     // Get the number of redirects that occurred.
     $redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0;
     /*
      * Split the response into headers and body. If cURL encountered redirects, the headers for the redirected requests will
      * also be included. So we split the response into header + body + the number of redirects and only use the last two
      * sections which should be the last set of headers and the actual body.
      */
     $response = explode("\r\n\r\n", $content, 2 + $redirects);
     // Set the body for the response.
     $return->getBody()->write(array_pop($response));
     $return->getBody()->rewind();
     // Get the last set of response headers as an array.
     $headers = explode("\r\n", array_pop($response));
     // Get the response code from the first offset of the response headers.
     preg_match('/[0-9]{3}/', array_shift($headers), $matches);
     $code = count($matches) ? $matches[0] : null;
     if (is_numeric($code)) {
         $return = $return->withStatus($code);
     } else {
         throw new \UnexpectedValueException('No HTTP response code found.');
     }
     // Add the response headers to the response object.
     foreach ($headers as $header) {
         $pos = strpos($header, ':');
         $return = $return->withHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
     }
     return $return;
 }