Skip to content

Commit

Permalink
Add object url support
Browse files Browse the repository at this point in the history
  • Loading branch information
GeLoLabs committed Aug 22, 2014
1 parent dec32a4 commit eeb745a
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 105 deletions.
5 changes: 4 additions & 1 deletion doc/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ Note that the request protocol version will be used instead of the one configure

## Url

The url defines the remote server where the request will be sent. If you want to get/set it, you can use:
The url defines the remote server where the request will be sent. It can be either a string or an object implementing
the `__toString` method. If you want to get/set it, you can use:

``` php
$url = $request->getUrl();
$request->setUrl('http://egeloen.fr/');
// or
$request->setUrl($object);
```

## Method
Expand Down
7 changes: 4 additions & 3 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ throw an `Ivory\HttpAdapter\HttpAdapterException` if an error occurred (I would
block everywhere) and return an `Ivory\HttpAdapter\Message\ResponseInterface`. If you want to learn more about the
response, you can read this [doc](/doc/response.md).

Additionally, the headers parameter can be an associative array describing an header key/value pair or an indexed array
already formatted. The datas can be an associative array or a string already formatted according to the content-type
you want to use. Finally, the files are an associative array describing key/path pair.
Additionally, the url can be a string or an object implementing the `__toString` method. The headers parameter can be
an associative array describing an header key/value pair or an indexed array already formatted. The datas can be an
associative array or a string already formatted according to the content-type you want to use. Finally, the files are
an associative array describing key/path pair.

## Send a GET request

Expand Down
2 changes: 1 addition & 1 deletion src/Ivory/HttpAdapter/AbstractHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function sendRequest(RequestInterface $request)
$this->protocolVersion = $request->getProtocolVersion();

$response = $this->send(
(string) $request->getUrl(),
$request->getUrl(),
$request->getMethod(),
$request->getHeaders(),
(string) $request->getBody()
Expand Down
10 changes: 4 additions & 6 deletions src/Ivory/HttpAdapter/AbstractStreamHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract class AbstractStreamHttpAdapter extends AbstractHttpAdapter
*/
protected function doSend(InternalRequestInterface $internalRequest)
{
$url = (string) $internalRequest->getUrl();

$context = stream_context_create(array(
'http' => array(
'follow_location' => false,
Expand All @@ -43,14 +45,10 @@ protected function doSend(InternalRequestInterface $internalRequest)
)
));

list($body, $headers) = $this->process($internalRequest->getUrl(), $context);
list($body, $headers) = $this->process($url, $context);

if ($body === false) {
throw HttpAdapterException::cannotFetchUrl(
$internalRequest->getUrl(),
$this->getName(),
print_r(error_get_last(), true)
);
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), print_r(error_get_last(), true));
}

return $this->createResponse(
Expand Down
8 changes: 3 additions & 5 deletions src/Ivory/HttpAdapter/BuzzHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ protected function doSend(InternalRequestInterface $internalRequest)
$this->browser->getClient()->setTimeout($this->timeout);
$this->browser->getClient()->setMaxRedirects(0);

$request = $this->browser->getMessageFactory()->createRequest(
$internalRequest->getMethod(),
$internalRequest->getUrl()
);
$url = (string) $internalRequest->getUrl();

$request = $this->browser->getMessageFactory()->createRequest($internalRequest->getMethod(), $url);
$request->setProtocolVersion($internalRequest->getProtocolVersion());
$request->setHeaders($this->prepareHeaders($internalRequest, false));

Expand All @@ -84,7 +82,7 @@ protected function doSend(InternalRequestInterface $internalRequest)
try {
$response = $this->browser->send($request);
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($internalRequest->getUrl(), $this->getName(), $e->getMessage());
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}

return $this->createResponse(
Expand Down
6 changes: 4 additions & 2 deletions src/Ivory/HttpAdapter/CurlHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ protected function doSend(InternalRequestInterface $internalRequest)
{
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $internalRequest->getUrl());
$url = (string) $internalRequest->getUrl();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, $this->prepareProtocolVersion($internalRequest));
curl_setopt($curl, CURLOPT_HEADER, true);
Expand Down Expand Up @@ -90,7 +92,7 @@ protected function doSend(InternalRequestInterface $internalRequest)
$error = curl_error($curl);
curl_close($curl);

throw HttpAdapterException::cannotFetchUrl($internalRequest->getUrl(), $this->getName(), $error);
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $error);
}

$headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
Expand Down
6 changes: 3 additions & 3 deletions src/Ivory/HttpAdapter/Event/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function matchDomain(InternalRequestInterface $request)
}

$cookieDomain = $this->getAttribute(self::ATTR_DOMAIN);
$domain = parse_url($request->getUrl(), PHP_URL_HOST);
$domain = parse_url((string) $request->getUrl(), PHP_URL_HOST);

if (strpos($cookieDomain, '.') === 0) {
return (bool) preg_match('/\b'.preg_quote(substr($cookieDomain, 1), '/').'$/i', $domain);
Expand All @@ -245,7 +245,7 @@ public function matchPath(InternalRequestInterface $request)
return true;
}

return strpos(parse_url($request->getUrl(), PHP_URL_PATH), $this->getAttribute(self::ATTR_PATH)) === 0;
return strpos(parse_url((string) $request->getUrl(), PHP_URL_PATH), $this->getAttribute(self::ATTR_PATH)) === 0;
}

/**
Expand All @@ -258,7 +258,7 @@ public function matchSecure(InternalRequestInterface $request)
}

$secure = $this->getAttribute(self::ATTR_SECURE);
$scheme = parse_url($request->getUrl(), PHP_URL_SCHEME);
$scheme = parse_url((string) $request->getUrl(), PHP_URL_SCHEME);

return ($secure && $scheme === 'https') || (!$secure && (($scheme === 'http') || empty($scheme)));
}
Expand Down
5 changes: 4 additions & 1 deletion src/Ivory/HttpAdapter/Event/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public function extract(InternalRequestInterface $request, ResponseInterface $re
$cookie = $this->cookieFactory->parse($header);

if (!$cookie->hasAttribute(CookieInterface::ATTR_DOMAIN)) {
$cookie->setAttribute(CookieInterface::ATTR_DOMAIN, parse_url($request->getUrl(), PHP_URL_HOST));
$cookie->setAttribute(
CookieInterface::ATTR_DOMAIN,
parse_url((string) $request->getUrl(), PHP_URL_HOST)
);
}

$this->addCookie($cookie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function match(InternalRequestInterface $request)
return true;
}

if (is_string($this->matcher) && preg_match($this->matcher, $request->getUrl())) {
if (is_string($this->matcher) && preg_match($this->matcher, (string) $request->getUrl())) {
return true;
}

Expand Down
10 changes: 7 additions & 3 deletions src/Ivory/HttpAdapter/Event/Subscriber/LoggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function onPostSend(PostSendEvent $event)
sprintf(
'Send "%s %s" in %.2f ms.',
$event->getRequest()->getMethod(),
$event->getRequest()->getUrl(),
(string) $event->getRequest()->getUrl(),
$this->time
),
array(
Expand All @@ -89,7 +89,11 @@ public function onPostSend(PostSendEvent $event)
public function onException(ExceptionEvent $event)
{
$this->logger->error(
sprintf('Unable to send "%s %s".', $event->getRequest()->getMethod(), $event->getRequest()->getUrl()),
sprintf(
'Unable to send "%s %s".',
$event->getRequest()->getMethod(),
(string) $event->getRequest()->getUrl()
),
array(
'request' => $this->formatRequest($event->getRequest()),
'exception' => $this->formatException($event->getException()),
Expand Down Expand Up @@ -119,7 +123,7 @@ protected function formatRequest(InternalRequestInterface $request)
{
return array(
'protocol_version' => $request->getProtocolVersion(),
'url' => $request->getUrl(),
'url' => (string) $request->getUrl(),
'method' => $request->getMethod(),
'headers' => $request->getHeaders(),
'raw_datas' => $request->getRawDatas(),
Expand Down
4 changes: 2 additions & 2 deletions src/Ivory/HttpAdapter/Event/Subscriber/RedirectSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function onPostSend(PostSendEvent $event)
if ($request->getParameter(self::REDIRECT_COUNT) + 1 > $this->maxRedirects) {
if ($this->throwException) {
throw HttpAdapterException::maxRedirectsExceeded(
$this->getRootRequest($request)->getUrl(),
(string) $this->getRootRequest($request)->getUrl(),
$this->maxRedirects,
$httpAdapter->getName()
);
Expand Down Expand Up @@ -213,7 +213,7 @@ protected function prepareRedirectRequest(
protected function prepareResponse(InternalRequestInterface $request, ResponseInterface $response)
{
$response->setParameter(self::REDIRECT_COUNT, (int) $request->getParameter(self::REDIRECT_COUNT));
$response->setParameter(self::EFFECTIVE_URL, $request->getUrl());
$response->setParameter(self::EFFECTIVE_URL, (string) $request->getUrl());
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Ivory/HttpAdapter/Guzzle3HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public function getName()
*/
protected function doSend(InternalRequestInterface $internalRequest)
{
$url = (string) $internalRequest->getUrl();

$request = $this->client->createRequest(
$internalRequest->getMethod(),
$internalRequest->getUrl(),
$url,
$this->prepareHeaders($internalRequest),
$this->prepareContent($internalRequest),
array(
Expand All @@ -68,7 +70,7 @@ protected function doSend(InternalRequestInterface $internalRequest)
try {
$response = $request->send();
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($internalRequest->getUrl(), $this->getName(), $e->getMessage());
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}

return $this->createResponse(
Expand Down
6 changes: 4 additions & 2 deletions src/Ivory/HttpAdapter/Guzzle4HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public function getName()
*/
protected function doSend(InternalRequestInterface $internalRequest)
{
$url = (string) $internalRequest->getUrl();

$request = $this->client->createRequest(
$internalRequest->getMethod(),
$internalRequest->getUrl(),
$url,
array(
'version' => $internalRequest->getProtocolVersion(),
'timeout' => $this->timeout,
Expand All @@ -67,7 +69,7 @@ protected function doSend(InternalRequestInterface $internalRequest)
try {
$response = $this->client->send($request);
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($internalRequest->getUrl(), $this->getName(), $e->getMessage());
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}

return $this->createResponse(
Expand Down
12 changes: 12 additions & 0 deletions src/Ivory/HttpAdapter/HttpAdapterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,16 @@ public static function timeoutExceeded($url, $timeout, $adapter)
{
return self::cannotFetchUrl($url, $adapter, sprintf('Timeout exceeded (%.2f)', $timeout));
}

/**
* Gets the "URL IS NOT VALID" exception.
*
* @param string $url The url.
*
* @return \Ivory\HttpAdapter\HttpAdapterException The "URL IS NOT VALID" exception.
*/
public static function urlIsNotValid($url)
{
return new self(sprintf('The url "%s" is not valid.', $url));
}
}
58 changes: 29 additions & 29 deletions src/Ivory/HttpAdapter/HttpAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface HttpAdapterInterface extends HttpAdapterConfigInterface
/**
* Sends a GET request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param string|object $url The url.
* @param array $headers The headers.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -36,8 +36,8 @@ public function get($url, array $headers = array());
/**
* Sends an HEAD request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param string|object $url The url.
* @param array $headers The headers.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -48,10 +48,10 @@ public function head($url, array $headers = array());
/**
* Sends a POST request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -62,10 +62,10 @@ public function post($url, array $headers = array(), $datas = array(), array $fi
/**
* Sends a PUT request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -76,10 +76,10 @@ public function put($url, array $headers = array(), $datas = array(), array $fil
/**
* Sends a PATCH request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -90,10 +90,10 @@ public function patch($url, array $headers = array(), $datas = array(), array $f
/**
* Sends a DELETE request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -104,10 +104,10 @@ public function delete($url, array $headers= array(), $datas = array(), array $f
/**
* Sends an OPTIONS request.
*
* @param string $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand All @@ -118,11 +118,11 @@ public function options($url, array $headers= array(), $datas = array(), array $
/**
* Sends a request.
*
* @param string $url The url.
* @param string $method The method.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
* @param string|object $url The url.
* @param string $method The method.
* @param array $headers The headers.
* @param array|string $datas The datas.
* @param array $files The files.
*
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
*
Expand Down

0 comments on commit eeb745a

Please sign in to comment.