Skip to content

Commit 1a99c17

Browse files
authored
Merge pull request #8 from JuliaWeb/mime_from_contenttype
2 parents c3ce1f5 + 01ebfe6 commit 1a99c17

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ A small package to transform between file extensions and MIME types, with bonus
55
```julia
66
julia> using MIMEs
77

8+
### For filename extensions:
89
julia> m = mime_from_extension(".json")
910
MIME type application/json
1011

1112
julia> extension_from_mime(m)
1213
".json"
1314

15+
16+
### For web servers:
1417
julia> compressible_from_mime(m) # whether content of this MIME can/should be gzipped
1518
true
1619

@@ -19,6 +22,9 @@ julia> charset_from_mime(m)
1922

2023
julia> contenttype_from_mime(m) # the Content-Type HTTP header
2124
"application/json; charset=utf-8"
25+
26+
julia> mime_from_contenttype("application/json; charset=utf-8")
27+
MIME type application/json
2228
```
2329

2430
# Implementation

src/MIMEs.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module MIMEs
22

3-
export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime
3+
export mime_from_extension, mime_from_path, extension_from_mime, charset_from_mime, compressible_from_mime, contenttype_from_mime, mime_from_contenttype
44

55
const _mimedb, _ext2mime, _mime2ext = include(joinpath(@__DIR__, "..", "mimedb", "mimedb.jlon"))
66

@@ -147,11 +147,35 @@ contenttype_from_mime(mime_from_extension(".png", MIME"application/octet-stream"
147147
```
148148
149149
# See also:
150-
[`charset_from_mime`](@ref)
150+
[`charset_from_mime`](@ref), [`mime_from_contenttype`](@ref)
151151
"""
152152
contenttype_from_mime(mime::MIME) = let c = charset_from_mime(mime)
153153
c === nothing ? string(mime) : "$(string(mime)); charset=$(lowercase(c))"
154154
end
155155

156156

157+
"""
158+
```julia
159+
mime_from_contenttype(content_type::String[, default::T=nothing])::Union{MIME,T}
160+
```
161+
162+
Extract a MIME from a Content-Type header value. If the input is empty, `default` is returned.
163+
164+
# Examples:
165+
```julia
166+
mime_from_contenttype("application/json; charset=utf-8") == MIME"application/json"()
167+
mime_from_contenttype("application/x-bogus") == MIME"application/x-bogus"()
168+
mime_from_contenttype("") == nothing
169+
mime_from_contenttype("", MIME"application/octet-stream"()) == MIME"application/octet-stream"()
170+
```
171+
172+
# See also:
173+
[`contenttype_from_mime`](@ref)
174+
"""
175+
function mime_from_contenttype(content_type::String, default=nothing)
176+
result = strip(split(content_type, ';')[1])
177+
isempty(result) ? default : MIME(result)
178+
end
179+
180+
157181
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ sub(s) = SubString(s, 1)
3838
@test contenttype_from_mime(MIME"application/x-bogus"()) == "application/x-bogus"
3939
@test contenttype_from_mime(mime_from_extension(".png", MIME"application/octet-stream"())) == "image/png"
4040

41+
42+
@test mime_from_contenttype("application/json; charset=utf-8") == MIME"application/json"()
43+
@test mime_from_contenttype("application/x-bogus") == MIME"application/x-bogus"()
44+
@test mime_from_contenttype("") == nothing
45+
@test mime_from_contenttype("", MIME"application/octet-stream"()) == MIME"application/octet-stream"()
46+
4147
# from https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
4248
const mdn = Dict(
4349
".bin" => "application/octet-stream",

0 commit comments

Comments
 (0)