Example #1
0
 /**
  * Performs an HTTP HEAD on a URI. $this->responseText and $this->responseObj
  * empty after this call, but you can read the content-length from
  * $this->responseLength.
  *
  * @param string $uri
  * @param string $accept_mime_type
  * @param array $custom_headers
  * @param array $options
  */
 public function head($uri = null, $accept_mime_type = 'application/json; charset=utf-8', array $custom_headers = array(), array $options = array())
 {
     $headers = array('accept' => $accept_mime_type);
     foreach ($custom_headers as $key => $value) {
         $headers[strtolower($key)] = $value;
     }
     $request = $this->client->head($uri, $headers, $options);
     $this->exec($request);
 }
Example #2
0
 /**
  * @return mixed
  * @throws SummonException
  * @throws \Guzzle\Http\Exception\ClientErrorResponseException
  */
 public function fetch()
 {
     try {
         $response = $this->client->head()->send();
     } catch (ClientErrorResponseException $e) {
         // HEAD method is not allowed, try GET instead
         $response = $this->client->get()->send();
     }
     // update the url to the actual endpoint (after redirects)
     $this->url = $response->getInfo('url');
     list($this->mimetype) = explode(';', $response->getContentType());
     $type = self::getType($this->mimetype);
     $handler = sprintf('parse%s', ucfirst($type));
     if (!method_exists($this, $handler)) {
         throw new SummonException("Unable to handle type [{$type}] in self");
     }
     return $this->{$handler}($response);
 }
 public function resolveUrl($url)
 {
     $client = new GuzzleClient($url);
     $history = new HistoryPlugin();
     $client->addSubscriber($history);
     $response = $client->head($url)->send();
     if (!$response->isSuccessful()) {
         throw new \Exception(sprintf("Url %s is not a valid URL or website is down.", $url));
     }
     return $response->getEffectiveUrl();
 }
 /**
  * @param string $repository
  * @param string $format
  *
  * @return \PUGX\Badge\Model\Badge
  */
 public function createComposerLockBadge($repository, $format = 'svg')
 {
     $repo = str_replace('.git', '', $this->packageRepository->fetchByRepository($repository)->getOriginalObject()->getRepository());
     $request = $this->client->head($repo . '/blob/master/composer.lock', array(), array('timeout' => 2, 'connect_timeout' => 1, 'exceptions' => false));
     $response = $this->client->send($request);
     $status = 500;
     if ($request) {
         $status = $response->getStatusCode();
     }
     $this->text = self::LOCK_ERROR;
     $color = self::COLOR_ERROR;
     $subject = self::SUBJECT_ERROR;
     if (200 === $status) {
         $this->text = self::LOCK_COMMITTED;
         $color = self::COLOR_COMMITTED;
         $subject = self::SUBJECT;
     } elseif (404 === $status) {
         $this->text = self::LOCK_UNCOMMITTED;
         $color = self::COLOR_UNCOMMITTED;
         $subject = self::SUBJECT;
     }
     return $this->createBadgeFromRepository($repository, $subject, $color, $format);
 }
 public function validate($value, Constraint $constraint)
 {
     if (empty($value)) {
         return;
     }
     $previousViolationsCount = $this->context->getViolations()->count();
     parent::validate($value, $constraint);
     if ($previousViolationsCount < $this->context->getViolations()->count()) {
         return;
     }
     $client = new Client();
     try {
         $request = $client->head($value);
         $response = $request->send();
         if (!$response->isSuccessful()) {
             $this->context->addViolation($constraint->clientError, array('%errorCode%' => $response->getStatusCode()));
         }
     } catch (CurlException $e) {
         $this->context->addViolation($constraint->websiteDoesntExist, array('%url' => $value));
     } catch (ClientErrorResponseException $e) {
         $errorCode = $e->getResponse()->getStatusCode();
         if ($errorCode == 403) {
             $this->context->addViolation($constraint->accessDenied);
         } elseif ($errorCode == 404) {
             $this->context->addViolation($constraint->resNotFound);
         } elseif ($errorCode == 405) {
             $allow = $e->getResponse()->getHeaders()['allow'];
             if (!preg_match('#GET#', $allow)) {
                 $this->context->addViolation($constraint->methodNotAllowed);
             }
         } else {
             $this->context->addViolation($constraint->clientError, array('%errorCode%' => $errorCode));
         }
     } catch (ServerErrorResponseException $e) {
         $this->context->addViolation($constraint->serverError, array('%errorCode%' => $e->getResponse()->getStatusCode()));
     }
 }
 /**
  * @covers Guzzle\Http\Curl\CurlMulti
  */
 public function testProperlyBlocksBasedOnRequestsInScope()
 {
     $this->getServer()->flush();
     $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest1", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest2", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest3", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest4", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest5", "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ntest6", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
     $client = new Client($this->getServer()->getUrl());
     $requests = array($client->get(), $client->get(), $client->get());
     // Sends 2 new requests in the middle of a CurlMulti loop while other requests
     // are completing.  This causes the scope of the multi handle to go up.
     $callback = function (Event $event) use($client) {
         $client->getConfig()->set('called', $client->getConfig('called') + 1);
         $request = $client->get();
         if ($client->getConfig('called') <= 2) {
             $request->getEventDispatcher()->addListener('request.complete', function (Event $event) use($client) {
                 $client->head()->send();
             });
         }
         $request->send();
     };
     $requests[0]->getEventDispatcher()->addListener('request.complete', $callback);
     $requests[1]->getEventDispatcher()->addListener('request.complete', $callback);
     $requests[2]->getEventDispatcher()->addListener('request.complete', $callback);
     $client->send($requests);
     $this->assertEquals(8, count($this->getServer()->getReceivedRequests(false)));
 }
 /**
  * @param string $uri
  * @param array $headers
  * @param array $options
  * @return \Guzzle\Http\Message\RequestInterface
  */
 public function head($uri, $headers = null, $options = [])
 {
     $this->request = $this->client->head($uri, $headers, $options);
     $this->response = $this->request->send();
     return $this->response;
 }
 public function testHeadCanUseOptions()
 {
     $client = new Client();
     $head = $client->head('http://www.foo.com', array(), array('query' => array('foo' => 'bar')));
     $this->assertEquals('bar', $head->getQuery()->get('foo'));
 }
Example #9
0
 /**
  * @covers Guzzle\Http\Client::createRequest
  * @covers Guzzle\Http\Client::get
  * @covers Guzzle\Http\Client::put
  * @covers Guzzle\Http\Client::post
  * @covers Guzzle\Http\Client::head
  * @covers Guzzle\Http\Client::options
  */
 public function testUriArrayAllowsCustomTemplateVariables()
 {
     $client = new Client();
     $vars = array('var' => 'hi');
     $this->assertEquals('/hi', (string) $client->createRequest('GET', array('/{var}', $vars))->getUrl());
     $this->assertEquals('/hi', (string) $client->get(array('/{var}', $vars))->getUrl());
     $this->assertEquals('/hi', (string) $client->put(array('/{var}', $vars))->getUrl());
     $this->assertEquals('/hi', (string) $client->post(array('/{var}', $vars))->getUrl());
     $this->assertEquals('/hi', (string) $client->head(array('/{var}', $vars))->getUrl());
     $this->assertEquals('/hi', (string) $client->options(array('/{var}', $vars))->getUrl());
 }
Example #10
0
 /**
  * @param string $location
  * @return int
  */
 private function getRemoteFileSize($location)
 {
     $parts = parse_url($location);
     $http = new HttpClient($parts['scheme'] . '://' . $parts['host']);
     $response = $http->head($parts['path'])->send();
     return (int) current($response->getHeader('Content-Length')->toArray());
 }