Browse Source

better blacklist for google URLs

pull/39/head
Aaron Parecki 8 years ago
parent
commit
e3000f8c06
4 changed files with 16 additions and 11 deletions
  1. +1
    -1
      lib/HTTP.php
  2. +3
    -5
      lib/HTTPCurl.php
  3. +3
    -5
      lib/HTTPStream.php
  4. +9
    -0
      lib/helpers.php

+ 1
- 1
lib/HTTP.php View File

@ -31,7 +31,7 @@ class HTTP {
}
private function _class($url) {
if(preg_match('/brid\.gy|appspot\.com/', $url)) {
if(should_follow_redirects($url)) {
return 'p3k\HTTPStream';
} else {
return 'p3k\HTTPCurl';

+ 3
- 5
lib/HTTPCurl.php View File

@ -54,18 +54,16 @@ class HTTPCurl {
}
private function _set_curlopts($ch, $url) {
$host = parse_url($url, PHP_URL_HOST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// Special-case appspot.com URLs to not follow redirects.
// https://cloud.google.com/appengine/docs/php/urlfetch/
if(substr($host, -12) == '.appspot.com') {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
} else {
if(should_follow_redirects($url)) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
}
curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000));

+ 3
- 5
lib/HTTPStream.php View File

@ -72,8 +72,6 @@ class HTTPStream {
}
private function _stream_context($method, $url, $body=false, $headers=[]) {
$host = parse_url($url, PHP_URL_HOST);
$options = [
'method' => $method,
'timeout' => $this->timeout,
@ -90,11 +88,11 @@ class HTTPStream {
// Special-case appspot.com URLs to not follow redirects.
// https://cloud.google.com/appengine/docs/php/urlfetch/
if(substr($host, -12) == '.appspot.com') {
$options['follow_location'] = 0;
} else {
if(should_follow_redirects($url)) {
$options['follow_location'] = 1;
$options['max_redirects'] = $this->max_redirects;
} else {
$options['follow_location'] = 0;
}
return stream_context_create(['http' => $options]);

+ 9
- 0
lib/helpers.php View File

@ -9,3 +9,12 @@ function view($template, $data=[]) {
function normalize_url($url) {
return parse_url($url, PHP_URL_PATH) == '' ? $url.'/' : $url;
}
function should_follow_redirects($url) {
$host = parse_url($url, PHP_URL_HOST);
if(preg_match('/brid\.gy|appspot\.com|blogspot\.com|youtube\.com/', $host)) {
return false;
} else {
return true;
}
}

Loading…
Cancel
Save