#line 766 "/home/ubuntu/felix/src/packages/web.fdoc"
publish """
Implements variant types representing MIME types.
Also implements Str instance for mime types and parses MIME type from string
Example:
open MIMETypes;
println (javascript);
println from_str("application/atom+xml");
println (application zip);
"""
class MIMEType {
open WebUtil;
variant application_mime_subtype =
| atom_PLUS_xml
| ecmascript
| EDI_DASH_X12
| EDIFACT
| json
| javascript
| octet_DASH_stream
| ogg
| pdf
| postscript
| rss_PLUS_xml
| soap_PLUS_xml
| font_DASH_woff
| xhtml_PLUS_xml
| xml_DASH_dtd
| xop_PLUS_xml
| zip
| x_DASH_gzip
| x_DASH_www_DASH_form_DASH_urlencoded;
variant audio_mime_subtype =
| basic
| L24
| mp4
| mpeg
| ogg1
| vorbis
| x_DASH_ms_DASH_wma
| x_DASH_ms_DASH_wax
| vnd_DOT_rn_DASH_realaudio
| vnd_DOT_wave
| webm
;
variant image_mime_subtype =
| gif
| jpeg
| pjpeg
| png
| svg_PLUS_xml
| tiff
| vnd_DOT_microsoft_DOT_icon
;
variant text_mime_subtype =
| cmd
| css
| csv
| html
| javascript1
| plain
| vcard
| xml
| x_DASH_felix
| x_DASH_fdoc
| x_DASH_fpc
| x_DASH_c
| x_DASH_ocaml
| x_DASH_python
;
variant multipart_mime_subtype =
| mixed
| alternative
| related
| form-data
| signed
| encrypted;
variant mime_type =
| application of application_mime_subtype
| audio of audio_mime_subtype
| image of image_mime_subtype
| text of text_mime_subtype
| multipart of multipart_mime_subtype;
typedef media_type = mime_type * list[string^2];
instance Str[application_mime_subtype] {
fun str : application_mime_subtype ->string =
| #atom_PLUS_xml => "application/atom+xml"
| #ecmascript => "application/ecmascript"
| #EDI_DASH_X12 => "application/EDI-X12"
| #EDIFACT => "application/EDIFACT"
| #json => "application/json"
| #javascript => "application/javascript"
| #octet_DASH_stream => "application/octet-stream"
| #ogg => "application/ogg"
| #pdf => "application/pdf"
| #postscript => "appliction/postscript"
| #rss_PLUS_xml => "application/rss+xml"
| #soap_PLUS_xml => "application/soap+xml"
| #font_DASH_woff => "application/font-woff"
| #xhtml_PLUS_xml => "application/xhtml+xml"
| #xml_DASH_dtd => "application/xml-dtd"
| #xop_PLUS_xml => "application/xop+xml"
| #zip => "application/zip"
| #x_DASH_gzip => "application/x-gzip"
| #x_DASH_www_DASH_form_DASH_urlencoded => "application/x-www-form-urlencoded";
}
instance Str[audio_mime_subtype] {
fun str : audio_mime_subtype ->string =
| #basic => "audio/basic"
| #L24 => "audio/L24"
| #mp4 => "audio/mp4"
| #mpeg => "audio/mpeg"
| #ogg1 => "audop/ogg"
| #vorbis => "audio/vorbis"
| #x_DASH_ms_DASH_wma => "audio/x-ms-wma"
| #x_DASH_ms_DASH_wax => "audio/x-ms-wax"
| #vnd_DOT_rn_DASH_realaudio => "audio/vnd.rn-realaudio"
| #vnd_DOT_wave => "audio/vnd.wave"
| #webm => "audio/webm";
}
instance Str[image_mime_subtype] {
fun str : image_mime_subtype ->string =
| #gif => "image/gif"
| #jpeg => "image/jpeg"
| #pjpeg => "image/pjpeg"
| #png => "image/png"
| #svg_PLUS_xml => "image/svg+xml"
| #tiff => "image/tiff"
| #vnd_DOT_microsoft_DOT_icon => "image/vnd.microsoft.icon";
}
instance Str[text_mime_subtype] {
fun str : text_mime_subtype ->string =
| #cmd => "text/cmd"
| #css => "text/css"
| #csv => "text/csv"
| #html => "text/html"
| #javascript1 => "text/javascript"
| #plain => "text/plain"
| #vcard => "text/vcard"
| #xml => "text/xml"
| #x_DASH_felix => "text/x-felix"
| #x_DASH_fdoc => "text/x-fdoc"
| #x_DASH_fpc => "text/x-fpc"
| #x_DASH_c => "text/x-c"
| #x_DASH_ocaml => "text/x-ocaml"
| #x_DASH_python => "text/x-python";
}
instance Str[multipart_mime_subtype] {
fun str : multipart_mime_subtype ->string =
| #mixed => "multipart/mixed"
| #alternative => "multipart/alternative"
| #related => "multipart/related"
| #form-data => "multipart/form-data"
| #signed => "multipart/signed"
| #encrypted => "multipart/encrypted";
}
instance Str[mime_type] {
fun str : mime_type ->string =
| application a => str a
| audio a => str a
| image a => str a
| text a => str a
| multipart a => str a;
}
fun application_type_from_str : string -> opt[application_mime_subtype] =
| "application/atom+xml" => Some atom_PLUS_xml
| "application/ecmascript" => Some ecmascript
| "application/EDI-X12" => Some EDI_DASH_X12
| "application/EDIFACT" => Some EDIFACT
| "application/json" => Some json
| "application/javascript" => Some javascript
| "application/octet-stream" => Some octet_DASH_stream
| "application/ogg" => Some ogg
| "application/pdf" => Some pdf
| "appliction/postscript" => Some postscript
| "application/rss+xml" => Some rss_PLUS_xml
| "application/soap+xml" => Some soap_PLUS_xml
| "application/font-woff" => Some font_DASH_woff
| "application/xhtml+xml" => Some xhtml_PLUS_xml
| "application/xml-dtd" => Some xml_DASH_dtd
| "application/xop+xml" => Some xop_PLUS_xml
| "application/zip" => Some zip
| "application/x-gzip" => Some x_DASH_gzip
| "application/x-www-form-urlencoded" => Some x_DASH_www_DASH_form_DASH_urlencoded
| _ => None[application_mime_subtype];
fun audio_type_from_str : string -> opt[audio_mime_subtype] =
| "audio/basic" => Some basic
| "audio/L24" => Some L24
| "audio/mp4" => Some mp4
| "audio/mpeg" => Some mpeg
| "audop/ogg" => Some ogg1
| "audio/vorbis" => Some vorbis
| "audio/x-ms-wma" => Some x_DASH_ms_DASH_wma
| "audio/x-ms-wax" => Some x_DASH_ms_DASH_wax
| "audio/vnd.rn-realaudio" => Some vnd_DOT_rn_DASH_realaudio
| "audio/vnd.wave" => Some vnd_DOT_wave
| "audio/webm" => Some webm
| _ => None[audio_mime_subtype] ;
fun image_type_from_str : string -> opt[image_mime_subtype] =
| "image/gif" => Some gif
| "image/jpeg" => Some jpeg
| "image/pjpeg" => Some pjpeg
| "image/png" => Some png
| "image/svg+xml" => Some svg_PLUS_xml
| "image/tiff" => Some tiff
| "image/vnd.microsoft.icon" => Some vnd_DOT_microsoft_DOT_icon
| _ => None[image_mime_subtype];
fun text_type_from_str : string -> opt[text_mime_subtype] =
| "text/cmd" => Some cmd
| "text/css" => Some css
| "text/csv" => Some csv
| "text/html" => Some html
| "text/javascript" => Some javascript1
| "text/plain" => Some plain
| "text/vcard" => Some vcard
| "text/xml" => Some xml
| "text/x-felix" => Some x_DASH_felix
| "text/x-fdoc" => Some x_DASH_fdoc
| "text/x-fpc" => Some x_DASH_fpc
| "text/x-c" => Some x_DASH_c
| "text/x-ocaml" => Some x_DASH_ocaml
| "text/x-python" => Some x_DASH_python
| _ => None[text_mime_subtype];
fun multipart_type_from_str : string -> opt[multipart_mime_subtype] =
| "multipart/mixed" => Some mixed
| "multipart/alternative" => Some alternative
| "multipart/related" => Some related
| "multipart/form-data" => Some form-data
| "multipart/signed" => Some signed
| "multipart/encrypted" => Some encrypted
;
fun from_str (s:string):opt[mime_type] =>
match application_type_from_str s with
| Some t => Some (application t)
| #None => match audio_type_from_str s with
| Some t => Some (audio t)
| #None => match image_type_from_str s with
| Some t => Some (image t)
| #None => match text_type_from_str s with
| Some t => Some (text t)
| #None => match multipart_type_from_str s with
| Some t => Some (multipart t)
| #None => None[mime_type]
endmatch
endmatch
endmatch
endmatch
endmatch;
fun mime_type_from_file(file:string) =>
match unbox (rev(split(file,'.'))) with
| Cons(hd,_) => mime_type_from_extension hd
| _ => text plain
endmatch;
fun mime_type_from_extension: string -> mime_type =
| "atom" => application atom_PLUS_xml
| "ecma" => application ecmascript
| "json" => application json
| "js" => application javascript
| "application/octet-stream" => application octet_DASH_stream
| "ogg" => application ogg
| "ogx" => application ogg
| "pdf" => application pdf
| "ps" => application postscript
| "eps" => application postscript
| "ai" => application postscript
| "xhtml" => application xhtml_PLUS_xml
| "xht" => application xhtml_PLUS_xml
| "dtd" => application xml_DASH_dtd
| "xop" => application xop_PLUS_xml
| "zip" => application zip
| "x-gzip" => application x_DASH_gzip
| "au" => audio basic
| "snd" => audio basic
| "mp4a" => audio mp4
| "mpega" => audio mpeg
| "mpga" => audio mpeg
| "mp2a" => audio mpeg
| "mp3a" => audio mpeg
| "mp4a" => audio mpeg
| "mp2" => audio mpeg
| "mp3" => audio mpeg
| "ogg" => audio ogg1
| "oga" => audio ogg1
| "spx" => audio ogg1
| "wma" => audio x_DASH_ms_DASH_wma
| "wax" => audio x_DASH_ms_DASH_wax
| "ra" => audio vnd_DOT_rn_DASH_realaudio
| "wav" => audio vnd_DOT_wave
| "webma" => audio webm
| "gif" => image gif
| "image/jpeg" => image jpeg
| "jpg" => image jpeg
| "pjpeg" => image pjpeg
| "png" => image png
| "svg" => image svg_PLUS_xml
| "tiff" => image tiff
| "css" => text css
| "csv" => text csv
| "html" => text html
| "htm" => text html
| "shtm" => text html
| "text/plain" => text plain
| "asc" => text plain
| "conf" => text plain
| "def" => text plain
| "diff" => text plain
| "in" => text plain
| "list" => text plain
| "log" => text plain
| "pot" => text plain
| "text" => text plain
| "txt" => text plain
| _ => text plain
;
instance Eq[mime_type] {
fun == : mime_type * mime_type -> bool = "$1==$2";
}
fun parse_media_type (s:string):opt[media_type] =>
match split( s, ";") with
| Cons(h,t) =>
match from_str(h) with
| Some m => Some (m,parse_attribute_list(t))
| _ => None[media_type]
endmatch
| _ => None[media_type]
endmatch
;
open Tord[mime_type];
}