Athlon1600 / php-proxy

A web proxy script written in PHP and built as an alternative to Glype.
https://www.php-proxy.com
MIT License
298 stars 158 forks source link

Better content-type check #37

Open jclerc opened 7 years ago

jclerc commented 7 years ago

As resources may have a header like Content-Type: text/javascript;charset=UTF-8

jclerc commented 7 years ago

Your commit will make it work as before. But the aim of this pull request is to ignore resources that have a charset at the end of their Content-Type header, such as Content-Type: text/javascript;charset=UTF-8.

So in_array won't work, but strpos === 0 will.

siverson101 commented 7 years ago

What you want is use a startsWith check. Maybe add these to the helper class: function startsWith($haystack, $needle) { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); }

function endsWith($haystack, $needle) { $length = strlen($needle); if ($length == 0) { return true; }

return (substr($haystack, -$length) === $needle);

}

Then call if(startsWith($content_type, $type) ){


Or, split the string on ';' and only use the first part for the test.

siverson101 commented 7 years ago

You could also do: // Strip off ;charset=UTF-8 from Content-Type: text/javascript;charset=UTF-8 $pos = strpos($content_type, ';'); if ($pos !== false) { $content_type = substr($content_type, 0, $pos - 1); }

jclerc commented 7 years ago

Why?

My solution just works in few line and is very efficient as it is.