From 251249b11e317abe526a07953aed09b6b1ec058c Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 19 Jun 2020 16:33:18 -0400 Subject: [PATCH] Tests for ad hoc content sniffing --- lib/Parser/Parser.php | 8 ++++---- tests/cases/Parser/ParserTest.php | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/Parser/Parser.php b/lib/Parser/Parser.php index 0f0c73c..a967d0e 100644 --- a/lib/Parser/Parser.php +++ b/lib/Parser/Parser.php @@ -35,21 +35,21 @@ abstract class Parser { return "application/xml"; } elseif (preg_match('/^\s*)*-->\s*)*/s', $data, $match) ? strlen($match[0]) : 0; + $offset = preg_match('/^\s*())*-->\s*)*/s', $data, $match) ? strlen($match[0]) : 0; $prefix = substr($data, $offset, 100); if (preg_match('/^<(?:!DOCTYPE\s+html|html|body|head|table|div|title|p|link|meta)[\s>]/si', $prefix)) { return "text/html"; } elseif (preg_match('/^\/]/', $prefix)) { return "application/rss+xml"; - } elseif (preg_match('/^<(?:[A-Za-z0-9\-\._]+:)?(feed|RDF)\s/', $prefix)) { + } elseif (preg_match('/^<(?:[A-Za-z0-9\-\._]+:)?(feed|RDF)\s/', $prefix, $match)) { if ($match[1] === "feed") { return "application/atom+xml"; } else { - return "applicatiojn/rdf+xml"; + return "application/rdf+xml"; } } else { // FIIXME: Is there a better fallback that could used here? - "application/xml"; + return "application/xml"; } } else { return "application/octet-stream"; diff --git a/tests/cases/Parser/ParserTest.php b/tests/cases/Parser/ParserTest.php index ff84757..63321c4 100644 --- a/tests/cases/Parser/ParserTest.php +++ b/tests/cases/Parser/ParserTest.php @@ -46,8 +46,22 @@ class ParserTest extends \PHPUnit\Framework\TestCase { public function provideDetectableContent(): iterable { return [ - ['{""}', "application/json"], - [" \n {\"v", "application/json"], + ['{""}', "application/json"], + [" \n {\"v", "application/json"], + ["", "text/html"], + ["", "text/html"], + [" \n \n ", "application/rss+xml"], + [" \n ", "application/rss+xml"], + ["", "application/xml"], + ["plain text", "application/octet-stream"], ]; } } \ No newline at end of file