/**
  * 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;
 }
 public function testConfigurationViaProperties()
 {
     $trace = new TraceHttpAdapter();
     $this->copyTasksAddingCustomRequest('config-properties', 'recipient', $this->createRequest($trace));
     $this->executeTarget('recipient');
     $request = new HTTP_Request2(null, 'GET', array('proxy' => 'http://localhost:8080/', 'max_redirects' => 9));
     $this->assertEquals($request->getConfig(), $trace->requests[0]['config']);
 }
Exemple #3
0
 /**
  * Handles HTTP redirection
  *
  * This method will throw an Exception if redirect to a non-HTTP(S) location
  * is attempted, also if number of redirects performed already is equal to
  * 'max_redirects' configuration parameter.
  *
  * @param    HTTP_Request2               Original request
  * @param    HTTP_Request2_Response      Response containing redirect
  * @return   HTTP_Request2_Response      Response from a new location
  * @throws   HTTP_Request2_Exception
  */
 protected function handleRedirect(HTTP_Request2 $request, HTTP_Request2_Response $response)
 {
     if (is_null($this->redirectCountdown)) {
         $this->redirectCountdown = $request->getConfig('max_redirects');
     }
     if (0 == $this->redirectCountdown) {
         $this->redirectCountdown = null;
         // Copying cURL behaviour
         throw new HTTP_Request2_MessageException('Maximum (' . $request->getConfig('max_redirects') . ') redirects followed', HTTP_Request2_Exception::TOO_MANY_REDIRECTS);
     }
     $redirectUrl = new Net_URL2($response->getHeader('location'), array(Net_URL2::OPTION_USE_BRACKETS => $request->getConfig('use_brackets')));
     // refuse non-HTTP redirect
     if ($redirectUrl->isAbsolute() && !in_array($redirectUrl->getScheme(), array('http', 'https'))) {
         $this->redirectCountdown = null;
         throw new HTTP_Request2_MessageException('Refusing to redirect to a non-HTTP URL ' . $redirectUrl->__toString(), HTTP_Request2_Exception::NON_HTTP_REDIRECT);
     }
     // Theoretically URL should be absolute (see http://tools.ietf.org/html/rfc2616#section-14.30),
     // but in practice it is often not
     if (!$redirectUrl->isAbsolute()) {
         $redirectUrl = $request->getUrl()->resolve($redirectUrl);
     }
     $redirect = clone $request;
     $redirect->setUrl($redirectUrl);
     if (303 == $response->getStatus() || !$request->getConfig('strict_redirects') && in_array($response->getStatus(), array(301, 302))) {
         $redirect->setMethod(HTTP_Request2::METHOD_GET);
         $redirect->setBody('');
     }
     if (0 < $this->redirectCountdown) {
         $this->redirectCountdown--;
     }
     return $this->sendRequest($redirect);
 }
Exemple #4
0
 public function testSetAndGetConfig()
 {
     $req = new HTTP_Request2();
     $this->assertArrayHasKey('connect_timeout', $req->getConfig());
     $req->setConfig(array('connect_timeout' => 123));
     $this->assertEquals(123, $req->getConfig('connect_timeout'));
     try {
         $req->setConfig(array('foo' => 'unknown parameter'));
     } catch (HTTP_Request2_Exception $e) {
         try {
             $req->getConfig('bar');
         } catch (HTTP_Request2_Exception $e) {
             return;
         }
     }
     $this->fail('Expected HTTP_Request2_Exception was not thrown');
 }
 /**
  * Gets value for configuration parameter.
  * 
  * @param string $name configuration parameter name.
  * 
  * @return string
  */
 public function getConfig($name)
 {
     return $this->_request->getConfig($name);
 }
 public function testSetProxyAsUrl()
 {
     $req = new HTTP_Request2();
     $req->setConfig('proxy', 'socks5://foo:bar%25baz@localhost:1080/');
     $this->assertEquals('socks5', $req->getConfig('proxy_type'));
     $this->assertEquals('localhost', $req->getConfig('proxy_host'));
     $this->assertEquals(1080, $req->getConfig('proxy_port'));
     $this->assertEquals('foo', $req->getConfig('proxy_user'));
     $this->assertEquals('bar%baz', $req->getConfig('proxy_password'));
 }
Exemple #7
0
 /**
  * Sends request to the remote server and returns its response
  *
  * @param    HTTP_Request2
  * @return   HTTP_Request2_Response
  * @throws   HTTP_Request2_Exception
  */
 public function sendRequest(HTTP_Request2 $request)
 {
     $this->request = $request;
     $keepAlive = $this->connect();
     $headers = $this->prepareHeaders();
     // Use global request timeout if given, see feature requests #5735, #8964
     if ($timeout = $request->getConfig('timeout')) {
         $this->timeout = time() + $timeout;
     } else {
         $this->timeout = null;
     }
     try {
         if (false === @fwrite($this->socket, $headers, strlen($headers))) {
             throw new HTTP_Request2_Exception('Error writing request');
         }
         // provide request headers to the observer, see request #7633
         $this->request->setLastEvent('sentHeaders', $headers);
         $this->writeBody();
         if ($this->timeout && time() > $this->timeout) {
             throw new HTTP_Request2_Exception('Request timed out after ' . $request->getConfig('timeout') . ' second(s)');
         }
         $response = $this->readResponse();
         if (!$this->canKeepAlive($keepAlive, $response)) {
             $this->disconnect();
         }
         if ($this->shouldUseProxyDigestAuth($response)) {
             return $this->sendRequest($request);
         }
         if ($this->shouldUseServerDigestAuth($response)) {
             return $this->sendRequest($request);
         }
         if ($authInfo = $response->getHeader('authentication-info')) {
             $this->updateChallenge($this->serverChallenge, $authInfo);
         }
         if ($proxyInfo = $response->getHeader('proxy-authentication-info')) {
             $this->updateChallenge($this->proxyChallenge, $proxyInfo);
         }
     } catch (Exception $e) {
         $this->disconnect();
         throw $e;
     }
     return $response;
 }
 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);
 }
 public function testConfigurationViaProperties()
 {
     $trace = new TraceHttpAdapter();
     $this->copyTasksAddingCustomRequest('config-properties', 'recipient', $this->createRequest($trace));
     try {
         $this->executeTarget('recipient');
     } catch (BuildException $e) {
         // the request returns error 400, but we don't really care
     }
     $request = new HTTP_Request2(null, 'GET', array('proxy' => 'http://localhost:8080/', 'timeout' => 20, 'max_redirects' => 9));
     $this->assertEquals($request->getConfig(), $trace->requests[0]['config']);
 }