#line 766 "/home/travis/build/felix-lang/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 {
  /*
  TODO: implement more MIME types.
  */
  
    open WebUtil;
    union application_mime_subtype =
      | atom_PLUS_xml //: Atom feeds
      | ecmascript // ECMAScript/JavaScript; Defined in RFC 4329
      | EDI_DASH_X12 // EDI X12 data; Defined in RFC 1767
      | EDIFACT  //EDI EDIFACT data; Defined in RFC 1767
      | json // JavaScript Object Notation JSON; Defined in RFC 4627
      | javascript // ECMAScript/JavaScript; Defined in RFC 4329
      | octet_DASH_stream // Arbitrary binary data.
      | ogg // Ogg, a multimedia bitstream container format;
      | pdf // Portable Document Format,
      | postscript // PostScript; Defined in RFC 2046
      | rss_PLUS_xml // RSS feeds
      | soap_PLUS_xml //SOAP; Defined by RFC 3902
      | font_DASH_woff //: Web Open Font Format;
      | xhtml_PLUS_xml //: XHTML; Defined by RFC 3236
      | xml_DASH_dtd //: DTD files; Defined by RFC 3023
      | xop_PLUS_xml //:XOP
      | zip //: ZIP archive files; Registered[7]
      | x_DASH_gzip //: Gzip
      | x_DASH_www_DASH_form_DASH_urlencoded;
  
    union audio_mime_subtype =
      | basic //: mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046
      | L24 //: 24bit Linear PCM audio at 8-48kHz, 1-N channels; Defined in RFC 3190
      | mp4 //: MP4 audio
      | mpeg //: MP3 or other MPEG audio; Defined in RFC 3003
      | ogg1 //: Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334
      | vorbis //: Vorbis encoded audio; Defined in RFC 5215
      | x_DASH_ms_DASH_wma //: Windows Media Audio; Documented in Microsoft KB 288102
      | x_DASH_ms_DASH_wax //: Windows Media Audio Redirector
      | vnd_DOT_rn_DASH_realaudio //: RealAudio; Documented in RealPlayer
      | vnd_DOT_wave //: WAV audio; Defined in RFC 2361
      | webm //: WebM open media format
    ;
  
    union image_mime_subtype =
      | gif //: GIF image; Defined in RFC 2045 and RFC 2046
      | jpeg // JPEG JFIF image; Defined in RFC 2045 and RFC 2046
      | pjpeg //: JPEG JFIF image; Associated with Internet Explorer;
      | png //: Portable Network Graphics; Registered,[8] Defined in RFC 2083
      | svg_PLUS_xml //: SVG vector image; Defined in SVG Tiny 1.2 Specification Appendix M
      | tiff // Tag Image File Format (only for Baseline TIFF); Defined in RFC 3302
      | vnd_DOT_microsoft_DOT_icon //: ICO image; Registered[9]
    ;
  
    union text_mime_subtype =
      | cmd //: commands; subtype resident in Gecko browsers like Firefox 3.5
      | css //: Cascading Style Sheets; Defined in RFC 2318
      | csv //: Comma-separated values; Defined in RFC 4180
      | html //: HTML; Defined in RFC 2854
      | javascript1 //(Obsolete): JavaScript; Defined in and obsoleted by RFC 4329
      | plain //: Textual data; Defined in RFC 2046 and RFC 3676
      | vcard //: vCard (contact information); Defined in RFC 6350
      | xml //: Extensible Markup Language; Defined in RFC 3023
      | x_DASH_felix
      | x_DASH_fdoc
      | x_DASH_fpc
      | x_DASH_c
      | x_DASH_ocaml
      | x_DASH_python
    ;
  
    union multipart_mime_subtype =
      | mixed
      | alternative
      | related
      | form-data
      | signed
      | encrypted;
  
    union 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 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
    ;
  
  //instance Tord[test_mime_subtype] {
  //    fun eq: t * t -> bool = "$1==$2";
  //}
  //open Tord[text_mime_subtype];
  open Tord[mime_type];
  /*
  Other unimplemented types:
  Type message
  message/http: Defined in RFC 2616
  message/imdn+xml: IMDN Instant Message Disposition Notification; Defined in RFC 5438
  message/partial: Email; Defined in RFC 2045 and RFC 2046
  message/rfc822: Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC 2045 and RFC 2046
  Type model
  For 3D models.
  model/example: Defined in RFC 4735
  model/iges: IGS files, IGES files; Defined in RFC 2077
  model/mesh: MSH files, MESH files; Defined in RFC 2077, SILO files
  model/vrml: WRL files, VRML files; Defined in RFC 2077
  model/x3d+binary: X3D ISO standard for representing 3D computer graphics, X3DB binary files
  model/x3d+vrml: X3D ISO standard for representing 3D computer graphics, X3DV VRML files
  model/x3d+xml: X3D ISO standard for representing 3D computer graphics, X3D XML files
  Type multipart
  Type video
  For video.
  video/mpeg: MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046
  video/mp4: MP4 video; Defined in RFC 4337
  video/ogg: Ogg Theora or other video (with audio); Defined in RFC 5334
  video/quicktime: QuickTime video; Registered[10]
  video/webm: WebM Matroska-based open media format
  video/x-matroska: Matroska open media format
  video/x-ms-wmv: Windows Media Video; Documented in Microsoft KB 288102
  Type vnd
  For vendor-specific files.
  application/vnd.oasis.opendocument.text: OpenDocument Text; Registered[11]
  application/vnd.oasis.opendocument.spreadsheet: OpenDocument Spreadsheet; Registered[12]
  application/vnd.oasis.opendocument.presentation: OpenDocument Presentation; Registered[13]
  application/vnd.oasis.opendocument.graphics: OpenDocument Graphics; Registered[14]
  application/vnd.ms-excel: Microsoft Excel files
  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet: Microsoft Excel 2007 files
  application/vnd.ms-powerpoint: Microsoft Powerpoint files
  application/vnd.openxmlformats-officedocument.presentationml.presentation: Microsoft Powerpoint 2007 files
  application/msword: Microsoft Word files
  application/vnd.openxmlformats-officedocument.wordprocessingml.document: Microsoft Word 2007 files
  application/vnd.mozilla.xul+xml: Mozilla XUL files
  application/vnd.google-earth.kml+xml: KML files (e.g. for Google Earth)
  Type x
  For non-standard files.
  application/x-www-form-urlencoded Form Encoded Data; Documented in HTML 4.01 Specification, Section 17.13.4.1
  application/x-dvi: device-independent document in DVI format
  application/x-latex: LaTeX files
  application/x-font-ttf: TrueType Font No registered MIME type, but this is the most commonly used
  application/x-shockwave-flash: Adobe Flash files for example with the extension .swf
  application/x-stuffit: StuffIt archive files
  application/x-rar-compressed: RAR archive files
  application/x-tar: Tarball files
  text/x-gwt-rpc: GoogleWebToolkit data
  text/x-jquery-tmpl: jQuery template data
  application/x-javascript:
  application/x-deb: deb_(file_format), a software package format used by the Debian project
  [edit]Type x-pkcs
  For PKCS standard files.
  application/x-pkcs12: p12 files
  application/x-pkcs12: pfx files
  application/x-pkcs7-certificates: p7b files
  application/x-pkcs7-certificates: spc files
  application/x-pkcs7-certreqresp: p7r files
  application/x-pkcs7-mime: p7c files
  application/x-pkcs7-mime: p7m files
  application/x-pkcs7-signature: p7s files
  */
  }