Parse and sniff MIME types the way browsers do, by the MIME Sniffing Standard.
A MIME type is a plain map:
The parameters can also be a vector of name-value pairs. Unlike the map, it keeps its order when you add a ninth parameter.
Every function that takes a MIME type also takes its text, and treats text that doesn't parse as nil.
Parse and sniff MIME types the way browsers do, by the MIME Sniffing Standard. A MIME type is a plain map: - :type and :subtype, in lower case, e.g. "text" and "html" - :parameters, when there are any, a map of lower-case names to values in the order of the text The parameters can also be a vector of name-value pairs. Unlike the map, it keeps its order when you add a ninth parameter. Every function that takes a MIME type also takes its text, and treats text that doesn't parse as nil.
(archive? m)Whether the MIME type m is an archive MIME type: ZIP, gzip or RAR.
Whether the MIME type `m` is an archive MIME type: ZIP, gzip or RAR.
(audio-or-video? m)Whether the MIME type m is of audio or video, application/ogg
included.
Whether the MIME type `m` is of audio or video, application/ogg included.
(charset m)The charset parameter of the MIME type m as it's written, or nil.
The charset parameter of the MIME type `m` as it's written, or nil.
(encoding m)The name of the encoding that the charset of the MIME type m names,
or nil.
The name of the encoding that the charset of the MIME type `m` names, or nil.
(essence m)The type and subtype of the MIME type m, e.g. text/html.
The type and subtype of the MIME type `m`, e.g. text/html.
(extract values)The MIME type of a response with the Content-Type values (a string or
a seq of strings), or nil when none of them parses.
It's the last value that parses, other than /. When it has no charset, it takes that of an earlier value with the same essence.
(extract ["text/html;charset=gbk;a=b" "text/html;x=y"])
;; => {:type "text" :subtype "html"
;; :parameters {"x" "y" "charset" "gbk"}}
The MIME type of a response with the Content-Type `values` (a string or
a seq of strings), or nil when none of them parses.
It's the last value that parses, other than */*. When it has no charset,
it takes that of an earlier value with the same essence.
(extract ["text/html;charset=gbk;a=b" "text/html;x=y"])
;; => {:type "text" :subtype "html"
;; :parameters {"x" "y" "charset" "gbk"}}(font? m)Whether the MIME type m is a font MIME type.
Whether the MIME type `m` is a font MIME type.
(html? m)Whether the MIME type m is text/html (application/xhtml+xml is XML).
Whether the MIME type `m` is text/html (application/xhtml+xml is XML).
(image? m)Whether the MIME type m is an image MIME type.
Whether the MIME type `m` is an image MIME type.
(javascript? m)Whether the MIME type m is a JavaScript MIME type, e.g.
text/javascript or application/x-javascript.
Whether the MIME type `m` is a JavaScript MIME type, e.g. text/javascript or application/x-javascript.
(json? m)Whether the MIME type m is a JSON MIME type, e.g. one with a +json
suffix.
Whether the MIME type `m` is a JSON MIME type, e.g. one with a +json suffix.
(label->encoding label)The name of the encoding that the text label names, e.g. windows-1252
for latin1, or nil.
The name of the encoding that the text `label` names, e.g. windows-1252 for latin1, or nil.
(nosniff? values)Whether the X-Content-Type-Options values (a string or a seq of
strings) forbid sniffing: whether the first is nosniff, in any case.
Whether the X-Content-Type-Options `values` (a string or a seq of strings) forbid sniffing: whether the first is nosniff, in any case.
(parse s)The MIME type in the text s, or nil when it doesn't parse.
(parse "Text/HTML; Charset="utf-8"") ;; => {:type "text" :subtype "html" ;; :parameters {"charset" "utf-8"}}
The MIME type in the text `s`, or nil when it doesn't parse.
(parse "Text/HTML; Charset=\"utf-8\"")
;; => {:type "text" :subtype "html"
;; :parameters {"charset" "utf-8"}}(scriptable? m)Whether the MIME type m is a scriptable MIME type: XML, HTML or PDF.
Whether the MIME type `m` is a scriptable MIME type: XML, HTML or PDF.
(serialize m)The MIME type m as text, with each parameter value that isn't a token
in quotes.
(serialize "Text/HTML; Charset=\"utf-8\"; x=(")
;; => "text/html;charset=utf-8;x=\"(\""
The MIME type `m` as text, with each parameter value that isn't a token
in quotes.
(serialize "Text/HTML; Charset=\"utf-8\"; x=(")
;; => "text/html;charset=utf-8;x=\"(\""(sniff b)(sniff b content-type)(sniff b content-type {:keys [no-sniff?] :as opts})The MIME type that a browser treats a resource as, by its first bytes
b and its content-type, with the opts:
The bytes are a byte array on the JVM and a Uint8Array in ClojureScript. Only the first 1445 count. The Content-Type is its values (as extract takes them), a MIME type or nil.
The result is the Content-Type unless it's missing or unknown, or it's an image, audio or video type that the bytes contradict. HTML and XML always stay.
(sniff (.getBytes "<!DOCTYPE html><title>Hi</title>") nil)
;; => {:type "text" :subtype "html"}
For an image or audio element, use the tables in patterns.
The MIME type that a browser treats a resource as, by its first bytes
`b` and its `content-type`, with the `opts`:
- :no-sniff?, when X-Content-Type-Options says nosniff
The bytes are a byte array on the JVM and a Uint8Array in ClojureScript.
Only the first 1445 count. The Content-Type is its values (as extract
takes them), a MIME type or nil.
The result is the Content-Type unless it's missing or unknown, or it's an
image, audio or video type that the bytes contradict. HTML and XML always
stay.
(sniff (.getBytes "<!DOCTYPE html><title>Hi</title>") nil)
;; => {:type "text" :subtype "html"}
For an image or audio element, use the tables in patterns.(valid? s)Whether HTTP's grammar allows the text s as a MIME type, e.g.
text/html but not text/html;.
Whether HTTP's grammar allows the text `s` as a MIME type, e.g. text/html but not text/html;.
(xml? m)Whether the MIME type m is an XML MIME type, e.g. one with a +xml
suffix.
Whether the MIME type `m` is an XML MIME type, e.g. one with a +xml suffix.
(zip-based? m)Whether the MIME type m is a ZIP-based MIME type, e.g. one with a +zip
suffix.
Whether the MIME type `m` is a ZIP-based MIME type, e.g. one with a +zip suffix.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |