예제 #1
1
 /**
  * Builds an array of post, get and server settings.
  * 
  * @return array
  */
 public function buildEnvironment(HTTP_Request2 $request)
 {
     $environment = array('_POST' => array(), '_GET' => array(), '_SERVER' => array());
     if ($request->getPostParams()) {
         $environment['_POST'] = $request->getPostParams();
     }
     $query = $request->getUrl()->getQuery();
     if (!empty($query)) {
         parse_str($query, $environment['_GET']);
     }
     $environment['_SERVER'] = $this->getServerGlobal($request->getConfig('host'), dirname($request->getConfig('index_file')), $request->getUrl(), $request->getMethod());
     return $environment;
 }
예제 #2
0
 /**
  * Calculates length of the request body, adds proper headers
  *
  * @param array &$headers associative array of request headers, this method
  *                        will add proper 'Content-Length' and 'Content-Type'
  *                        headers to this array (or remove them if not needed)
  */
 protected function calculateRequestLength(&$headers)
 {
     $this->requestBody = $this->request->getBody();
     if (is_string($this->requestBody)) {
         $this->contentLength = strlen($this->requestBody);
     } elseif (is_resource($this->requestBody)) {
         $stat = fstat($this->requestBody);
         $this->contentLength = $stat['size'];
         rewind($this->requestBody);
     } else {
         $this->contentLength = $this->requestBody->getLength();
         $headers['content-type'] = 'multipart/form-data; boundary=' . $this->requestBody->getBoundary();
         $this->requestBody->rewind();
     }
     if (in_array($this->request->getMethod(), self::$bodyDisallowed) || 0 == $this->contentLength) {
         // No body: send a Content-Length header nonetheless (request #12900),
         // but do that only for methods that require a body (bug #14740)
         if (in_array($this->request->getMethod(), self::$bodyRequired)) {
             $headers['content-length'] = 0;
         } else {
             unset($headers['content-length']);
             // if the method doesn't require a body and doesn't have a
             // body, don't send a Content-Type header. (request #16799)
             unset($headers['content-type']);
         }
     } else {
         if (empty($headers['content-type'])) {
             $headers['content-type'] = 'application/x-www-form-urlencoded';
         }
         $headers['content-length'] = $this->contentLength;
     }
 }
 /**
  *
  * @expectedException HTTP_Request2_LogicException
  */
 public function testSetMethod()
 {
     $req = new HTTP_Request2();
     $req->setMethod(HTTP_Request2::METHOD_PUT);
     $this->assertEquals(HTTP_Request2::METHOD_PUT, $req->getMethod());
     $req->setMethod('Invalid method');
 }
 /**
  * {@inheritdoc }
  */
 public function sendRequest(\HTTP_Request2 $request)
 {
     if (array_key_exists($request->getMethod(), array_flip($this->getDisallowedMethods()))) {
         throw new NotAllowedMethodTypeException();
     }
     return parent::sendRequest($request);
 }
예제 #5
0
 public function testSetMethod()
 {
     $req = new HTTP_Request2();
     $req->setMethod(HTTP_Request2::METHOD_PUT);
     $this->assertEquals(HTTP_Request2::METHOD_PUT, $req->getMethod());
     try {
         $req->setMethod('Invalid method');
     } catch (HTTP_Request2_Exception $e) {
         return;
     }
     $this->fail('Expected HTTP_Request2_Exception was not thrown');
 }
 /**
  * Gets request's HTTP method.
  *
  * @return string
  */
 public function getMethod()
 {
     return $this->_request->getMethod();
 }
예제 #7
0
 /**
  * we use the Pear::\HTTP_Request2 for all the heavy HTTP protocol lifting.
  *
  * @param string $resource api-resource
  * @param string $method request method [default:"GET"]
  * @param array $data request data as associative array [default:array()]
  * @param array $headers optional request header [default:array()]
  *
  * @throws ConnectionException
  * @throws BadRequestError
  * @throws UnauthorizedError
  * @throws ForbiddenError
  * @throws ConflictDuplicateError
  * @throws GoneError
  * @throws InternalServerError
  * @throws NotImplementedError
  * @throws ThrottledError
  * @throws CCException
  *
  * @return string json encoded servers response
  */
 private function _request($resource, $method = \HTTP_Request2::METHOD_GET, $data = array(), $headers = array())
 {
     $url = $this->_url . $resource;
     $request = new \HTTP_Request2($url);
     $request->setConfig(array('ssl_verify_peer' => API::SSL_VERIFY_PEER, 'ssl_cafile' => realpath(dirname(__FILE__)) . '/cacert.pem'));
     $methods = array('options' => \HTTP_Request2::METHOD_OPTIONS, 'get' => \HTTP_Request2::METHOD_GET, 'head' => \HTTP_Request2::METHOD_HEAD, 'post' => \HTTP_Request2::METHOD_POST, 'put' => \HTTP_Request2::METHOD_PUT, 'delete' => \HTTP_Request2::METHOD_DELETE, 'trace' => \HTTP_Request2::METHOD_TRACE, 'connect' => \HTTP_Request2::METHOD_CONNECT);
     $request->setMethod($methods[strtolower($method)]);
     #
     # If the current API instance has a valid token we add the Authorization
     # header with the correct token.
     #
     # In case we do not have a valid token but email and password are
     # provided we automatically use them to add a HTTP Basic Authenticaion
     # header to the request to create a new token.
     #
     if ($this->_token) {
         $headers['Authorization'] = sprintf('cc_auth_token="%s"', $this->_token);
     } else {
         if ($this->_email && $this->_password) {
             $request->setAuth($this->_email, $this->_password, \HTTP_Request2::AUTH_BASIC);
         }
     }
     #
     # The API expects the body to be urlencoded. If data was passed to
     # the request method we therefore use urlencode from urllib.
     #
     if (!empty($data)) {
         if ($request->getMethod() == \HTTP_Request2::METHOD_GET) {
             $url = $request->getUrl();
             $url->setQueryVariables($data);
         } else {
             // works with post and put
             $request->addPostParameter($data);
             $request->setBody(http_build_query($data));
         }
     }
     #
     # We set the User-Agent Header to pycclib and the local version.
     # This enables basic statistics about still used pycclib versions in
     # the wild.
     #
     $headers['User-Agent'] = sprintf('phpcclib/%s', $this->_version);
     #
     # The API expects PUT or POST data to be x-www-form-urlencoded so we
     # also set the correct Content-Type header.
     #
     if (strtoupper($method) == 'PUT' || strtoupper($method) == 'POST') {
         $headers['Content-Type'] = 'application/x-www-form-urlencoded';
     }
     #
     # We also set the Content-Length and Accept-Encoding headers.
     #
     //$headers['Content-Length'] = strlen($body);
     $headers['Accept-Encoding'] = 'compress, gzip';
     #
     # Finally we fire the actual request.
     #
     foreach ($headers as $k => $v) {
         $request->setHeader(sprintf('%s: %s', $k, $v));
     }
     for ($i = 1; $i < 6; $i++) {
         try {
             $response = $request->send();
             return $this->_return($response);
         } catch (\HTTP_Request2_Exception $e) {
             # if we could not reach the API we wait 1s and try again
             sleep(1);
             # if we tried for the fifth time we give up - and cry a little
             if ($i == 5) {
                 throw new ConnectionException('Could not connect to API...');
             }
         }
     }
 }
예제 #8
0
 /**
  * Collect debug information on the last call.
  *
  * @return array
  * @uses   self::$client
  */
 public function debugCall()
 {
     return array('event' => $this->client->getLastEvent(), 'url' => (string) $this->client->getUrl(), 'data' => $this->client->getBody(), 'method' => $this->client->getMethod());
 }
 public function sendRequest(HTTP_Request2 $request)
 {
     $this->requests[] = array('config' => $request->getConfig(), 'url' => $request->getUrl(), 'method' => $request->getMethod(), 'headers' => $request->getHeaders(), 'auth' => $request->getAuth(), 'body' => (string) $request->getBody());
     return parent::sendRequest($request);
 }