Qore Mime Module Reference  1.3.2
 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  * 2014-01-15 v1.3.2: David Nichols <david@qore.org>
29  + added mime type for WSDL files (application/wsdl+xml)
30  + added mappings for xls and xlst extensions to MimeTypeXml
31 
32  * 2013-04-05 v1.3.1: David Nichols <david@qore.org>
33  + added get_mime_type_from_ext()
34  + minor corrections
35  + added constants for css and js files + more extension -> mime type mappings
36 
37  * 2012-11-02 v1.3: David Nichols <david@qore.org>
38  + made get MultiPartMessage::getMsgAndHeaders() abstract as originally intended
39  + added MultiPartMessage::parseBody() static method
40 
41  * 2012-09-17 v1.2.1: David Nichols <david@qore.org>
42  + fixed q-encoding mail headers (always encode '_' chars otherwise they will be decoded as spaces)
43 
44  * 2012-06-14 v1.2: David Nichols <david@qore.org>
45  + added MIME decoding functions
46 
47  * 2012-05-31 v1.1: David Nichols <david@qore.org>
48  + added initial MultiPartMessage classes
49 
50  * 2012-05-24 v1.0: David Nichols <david@qore.org>
51  + pulled MIME implementation out of other code and made a user module to aggregate MIME definitions and functions
52 
53  based on:
54  - http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
55  - http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
56  - http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
57 */
58 
100 namespace Mime {
106  const MimeTypeUnknown = "application/octet-stream";
108 
110  const MimeTypeText = "text/plain";
111 
113  const MimeTypeHtml = "text/html";
114 
116  const MimeTypeCsv = "text/csv";
117 
119  const MimeTypeJpeg = "image/jpeg";
120 
122  const MimeTypePng = "image/png";
123 
125  const MimeTypeSoapXml = "application/soap+xml";
126 
128  const MimeTypeYaml = "text/x-yaml";
129 
131  const MimeTypeXml = "text/xml";
132 
134  const MimeTypeXmlApp = "application/xml";
135 
137  const MimeTypeJson = "application/json";
138 
141 
144 
146  const MimeTypeYamlRpc = "application/x-yaml";
147 
149  const MimeTypeJavascript = "application/javascript";
150 
152  const MimeTypeCss = "text/css";
153 
155 
157  const MimeTypes = (
158  "7z": "application/x-7z-compressed",
159  "ai": "application/postscript",
160  "aif": "audio/x-aiff",
161  "aifc": "audio/x-aiff",
162  "aiff": "audio/x-aiff",
163  "asc": MimeTypeText,
164  "atom": "application/atom+xml",
165  "au": "audio/basic",
166  "avi": "video/x-msvideo",
167  "bcpio": "application/x-bcpio",
168  "bin": MimeTypeUnknown,
169  "bmp": "image/bmp",
170  "bz2": "application/x-bzip",
171  "cc": MimeTypeText,
172  "cdf": "application/x-netcdf",
173  "cgm": "image/cgm",
174  "class": MimeTypeUnknown,
175  "cpio": "application/x-cpio",
176  "cpp": MimeTypeText,
177  "cpt": "application/mac-compactpro",
178  "csh": "application/x-csh",
179  "css": MimeTypeCss,
180  "csv": MimeTypeCsv,
181  "dcr": "application/x-director",
182  "dif": "video/x-dv",
183  "dir": "application/x-director",
184  "djv": "image/vnd.djvu",
185  "djvu": "image/vnd.djvu",
186  "dll": MimeTypeUnknown,
187  "dmg": MimeTypeUnknown,
188  "dms": MimeTypeUnknown,
189  "doc": "application/msword",
190  "dtd": "application/xml-dtd",
191  "dv": "video/x-dv",
192  "dvi": "application/x-dvi",
193  "dxr": "application/x-director",
194  "eps": "application/postscript",
195  "etx": "text/x-setext",
196  "exe": MimeTypeUnknown,
197  "ez": "application/andrew-inset",
198  "gif": "image/gif",
199  "gram": "application/srgs",
200  "grxml": "application/srgs+xml",
201  "gtar": "application/x-gtar",
202  "gz": "application/x-gzip",
203  "h": MimeTypeText,
204  "hdf": "application/x-hdf",
205  "hh": MimeTypeText,
206  "hpp": MimeTypeText,
207  "hqx": "application/mac-binhex40",
208  "htm": MimeTypeHtml,
209  "html": MimeTypeHtml,
210  "ice": "x-conference/x-cooltalk",
211  "ico": "image/x-icon",
212  "ics": "text/calendar",
213  "ief": "image/ief",
214  "ifb": "text/calendar",
215  "iges": "model/iges",
216  "igs": "model/iges",
217  "jar": "application/java-archive",
218  "java": MimeTypeText,
219  "jnlp": "application/x-java-jnlp-file",
220  "jp2": "image/jp2",
221  "jpe": MimeTypeJpeg,
222  "jpeg": MimeTypeJpeg,
223  "jpg": MimeTypeJpeg,
224  "js": MimeTypeJavascript,
225  "kar": "audio/midi",
226  "latex": "application/x-latex",
227  "lha": MimeTypeUnknown,
228  "lzh": MimeTypeUnknown,
229  "m3u": "audio/x-mpegurl",
230  "m4a": "audio/mp4a-latm",
231  "m4b": "audio/mp4a-latm",
232  "m4p": "audio/mp4a-latm",
233  "m4u": "video/vnd.mpegurl",
234  "m4v": "video/x-m4v",
235  "mac": "image/x-macpaint",
236  "man": "application/x-troff-man",
237  "mathml": "application/mathml+xml",
238  "me": "application/x-troff-me",
239  "mesh": "model/mesh",
240  "mid": "audio/midi",
241  "midi": "audio/midi",
242  "mif": "application/vnd.mif",
243  "mov": "video/quicktime",
244  "movie": "video/x-sgi-movie",
245  "mp2": "audio/mpeg",
246  "mp3": "audio/mpeg",
247  "mp4": "video/mp4",
248  "mpe": "video/mpeg",
249  "mpeg": "video/mpeg",
250  "mpg": "video/mpeg",
251  "mpga": "audio/mpeg",
252  "ms": "application/x-troff-ms",
253  "msh": "model/mesh",
254  "mxu": "video/vnd.mpegurl",
255  "nc": "application/x-netcdf",
256  "oda": "application/oda",
257  "ogg": "application/ogg",
258  "pbm": "image/x-portable-bitmap",
259  "pct": "image/pict",
260  "pdb": "chemical/x-pdb",
261  "pdf": "application/pdf",
262  "pgm": "image/x-portable-graymap",
263  "pgn": "application/x-chess-pgn",
264  "pic": "image/pict",
265  "pict": "image/pict",
266  "png": MimeTypePng,
267  "pnm": "image/x-portable-anymap",
268  "pnt": "image/x-macpaint",
269  "pntg": "image/x-macpaint",
270  "ppm": "image/x-portable-pixmap",
271  "ppt": "application/vnd.ms-powerpoint",
272  "ps": "application/postscript",
273 
274  // Qore script
275  "q": MimeTypeText,
276 
277  // Qore class include file
278  "qc": MimeTypeText,
279 
280  // Qorus function definition file
281  "qfd": MimeTypeText,
282 
283  // Qorus class definition file
284  "qclass": MimeTypeText,
285 
286  // Qorus constant definition file
287  "qconst": MimeTypeText,
288 
289  // Qorus job definition
290  "qjob": MimeTypeText,
291 
292  // Qore include file
293  "ql": MimeTypeText,
294 
295  // Qore user module source
296  "qm": MimeTypeText,
297 
298  // Qorus service definition
299  "qsd": MimeTypeText,
300 
301  // Qorus workflow definition
302  "qwf": MimeTypeText,
303 
304  "qt": "video/quicktime",
305  "qti": "image/x-quicktime",
306  "qtif": "image/x-quicktime",
307  "ra": "audio/x-pn-realaudio",
308  "ram": "audio/x-pn-realaudio",
309  "ras": "image/x-cmu-raster",
310  "rdf": "application/rdf+xml",
311  "rgb": "image/x-rgb",
312  "rm": "application/vnd.rn-realmedia",
313  "roff": "application/x-troff",
314  "rtf": "text/rtf",
315  "rtx": "text/richtext",
316  "sgm": "text/sgml",
317  "sgml": "text/sgml",
318  "sh": "application/x-sh",
319  "shar": "application/x-shar",
320  "silo": "model/mesh",
321  "sit": "application/x-stuffit",
322  "skd": "application/x-koan",
323  "skm": "application/x-koan",
324  "skp": "application/x-koan",
325  "skt": "application/x-koan",
326  "smi": "application/smil",
327  "smil": "application/smil",
328  "snd": "audio/basic",
329  "so": MimeTypeUnknown,
330  "spl": "application/x-futuresplash",
331  "src": "application/x-wais-source",
332  "sv4cpio": "application/x-sv4cpio",
333  "sv4crc": "application/x-sv4crc",
334  "svg": "image/svg+xml",
335  "swf": "application/x-shockwave-flash",
336  "t": "application/x-troff",
337  "tar": "application/x-tar",
338  "tar.bz2": "application/x-bzip-compressed-tar",
339  "tar.gz": "application/x-tar-gz",
340  "tbz": "application/x-bzip-compressed-tar",
341  "tcl": "application/x-tcl",
342  "tex": "application/x-tex",
343  "texi": "application/x-texinfo",
344  "texinfo": "application/x-texinfo",
345  "text": MimeTypeText,
346  "tgz": "application/x-tar-gz",
347  "tif": "image/tiff",
348  "tiff": "image/tiff",
349  "tr": "application/x-troff",
350  "tsv": "text/tab-separated-values",
351  "txt": MimeTypeText,
352  "ustar": "application/x-ustar",
353  "vcd": "application/x-cdlink",
354  "vrml": "model/vrml",
355  "vxml": "application/voicexml+xml",
356  "wav": "audio/x-wav",
357  "wbmp": "image/vnd.wap.wbmp",
358  "wbmxl": "application/vnd.wap.wbxml",
359  "wml": "text/vnd.wap.wml",
360  "wmlc": "application/vnd.wap.wmlc",
361  "wmls": "text/vnd.wap.wmlscript",
362  "wmlsc": "application/vnd.wap.wmlscriptc",
363  "wrl": "model/vrml",
364  "wsdl": "application/wsdl+xml",
365  "xbm": "image/x-xbitmap",
366  "xht": "application/xhtml+xml",
367  "xhtml": "application/xhtml+xml",
368  "xls": "application/vnd.ms-excel",
369  "xml": MimeTypeXml,
370  "xpm": "image/x-xpixmap",
371  "xsd": MimeTypeXml,
372  "xsl": "application/xml",
373  "xslt": "application/xslt+xml",
374  "xul": "application/vnd.mozilla.xul+xml",
375  "xwd": "image/x-xwindowdump",
376  "xyz": "chemical/x-xyz",
377  "yaml": MimeTypeYaml,
378  "zip": "application/zip",
379  );
381 
385  const ContentTransEnc7Bit = "7bit";
387 
389  const ContentTransEnc8Bit = "8bit";
390 
392  const ContentTransEncBinary = "binary";
393 
395  const ContentTransEncBase64 = "base64";
396 
398  const ContentTransEncQuotedPrintable = "quoted-printable";
399 
401 
403  const MimeBase64LineLen = 76;
404 
406 
410 
414 
420  string mime_encode_base64(data str, int len = MimeBase64LineLen);
421 
422 
424 
427  string mime_encode_quoted_printable(string str, bool hdr = False);
428 
429 
431 
436  data mime_encode_transfer_data(data data, string enc);
437 
438 
440  string mime_encode_header_word_b(string str);
441 
442 
444  string mime_encode_header_word_q(string str);
445 
447 
451 
456  binary mime_decode_base64(data str);
457 
458 
460 
465  string mime_decode_base64_to_string(data str, *string encoding);
466 
467 
469 
474  string mime_decode_quoted_printable(string str, *string encoding);
475 
476 
478 
484  data mime_decode_transfer_data(data data, string enc, *string ct);
485 
486 
488 
491  string mime_decode_header(string hdr);
492 
494 
498  string get_mime_type_from_ext(string path);
500 
502 
506  const MPT_FORM_DATA = "form-data";
508 
510  const MPT_MIXED = "mixed";
511 
513  const MPT_MESSAGE = "message";
514 
516  const MPT_DIGEST = "digest";
517 
519  const MPT_ALTERNATIVE = "alternative";
520 
522  const MPT_RELATED = "related";
523 
525  const MPT_SIGNED = "signed";
526 
528  const MPT_ENCRYPTED = "encrypted";
529 
531  const MPT_BYTERANGES = "byteranges";
533 
536 
537 public:
538  const MP_DEFAULT_MSG = "This is a MIME multipart message";
539 
541  private :
543  string mptype;
544 
546  string boundary;
547 
549  string ct;
550 
552  list l = ();
553 
555  hash hdr;
556 
557 public:
559 
561 
564  constructor(string mptype, string boundary = MultiPartMessage::getBoundary());
565 
566 
568  static string getBoundary();
569 
571 
575  static string getRandomString(int len);
576 
578 
636  static hash parseBody(string boundary, string body, bool decode = True);
637 
639  binary serialize();
640 
641 
643 
647  static binary serializeHeaders(hash hdr);
648 
650  abstract hash getMsgAndHeaders();
651 
652  // don't reimplement this method; fix/enhance it in the module
653  final private hash getMsgAndHeadersIntern(*string content_type, bool conlen = True);
654 
655 
656  // don't reimplement this method; fix/enhance it in the module
657  private splicePartIntern(data data, hash hdr);
658 
659 
660  // don't reimplement this method; fix/enhance it in the module
661  private addPartIntern(data data, hash hdr);
662 
663 
664 
665 private:
666  static hash getPart(data data, hash hdr);
667 public:
668 
670  };
671 
674 
675 public:
677  private :
678  // the starting part ID
679  string startid;
680 
681 public:
683 
685 
687  constructor(string boundary = MultiPartMessage::getBoundary());
688 
689 
691 
698  splicePart(data data, string id, string content_type, *hash hdr);
699 
700 
702 
709  addPart(data data, string id, string content_type, *hash hdr);
710 
711 
713 
718 
719  };
720 
723 
724 public:
726 
728  constructor(string boundary = MultiPartMessage::getBoundary());
729 
730 
732 
744  splicePart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
745 
746 
748 
760  addPart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
761 
762 
764 
778  spliceEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
779 
780 
782 
796  addEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
797 
798 
800 
805 
806  };
807 };
const MPT_RELATED
for sending multiple components of an aggregated whole (http://tools.ietf.org/html/rfc2387) ...
Definition: Mime.qm.dox.h:522
data mime_encode_transfer_data(data data, string enc)
encodes data according to the given encoding
const MimeTypeJpeg
MIME type for jpeg images.
Definition: Mime.qm.dox.h:119
constructor(string boundary=MultiPartMessage::getBoundary())
creates the object
const MPT_MESSAGE
for email/MIME messages with headers (http://tools.ietf.org/html/rfc2046)
Definition: Mime.qm.dox.h:513
constructor(string mptype, string boundary=MultiPartMessage::getBoundary())
creates the object
string mime_encode_header_word_q(string str)
returns a string in &quot;Q&quot; (&quot;quoted-printable&quot;) encoding for MIME header string words according to RFC 2...
const ContentTransEnc8Bit
implies lines &lt; 1000 chars (= no encoding)
Definition: Mime.qm.dox.h:389
MultiPartMessage class implementation.
Definition: Mime.qm.dox.h:535
const MimeTypeYaml
Mime type for yaml data files.
Definition: Mime.qm.dox.h:128
const MimeTypeXml
Mime type for plain (human-readable) xml files.
Definition: Mime.qm.dox.h:131
const MPT_ENCRYPTED
for sending encrypted messages (http://tools.ietf.org/html/rfc1847#section-2.2)
Definition: Mime.qm.dox.h:528
string mime_encode_quoted_printable(string str, bool hdr=False)
returns a string in &quot;quoted-printable&quot; (or &quot;QP&quot;) encoding according to RFC 2045 section 6...
static binary serializeHeaders(hash hdr)
serializes a header hash to a binary object
addEncodePart(data data, string enc, string content_type=MimeTypeText, string disp="inline", *hash hdr)
adds a message part to the end of the list; encodes the data according to the transfer encoding argum...
const True
implements a MultiPartMixedMessage class, a specialization of MultiPartMessage
Definition: Mime.qm.dox.h:722
const MPT_ALTERNATIVE
for sending multiple &quot;alternatives&quot; of the same content (http://tools.ietf.org/html/rfc2046#section-5...
Definition: Mime.qm.dox.h:519
binary binary()
const MimeTypeJsonRpc
Mime type for JSON-RPC.
Definition: Mime.qm.dox.h:140
const MPT_BYTERANGES
for sending noncontiguous byte ranges of a single message (http://tools.ietf.org/html/rfc2616) ...
Definition: Mime.qm.dox.h:531
const MimeTypeJson
Mime type for JSON.
Definition: Mime.qm.dox.h:137
const MPT_SIGNED
to attach a digital signature to a message (http://tools.ietf.org/html/rfc1847#section-2.1)
Definition: Mime.qm.dox.h:525
const ContentTransEncBase64
base-64 Content-Transfer-Encoding
Definition: Mime.qm.dox.h:395
binary serialize()
serializes the message and returns a binary object ready to send over a socket
const MimeTypePng
MIME type for png images.
Definition: Mime.qm.dox.h:122
const MimeQuotedPrintableLineLen
maximum line length for quoted-printable encoding
Definition: Mime.qm.dox.h:408
const False
const MimeTypeJavascript
Mime type for Javascript.
Definition: Mime.qm.dox.h:149
const MimeTypeUnknown
MIME type for unknown file types.
Definition: Mime.qm.dox.h:107
list list(...)
string mime_decode_base64_to_string(data str, *string encoding)
returns a string value from a string in &quot;BASE64&quot; encoding according to RFC 2045
splicePart(data data, string content_type=MimeTypeText, string disp="inline", *hash hdr)
adds a message part to the start of the list; the data muyst already be encoded and any Content-Trans...
const MimeTypeCss
Mime type for css.
Definition: Mime.qm.dox.h:152
string mime_decode_header(string hdr)
decodes a header string; if any part of the string is encoded with &quot;B&quot; (&quot;BASE64&quot;) or &quot;Q&quot; (&quot;quoted-pri...
binary mime_decode_base64(data str)
returns a binary value from a string in &quot;BASE64&quot; encoding according to RFC 2045
const MimeTypeYamlRpc
Mime type for YAML-RPC.
Definition: Mime.qm.dox.h:146
const MimeTypeXmlRpc
Mime type for XML-RPC.
Definition: Mime.qm.dox.h:143
string get_mime_type_from_ext(string path)
returns the mime type for the given filename from the extension or MimeTypeUnknown if the extension i...
const MPT_MIXED
for sending files with different &quot;Content-Type&quot; headers (http://tools.ietf.org/html/rfc2046#section-5...
Definition: Mime.qm.dox.h:510
string mime_encode_base64(data str, int len=MimeBase64LineLen)
returns a string in &quot;BASE64&quot; encoding according to RFC 2045
const MPT_DIGEST
for sending multiple text messages (http://tools.ietf.org/html/rfc2046#section-5.1.5)
Definition: Mime.qm.dox.h:516
const MPT_FORM_DATA
for form data (http://tools.ietf.org/html/rfc1867, http://tools.ietf.org/html/rfc2388) ...
Definition: Mime.qm.dox.h:507
spliceEncodePart(data data, string enc, string content_type=MimeTypeText, string disp="inline", *hash hdr)
adds a message part to the start of the list; encodes the data according to the transfer encoding arg...
const ContentTransEncQuotedPrintable
quoted-printable Content-Transfer-Encoding
Definition: Mime.qm.dox.h:398
hash getMsgAndHeaders()
returns a hash of the message
string mime_encode_header_word_b(string str)
returns a string in &quot;B&quot; (&quot;BASE64&quot;) encoding for MIME header string words according to RFC 2047 ...
static hash parseBody(string boundary, string body, bool decode=True)
returns a hash representing a parsed multipart message body from a boundary string and body arguments...
static string getRandomString(int len)
returns a string of random characters
data mime_decode_transfer_data(data data, string enc, *string ct)
decodes data according to the given encoding
const MimeBase64LineLen
maximum line length for base64 encoding
Definition: Mime.qm.dox.h:403
const MimeTypeHtml
MIME type for HTML.
Definition: Mime.qm.dox.h:113
const MimeTypes
A map of default mime types per file extension; the hash keys are file extensions in lower-case witho...
Definition: Mime.qm.dox.h:157
static string getBoundary()
returns a string embedded with the current timestamp designed to be used as MultiPart boundary string...
hash hash(object obj)
const MimeTypeSoapXml
Mime type for SOAP XML messages (XML MIME type reference: http://tools.ietf.org/html/rfc3023) ...
Definition: Mime.qm.dox.h:125
const ContentTransEncBinary
binary transfer encoding; implies no maximum line length (= no encoding)
Definition: Mime.qm.dox.h:392
const MimeTypeXmlApp
Mime type for more complicated (not human-readable) xml files.
Definition: Mime.qm.dox.h:134
const ContentTransEnc7Bit
default Content-Transfer-Encoding if none is present; implies lines &lt; 1000 chars (= no encoding) ...
Definition: Mime.qm.dox.h:386
const MimeTypeCsv
MIME type for csv files (http://tools.ietf.org/html/rfc4180)
Definition: Mime.qm.dox.h:116
addPart(data data, string content_type=MimeTypeText, string disp="inline", *hash hdr)
adds a message part to the end of the list; the data muyst already be encoded and any Content-Transfe...
const MimeTypeText
MIME type for text.
Definition: Mime.qm.dox.h:110
string mime_decode_quoted_printable(string str, *string encoding)
returns a string parsed from &quot;quoted-printable&quot; (or &quot;QP&quot;) encoding according to RFC 2045 section 6...