Yate
yatemime.h
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 private:
00182     void operator=(const MimeHeaderLine&); // no assignment
00183 };
00184 
00189 class YATE_API MimeAuthLine : public MimeHeaderLine
00190 {
00191 public:
00199     MimeAuthLine(const char* name, const String& value);
00200 
00207     MimeAuthLine(const MimeAuthLine& original, const char* newName = 0);
00208 
00214     virtual void* getObject(const String& name) const;
00215 
00221     virtual MimeHeaderLine* clone(const char* newName = 0) const;
00222 
00227     virtual void buildLine(String& line) const;
00228 private:
00229     void operator=(const MimeAuthLine&); // no assignment
00230 };
00231 
00239 class YATE_API MimeBody : public GenObject
00240 {
00241     YNOCOPY(MimeBody); // no automatic copies please
00242 public:
00246     virtual ~MimeBody();
00247 
00253     virtual void* getObject(const String& name) const;
00254 
00259     inline const MimeHeaderLine& getType() const
00260         { return m_type; }
00261 
00267     MimeBody* getFirst(const String& type) const;
00268 
00273     inline const ObjList& headers() const
00274         { return m_headers; }
00275 
00280     inline void appendHdr(MimeHeaderLine* hdr)
00281         { if (hdr) m_headers.append(hdr); }
00282 
00288     inline void removeHdr(MimeHeaderLine* hdr, bool delobj = true)
00289         { if (hdr) m_headers.remove(hdr,delobj); }
00290 
00297     MimeHeaderLine* findHdr(const String& name, const MimeHeaderLine* start = 0) const;
00298 
00303     inline void buildHeaders(String& buf) {
00304             m_type.buildLine(buf);
00305             buf << "\r\n";
00306             MimeHeaderLine::buildHeaders(buf,m_headers);
00307         }
00308 
00317     bool setParam(const char* name, const char* value = 0, const char* header = 0);
00318 
00326     bool delParam(const char* name, const char* header = 0);
00327 
00335     const NamedString* getParam(const char* name, const char* header = 0) const;
00336 
00342     const DataBlock& getBody() const;
00343 
00348     inline const DataBlock& body() const
00349         { return m_body; }
00350 
00355     virtual bool isSDP() const
00356         { return false; }
00357 
00362     virtual bool isMultipart() const
00363         { return false; }
00364 
00369     virtual MimeBody* clone() const = 0;
00370 
00381     static MimeBody* build(const char* buf, int len, const MimeHeaderLine& type);
00382 
00389     static String* getUnfoldedLine(const char*& buf, int& len);
00390 
00391 protected:
00397     MimeBody(const String& type);
00398 
00405     MimeBody(const MimeHeaderLine& type);
00406 
00410     virtual void buildBody() const = 0;
00411 
00415     mutable DataBlock m_body;
00416 
00420     ObjList m_headers;
00421 
00422 private:
00423     MimeHeaderLine m_type;               // Content type header line
00424 };
00425 
00430 class YATE_API MimeMultipartBody : public MimeBody
00431 {
00432 public:
00440     explicit MimeMultipartBody(const char* subtype = "mixed", const char* boundary = 0);
00441 
00448     MimeMultipartBody(const String& type, const char* buf, int len);
00449 
00456     MimeMultipartBody(const MimeHeaderLine& type, const char* buf, int len);
00457 
00461     virtual ~MimeMultipartBody();
00462 
00467     inline const ObjList& bodies() const
00468         { return m_bodies; }
00469 
00474     inline void appendBody(MimeBody* body)
00475         { if (body) m_bodies.append(body); }
00476 
00482     inline void removeBody(MimeBody* body, bool delobj = true)
00483         { if (body) m_bodies.remove(body,delobj); }
00484 
00493     MimeBody* findBody(const String& content, MimeBody** start = 0) const;
00494 
00500     virtual void* getObject(const String& name) const;
00501 
00506     virtual bool isMultipart() const
00507         { return true; }
00508 
00513     virtual MimeBody* clone() const;
00514 
00515 protected:
00519     MimeMultipartBody(const MimeMultipartBody& original);
00520 
00524     virtual void buildBody() const;
00525 
00532     void parse(const char* buf, int len);
00533 
00534 private:
00535     // Parse input buffer for first body boundary or data end
00536     // Advance buffer pass the boundary line and decrease the buffer length
00537     // Set endBody to true if the last boundary was found
00538     // Return the length of data before the found boundary
00539     int findBoundary(const char*& buf, int& len,
00540         const char* boundary, unsigned int bLen, bool& endBody);
00541     // Build a boundary string to be used when parsing or building body
00542     // Remove quotes if present. Trim blanks
00543     // Insert CRLF and boundary marks ('--') before parameter
00544     // @param boundary Destination string
00545     // @return False if the parameter is missing or the boundary is empty
00546     bool getBoundary(String& boundary) const;
00547 
00548 
00549     ObjList m_bodies;                    // The list of bodies contained in this multipart
00550 };
00551 
00556 class YATE_API MimeSdpBody : public MimeBody
00557 {
00558 public:
00562     MimeSdpBody();
00563 
00570     MimeSdpBody(const String& type, const char* buf, int len);
00571 
00578     MimeSdpBody(const MimeHeaderLine& type, const char* buf, int len);
00579 
00583     virtual ~MimeSdpBody();
00584 
00590     virtual void* getObject(const String& name) const;
00591 
00596     virtual bool isSDP() const
00597         { return true; }
00598 
00603     virtual MimeBody* clone() const;
00604 
00609     inline const ObjList& lines() const
00610         { return m_lines; }
00611 
00617     inline void addLine(const char* name, const char* value = 0)
00618         { m_lines.append(new NamedString(name,value)); }
00619 
00625     const NamedString* getLine(const char* name) const;
00626 
00632     const NamedString* getNextLine(const NamedString* line) const;
00633 
00634 protected:
00638     MimeSdpBody(const MimeSdpBody& original);
00639 
00643     virtual void buildBody() const;
00644 
00645 private:
00646     // Build the lines from a data buffer
00647     void buildLines(const char* buf, int len);
00648 
00649     ObjList m_lines;
00650 };
00651 
00656 class YATE_API MimeBinaryBody : public MimeBody
00657 {
00658 public:
00665     MimeBinaryBody(const String& type, const char* buf, int len);
00666 
00673     MimeBinaryBody(const MimeHeaderLine& type, const char* buf, int len);
00674 
00678     virtual ~MimeBinaryBody();
00679 
00685     virtual void* getObject(const String& name) const;
00686 
00691     virtual MimeBody* clone() const;
00692 
00693 protected:
00697     MimeBinaryBody(const MimeBinaryBody& original);
00698 
00702     virtual void buildBody() const;
00703 };
00704 
00709 class YATE_API MimeStringBody : public MimeBody
00710 {
00711 public:
00718     MimeStringBody(const String& type, const char* buf, int len = -1);
00719 
00726     MimeStringBody(const MimeHeaderLine& type, const char* buf, int len = -1);
00727 
00731     virtual ~MimeStringBody();
00732 
00738     virtual void* getObject(const String& name) const;
00739 
00744     virtual MimeBody* clone() const;
00745 
00750     inline const String& text() const
00751         { return m_text; }
00752 
00753 protected:
00757     MimeStringBody(const MimeStringBody& original);
00758 
00762     virtual void buildBody() const;
00763 
00764 private:
00765     String m_text;
00766 };
00767 
00772 class YATE_API MimeLinesBody : public MimeBody
00773 {
00774 public:
00781     MimeLinesBody(const String& type, const char* buf, int len);
00782 
00789     MimeLinesBody(const MimeHeaderLine& type, const char* buf, int len);
00790 
00794     virtual ~MimeLinesBody();
00795 
00801     virtual void* getObject(const String& name) const;
00802 
00807     virtual MimeBody* clone() const;
00808 
00813     inline const ObjList& lines() const
00814         { return m_lines; }
00815 
00820     inline void addLine(const char* line)
00821         { m_lines.append(new String(line)); }
00822 
00823 protected:
00827     MimeLinesBody(const MimeLinesBody& original);
00828 
00832     virtual void buildBody() const;
00833 
00834 private:
00835     ObjList m_lines;
00836 };
00837 
00838 }; // namespace TelEngine
00839 
00840 #endif /* __YATEMIME_H */
00841 
00842 /* vi: set ts=8 sw=4 sts=4 noet: */