CX · Reference guidemime
PlaygroundDownloadsAboutv0.18.0-pre.1

mime

MIME data-shape handling: a built-in extension-to-type registry, Content-Type and Content-Disposition parsing, multipart boundary generation, type classification, and Accept-header content negotiation. The registry covers ~200 common extensions offline and is extensible at runtime. The data-shape companion to cx-stdlib/url, composing with cx-stdlib/email for multipart messages.

mime:type-for-extension

[$mime:type-for-extension] -> string — Look up the canonical MIME type for a file extension (dot optional); 'application/octet-stream' if unknown.

      [?lib 'cx-stdlib/mime']
[$mime:type-for-extension ".txt"]
    
      text/plain
    

mime:extension-for-type

[$mime:extension-for-type] -> string — Return the most common file extension for a MIME type; empty string if unknown.

      [?lib 'cx-stdlib/mime']
[$mime:extension-for-type "image/jpeg"]
    
      '.jpg'
    

mime:all-extensions-for-type

[$mime:all-extensions-for-type] -> [sequence string] — Return every known file extension for a MIME type.

      [?lib 'cx-stdlib/mime']
[$count [$mime:all-extensions-for-type "image/jpeg"]]
    
      2
    

mime:register-type

[$mime:register-type] -> null — Add one extension-to-type mapping to the process-wide registry, overriding any built-in.

      [?lib 'cx-stdlib/mime']
[?let [= $_ [$mime:register-type ".avif" "image/avif"]] [$mime:type-for-extension ".avif"]]
    
      image/avif
    

mime:load-mime-types

[$mime:load-mime-types] -> int — Merge a mime.types file into the registry and return the count loaded; raises CXER2804 on a missing or malformed file.

      [?lib 'cx-stdlib/mime']
[$mime:load-mime-types "/no/such/mime.types/file/xyz"]
    
      cx-err:CXER2804
    

mime:parse-content-type

[$mime:parse-content-type] -> element — Parse a Content-Type header into a [content-type] element; raises CXER2800 on parse failure.

      [?lib 'cx-stdlib/mime']
[?let [= $p [$mime:parse-content-type "text/html; charset=utf-8"]] $p/@type]
    
      text
    

mime:format-content-type

[$mime:format-content-type] -> string — Serialize a parsed content-type back to a header string, quoting values where required.

      [?lib 'cx-stdlib/mime']
[$mime:format-content-type [$mime:parse-content-type "text/html"]]
    
      text/html
    

mime:get-parameter

[$mime:get-parameter] -> string — Return a named parameter's value from a parsed content-type; empty string if absent.

      [?lib 'cx-stdlib/mime']
[$mime:get-parameter [$mime:parse-content-type "text/html; charset=\"UTF-8\""] "charset"]
    
      UTF-8
    

mime:parse-content-disposition

[$mime:parse-content-disposition] -> element — Parse a Content-Disposition header into a [content-disposition] element; raises CXER2801 on parse failure.

      [?lib 'cx-stdlib/mime']
[?let [= $p [$mime:parse-content-disposition "attachment; filename=\"report.pdf\""]] $p/@type]
    
      attachment
    

mime:format-content-disposition

[$mime:format-content-disposition] -> string — Serialize a parsed content-disposition, emitting an RFC 5987 filename* plus ASCII fallback for non-ASCII names.

      [?lib 'cx-stdlib/mime']
[$mime:disposition-filename [$mime:parse-content-disposition [$mime:format-content-disposition [$mime:parse-content-disposition "attachment; filename=\"report.pdf\""]]]]
    
      'report.pdf'
    

mime:disposition-filename

[$mime:disposition-filename] -> string — Return the decoded filename, preferring filename* over filename; raises CXER2803 on a malformed filename*.

      [?lib 'cx-stdlib/mime']
[$mime:disposition-filename [$mime:parse-content-disposition "attachment; filename=\"report.pdf\""]]
    
      'report.pdf'
    

mime:multipart-boundary

[$mime:multipart-boundary] -> string — Generate a fresh 24-char multipart boundary ('=_Part_' prefix plus 17 random hex chars).

      [?lib 'cx-stdlib/mime']
[?lib 'cx-stdlib/strings']
[$strings:starts-with [$mime:multipart-boundary] "=_Part_"]
    
      true
    

mime:is-valid-boundary

[$mime:is-valid-boundary] -> bool — Report whether a string is a valid multipart boundary per RFC 2046 (1-70 chars, restricted set).

      [?lib 'cx-stdlib/mime']
[$mime:is-valid-boundary "----=_Part_12345"]
    
      true
    

mime:is-text-type

[$mime:is-text-type] -> bool — Report whether a MIME type has the text top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-text-type "text/html"]
    
      true
    

mime:is-binary-type

[$mime:is-binary-type] -> bool — Report whether a MIME type denotes binary (non-text) content.

      [?lib 'cx-stdlib/mime']
[$mime:is-binary-type "application/pdf"]
    
      true
    

mime:is-image-type

[$mime:is-image-type] -> bool — Report whether a MIME type has the image top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-image-type "image/svg+xml"]
    
      true
    

mime:is-audio-type

[$mime:is-audio-type] -> bool — Report whether a MIME type has the audio top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-audio-type "audio/mpeg"]
    
      true
    

mime:is-video-type

[$mime:is-video-type] -> bool — Report whether a MIME type has the video top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-video-type "video/mp4"]
    
      true
    

mime:is-message-type

[$mime:is-message-type] -> bool — Report whether a MIME type has the message top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-message-type "message/rfc822"]
    
      true
    

mime:is-multipart-type

[$mime:is-multipart-type] -> bool — Report whether a MIME type has the multipart top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-multipart-type "multipart/mixed"]
    
      true
    

mime:is-application-type

[$mime:is-application-type] -> bool — Report whether a MIME type has the application top-level type.

      [?lib 'cx-stdlib/mime']
[$mime:is-application-type "application/pdf"]
    
      true
    

mime:is-structured-syntax

[$mime:is-structured-syntax] -> string — Return the structured-syntax suffix per RFC 6838 (svg+xml gives 'xml'); empty string if none.

      [?lib 'cx-stdlib/mime']
[$mime:is-structured-syntax "image/svg+xml"]
    
      xml
    

mime:charset-of

[$mime:charset-of] -> string — Extract the charset parameter from a parsed content-type; empty string if absent.

      [?lib 'cx-stdlib/mime']
[$mime:charset-of [$mime:parse-content-type "text/html; charset=utf-8"]]
    
      utf-8
    

mime:with-charset

[$mime:with-charset] -> element — Return a new content-type element with the charset parameter set, replacing any existing.

      [?lib 'cx-stdlib/mime']
[$mime:charset-of [$mime:with-charset [$mime:parse-content-type "text/html"] "iso-8859-1"]]
    
      iso-8859-1
    

mime:parse-accept

[$mime:parse-accept] -> [sequence element] — Parse an Accept header into [accept] elements sorted by q-value then specificity.

      [?lib 'cx-stdlib/mime']
[$count [$mime:parse-accept "text/html;q=0.9, */*;q=0.8"]]
    
      2
    

mime:match-accept

[$mime:match-accept] -> string — Return the best-matching offered MIME type for an Accept header per RFC 7231; empty if none acceptable.

      [?lib 'cx-stdlib/mime']
[$mime:match-accept "text/*;q=0.5, text/html;q=0.9" ["text/plain", "text/html"]]
    
      text/html