예제 #1
0
 public static function createRequest(PsrServerRequestInterface $psrRequest)
 {
     $request = new Request(new Uri($psrRequest->getUri()), $psrRequest->getMethod(), $psrRequest->getHeaders(), $psrRequest->getBody());
     $attributes = $psrRequest->getAttributes();
     foreach ($attributes as $name => $value) {
         $request->setAttribute($name, $value);
     }
     return $request;
 }
예제 #2
0
파일: Relation.php 프로젝트: visapi/amun
 public function request(Url $url, $mode, $host, $name, $header = array())
 {
     if (empty($mode) || ($mode = self::getMode($mode)) === false) {
         throw new Exception('Invalid mode');
     }
     // discover service
     $url = $this->discover($url);
     // headers
     if ($this->oauth !== null) {
         $header = Request::mergeHeader(array('Accept' => 'application/json', 'Authorization' => $this->oauth->getAuthorizationHeader($url, $this->cred->getConsumerKey(), $this->cred->getConsumerSecret(), $this->cred->getToken(), $this->cred->getTokenSecret(), 'HMAC-SHA1', 'POST')), $header);
     } else {
         $header = Request::mergeHeader(array('Accept' => 'application/json'), $header);
     }
     // body
     $body = array('relation.ns' => self::NS, 'relation.mode' => $mode, 'relation.host' => $host, 'relation.name' => $name);
     $request = new PostRequest($url, $header, $body);
     $response = $this->http->request($request);
     if ($response->getCode() == 200) {
         $data = Json::decode($response->getBody());
         if (isset($data['success']) && $data['success'] === true) {
             return true;
         } else {
             $msg = isset($data['text']) ? $data['text'] : 'An error occured';
             throw new Exception($msg);
         }
     } else {
         throw new Exception('Invalid response code ' . $response->getCode());
     }
 }
예제 #3
0
 /**
  * Converts an raw http request into an PSX\Http\Request object
  *
  * @param string $content
  * @return \PSX\Http\Request
  */
 public function parse($content)
 {
     $content = $this->normalize($content);
     list($method, $path, $scheme) = $this->getStatus($content);
     // resolve uri path
     if ($this->baseUrl !== null) {
         $path = UriResolver::resolve($this->baseUrl, new Uri($path));
     } else {
         $path = new Uri($path);
     }
     $request = new Request($path, $method);
     $request->setProtocolVersion($scheme);
     list($header, $body) = $this->splitMessage($content);
     $this->headerToArray($request, $header);
     $request->setBody(new StringStream($body));
     return $request;
 }
예제 #4
0
파일: PostRequest.php 프로젝트: seytar/psx
 /**
  * @param \PSX\Url|string $url
  * @param array $headers
  * @param \PSX\Http\StreamInterface|string|array $body
  */
 public function __construct($url, array $headers = array(), $body = null)
 {
     $url = $url instanceof Url ? $url : new Url((string) $url);
     if (is_array($body)) {
         $headers['Content-Type'] = 'application/x-www-form-urlencoded';
         $body = http_build_query($body, '', '&');
     }
     parent::__construct($url, 'POST', $headers, $body);
     $this->setHeader('Host', $url->getHost());
 }
예제 #5
0
 /**
  * @expectedException \PSX\Exception
  */
 public function testBuildStatusLineNoTarget()
 {
     $request = new Request(new Url('http://127.0.0.1'), 'GET');
     $request->setRequestTarget('');
     RequestParser::buildStatusLine($request);
 }
예제 #6
0
 public function testDecryptCookieDataChanged()
 {
     $request = new Request(new Url('http://localhost'), 'GET', array('Cookie' => 'psx_cookie=eyJmb28iOiJkYXRhIn0=.2IwNbitA6b1VccgR1pGVIRzro4AwYN1IqNXvNHYebog='));
     $response = new Response();
     $filters = array();
     $filters[] = new CookieSigner('secret_key');
     $filters[] = function ($request, $response, $filterChain) {
         $this->assertEquals(array('foo' => 'data'), $request->getAttribute(CookieSigner::COOKIE_NAME));
         $request->setAttribute(CookieSigner::COOKIE_NAME, array('foo' => 'foo'));
         $filterChain->handle($request, $response);
     };
     $filterChain = new FilterChain($filters);
     $filterChain->handle($request, $response);
     $this->assertEquals('psx_cookie=eyJmb28iOiJmb28ifQ==.lUT+fW1P+JlRv1+v0MtN7mQ9cx/OK5Jevt3wn/HXsj0=', $response->getHeader('Set-Cookie'));
     $this->assertEquals(array('foo' => 'foo'), $request->getAttribute(CookieSigner::COOKIE_NAME));
 }
예제 #7
0
 /**
  * @param \PSX\Url|string $url
  * @param array $headers
  * @param \PSX\Http\StreamInterface|string $body
  */
 public function __construct($url, array $headers = array(), $body = null)
 {
     $url = $url instanceof Url ? $url : new Url((string) $url);
     parent::__construct($url, 'DELETE', $headers, $body);
     $this->setHeader('Host', $url->getHost());
 }
예제 #8
0
파일: RequestTest.php 프로젝트: seytar/psx
 public function testGetSetAttributes()
 {
     $request = new Request(new Url('http://127.0.0.1'), 'POST');
     $request->setAttribute('foo', 'bar');
     $this->assertEquals('bar', $request->getAttribute('foo'));
     $this->assertEquals(null, $request->getAttribute('bar'));
     $this->assertEquals(array('foo' => 'bar'), $request->getAttributes());
     $request->setAttribute('bar', 'foo');
     $this->assertEquals('foo', $request->getAttribute('bar'));
     $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo'), $request->getAttributes());
     $request->removeAttribute('bar');
     $request->removeAttribute('fooo');
     // unknown value
     $this->assertEquals(null, $request->getAttribute('bar'));
 }
예제 #9
0
파일: GetRequest.php 프로젝트: seytar/psx
 /**
  * @param \PSX\Url|string $url
  * @param array $headers
  */
 public function __construct($url, array $headers = array())
 {
     $url = $url instanceof Url ? $url : new Url((string) $url);
     parent::__construct($url, 'GET', $headers);
     $this->setHeader('Host', $url->getHost());
 }
예제 #10
0
파일: Http.php 프로젝트: seytar/psx
 /**
  * Sends the request through the given handler and returns the response
  *
  * @param \PSX\Http\Request $request
  * @param \PSX\Http\Options $options
  * @param integer $count
  * @return \PSX\Http\Response
  */
 public function request(Request $request, Options $options = null, $count = 0)
 {
     if (!$request->getUri()->isAbsolute()) {
         throw new InvalidArgumentException('Request url must be absolute');
     }
     // set cookie headers
     if ($this->cookieStore !== null) {
         $cookies = $this->cookieStore->load($request->getUri()->getHost());
         if (!empty($cookies)) {
             $kv = array();
             foreach ($cookies as $cookie) {
                 $path = ltrim($cookie->getPath(), '/');
                 if ($cookie->getExpires() !== null && $cookie->getExpires()->getTimestamp() < time()) {
                     $this->cookieStore->remove($request->getUri()->getHost(), $cookie);
                 } elseif ($cookie->getPath() !== null && substr($request->getUri()->getPath(), 0, strlen($path)) != $path) {
                     // path does not fit
                 } else {
                     $kv[] = $cookie->getName() . '=' . $cookie->getValue();
                 }
             }
             $request->addHeader('Cookie', implode('; ', $kv));
         }
     }
     // set content length
     $body = $request->getBody();
     if ($body !== null && $request->hasHeader('Transfer-Encoding') != 'chunked' && !in_array($request->getMethod(), array('HEAD', 'GET'))) {
         $size = $body->getSize();
         if ($size !== false) {
             $request->setHeader('Content-Length', $size);
         }
     }
     // set default options
     if ($options === null) {
         $options = new Options();
     }
     // make request
     $response = $this->handler->request($request, $options);
     // store cookies
     if ($this->cookieStore !== null) {
         $cookies = $response->getHeaderLines('Set-Cookie');
         foreach ($cookies as $rawCookie) {
             try {
                 $cookie = new Cookie($rawCookie);
                 $domain = $cookie->getDomain() !== null ? $cookie->getDomain() : $request->getUri()->getHost();
                 $this->cookieStore->store($domain, $cookie);
             } catch (InvalidArgumentException $e) {
                 // invalid cookies
             }
         }
     }
     // check follow location
     if ($options->getFollowLocation() && ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400)) {
         $location = (string) $response->getHeader('Location');
         if (!empty($location) && $location != $request->getUri()->toString()) {
             if ($options->getMaxRedirects() > $count) {
                 $location = UriResolver::resolve($request->getUri(), new Uri($location));
                 return $this->request(new GetRequest($location), $options, ++$count);
             } else {
                 throw new RedirectException('Max redirection reached');
             }
         }
     }
     return $response;
 }