Yate: yatemime.h Source File

Yate

yatemime.h
1
22#ifndef __YATEMIME_H
23#define __YATEMIME_H
24
25#ifndef __cplusplus
26#error C++ is required
27#endif
28
29#include <yateclass.h>
30
34 namespace TelEngine {
35
41 class YATE_API MimeHeaderLine : public NamedString
42{
43public:
52 MimeHeaderLine(const char* name, const String& value, char sep = 0);
53
60 MimeHeaderLine(const MimeHeaderLine& original, const char* newName = 0);
61
65 virtual ~MimeHeaderLine();
66
72 virtual void* getObject(const String& name) const;
73
79 virtual MimeHeaderLine* clone(const char* newName = 0) const;
80
86 virtual void buildLine(String& line, bool header = true) const;
87
92 inline MimeHeaderLine& operator=(const char* value)
93 { NamedString::operator=(value); return *this; }
94
99 inline const ObjList& params() const
100 { return m_params; }
101
106 inline char separator() const
107 { return m_separator; }
108
114 void setParam(const char* name, const char* value = 0);
115
120 void delParam(const char* name);
121
127 const NamedString* getParam(const char* name) const;
128
134 static void addQuotes(String& str, bool force = false);
135
141 static void delQuotes(String& str, bool force = false);
142
149 static String quote(const String& str, bool force = false);
150
157 static String unquote(const String& str, bool force = false);
158
166 static int findSep(const char* str, char sep, int offs = 0);
167
174 static void buildHeaders(String& buf, const ObjList& headers);
175
176protected:
177 ObjList m_params; // Header list of parameters
178 char m_separator; // Parameter separator
179private:
180 void operator=(const MimeHeaderLine&); // no assignment
181};
182
187 class YATE_API MimeAuthLine : public MimeHeaderLine
188{
189public:
197 MimeAuthLine(const char* name, const String& value);
198
205 MimeAuthLine(const MimeAuthLine& original, const char* newName = 0);
206
212 virtual void* getObject(const String& name) const;
213
219 virtual MimeHeaderLine* clone(const char* newName = 0) const;
220
226 virtual void buildLine(String& line, bool header = true) const;
227
228private:
229 void operator=(const MimeAuthLine&); // no assignment
230};
231
239 class YATE_API MimeBody : public GenObject
240{
241 YNOCOPY(MimeBody); // no automatic copies please
242public:
246 virtual ~MimeBody();
247
253 virtual void* getObject(const String& name) const;
254
259 inline const MimeHeaderLine& getType() const
260 { return m_type; }
261
267 MimeBody* getFirst(const String& type) const;
268
273 inline const ObjList& headers() const
274 { return m_headers; }
275
280 inline void appendHdr(MimeHeaderLine* hdr)
281 { if (hdr) m_headers.append(hdr); }
282
288 inline void removeHdr(MimeHeaderLine* hdr, bool delobj = true)
289 { if (hdr) m_headers.remove(hdr,delobj); }
290
297 MimeHeaderLine* findHdr(const String& name, const MimeHeaderLine* start = 0) const;
298
303 inline void buildHeaders(String& buf) {
304 m_type.buildLine(buf);
305 buf << "\r\n";
306 MimeHeaderLine::buildHeaders(buf,m_headers);
307 }
308
317 bool setParam(const char* name, const char* value = 0, const char* header = 0);
318
326 bool delParam(const char* name, const char* header = 0);
327
335 const NamedString* getParam(const char* name, const char* header = 0) const;
336
342 const DataBlock& getBody() const;
343
348 inline const DataBlock& body() const
349 { return m_body; }
350
355 virtual bool isSDP() const
356 { return false; }
357
362 virtual bool isMultipart() const
363 { return false; }
364
369 virtual MimeBody* clone() const = 0;
370
381 static MimeBody* build(const char* buf, int len, const MimeHeaderLine& type);
382
389 static String* getUnfoldedLine(const char*& buf, int& len);
390
391protected:
397 MimeBody(const String& type);
398
405 MimeBody(const MimeHeaderLine& type);
406
410 virtual void buildBody() const = 0;
411
415 mutable DataBlock m_body;
416
420 ObjList m_headers;
421
422private:
423 MimeHeaderLine m_type; // Content type header line
424};
425
430 class YATE_API MimeMultipartBody : public MimeBody
431{
432public:
440 explicit MimeMultipartBody(const char* subtype = "mixed", const char* boundary = 0);
441
448 MimeMultipartBody(const String& type, const char* buf, int len);
449
456 MimeMultipartBody(const MimeHeaderLine& type, const char* buf, int len);
457
461 virtual ~MimeMultipartBody();
462
467 inline const ObjList& bodies() const
468 { return m_bodies; }
469
474 inline void appendBody(MimeBody* body)
475 { if (body) m_bodies.append(body); }
476
482 inline void removeBody(MimeBody* body, bool delobj = true)
483 { if (body) m_bodies.remove(body,delobj); }
484
493 MimeBody* findBody(const String& content, MimeBody** start = 0) const;
494
500 virtual void* getObject(const String& name) const;
501
506 virtual bool isMultipart() const
507 { return true; }
508
513 virtual MimeBody* clone() const;
514
515protected:
519 MimeMultipartBody(const MimeMultipartBody& original);
520
524 virtual void buildBody() const;
525
532 void parse(const char* buf, int len);
533
534private:
535 // Parse input buffer for first body boundary or data end
536 // Advance buffer pass the boundary line and decrease the buffer length
537 // Set endBody to true if the last boundary was found
538 // Return the length of data before the found boundary
539 int findBoundary(const char*& buf, int& len,
540 const char* boundary, unsigned int bLen, bool& endBody);
541 // Build a boundary string to be used when parsing or building body
542 // Remove quotes if present. Trim blanks
543 // Insert CRLF and boundary marks ('--') before parameter
544 // @param boundary Destination string
545 // @return False if the parameter is missing or the boundary is empty
546 bool getBoundary(String& boundary) const;
547
548
549 ObjList m_bodies; // The list of bodies contained in this multipart
550};
551
556 class YATE_API MimeSdpBody : public MimeBody
557{
558public:
563 MimeSdpBody(bool hashing = false);
564
571 MimeSdpBody(const String& type, const char* buf, int len);
572
579 MimeSdpBody(const MimeHeaderLine& type, const char* buf, int len);
580
584 virtual ~MimeSdpBody();
585
591 virtual void* getObject(const String& name) const;
592
597 virtual bool isSDP() const
598 { return true; }
599
604 virtual MimeBody* clone() const;
605
610 inline const ObjList& lines() const
611 { return m_lines; }
612
617 inline unsigned int hash() const
618 { return m_hash; }
619
626 NamedString* addLine(const char* name, const char* value = 0);
627
633 const NamedString* getLine(const char* name) const;
634
640 const NamedString* getNextLine(const NamedString* line) const;
641
642protected:
646 MimeSdpBody(const MimeSdpBody& original);
647
651 virtual void buildBody() const;
652
653private:
654 // Build the lines from a data buffer
655 void buildLines(const char* buf, int len);
656
657 ObjList m_lines;
658 ObjList* m_lineAppend;
659 unsigned int m_hash;
660 bool m_hashing;
661};
662
667 class YATE_API MimeBinaryBody : public MimeBody
668{
669public:
676 MimeBinaryBody(const String& type, const char* buf, int len);
677
684 MimeBinaryBody(const MimeHeaderLine& type, const char* buf, int len);
685
689 virtual ~MimeBinaryBody();
690
696 virtual void* getObject(const String& name) const;
697
702 virtual MimeBody* clone() const;
703
704protected:
708 MimeBinaryBody(const MimeBinaryBody& original);
709
713 virtual void buildBody() const;
714};
715
720 class YATE_API MimeStringBody : public MimeBody
721{
722public:
729 MimeStringBody(const String& type, const char* buf, int len = -1);
730
737 MimeStringBody(const MimeHeaderLine& type, const char* buf, int len = -1);
738
742 virtual ~MimeStringBody();
743
749 virtual void* getObject(const String& name) const;
750
755 virtual MimeBody* clone() const;
756
761 inline const String& text() const
762 { return m_text; }
763
764protected:
768 MimeStringBody(const MimeStringBody& original);
769
773 virtual void buildBody() const;
774
775private:
776 String m_text;
777};
778
783 class YATE_API MimeLinesBody : public MimeBody
784{
785public:
792 MimeLinesBody(const String& type, const char* buf, int len);
793
800 MimeLinesBody(const MimeHeaderLine& type, const char* buf, int len);
801
805 virtual ~MimeLinesBody();
806
812 virtual void* getObject(const String& name) const;
813
818 virtual MimeBody* clone() const;
819
824 inline const ObjList& lines() const
825 { return m_lines; }
826
831 inline void addLine(const char* line)
832 { m_lines.append(new String(line)); }
833
834protected:
838 MimeLinesBody(const MimeLinesBody& original);
839
843 virtual void buildBody() const;
844
845private:
846 ObjList m_lines;
847};
848
849}; // namespace TelEngine
850
851#endif /* __YATEMIME_H */
852
853/* vi: set ts=8 sw=4 sts=4 noet: */
DataBlock
A class that holds just a block of raw data.
Definition yateclass.h:6074
GenObject
Definition yateclass.h:1627
NamedString
A named string class.
Definition yateclass.h:5304
NamedString::operator=
NamedString & operator=(const char *value)
Definition yateclass.h:5338
NamedString::name
const String & name() const
Definition yateclass.h:5319
ObjList
An object list class.
Definition yateclass.h:2032
ObjList::append
ObjList * append(const GenObject *obj, bool compact=true)
ObjList::remove
GenObject * remove(bool delobj=true)
String
A C-style string handling class.
Definition yateclass.h:3055
TelEngine::MimeAuthLine
MIME authentication line.
Definition yatemime.h:188
TelEngine::MimeAuthLine::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeAuthLine::clone
virtual MimeHeaderLine * clone(const char *newName=0) const
TelEngine::MimeAuthLine::MimeAuthLine
MimeAuthLine(const MimeAuthLine &original, const char *newName=0)
TelEngine::MimeAuthLine::MimeAuthLine
MimeAuthLine(const char *name, const String &value)
TelEngine::MimeAuthLine::buildLine
virtual void buildLine(String &line, bool header=true) const
TelEngine::MimeBinaryBody
MIME for obscure binary data.
Definition yatemime.h:668
TelEngine::MimeBinaryBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeBinaryBody::MimeBinaryBody
MimeBinaryBody(const String &type, const char *buf, int len)
TelEngine::MimeBinaryBody::buildBody
virtual void buildBody() const
TelEngine::MimeBinaryBody::~MimeBinaryBody
virtual ~MimeBinaryBody()
TelEngine::MimeBinaryBody::clone
virtual MimeBody * clone() const
TelEngine::MimeBinaryBody::MimeBinaryBody
MimeBinaryBody(const MimeBinaryBody &original)
TelEngine::MimeBinaryBody::MimeBinaryBody
MimeBinaryBody(const MimeHeaderLine &type, const char *buf, int len)
TelEngine::MimeBody
Abstract MIME data holder.
Definition yatemime.h:240
TelEngine::MimeBody::isSDP
virtual bool isSDP() const
Definition yatemime.h:355
TelEngine::MimeBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeBody::getBody
const DataBlock & getBody() const
TelEngine::MimeBody::findHdr
MimeHeaderLine * findHdr(const String &name, const MimeHeaderLine *start=0) const
TelEngine::MimeBody::setParam
bool setParam(const char *name, const char *value=0, const char *header=0)
TelEngine::MimeBody::getFirst
MimeBody * getFirst(const String &type) const
TelEngine::MimeBody::delParam
bool delParam(const char *name, const char *header=0)
TelEngine::MimeBody::appendHdr
void appendHdr(MimeHeaderLine *hdr)
Definition yatemime.h:280
TelEngine::MimeBody::getParam
const NamedString * getParam(const char *name, const char *header=0) const
TelEngine::MimeBody::m_headers
ObjList m_headers
Definition yatemime.h:420
TelEngine::MimeBody::removeHdr
void removeHdr(MimeHeaderLine *hdr, bool delobj=true)
Definition yatemime.h:288
TelEngine::MimeBody::build
static MimeBody * build(const char *buf, int len, const MimeHeaderLine &type)
TelEngine::MimeBody::MimeBody
MimeBody(const MimeHeaderLine &type)
TelEngine::MimeBody::getType
const MimeHeaderLine & getType() const
Definition yatemime.h:259
TelEngine::MimeBody::body
const DataBlock & body() const
Definition yatemime.h:348
TelEngine::MimeBody::MimeBody
MimeBody(const String &type)
TelEngine::MimeBody::isMultipart
virtual bool isMultipart() const
Definition yatemime.h:362
TelEngine::MimeBody::getUnfoldedLine
static String * getUnfoldedLine(const char *&buf, int &len)
TelEngine::MimeBody::buildBody
virtual void buildBody() const =0
TelEngine::MimeBody::m_body
DataBlock m_body
Definition yatemime.h:415
TelEngine::MimeBody::clone
virtual MimeBody * clone() const =0
TelEngine::MimeBody::headers
const ObjList & headers() const
Definition yatemime.h:273
TelEngine::MimeBody::buildHeaders
void buildHeaders(String &buf)
Definition yatemime.h:303
TelEngine::MimeBody::~MimeBody
virtual ~MimeBody()
TelEngine::MimeHeaderLine
MIME header line.
Definition yatemime.h:42
TelEngine::MimeHeaderLine::unquote
static String unquote(const String &str, bool force=false)
TelEngine::MimeHeaderLine::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeHeaderLine::clone
virtual MimeHeaderLine * clone(const char *newName=0) const
TelEngine::MimeHeaderLine::MimeHeaderLine
MimeHeaderLine(const char *name, const String &value, char sep=0)
TelEngine::MimeHeaderLine::delQuotes
static void delQuotes(String &str, bool force=false)
TelEngine::MimeHeaderLine::addQuotes
static void addQuotes(String &str, bool force=false)
TelEngine::MimeHeaderLine::quote
static String quote(const String &str, bool force=false)
TelEngine::MimeHeaderLine::findSep
static int findSep(const char *str, char sep, int offs=0)
TelEngine::MimeHeaderLine::getParam
const NamedString * getParam(const char *name) const
TelEngine::MimeHeaderLine::params
const ObjList & params() const
Definition yatemime.h:99
TelEngine::MimeHeaderLine::buildLine
virtual void buildLine(String &line, bool header=true) const
TelEngine::MimeHeaderLine::separator
char separator() const
Definition yatemime.h:106
TelEngine::MimeHeaderLine::~MimeHeaderLine
virtual ~MimeHeaderLine()
TelEngine::MimeHeaderLine::MimeHeaderLine
MimeHeaderLine(const MimeHeaderLine &original, const char *newName=0)
TelEngine::MimeHeaderLine::operator=
MimeHeaderLine & operator=(const char *value)
Definition yatemime.h:92
TelEngine::MimeHeaderLine::setParam
void setParam(const char *name, const char *value=0)
TelEngine::MimeHeaderLine::buildHeaders
static void buildHeaders(String &buf, const ObjList &headers)
TelEngine::MimeHeaderLine::delParam
void delParam(const char *name)
TelEngine::MimeLinesBody
MIME for multiple text lines.
Definition yatemime.h:784
TelEngine::MimeLinesBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeLinesBody::addLine
void addLine(const char *line)
Definition yatemime.h:831
TelEngine::MimeLinesBody::~MimeLinesBody
virtual ~MimeLinesBody()
TelEngine::MimeLinesBody::buildBody
virtual void buildBody() const
TelEngine::MimeLinesBody::MimeLinesBody
MimeLinesBody(const MimeHeaderLine &type, const char *buf, int len)
TelEngine::MimeLinesBody::lines
const ObjList & lines() const
Definition yatemime.h:824
TelEngine::MimeLinesBody::MimeLinesBody
MimeLinesBody(const String &type, const char *buf, int len)
TelEngine::MimeLinesBody::clone
virtual MimeBody * clone() const
TelEngine::MimeLinesBody::MimeLinesBody
MimeLinesBody(const MimeLinesBody &original)
TelEngine::MimeMultipartBody
MIME multipart container.
Definition yatemime.h:431
TelEngine::MimeMultipartBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeMultipartBody::findBody
MimeBody * findBody(const String &content, MimeBody **start=0) const
TelEngine::MimeMultipartBody::bodies
const ObjList & bodies() const
Definition yatemime.h:467
TelEngine::MimeMultipartBody::buildBody
virtual void buildBody() const
TelEngine::MimeMultipartBody::MimeMultipartBody
MimeMultipartBody(const MimeHeaderLine &type, const char *buf, int len)
TelEngine::MimeMultipartBody::MimeMultipartBody
MimeMultipartBody(const String &type, const char *buf, int len)
TelEngine::MimeMultipartBody::removeBody
void removeBody(MimeBody *body, bool delobj=true)
Definition yatemime.h:482
TelEngine::MimeMultipartBody::MimeMultipartBody
MimeMultipartBody(const MimeMultipartBody &original)
TelEngine::MimeMultipartBody::~MimeMultipartBody
virtual ~MimeMultipartBody()
TelEngine::MimeMultipartBody::clone
virtual MimeBody * clone() const
TelEngine::MimeMultipartBody::isMultipart
virtual bool isMultipart() const
Definition yatemime.h:506
TelEngine::MimeMultipartBody::MimeMultipartBody
MimeMultipartBody(const char *subtype="mixed", const char *boundary=0)
TelEngine::MimeMultipartBody::appendBody
void appendBody(MimeBody *body)
Definition yatemime.h:474
TelEngine::MimeMultipartBody::parse
void parse(const char *buf, int len)
TelEngine::MimeSdpBody
MIME for application/sdp.
Definition yatemime.h:557
TelEngine::MimeSdpBody::~MimeSdpBody
virtual ~MimeSdpBody()
TelEngine::MimeSdpBody::isSDP
virtual bool isSDP() const
Definition yatemime.h:597
TelEngine::MimeSdpBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeSdpBody::buildBody
virtual void buildBody() const
TelEngine::MimeSdpBody::getNextLine
const NamedString * getNextLine(const NamedString *line) const
TelEngine::MimeSdpBody::lines
const ObjList & lines() const
Definition yatemime.h:610
TelEngine::MimeSdpBody::addLine
NamedString * addLine(const char *name, const char *value=0)
TelEngine::MimeSdpBody::MimeSdpBody
MimeSdpBody(const String &type, const char *buf, int len)
TelEngine::MimeSdpBody::clone
virtual MimeBody * clone() const
TelEngine::MimeSdpBody::hash
unsigned int hash() const
Definition yatemime.h:617
TelEngine::MimeSdpBody::MimeSdpBody
MimeSdpBody(bool hashing=false)
TelEngine::MimeSdpBody::MimeSdpBody
MimeSdpBody(const MimeHeaderLine &type, const char *buf, int len)
TelEngine::MimeSdpBody::getLine
const NamedString * getLine(const char *name) const
TelEngine::MimeSdpBody::MimeSdpBody
MimeSdpBody(const MimeSdpBody &original)
TelEngine::MimeStringBody
MIME for one text string.
Definition yatemime.h:721
TelEngine::MimeStringBody::MimeStringBody
MimeStringBody(const String &type, const char *buf, int len=-1)
TelEngine::MimeStringBody::getObject
virtual void * getObject(const String &name) const
TelEngine::MimeStringBody::MimeStringBody
MimeStringBody(const MimeStringBody &original)
TelEngine::MimeStringBody::buildBody
virtual void buildBody() const
TelEngine::MimeStringBody::~MimeStringBody
virtual ~MimeStringBody()
TelEngine::MimeStringBody::text
const String & text() const
Definition yatemime.h:761
TelEngine::MimeStringBody::clone
virtual MimeBody * clone() const
TelEngine::MimeStringBody::MimeStringBody
MimeStringBody(const MimeHeaderLine &type, const char *buf, int len=-1)
TelEngine
Definition yatemime.h:34