Qore Mime Module Reference  1.3.3
 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 (C) 2012 - 2014 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 // minimum required Qore version
26 
27 
28 /* Version History
29  * 2014-04-08 v1.3.3: David Nichols <david@qore.org>
30  + added MimeTypeOctetStream constant
31 
32  * 2014-01-15 v1.3.2: David Nichols <david@qore.org>
33  + added mime type for WSDL files (application/wsdl+xml)
34  + added mappings for xls and xlst extensions to MimeTypeXml
35 
36  * 2013-04-05 v1.3.1: David Nichols <david@qore.org>
37  + added get_mime_type_from_ext()
38  + minor corrections
39  + added constants for css and js files + more extension -> mime type mappings
40 
41  * 2012-11-02 v1.3: David Nichols <david@qore.org>
42  + made get MultiPartMessage::getMsgAndHeaders() abstract as originally intended
43  + added MultiPartMessage::parseBody() static method
44 
45  * 2012-09-17 v1.2.1: David Nichols <david@qore.org>
46  + fixed q-encoding mail headers (always encode '_' chars otherwise they will be decoded as spaces)
47 
48  * 2012-06-14 v1.2: David Nichols <david@qore.org>
49  + added MIME decoding functions
50 
51  * 2012-05-31 v1.1: David Nichols <david@qore.org>
52  + added initial MultiPartMessage classes
53 
54  * 2012-05-24 v1.0: David Nichols <david@qore.org>
55  + pulled MIME implementation out of other code and made a user module to aggregate MIME definitions and functions
56 
57  based on:
58  - http://tools.ietf.org/html/rfc1521: quoted printable & base 64 transfer encodings
59  - http://tools.ietf.org/html/rfc2045: mime headers, content types, etc
60  - http://tools.ietf.org/html/rfc2047: "Q" and "B" encoded words (implemented by the Mime module)
61 */
62 
104 namespace Mime {
110  const MimeTypeUnknown = MimeTypeOctetStream;
111 
113  const MimeTypeOctetStream = "application/octet-stream";
114 
116  const MimeTypeText = "text/plain";
117 
119  const MimeTypeHtml = "text/html";
120 
122  const MimeTypeCsv = "text/csv";
123 
125  const MimeTypeJpeg = "image/jpeg";
126 
128  const MimeTypePng = "image/png";
129 
131  const MimeTypeSoapXml = "application/soap+xml";
132 
134  const MimeTypeYaml = "text/x-yaml";
135 
137  const MimeTypeXml = "text/xml";
138 
140  const MimeTypeXmlApp = "application/xml";
141 
143  const MimeTypeJson = "application/json";
144 
147 
150 
152  const MimeTypeYamlRpc = "application/x-yaml";
153 
155  const MimeTypeJavascript = "application/javascript";
156 
158  const MimeTypeCss = "text/css";
159 
161 
163  const MimeTypes = (
164  "7z": "application/x-7z-compressed",
165  "ai": "application/postscript",
166  "aif": "audio/x-aiff",
167  "aifc": "audio/x-aiff",
168  "aiff": "audio/x-aiff",
169  "asc": MimeTypeText,
170  "atom": "application/atom+xml",
171  "au": "audio/basic",
172  "avi": "video/x-msvideo",
173  "bcpio": "application/x-bcpio",
174  "bin": MimeTypeUnknown,
175  "bmp": "image/bmp",
176  "bz2": "application/x-bzip",
177  "cc": MimeTypeText,
178  "cdf": "application/x-netcdf",
179  "cgm": "image/cgm",
180  "class": MimeTypeUnknown,
181  "cpio": "application/x-cpio",
182  "cpp": MimeTypeText,
183  "cpt": "application/mac-compactpro",
184  "csh": "application/x-csh",
185  "css": MimeTypeCss,
186  "csv": MimeTypeCsv,
187  "dcr": "application/x-director",
188  "dif": "video/x-dv",
189  "dir": "application/x-director",
190  "djv": "image/vnd.djvu",
191  "djvu": "image/vnd.djvu",
192  "dll": MimeTypeUnknown,
193  "dmg": MimeTypeUnknown,
194  "dms": MimeTypeUnknown,
195  "doc": "application/msword",
196  "dtd": "application/xml-dtd",
197  "dv": "video/x-dv",
198  "dvi": "application/x-dvi",
199  "dxr": "application/x-director",
200  "eps": "application/postscript",
201  "etx": "text/x-setext",
202  "exe": MimeTypeUnknown,
203  "ez": "application/andrew-inset",
204  "gif": "image/gif",
205  "gram": "application/srgs",
206  "grxml": "application/srgs+xml",
207  "gtar": "application/x-gtar",
208  "gz": "application/x-gzip",
209  "h": MimeTypeText,
210  "hdf": "application/x-hdf",
211  "hh": MimeTypeText,
212  "hpp": MimeTypeText,
213  "hqx": "application/mac-binhex40",
214  "htm": MimeTypeHtml,
215  "html": MimeTypeHtml,
216  "ice": "x-conference/x-cooltalk",
217  "ico": "image/x-icon",
218  "ics": "text/calendar",
219  "ief": "image/ief",
220  "ifb": "text/calendar",
221  "iges": "model/iges",
222  "igs": "model/iges",
223  "jar": "application/java-archive",
224  "java": MimeTypeText,
225  "jnlp": "application/x-java-jnlp-file",
226  "jp2": "image/jp2",
227  "jpe": MimeTypeJpeg,
228  "jpeg": MimeTypeJpeg,
229  "jpg": MimeTypeJpeg,
230  "js": MimeTypeJavascript,
231  "kar": "audio/midi",
232  "latex": "application/x-latex",
233  "lha": MimeTypeUnknown,
234  "lzh": MimeTypeUnknown,
235  "m3u": "audio/x-mpegurl",
236  "m4a": "audio/mp4a-latm",
237  "m4b": "audio/mp4a-latm",
238  "m4p": "audio/mp4a-latm",
239  "m4u": "video/vnd.mpegurl",
240  "m4v": "video/x-m4v",
241  "mac": "image/x-macpaint",
242  "man": "application/x-troff-man",
243  "mathml": "application/mathml+xml",
244  "me": "application/x-troff-me",
245  "mesh": "model/mesh",
246  "mid": "audio/midi",
247  "midi": "audio/midi",
248  "mif": "application/vnd.mif",
249  "mov": "video/quicktime",
250  "movie": "video/x-sgi-movie",
251  "mp2": "audio/mpeg",
252  "mp3": "audio/mpeg",
253  "mp4": "video/mp4",
254  "mpe": "video/mpeg",
255  "mpeg": "video/mpeg",
256  "mpg": "video/mpeg",
257  "mpga": "audio/mpeg",
258  "ms": "application/x-troff-ms",
259  "msh": "model/mesh",
260  "mxu": "video/vnd.mpegurl",
261  "nc": "application/x-netcdf",
262  "oda": "application/oda",
263  "ogg": "application/ogg",
264  "pbm": "image/x-portable-bitmap",
265  "pct": "image/pict",
266  "pdb": "chemical/x-pdb",
267  "pdf": "application/pdf",
268  "pgm": "image/x-portable-graymap",
269  "pgn": "application/x-chess-pgn",
270  "pic": "image/pict",
271  "pict": "image/pict",
272  "png": MimeTypePng,
273  "pnm": "image/x-portable-anymap",
274  "pnt": "image/x-macpaint",
275  "pntg": "image/x-macpaint",
276  "ppm": "image/x-portable-pixmap",
277  "ppt": "application/vnd.ms-powerpoint",
278  "ps": "application/postscript",
279 
280  // Qore script
281  "q": MimeTypeText,
282 
283  // Qore class include file
284  "qc": MimeTypeText,
285 
286  // Qorus function definition file
287  "qfd": MimeTypeText,
288 
289  // Qorus class definition file
290  "qclass": MimeTypeText,
291 
292  // Qorus constant definition file
293  "qconst": MimeTypeText,
294 
295  // Qorus job definition
296  "qjob": MimeTypeText,
297 
298  // Qore include file
299  "ql": MimeTypeText,
300 
301  // Qore user module source
302  "qm": MimeTypeText,
303 
304  // Qorus service definition
305  "qsd": MimeTypeText,
306 
307  // Qorus workflow definition
308  "qwf": MimeTypeText,
309 
310  "qt": "video/quicktime",
311  "qti": "image/x-quicktime",
312  "qtif": "image/x-quicktime",
313  "ra": "audio/x-pn-realaudio",
314  "ram": "audio/x-pn-realaudio",
315  "ras": "image/x-cmu-raster",
316  "rdf": "application/rdf+xml",
317  "rgb": "image/x-rgb",
318  "rm": "application/vnd.rn-realmedia",
319  "roff": "application/x-troff",
320  "rtf": "text/rtf",
321  "rtx": "text/richtext",
322  "sgm": "text/sgml",
323  "sgml": "text/sgml",
324  "sh": "application/x-sh",
325  "shar": "application/x-shar",
326  "silo": "model/mesh",
327  "sit": "application/x-stuffit",
328  "skd": "application/x-koan",
329  "skm": "application/x-koan",
330  "skp": "application/x-koan",
331  "skt": "application/x-koan",
332  "smi": "application/smil",
333  "smil": "application/smil",
334  "snd": "audio/basic",
335  "so": MimeTypeUnknown,
336  "spl": "application/x-futuresplash",
337  "src": "application/x-wais-source",
338  "sv4cpio": "application/x-sv4cpio",
339  "sv4crc": "application/x-sv4crc",
340  "svg": "image/svg+xml",
341  "swf": "application/x-shockwave-flash",
342  "t": "application/x-troff",
343  "tar": "application/x-tar",
344  "tar.bz2": "application/x-bzip-compressed-tar",
345  "tar.gz": "application/x-tar-gz",
346  "tbz": "application/x-bzip-compressed-tar",
347  "tcl": "application/x-tcl",
348  "tex": "application/x-tex",
349  "texi": "application/x-texinfo",
350  "texinfo": "application/x-texinfo",
351  "text": MimeTypeText,
352  "tgz": "application/x-tar-gz",
353  "tif": "image/tiff",
354  "tiff": "image/tiff",
355  "tr": "application/x-troff",
356  "tsv": "text/tab-separated-values",
357  "txt": MimeTypeText,
358  "ustar": "application/x-ustar",
359  "vcd": "application/x-cdlink",
360  "vrml": "model/vrml",
361  "vxml": "application/voicexml+xml",
362  "wav": "audio/x-wav",
363  "wbmp": "image/vnd.wap.wbmp",
364  "wbmxl": "application/vnd.wap.wbxml",
365  "wml": "text/vnd.wap.wml",
366  "wmlc": "application/vnd.wap.wmlc",
367  "wmls": "text/vnd.wap.wmlscript",
368  "wmlsc": "application/vnd.wap.wmlscriptc",
369  "wrl": "model/vrml",
370  "wsdl": "application/wsdl+xml",
371  "xbm": "image/x-xbitmap",
372  "xht": "application/xhtml+xml",
373  "xhtml": "application/xhtml+xml",
374  "xls": "application/vnd.ms-excel",
375  "xml": MimeTypeXml,
376  "xpm": "image/x-xpixmap",
377  "xsd": MimeTypeXml,
378  "xsl": "application/xml",
379  "xslt": "application/xslt+xml",
380  "xul": "application/vnd.mozilla.xul+xml",
381  "xwd": "image/x-xwindowdump",
382  "xyz": "chemical/x-xyz",
383  "yaml": MimeTypeYaml,
384  "zip": "application/zip",
385  );
387 
391  const ContentTransEnc7Bit = "7bit";
393 
395  const ContentTransEnc8Bit = "8bit";
396 
398  const ContentTransEncBinary = "binary";
399 
401  const ContentTransEncBase64 = "base64";
402 
404  const ContentTransEncQuotedPrintable = "quoted-printable";
405 
407 
409  const MimeBase64LineLen = 76;
410 
412 
416 
420 
426  string mime_encode_base64(data str, int len = MimeBase64LineLen);
427 
428 
430 
433  string mime_encode_quoted_printable(string str, bool hdr = False);
434 
435 
437 
442  data mime_encode_transfer_data(data data, string enc);
443 
444 
446  string mime_encode_header_word_b(string str);
447 
448 
450  string mime_encode_header_word_q(string str);
451 
453 
457 
462  binary mime_decode_base64(data str);
463 
464 
466 
471  string mime_decode_base64_to_string(data str, *string encoding);
472 
473 
475 
480  string mime_decode_quoted_printable(string str, *string encoding);
481 
482 
484 
490  data mime_decode_transfer_data(data data, string enc, *string ct);
491 
492 
494 
497  string mime_decode_header(string hdr);
498 
500 
504  string get_mime_type_from_ext(string path);
506 
508 
512  const MPT_FORM_DATA = "form-data";
514 
516  const MPT_MIXED = "mixed";
517 
519  const MPT_MESSAGE = "message";
520 
522  const MPT_DIGEST = "digest";
523 
525  const MPT_ALTERNATIVE = "alternative";
526 
528  const MPT_RELATED = "related";
529 
531  const MPT_SIGNED = "signed";
532 
534  const MPT_ENCRYPTED = "encrypted";
535 
537  const MPT_BYTERANGES = "byteranges";
539 
542 
543 public:
544  const MP_DEFAULT_MSG = "This is a MIME multipart message";
545 
547  private :
549  string mptype;
550 
552  string boundary;
553 
555  string ct;
556 
558  list l = ();
559 
561  hash hdr;
562 
563 public:
565 
567 
570  constructor(string mptype, string boundary = MultiPartMessage::getBoundary());
571 
572 
574  static string getBoundary();
575 
577 
581  static string getRandomString(int len);
582 
584 
642  static hash parseBody(string boundary, string body, bool decode = True);
643 
645  binary serialize();
646 
647 
649 
653  static binary serializeHeaders(hash hdr);
654 
656  abstract hash getMsgAndHeaders();
657 
658  // don't reimplement this method; fix/enhance it in the module
659  final private hash getMsgAndHeadersIntern(*string content_type, bool conlen = True);
660 
661 
662  // don't reimplement this method; fix/enhance it in the module
663  private splicePartIntern(data data, hash hdr);
664 
665 
666  // don't reimplement this method; fix/enhance it in the module
667  private addPartIntern(data data, hash hdr);
668 
669 
670 
671 private:
672  static hash getPart(data data, hash hdr);
673 public:
674 
676  };
677 
680 
681 public:
683  private :
684  // the starting part ID
685  string startid;
686 
687 public:
689 
691 
693  constructor(string boundary = MultiPartMessage::getBoundary());
694 
695 
697 
704  splicePart(data data, string id, string content_type, *hash hdr);
705 
706 
708 
715  addPart(data data, string id, string content_type, *hash hdr);
716 
717 
719 
724 
725  };
726 
729 
730 public:
732 
734  constructor(string boundary = MultiPartMessage::getBoundary());
735 
736 
738 
750  splicePart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
751 
752 
754 
766  addPart(data data, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
767 
768 
770 
784  spliceEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
785 
786 
788 
802  addEncodePart(data data, string enc, string content_type = MimeTypeText, string disp = "inline", *hash hdr);
803 
804 
806 
811 
812  };
813 };
const MPT_RELATED
for sending multiple components of an aggregated whole (http://tools.ietf.org/html/rfc2387) ...
Definition: Mime.qm.dox.h:528
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:125
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:519
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:395
MultiPartMessage class implementation.
Definition: Mime.qm.dox.h:541
const MimeTypeYaml
Mime type for yaml data files.
Definition: Mime.qm.dox.h:134
const MimeTypeXml
Mime type for plain (human-readable) xml files.
Definition: Mime.qm.dox.h:137
const MPT_ENCRYPTED
for sending encrypted messages (http://tools.ietf.org/html/rfc1847#section-2.2)
Definition: Mime.qm.dox.h:534
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:728
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:525
binary binary()
const MimeTypeJsonRpc
Mime type for JSON-RPC.
Definition: Mime.qm.dox.h:146
const MPT_BYTERANGES
for sending noncontiguous byte ranges of a single message (http://tools.ietf.org/html/rfc2616) ...
Definition: Mime.qm.dox.h:537
const MimeTypeJson
Mime type for JSON.
Definition: Mime.qm.dox.h:143
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:531
const ContentTransEncBase64
base-64 Content-Transfer-Encoding
Definition: Mime.qm.dox.h:401
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:128
const MimeQuotedPrintableLineLen
maximum line length for quoted-printable encoding
Definition: Mime.qm.dox.h:414
const False
const MimeTypeJavascript
Mime type for Javascript.
Definition: Mime.qm.dox.h:155
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 MimeTypeOctetStream
MIME type for unknown file types.
Definition: Mime.qm.dox.h:113
const MimeTypeCss
Mime type for css.
Definition: Mime.qm.dox.h:158
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:152
const MimeTypeXmlRpc
Mime type for XML-RPC.
Definition: Mime.qm.dox.h:149
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:516
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:522
const MPT_FORM_DATA
for form data (http://tools.ietf.org/html/rfc1867, http://tools.ietf.org/html/rfc2388) ...
Definition: Mime.qm.dox.h:513
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:404
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:409
const MimeTypeHtml
MIME type for HTML.
Definition: Mime.qm.dox.h:119
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:163
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:131
const ContentTransEncBinary
binary transfer encoding; implies no maximum line length (= no encoding)
Definition: Mime.qm.dox.h:398
const MimeTypeXmlApp
Mime type for more complicated (not human-readable) xml files.
Definition: Mime.qm.dox.h:140
const ContentTransEnc7Bit
default Content-Transfer-Encoding if none is present; implies lines &lt; 1000 chars (= no encoding) ...
Definition: Mime.qm.dox.h:392
const MimeTypeCsv
MIME type for csv files (http://tools.ietf.org/html/rfc4180)
Definition: Mime.qm.dox.h:122
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:116
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...