Skip to content

Commit

Permalink
Only perform a HEAD check instead of fetching an entire file to check…
Browse files Browse the repository at this point in the history
… its existence
  • Loading branch information
coreation committed Feb 1, 2016
1 parent ea43bdf commit 34b25a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/Tdt/Core/DataControllers/CSVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public function readData($source_definition, $rest_parameters = array())
}
}
fclose($handle);

} else {
\App::abort(500, "Cannot retrieve any data from the CSV file on location $uri.");
}
Expand Down
34 changes: 30 additions & 4 deletions app/Tdt/Core/Validators/CustomValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
*/
class CustomValidator extends \Illuminate\Validation\Validator
{

/**
* Check if the URI can be resolved externally or locally
*/
public function validateUri($attribute, $value, $parameters)
{
try {
if (!filter_var($value, FILTER_VALIDATE_URL) === false) {
$data = $this->getRemoteData($value);
$url_pieces = parse_url($value);

return !empty($data);
if (!filter_var($value, FILTER_VALIDATE_URL) === false && ($url_pieces['scheme'] == 'http' || $url_pieces['scheme'] == 'https')) {
$status = $this->getHeadInfo($value);

return $status == 200;
} else {
$data =@ file_get_contents($value);

Expand All @@ -31,6 +32,31 @@ public function validateUri($attribute, $value, $parameters)
}
}

private function getHeadInfo($uri)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $uri);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($c, CURLOPT_NOBODY, true);

curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);

if (!empty($status['http_code'])) {
return $status['http_code'];
} else {
return 500;
}
}

private function getRemoteData($url)
{
$c = curl_init();
Expand Down

0 comments on commit 34b25a1

Please sign in to comment.