Qore Mime Module Reference  1.3.1
 All Classes Namespaces Functions Variables Groups Pages
Mime.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file Mime.qm Mime module definition
3 
4 /* Mime.qm Copyright 2012 - 2013 David Nichols
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 
26 
27 /* Version History
28  * 2013-04-05 v1.3.1: David Nichols <david@qore.org>
29  + added get_mime_type_from_ext()
30  + minor corrections
31  + added constants for css and js files + more extension -> mime type mappings
32 
33  * 2012-11-02 v1.3: David Nichols <david@qore.org>
34  + made get MultiPartMessage::getMsgAndHeaders() abstract as originally intended
35  + added MultiPartMessage::parseBody() static method
36 
37  * 2012-09-17 v1.2.1: David Nichols <david@qore.org>
38  + fixed q-encoding mail headers (always encode '_' chars otherwise they will be decoded as spaces)
39 
40  * 2012-06-14 v1.2: David Nichols <david@qore.org>
41  + added MIME decoding functions
42 
43  * 2012-05-31 v1.1: David Nichols <david@qore.org>
44  + added initial MultiPartMessage classes
45 
46  * 2012-05-24 v1.0: David Nichols <david@qore.org>
47  + pulled MIME implementation out of other code and made a user module to aggregate MIME definitions and functions
48 
49  based on:
50  - http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
51  - http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
52  - http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
53 */
54 
94 
95 namespace Mime {
100 
101  const MimeTypeUnknown = "application/octet-stream";
102 
104  const MimeTypeText = "text/plain";
105 
107  const MimeTypeHtml = "text/html";
108 
110  const MimeTypeCsv = "text/csv";
111 
113  const MimeTypeJpeg = "image/jpeg";
114 
116  const MimeTypePng = "image/png";
117 
119  const MimeTypeSoapXml = "application/soap+xml";
120 
122  const MimeTypeYaml = "text/x-yaml";
123 
125  const MimeTypeXml = "text/xml";
126 
128  const MimeTypeXmlApp = "application/xml";
129 
131  const MimeTypeJson = "application/json";
132 
135 
138 
140  const MimeTypeYamlRpc = "application/x-yaml";
141 
143  const MimeTypeJavascript = "application/javascript";
144 
146  const MimeTypeCss = "text/css";
147 
149 
151  const MimeTypes = (
152  "7z": "application/x-7z-compressed",
153  "ai": "application/postscript",
154  "aif": "audio/x-aiff",
155  "aifc": "audio/x-aiff",
156  "aiff": "audio/x-aiff",
157  "asc": MimeTypeText,
158  "atom": "application/atom+xml",
159  "au": "audio/basic",
160  "avi": "video/x-msvideo",
161  "bcpio": "application/x-bcpio",
162  "bin": MimeTypeUnknown,
163  "bmp": "image/bmp",
164  "bz2": "application/x-bzip",
165  "cc": MimeTypeText,
166  "cdf": "application/x-netcdf",
167  "cgm": "image/cgm",
168  "class": MimeTypeUnknown,
169  "cpio": "application/x-cpio",
170  "cpp": MimeTypeText,
171  "cpt": "application/mac-compactpro",
172  "csh": "application/x-csh",
173  "css": MimeTypeCss,
174  "csv": MimeTypeCsv,
175  "dcr": "application/x-director",
176  "dif": "video/x-dv",
177  "dir": "application/x-director",
178  "djv": "image/vnd.djvu",
179  "djvu": "image/vnd.djvu",
180  "dll": MimeTypeUnknown,
181  "dmg": MimeTypeUnknown,
182  "dms": MimeTypeUnknown,
183  "doc": "application/msword",
184  "dtd": "application/xml-dtd",
185  "dv": "video/x-dv",
186  "dvi": "application/x-dvi",
187  "dxr": "application/x-director",
188  "eps": "application/postscript",
189  "etx": "text/x-setext",
190  "exe": MimeTypeUnknown,
191  "ez": "application/andrew-inset",
192  "gif": "image/gif",
193  "gram": "application/srgs",
194  "grxml": "application/srgs+xml",
195  "gtar": "application/x-gtar",
196  "gz": "application/x-gzip",
197  "h": MimeTypeText,
198  "hdf": "application/x-hdf",
199  "hh": MimeTypeText,
200  "hpp": MimeTypeText,
201  "hqx": "application/mac-binhex40",
202  "htm": MimeTypeHtml,
203  "html": MimeTypeHtml,
204  "ice": "x-conference/x-cooltalk",
205  "ico": "image/x-icon",
206  "ics": "text/calendar",
207  "ief": "image/ief",
208  "ifb": "text/calendar",
209  "iges": "model/iges",
210  "igs": "model/iges",
211  "jar": "application/java-archive",
212  "java": MimeTypeText,
213  "jnlp": "application/x-java-jnlp-file",
214  "jp2": "image/jp2",
215  "jpe": MimeTypeJpeg,
216  "jpeg": MimeTypeJpeg,
217  "jpg": MimeTypeJpeg,
218  "js": MimeTypeJavascript,
219  "kar": "audio/midi",
220  "latex": "application/x-latex",
221  "lha": MimeTypeUnknown,
222  "lzh": MimeTypeUnknown,
223  "m3u": "audio/x-mpegurl",
224  "m4a": "audio/mp4a-latm",
225  "m4b": "audio/mp4a-latm",
226  "m4p": "audio/mp4a-latm",
227  "m4u": "video/vnd.mpegurl",
228  "m4v": "video/x-m4v",
229  "mac": "image/x-macpaint",
230  "man": "application/x-troff-man",
231  "mathml": "application/mathml+xml",
232  "me": "application/x-troff-me",
233  "mesh": "model/mesh",
234  "mid": "audio/midi",
235  "midi": "audio/midi",
236  "mif": "application/vnd.mif",
237  "mov": "video/quicktime",
238  "movie": "video/x-sgi-movie",
239  "mp2": "audio/mpeg",
240  "mp3": "audio/mpeg",
241  "mp4": "video/mp4",
242  "mpe": "video/mpeg",
243  "mpeg": "video/mpeg",
244  "mpg": "video/mpeg",
245  "mpga": "audio/mpeg",
246  "ms": "application/x-troff-ms",
247  "msh": "model/mesh",
248  "mxu": "video/vnd.mpegurl",
249  "nc": "application/x-netcdf",
250  "oda": "application/oda",
251  "ogg": "application/ogg",
252  "pbm": "image/x-portable-bitmap",
253  "pct": "image/pict",
254  "pdb": "chemical/x-pdb",
255  "pdf": "application/pdf",
256  "pgm": "image/x-portable-graymap",
257  "pgn": "application/x-chess-pgn",
258  "pic": "image/pict",
259  "pict": "image/pict",
260  "png": MimeTypePng,
261  "pnm": "image/x-portable-anymap",
262  "pnt": "image/x-macpaint",
263  "pntg": "image/x-macpaint",
264  "ppm": "image/x-portable-pixmap",
265  "ppt": "application/vnd.ms-powerpoint",
266  "ps": "application/postscript",
267 
268  // Qore script
269  "q": MimeTypeText,
270 
271  // Qore class include file
272  "qc": MimeTypeText,
273 
274  // Qorus function definition file
275  "qfd": MimeTypeText,
276 
277  // Qorus class definition file
278  "qclass": MimeTypeText,
279 
280  // Qorus constant definition file
281  "qconst": MimeTypeText,
282 
283  // Qorus job definition
284  "qjob": MimeTypeText,
285 
286  // Qore include file
287  "ql": MimeTypeText,
288 
289  // Qore user module source
290  "qm": MimeTypeText,
291 
292  // Qorus service definition
293  "qsd": MimeTypeText,
294 
295  // Qorus workflow definition
296  "qwf": MimeTypeText,
297 
298  "qt": "video/quicktime",
299  "qti": "image/x-quicktime",
300  "qtif": "image/x-quicktime",
301  "ra": "audio/x-pn-realaudio",
302  "ram": "audio/x-pn-realaudio",
303  "ras": "image/x-cmu-raster",
304  "rdf": "application/rdf+xml",
305  "rgb": "image/x-rgb",
306  "rm": "application/vnd.rn-realmedia",
307  "roff": "application/x-troff",
308  "rtf": "text/rtf",
309  "rtx": "text/richtext",
310  "sgm": "text/sgml",
311  "sgml": "text/sgml",
312  "sh": "application/x-sh",
313  "shar": "application/x-shar",
314  "silo": "model/mesh",
315  "sit": "application/x-stuffit",
316  "skd": "application/x-koan",
317  "skm": "application/x-koan",
318  "skp": "application/x-koan",
319  "skt": "application/x-koan",
320  "smi": "application/smil",
321  "smil": "application/smil",
322  "snd": "audio/basic",
323  "so": MimeTypeUnknown,
324  "spl": "application/x-futuresplash",
325  "src": "application/x-wais-source",
326  "sv4cpio": "application/x-sv4cpio",
327  "sv4crc": "application/x-sv4crc",
328  "svg": "image/svg+xml",
329  "swf": "application/x-shockwave-flash",
330  "t": "application/x-troff",
331  "tar": "application/x-tar",
332  "tar.bz2": "application/x-bzip-compressed-tar",
333  "tar.gz": "application/x-tar-gz",
334  "tbz": "application/x-bzip-compressed-tar",
335  "tcl": "application/x-tcl",
336  "tex": "application/x-tex",
337  "texi": "application/x-texinfo",
338  "texinfo": "application/x-texinfo",
339  "text": MimeTypeText,
340  "tgz": "application/x-tar-gz",
341  "tif": "image/tiff",
342  "tiff": "image/tiff",
343  "tr": "application/x-troff",
344  "tsv": "text/tab-separated-values",
345  "txt": MimeTypeText,
346  "ustar": "application/x-ustar",
347  "vcd": "application/x-cdlink",
348  "vrml": "model/vrml",
349  "vxml": "application/voicexml+xml",
350  "wav": "audio/x-wav",
351  "wbmp": "image/vnd.wap.wbmp",
352  "wbmxl": "application/vnd.wap.wbxml",
353  "wml": "text/vnd.wap.wml",
354  "wmlc": "application/vnd.wap.wmlc",
355  "wmls": "text/vnd.wap.wmlscript",
356  "wmlsc": "application/vnd.wap.wmlscriptc",
357  "wrl": "model/vrml",
358  "xbm": "image/x-xbitmap",
359  "xht": "application/xhtml+xml",
360  "xhtml": "application/xhtml+xml",
361  "xls": "application/vnd.ms-excel",
362  "xml": MimeTypeXml,
363  "xpm": "image/x-xpixmap",
364  "xsl": "application/xml",
365  "xslt": "application/xslt+xml",
366  "xul": "application/vnd.mozilla.xul+xml",
367  "xwd": "image/x-xwindowdump",
368  "xyz": "chemical/x-xyz",
369  "yaml": MimeTypeYaml,
370  "zip": "application/zip",
371  );
373 
377 
378  const ContentTransEnc7Bit = "7bit";
379 
381  const ContentTransEnc8Bit = "8bit";
382 
384  const ContentTransEncBinary = "binary";
385 
387  const ContentTransEncBase64 = "base64";
388 
390  const ContentTransEncQuotedPrintable = "quoted-printable";
391 
393 
395  const MimeBase64LineLen = 76;
396 
398 
402 
406 
407 
412  string mime_encode_base64(data str, int len = MimeBase64LineLen);
413 
414 
416 
419  string mime_encode_quoted_printable(string str, bool hdr = False);
420 
421 
423 
428  data mime_encode_transfer_data(data data, string enc);
429 
430 
432  string mime_encode_header_word_b(string str);
433 
434 
436  string mime_encode_header_word_q(string str);
437 
439 
443 
444 
448  binary mime_decode_base64(data str);
449 
450 
452 
457  string mime_decode_base64_to_string(data str, *string encoding);
458 
459 
461 
466  string mime_decode_quoted_printable(string str, *string encoding);
467 
468 
470 
476  data mime_decode_transfer_data(data data, string enc, *string ct);
477 
478 
480 
483  string mime_decode_header(string hdr);
484 
486 
490 
491  string get_mime_type_from_ext(string path);
492 
494 
498 
499  const MPT_FORM_DATA = "form-data";
500 
502  const MPT_MIXED = "mixed";
503 
505  const MPT_MESSAGE = "message";
506 
508  const MPT_DIGEST = "digest";
509 
511  const MPT_ALTERNATIVE = "alternative";
512 
514  const MPT_RELATED = "related";
515 
517  const MPT_SIGNED = "signed";
518 
520  const MPT_ENCRYPTED = "encrypted";
521 
523  const MPT_BYTERANGES = "byteranges";
525 
528 
529 public:
530  const MP_DEFAULT_MSG = "This is a MIME multipart message";
531 
533  private :
535  string mptype;
536 
538  string boundary;
539 
541  string ct;
542 
544  list l = ();
545 
547  hash hdr;
548 
549 public:
551 
553 
556  constructor(string mptype, string boundary = MultiPartMessage::getBoundary());
557 
558 
560  static string getBoundary();
561 
563 
567  static string getRandomString(int len);
568 
570 
628  static hash parseBody(string boundary, string body, bool decode = True);
629 
631  binary serialize();
632 
633 
635 
639  static binary serializeHeaders(hash hdr);
640 
642  abstract hash getMsgAndHeaders();
643 
644  // don't reimplement this method; fix/enhance it in the module
645 
646 private:
647  final hash getMsgAndHeadersIntern(*string content_type, bool conlen = True);
648 public:
649 
650 
651  // don't reimplement this method; fix/enhance it in the module
652 
653 private:
654  splicePartIntern(data data, hash hdr);
655 public:
656 
657 
658  // don't reimplement this method; fix/enhance it in the module
659 
660 private:
661  addPartIntern(data data, hash hdr);
662 public:
663 
664 
665 
666 private:
667  static hash getPart(data data, hash hdr);
668 public:
669 
671  };
672 
675 
676 public:
678  private :
679  // the starting part ID
680  string startid;
681 
682 public:
684 
686 
688  constructor(string boundary = MultiPartMessage::getBoundary());
689 
690 
692 
699  splicePart(data data, string id, string content_type, *hash hdr);
700 
701 
703 
710  addPart(data data, string id, string content_type, *hash hdr);
711 
712 
714 
719 
720  };
721 
724 
725 public:
727 
729  constructor(string boundary = MultiPartMessage::getBoundary());
730 
731 
733 
745  splicePart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
746 
747 
749 
761  addPart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
762 
763 
765 
779  spliceEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
780 
781 
783 
797  addEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
798 
799 
801 
806 
807  };
808 };