Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI Retriever: follow http redirects using Curl by default #646

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/JsonSchema/Uri/Retrievers/Curl.php
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Uri\Retrievers;

use JsonSchema\Exception\InvalidSourceUriException;
use JsonSchema\Exception\RuntimeException;
use JsonSchema\Validator;

Expand All @@ -19,14 +20,17 @@
*/
class Curl extends AbstractRetriever
{
private $decorated;
protected $messageBody;

public function __construct()
public function __construct(UriRetrieverInterface $decorated = null)
{
if (!function_exists('curl_init')) {
// Cannot test this, because curl_init is present on all test platforms plus mock
throw new RuntimeException('cURL not installed'); // @codeCoverageIgnore
}

$this->decorated = $decorated;
}

/**
Expand All @@ -36,44 +40,47 @@ public function __construct()
*/
public function retrieve($uri)
{
$scheme = parse_url($uri, PHP_URL_SCHEME);
if (!$scheme) {
if ($decorated) {
return $this->decorated->retrieve($uri);
}

throw new InvalidSourceUriExceptiog('No scheme provided in URI: '. $uri);
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ' . Validator::SCHEMA_MEDIA_TYPE));

$response = curl_exec($ch);
if (false === $response) {
throw new \JsonSchema\Exception\ResourceNotFoundException('JSON schema not found');
}

$this->fetchMessageBody($response);
$this->fetchContentType($response);
$curlInfo = curl_getinfo($ch);
$headers = substr($response, 0, $curlInfo['header_size']);
$this->fetchContentType($headers, $curlInfo);
$this->messageBody = substr($response, $curlInfo['header_size']);

curl_close($ch);

return $this->messageBody;
}

/**
* @param string $response cURL HTTP response
*/
private function fetchMessageBody($response)
{
preg_match("/(?:\r\n){2}(.*)$/ms", $response, $match);
$this->messageBody = $match[1];
}

/**
* @param string $response cURL HTTP response
*
* @return bool Whether the Content-Type header was found or not
*/
protected function fetchContentType($response)
private function fetchContentType($headers)
{
if (0 < preg_match("/Content-Type:(\V*)/ims", $response, $match)) {
$this->contentType = trim($match[1]);
if (0 < preg_match_all("/content-type: (.+?(?=;|$))/ims", $headers, $match, PREG_SET_ORDER)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->contentType = trim(end($match[1]));

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/JsonSchema/Uri/UriRetriever.php
Expand Up @@ -12,6 +12,7 @@
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Exception\ResourceNotFoundException;
use JsonSchema\Uri\Retrievers\Curl;
use JsonSchema\Uri\Retrievers\FileGetContents;
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
use JsonSchema\UriRetrieverInterface as BaseUriRetrieverInterface;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function confirmMediaType($uriRetriever, $uri)
public function getUriRetriever()
{
if (is_null($this->uriRetriever)) {
$this->setUriRetriever(new FileGetContents());
$this->setUriRetriever(\extension_loaded('curl') ? new Curl(new FileGetContents) : new FileGetContents);
}

return $this->uriRetriever;
Expand Down
16 changes: 16 additions & 0 deletions tests/Uri/Retrievers/CurlTest.php
Expand Up @@ -29,6 +29,22 @@ public function testNoContentType()
$c = new Curl();
$c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json');
}

public function testCurlFallbackFileGetContents()
{
$retriever = new Curl(new FileGetContents());
$result = $retriever->retrieve(__DIR__.'/../Fixture/child.json');
$this->assertNotEmpty($result);
}

/**
* @expectedException JsonSchema\Exception\ResourceNotFoundException
*/
public function testFetchMissingFile()
{
$retriever = new Curl(new FileGetContents());
$retriever->retrieve(__DIR__.'/Fixture/missing.json');
}
}
}

Expand Down