mirror of
https://github.com/KevinMidboe/linguist.git
synced 2026-02-13 19:59:34 +00:00
Set content-type to text/plain for all non binary files
This commit is contained in:
@@ -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
|
|
||||||
@@ -51,36 +51,6 @@ module Linguist
|
|||||||
mime_type ? mime_type.simplified : 'text/plain'
|
mime_type ? mime_type.simplified : 'text/plain'
|
||||||
end
|
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.
|
# Internal: Determine if extension or mime type is binary.
|
||||||
#
|
#
|
||||||
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns a content type String.
|
# Returns a content type String.
|
||||||
def content_type
|
def content_type
|
||||||
@content_type ||= Mime.content_type_for(extname)
|
@content_type ||= binary? ? mime_type : 'text/plain'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Is the path binary?
|
# Public: Is the path binary?
|
||||||
|
|||||||
@@ -31,12 +31,17 @@ class TestBlob < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_content_type
|
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 "application/octet-stream", blob("dog.o").content_type
|
||||||
assert_equal "text/plain; charset=utf-8", blob("script.sh").content_type
|
assert_equal "application/pdf", blob("foo.pdf").content_type
|
||||||
assert_equal "text/plain; charset=utf-8", blob("README").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
|
end
|
||||||
|
|
||||||
def test_disposition
|
def test_disposition
|
||||||
|
|||||||
@@ -37,30 +37,6 @@ class TestMime < Test::Unit::TestCase
|
|||||||
assert_equal 'application/java-archive', Mime.mime_for(".war")
|
assert_equal 'application/java-archive', Mime.mime_for(".war")
|
||||||
end
|
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
|
def test_binary
|
||||||
assert !Mime.binary?(nil)
|
assert !Mime.binary?(nil)
|
||||||
assert !Mime.binary?("")
|
assert !Mime.binary?("")
|
||||||
|
|||||||
@@ -59,8 +59,8 @@ class TestPathname < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_content_type
|
def test_content_type
|
||||||
assert_equal 'text/plain; charset=utf-8', Pathname.new("file.txt").content_type
|
assert_equal 'text/plain', 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.rb").content_type
|
||||||
assert_equal 'image/png', Pathname.new("octocat.png").content_type
|
assert_equal 'image/png', Pathname.new("octocat.png").content_type
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user