From 734cc2939121ad4e9bbe149f7e013eb242139593 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 3 Jun 2011 11:21:16 -0500 Subject: [PATCH] Set content-type to text/plain for all non binary files --- lib/linguist/content_types.yml | 66 ---------------------------------- lib/linguist/mime.rb | 30 ---------------- lib/linguist/pathname.rb | 2 +- test/test_blob.rb | 15 +++++--- test/test_mime.rb | 24 ------------- test/test_pathname.rb | 4 +-- 6 files changed, 13 insertions(+), 128 deletions(-) delete mode 100644 lib/linguist/content_types.yml diff --git a/lib/linguist/content_types.yml b/lib/linguist/content_types.yml deleted file mode 100644 index c27a6ced..00000000 --- a/lib/linguist/content_types.yml +++ /dev/null @@ -1,66 +0,0 @@ -# MIME types and extensions to override when the raw blob is served - -# mime types -application/javascript: text/plain -application/perl: text/plain -application/python: text/plain -application/rdf+xml: text/plain -application/ruby: text/plain -application/sh: text/plain -application/tex: text/plain -application/xhtml+xml: text/plain -application/xml: text/plain -text/html: text/plain - -# binary files -a: application/octet-stream -air: application/octet-stream -blend: application/octet-stream -crx: application/octet-stream -deb: application/octet-stream -gem: application/octet-stream -graffle: application/octet-stream -gz: application/octet-stream -icns: application/octet-stream -ipa: application/octet-stream -lib: application/octet-stream -mcz: application/octet-stream -mov: application/octet-stream -mp3: application/octet-stream -nib: application/octet-stream -o: application/octet-stream -odp: application/octet-stream -ods: application/octet-stream -odt: application/octet-stream -ogg: application/octet-stream -ogv: application/octet-stream -otf: application/octet-stream -pfx: application/octet-stream -pigx: application/octet-stream -plgx: application/octet-stream -pptx: application/octet-stream -psd: application/octet-stream -sib: application/octet-stream -so: application/octet-stream -spl: application/octet-stream -sqlite3: application/octet-stream -swc: application/octet-stream -swf: application/octet-stream -tar: application/octet-stream -ucode: application/octet-stream -xpi: application/octet-stream -zip: application/octet-stream - -# plaintext -latex: text/plain -ms: text/plain -nc: text/plain -nim: text/plain -ps: text/plain -sc: text/plain -sh: text/plain -src: text/plain -tcl: text/plain -texi: text/plain -texinfo: text/plain -xul: text/plain diff --git a/lib/linguist/mime.rb b/lib/linguist/mime.rb index 2b2ff690..e8203169 100644 --- a/lib/linguist/mime.rb +++ b/lib/linguist/mime.rb @@ -51,36 +51,6 @@ module Linguist mime_type ? mime_type.simplified : 'text/plain' end - Special = YAML.load_file(File.expand_path("../content_types.yml", __FILE__)) - - # Internal: Look up Content-Type header to serve for extension. - # - # This value is used when serving raw blobs. - # - # /github/linguist/raw/master/lib/linguist/mime.rb - # - # ext - The extension String. May include leading "." - # - # Mime.content_type_for('.html') - # # => 'text/plain; charset=utf-8' - # - # Return Content-Type String otherwise falls back to - # 'text/plain; charset=utf-8'. - def self.content_type_for(ext) - ext ||= '' - - # Lookup mime type - type = mime_for(ext) - - # Substitute actual mime type if an override exists in content_types.yml - type = Special[type] || Special[ext.sub(/^\./, '')] || type - - # Append default charset to text files - type += '; charset=utf-8' if type =~ /^text\// - - type - end - # Internal: Determine if extension or mime type is binary. # # ext_or_mime_type - A file extension ".txt" or mime type "text/plain". diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index 3272427c..d3847f11 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -107,7 +107,7 @@ module Linguist # # Returns a content type String. def content_type - @content_type ||= Mime.content_type_for(extname) + @content_type ||= binary? ? mime_type : 'text/plain' end # Public: Is the path binary? diff --git a/test/test_blob.rb b/test/test_blob.rb index 867a29f4..6d9a1f0f 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -31,12 +31,17 @@ class TestBlob < Test::Unit::TestCase end def test_content_type - assert_equal "text/plain; charset=utf-8", blob("grit.rb").content_type - assert_equal "text/plain; charset=utf-8", blob("foo.pl").content_type - assert_equal "text/plain; charset=utf-8", blob("bar.xml").content_type assert_equal "application/octet-stream", blob("dog.o").content_type - assert_equal "text/plain; charset=utf-8", blob("script.sh").content_type - assert_equal "text/plain; charset=utf-8", blob("README").content_type + assert_equal "application/pdf", blob("foo.pdf").content_type + assert_equal "image/png", blob("foo.png").content_type + assert_equal "text/plain", blob("README").content_type + assert_equal "text/plain", blob("foo.html").content_type + assert_equal "text/plain", blob("foo.pl").content_type + assert_equal "text/plain", blob("foo.py").content_type + assert_equal "text/plain", blob("foo.rb").content_type + assert_equal "text/plain", blob("foo.sh").content_type + assert_equal "text/plain", blob("foo.xhtml").content_type + assert_equal "text/plain", blob("foo.xml").content_type end def test_disposition diff --git a/test/test_mime.rb b/test/test_mime.rb index 89aa7207..13782c3d 100644 --- a/test/test_mime.rb +++ b/test/test_mime.rb @@ -37,30 +37,6 @@ class TestMime < Test::Unit::TestCase assert_equal 'application/java-archive', Mime.mime_for(".war") end - def test_content_type - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(nil) - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for("") - - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".rb") - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".js") - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".py") - - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".kt") - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".html") - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".sh") - assert_equal 'text/plain; charset=utf-8', Mime.content_type_for(".latex") - - assert_equal 'application/octet-stream', Mime.content_type_for(".air") - assert_equal 'application/octet-stream', Mime.content_type_for(".dll") - assert_equal 'application/octet-stream', Mime.content_type_for(".dmg") - assert_equal 'application/octet-stream', Mime.content_type_for(".exe") - assert_equal 'application/octet-stream', Mime.content_type_for(".swf") - - assert_equal 'application/java-archive', Mime.content_type_for(".jar") - assert_equal 'application/java-archive', Mime.content_type_for(".ear") - assert_equal 'application/java-archive', Mime.content_type_for(".war") - end - def test_binary assert !Mime.binary?(nil) assert !Mime.binary?("") diff --git a/test/test_pathname.rb b/test/test_pathname.rb index 310394e5..881c2a17 100644 --- a/test/test_pathname.rb +++ b/test/test_pathname.rb @@ -59,8 +59,8 @@ class TestPathname < Test::Unit::TestCase end def test_content_type - assert_equal 'text/plain; charset=utf-8', Pathname.new("file.txt").content_type - assert_equal 'text/plain; charset=utf-8', Pathname.new("file.rb").content_type + assert_equal 'text/plain', Pathname.new("file.txt").content_type + assert_equal 'text/plain', Pathname.new("file.rb").content_type assert_equal 'image/png', Pathname.new("octocat.png").content_type end end