PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mime.h
Go to the documentation of this file.
1 /*
2  * mime.h
3  *
4  * Multipurpose Internet Mail Extensions support classes.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef PTLIB_PMIME_H
28 #define PTLIB_PMIME_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <ptclib/cypher.h>
35 
36 
37 class PMultiPartList;
38 class PInternetProtocol;
39 
40 
42 // PMIMEInfo
43 
48 class PMIMEInfo : public PStringOptions
49 {
50  public:
51  PMIMEInfo() { }
52 
54  PMIMEInfo(
55  istream &strm
56  );
57  PMIMEInfo(
58  PInternetProtocol & socket
59  );
60  PMIMEInfo(
61  const PStringToString & dict
62  );
63  PMIMEInfo(
64  const PString & str
65  );
66 
67 
68  // Overrides from class PObject
72  virtual void PrintOn(
73  ostream &strm
74  ) const;
75 
79  virtual void ReadFrom(
80  istream &strm
81  );
82 
83 
84  // New functions for class.
90  PBoolean Read(
91  PInternetProtocol & socket
92  );
93 
100  PInternetProtocol & socket
101  ) const;
102 
105  PString AsString() const;
106 
115  bool AddMIME(
116  const PString & line
117  );
118  bool AddMIME(
119  const PString & fieldName,
120  const PString & fieldValue
121  ) { return InternalAddMIME(fieldName, fieldValue); }
122  bool AddMIME(
123  const PMIMEInfo & mime
124  );
125 
126  virtual bool InternalAddMIME(
127  const PString & fieldName,
128  const PString & fieldValue
129  );
130 
161  const char * key,
162  PStringToString & info
163  ) const { return ParseComplex(GetString(key), info); }
165  const PString & key,
166  PStringToString & info
167  ) const { return ParseComplex(GetString(key), info); }
169  const PCaselessString & key,
170  PStringToString & info
171  ) const { return ParseComplex(GetString(key), info); }
173  const PCaselessString & (*key)(),
174  PStringToString & info
175  ) const { return ParseComplex(GetString(key), info); }
176 
178  static bool ParseComplex(
179  const PString & str,
180  PStringToString & info
181  );
182 
185  bool DecodeMultiPartList(
186  PMultiPartList & parts,
187  const PString & body,
188  const PCaselessString & key
189  ) const;
190 
194  PMultiPartList & parts,
195  const PString & body,
196  const PCaselessString & (*key)() = ContentTypeTag
197  ) const { return DecodeMultiPartList(parts, body, key()); }
198 
199  void AddMultiPartList(
200  PMultiPartList & parts,
201  const PCaselessString & (*key)() = ContentTypeTag
202  );
203 
204  static const PCaselessString & ContentTypeTag();
205  static const PCaselessString & ContentDispositionTag();
207  static const PCaselessString & ContentDescriptionTag();
208  static const PCaselessString & ContentIdTag();
209 
210  static const PCaselessString & TextPlain();
211  static const PCaselessString & TextHTML();
212  static const PCaselessString & ApplicationJSON();
213 
214 
253  static void SetAssociation(
254  const PStringToString & allTypes,
255  PBoolean merge = true
256  );
257  static void SetAssociation(
258  const PString & fileType,
259  const PString & contentType
260  ) { GetContentTypes().SetAt(fileType, contentType); }
261 
269  static PString GetContentType(
270  const PString & fileType
271  );
272 
275  virtual ostream & PrintContents(
276  ostream & strm
277  ) const;
278 
279  private:
280  static PStringToString & GetContentTypes();
281 };
282 
283 
285 // PMultiPartInfo
286 
294 class PMultiPartInfo : public PObject
295 {
296  PCLASSINFO(PMultiPartInfo, PObject);
297  public:
300  const PString & data,
301  const PString & contentType,
302  const PString & disposition = PString::Empty()
303  );
305  const PBYTEArray & data,
306  const PString & contentType,
307  const PString & disposition = PString::Empty()
308  );
309 
310  virtual void PrintOn(ostream & strm) const;
311 
313  void SetMIME();
314 
316  PCaselessString m_charset; // UTF-8, UTF-16, UCS-2
317  PCaselessString m_encoding; // "base64", "7bit", "binary", or "8bit"
322 };
323 
324 
327 class PMultiPartList : public PList<PMultiPartInfo>
328 {
330  public:
331  PMultiPartList();
333  const PString & data,
334  const PString & contentType = PMIMEInfo::TextPlain(),
335  const PString & disposition = PString::Empty()
336  );
337 
338  bool Decode(
339  const PString & body,
340  const PStringToString & contentInfo
341  );
342 
343  PString AsString() const;
344  virtual void PrintOn(ostream & strm) const;
345 
346  template <typename T> void Set(
347  const T & data,
348  const PString & contentType,
349  const PString & disposition = PString::Empty()
350  ) {
351  RemoveAll();
352  Append(new PMultiPartInfo(data, contentType, disposition));
353  }
354 
355  template <typename T> void AddPart(
356  const T & data,
357  const PString & contentType,
358  const PString & disposition = PString::Empty()
359  ) { Append(new PMultiPartInfo(data, contentType, disposition)); }
360 
364  void SetBoundary(const PString & boundary) { m_boundary = boundary; }
365 
367  const PString & GetBoundary() const { return m_boundary; }
368 
369  protected:
371 };
372 
373 
374 #endif // PTLIB_PMIME_H
375 
376 
377 // End Of File ///////////////////////////////////////////////////////////////
virtual void PrintOn(ostream &strm) const
Output the contents of the MIME dictionary to the stream.
PString AsString() const
static const PCaselessString & ContentTypeTag()
bool DecodeMultiPartList(PMultiPartList &parts, const PString &body, const PCaselessString &key) const
Decode parts from a multipart body using the field value.
This class contains the Multipurpose Internet Mail Extensions parameters and variables.
Definition: mime.h:48
This object describes the information associated with multi-part bodies.
Definition: mime.h:327
virtual void PrintOn(ostream &strm) const
virtual bool InternalAddMIME(const PString &fieldName, const PString &fieldValue)
PString m_textBody
Definition: mime.h:320
bool GetComplex(const PString &key, PStringToString &info) const
Definition: mime.h:164
bool GetComplex(const char *key, PStringToString &info) const
Get a complex MIME field.
Definition: mime.h:160
static bool ParseComplex(const PString &str, PStringToString &info)
Parse the string as a complex field, see GetComplex()
This is a dictionary collection class of PString objects, keyed by another string.
Definition: pstring.h:3151
PBoolean Write(PInternetProtocol &socket) const
Write MIME information to the socket.
PCaselessString m_contentType
Definition: mime.h:315
PCaselessString m_disposition
Definition: mime.h:318
bool Decode(const PString &body, const PStringToString &contentInfo)
This class is a variation of a string that ignores case.
Definition: pstring.h:2012
static const PCaselessString & TextHTML()
void Set(const T &data, const PString &contentType, const PString &disposition=PString::Empty())
Definition: mime.h:346
bool AddMIME(const PString &line)
Add a MIME field given a &quot;name: value&quot; format string.
static PString GetContentType(const PString &fileType)
Look up the file type to MIME content type association dictionary and return the MIME content type st...
virtual PINDEX Append(PObject *obj)
Append a new object to the collection.
void AddPart(const T &data, const PString &contentType, const PString &disposition=PString::Empty())
Definition: mime.h:355
This template class maps the PAbstractList to a specific object type.
Definition: lists.h:322
PBYTEArray m_binaryBody
Definition: mime.h:321
virtual void RemoveAll()
Remove all of the elements in the collection.
A TCP/IP socket for process/application layer high level protocols.
Definition: inetprot.h:61
Array of unsigned characters.
Definition: array.h:605
Specialised version of PStringToString to contain a dictionary of options/attributes.
Definition: pstring.h:3249
static const PCaselessString & ContentDispositionTag()
virtual void PrintOn(ostream &strm) const
static const PCaselessString & ApplicationJSON()
PCaselessString m_encoding
Definition: mime.h:317
PMultiPartInfo()
Definition: mime.h:298
PMIMEInfo m_mime
Definition: mime.h:319
static const PCaselessString & ContentTransferEncodingTag()
bool PBoolean
Definition: object.h:174
virtual ostream & PrintContents(ostream &strm) const
Output the contents without the trailing CRLF.
The character string class.
Definition: pstring.h:108
bool GetComplex(const PCaselessString &key, PStringToString &info) const
Definition: mime.h:168
PBoolean Read(PInternetProtocol &socket)
Read MIME information from the socket.
virtual PBoolean SetAt(const K &key, const PString &str)
Add a new object to the collection.
Definition: pstring.h:2978
PString m_boundary
Definition: mime.h:370
bool AddMIME(const PString &fieldName, const PString &fieldValue)
Definition: mime.h:118
const PString & GetBoundary() const
Get the boundary string used for encoding the multipart MIME body.
Definition: mime.h:367
static const PString & Empty()
Return an empty string.
bool GetComplex(const PCaselessString &(*key)(), PStringToString &info) const
Definition: mime.h:172
static void SetAssociation(const PStringToString &allTypes, PBoolean merge=true)
Set an association between a file type and a MIME content type.
void SetBoundary(const PString &boundary)
Set the boundary string used for encoding the multipart MIME body.
Definition: mime.h:364
PString GetString(const char *key, const char *dflt=NULL) const
Get an option value.
Definition: pstring.h:3286
static const PCaselessString & ContentIdTag()
static void SetAssociation(const PString &fileType, const PString &contentType)
Definition: mime.h:257
void AddMultiPartList(PMultiPartList &parts, const PCaselessString &(*key)()=ContentTypeTag)
bool DecodeMultiPartList(PMultiPartList &parts, const PString &body, const PCaselessString &(*key)()=ContentTypeTag) const
Decode parts from a multipart body using the field value.
Definition: mime.h:193
PMIMEInfo()
Definition: mime.h:51
void SetMIME()
Set the m_mime headers from fields.
static const PCaselessString & TextPlain()
This object describes the information associated with one part of a multi-part body.
Definition: mime.h:294
virtual void ReadFrom(istream &strm)
Input the contents of the MIME dictionary from the stream.
PString AsString() const
Return a string presentation of the MIME.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
PCaselessString m_charset
Definition: mime.h:316
static const PCaselessString & ContentDescriptionTag()