Yate
|
00001 /* 00002 * yatemime.h 00003 * This file is part of the YATE Project http://YATE.null.ro 00004 * 00005 * MIME types, body codecs and related functions 00006 * 00007 * Yet Another Telephony Engine - a fully featured software PBX and IVR 00008 * Copyright (C) 2004-2006 Null Team 00009 * 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00023 */ 00024 00025 #ifndef __YATEMIME_H 00026 #define __YATEMIME_H 00027 00028 #ifndef __cplusplus 00029 #error C++ is required 00030 #endif 00031 00032 #include <yateclass.h> 00033 00037 namespace TelEngine { 00038 00044 class YATE_API MimeHeaderLine : public NamedString 00045 { 00046 public: 00055 MimeHeaderLine(const char* name, const String& value, char sep = 0); 00056 00063 MimeHeaderLine(const MimeHeaderLine& original, const char* newName = 0); 00064 00068 virtual ~MimeHeaderLine(); 00069 00075 virtual void* getObject(const String& name) const; 00076 00082 virtual MimeHeaderLine* clone(const char* newName = 0) const; 00083 00088 virtual void buildLine(String& line) const; 00089 00094 inline MimeHeaderLine& operator=(const char* value) 00095 { NamedString::operator=(value); return *this; } 00096 00101 inline const ObjList& params() const 00102 { return m_params; } 00103 00108 inline char separator() const 00109 { return m_separator; } 00110 00116 void setParam(const char* name, const char* value = 0); 00117 00122 void delParam(const char* name); 00123 00129 const NamedString* getParam(const char* name) const; 00130 00136 static void addQuotes(String& str, bool force = false); 00137 00143 static void delQuotes(String& str, bool force = false); 00144 00151 static String quote(const String& str, bool force = false); 00152 00159 static String unquote(const String& str, bool force = false); 00160 00168 static int findSep(const char* str, char sep, int offs = 0); 00169 00176 static void buildHeaders(String& buf, const ObjList& headers); 00177 00178 protected: 00179 ObjList m_params; // Header list of parameters 00180 char m_separator; // Parameter separator 00181 }; 00182 00187 class YATE_API MimeAuthLine : public MimeHeaderLine 00188 { 00189 public: 00197 MimeAuthLine(const char* name, const String& value); 00198 00205 MimeAuthLine(const MimeAuthLine& original, const char* newName = 0); 00206 00212 virtual void* getObject(const String& name) const; 00213 00219 virtual MimeHeaderLine* clone(const char* newName = 0) const; 00220 00225 virtual void buildLine(String& line) const; 00226 }; 00227 00235 class YATE_API MimeBody : public GenObject 00236 { 00237 public: 00241 virtual ~MimeBody(); 00242 00248 virtual void* getObject(const String& name) const; 00249 00254 inline const MimeHeaderLine& getType() const 00255 { return m_type; } 00256 00262 MimeBody* getFirst(const String& type) const; 00263 00268 inline const ObjList& headers() const 00269 { return m_headers; } 00270 00275 inline void appendHdr(MimeHeaderLine* hdr) 00276 { if (hdr) m_headers.append(hdr); } 00277 00283 inline void removeHdr(MimeHeaderLine* hdr, bool delobj = true) 00284 { if (hdr) m_headers.remove(hdr,delobj); } 00285 00292 MimeHeaderLine* findHdr(const String& name, const MimeHeaderLine* start = 0) const; 00293 00298 inline void buildHeaders(String& buf) { 00299 m_type.buildLine(buf); 00300 buf << "\r\n"; 00301 MimeHeaderLine::buildHeaders(buf,m_headers); 00302 } 00303 00312 bool setParam(const char* name, const char* value = 0, const char* header = 0); 00313 00321 bool delParam(const char* name, const char* header = 0); 00322 00330 const NamedString* getParam(const char* name, const char* header = 0) const; 00331 00337 const DataBlock& getBody() const; 00338 00343 inline const DataBlock& body() const 00344 { return m_body; } 00345 00350 virtual bool isSDP() const 00351 { return false; } 00352 00357 virtual bool isMultipart() const 00358 { return false; } 00359 00364 virtual MimeBody* clone() const = 0; 00365 00376 static MimeBody* build(const char* buf, int len, const MimeHeaderLine& type); 00377 00384 static String* getUnfoldedLine(const char*& buf, int& len); 00385 00386 protected: 00392 MimeBody(const String& type); 00393 00400 MimeBody(const MimeHeaderLine& type); 00401 00405 virtual void buildBody() const = 0; 00406 00410 mutable DataBlock m_body; 00411 00415 ObjList m_headers; 00416 00417 private: 00418 MimeHeaderLine m_type; // Content type header line 00419 }; 00420 00425 class YATE_API MimeMultipartBody : public MimeBody 00426 { 00427 public: 00435 MimeMultipartBody(const char* subtype = "mixed", const char* boundary = 0); 00436 00443 MimeMultipartBody(const String& type, const char* buf, int len); 00444 00451 MimeMultipartBody(const MimeHeaderLine& type, const char* buf, int len); 00452 00456 virtual ~MimeMultipartBody(); 00457 00462 inline const ObjList& bodies() const 00463 { return m_bodies; } 00464 00469 inline void appendBody(MimeBody* body) 00470 { if (body) m_bodies.append(body); } 00471 00477 inline void removeBody(MimeBody* body, bool delobj = true) 00478 { if (body) m_bodies.remove(body,delobj); } 00479 00488 MimeBody* findBody(const String& content, MimeBody** start = 0) const; 00489 00495 virtual void* getObject(const String& name) const; 00496 00501 virtual bool isMultipart() const 00502 { return true; } 00503 00508 virtual MimeBody* clone() const; 00509 00510 protected: 00514 MimeMultipartBody(const MimeMultipartBody& original); 00515 00519 virtual void buildBody() const; 00520 00527 void parse(const char* buf, int len); 00528 00529 private: 00530 // Parse input buffer for first body boundary or data end 00531 // Advance buffer pass the boundary line and decrease the buffer length 00532 // Set endBody to true if the last boundary was found 00533 // Return the length of data before the found boundary 00534 int findBoundary(const char*& buf, int& len, 00535 const char* boundary, unsigned int bLen, bool& endBody); 00536 // Build a boundary string to be used when parsing or building body 00537 // Remove quotes if present. Trim blanks 00538 // Insert CRLF and boundary marks ('--') before parameter 00539 // @param boundary Destination string 00540 // @return False if the parameter is missing or the boundary is empty 00541 bool getBoundary(String& boundary) const; 00542 00543 00544 ObjList m_bodies; // The list of bodies contained in this multipart 00545 }; 00546 00551 class YATE_API MimeSdpBody : public MimeBody 00552 { 00553 public: 00557 MimeSdpBody(); 00558 00565 MimeSdpBody(const String& type, const char* buf, int len); 00566 00573 MimeSdpBody(const MimeHeaderLine& type, const char* buf, int len); 00574 00578 virtual ~MimeSdpBody(); 00579 00585 virtual void* getObject(const String& name) const; 00586 00591 virtual bool isSDP() const 00592 { return true; } 00593 00598 virtual MimeBody* clone() const; 00599 00604 inline const ObjList& lines() const 00605 { return m_lines; } 00606 00612 inline void addLine(const char* name, const char* value = 0) 00613 { m_lines.append(new NamedString(name,value)); } 00614 00620 const NamedString* getLine(const char* name) const; 00621 00627 const NamedString* getNextLine(const NamedString* line) const; 00628 00629 protected: 00633 MimeSdpBody(const MimeSdpBody& original); 00634 00638 virtual void buildBody() const; 00639 00640 private: 00641 // Build the lines from a data buffer 00642 void buildLines(const char* buf, int len); 00643 00644 ObjList m_lines; 00645 }; 00646 00651 class YATE_API MimeBinaryBody : public MimeBody 00652 { 00653 public: 00660 MimeBinaryBody(const String& type, const char* buf, int len); 00661 00668 MimeBinaryBody(const MimeHeaderLine& type, const char* buf, int len); 00669 00673 virtual ~MimeBinaryBody(); 00674 00680 virtual void* getObject(const String& name) const; 00681 00686 virtual MimeBody* clone() const; 00687 00688 protected: 00692 MimeBinaryBody(const MimeBinaryBody& original); 00693 00697 virtual void buildBody() const; 00698 }; 00699 00704 class YATE_API MimeStringBody : public MimeBody 00705 { 00706 public: 00713 MimeStringBody(const String& type, const char* buf, int len = -1); 00714 00721 MimeStringBody(const MimeHeaderLine& type, const char* buf, int len = -1); 00722 00726 virtual ~MimeStringBody(); 00727 00733 virtual void* getObject(const String& name) const; 00734 00739 virtual MimeBody* clone() const; 00740 00745 inline const String& text() const 00746 { return m_text; } 00747 00748 protected: 00752 MimeStringBody(const MimeStringBody& original); 00753 00757 virtual void buildBody() const; 00758 00759 private: 00760 String m_text; 00761 }; 00762 00767 class YATE_API MimeLinesBody : public MimeBody 00768 { 00769 public: 00776 MimeLinesBody(const String& type, const char* buf, int len); 00777 00784 MimeLinesBody(const MimeHeaderLine& type, const char* buf, int len); 00785 00789 virtual ~MimeLinesBody(); 00790 00796 virtual void* getObject(const String& name) const; 00797 00802 virtual MimeBody* clone() const; 00803 00808 inline const ObjList& lines() const 00809 { return m_lines; } 00810 00815 inline void addLine(const char* line) 00816 { m_lines.append(new String(line)); } 00817 00818 protected: 00822 MimeLinesBody(const MimeLinesBody& original); 00823 00827 virtual void buildBody() const; 00828 00829 private: 00830 ObjList m_lines; 00831 }; 00832 00833 }; // namespace TelEngine 00834 00835 #endif /* __YATEMIME_H */ 00836 00837 /* vi: set ts=8 sw=4 sts=4 noet: */