php-proxy icon indicating copy to clipboard operation
php-proxy copied to clipboard

Better content-type check

Open jclerc opened this issue 7 years ago • 4 comments

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

jclerc avatar Oct 05 '16 10:10 jclerc

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.

jclerc avatar Oct 18 '16 20:10 jclerc

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 avatar Nov 08 '16 18:11 siverson101

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); }

siverson101 avatar Nov 08 '16 18:11 siverson101

Why?

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

jclerc avatar Nov 08 '16 19:11 jclerc