mime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_MIME_H
30 #define CXXTOOLS_MIME_H
31 
32 #include <iosfwd>
33 #include <string>
34 #include <vector>
35 #include <utility>
36 
37 namespace cxxtools
38 {
39 class MimeEntity;
40 class MimeMultipart;
41 class SerializationInfo;
42 
46 {
47  friend std::ostream& operator<< (std::ostream& out, const MimeHeader& mimeHeader);
48  friend void operator<<= (SerializationInfo& si, const MimeHeader& mh);
49  friend void operator>>= (const SerializationInfo& si, MimeHeader& mh);
50 
51  typedef std::vector<std::pair<std::string, std::string> > HeadersType;
52  HeadersType headers;
53 
54  public:
56  std::string getHeader(const std::string& key, const std::string& def = std::string()) const;
57 
59  void setHeader(const std::string& key, const std::string& value, bool replace = true);
60 
62  void addHeader(const std::string& key, const std::string& value)
63  { headers.push_back(HeadersType::value_type(key, value)); }
64 
66  void unsetHeader(const std::string& key);
67 
69  bool isMultipart() const;
70 };
71 
77 class MimeEntity : public MimeHeader
78 {
79  friend std::ostream& operator<< (std::ostream& out, const MimeEntity& mimeEntity);
80  friend void operator<<= (SerializationInfo& si, const MimeEntity& mo);
81  friend void operator>>= (const SerializationInfo& si, MimeEntity& mo);
82  friend MimeEntity& operator<< (MimeEntity& me, const std::string& str);
83  friend MimeEntity& operator<< (MimeEntity& me, const char* str);
84 
85  public:
90  };
91 
92  private:
93  std::string body;
94 
95  public:
97  MimeEntity() { }
98 
101  explicit MimeEntity(const std::string& data);
102 
104  explicit MimeEntity(std::istream& in);
105 
107  const std::string& getBody() const { return body; }
109  std::string& getBody() { return body; }
110 
112  MimeEntity& setBody(const std::string& b) { body = b; return *this; }
113  MimeEntity& readBody(std::istream& in);
114 
117 
118  MimeEntity& setContentType(const std::string& ct)
119  { setHeader("Content-Type", ct); return *this; }
120  std::string getContentType() const
121  { return getHeader("Content-Type"); }
122 
123  MimeEntity& setHeader(const std::string& key, const std::string& value, bool replace = true)
124  { MimeHeader::setHeader(key, value, replace); return *this; }
125 };
126 
128 inline MimeEntity& operator<< (MimeEntity& me, const std::string& str)
129 {
130  me.body.append(str);
131  return me;
132 }
133 
135 inline MimeEntity& operator<< (MimeEntity& me, const char* str)
136 {
137  me.body.append(str);
138  return me;
139 }
140 
141 
144 class MimeMultipart : public MimeHeader
145 {
146  friend std::ostream& operator<< (std::ostream& out, const MimeMultipart& mime);
147  friend void operator<<= (SerializationInfo& si, const MimeMultipart& mm);
148  friend void operator>>= (const SerializationInfo& si, MimeMultipart& mm);
149  friend void operator>>= (const SerializationInfo& si, MimeEntity& mo);
150 
151  public:
153 
154  enum Type {
160  };
161 
162  private:
163  typedef std::vector<MimeEntity> PartsType;
164  PartsType parts;
165 
166  std::string type; // mixed or alternative
167 
168  void partsFromBody(const std::string& body, std::string::size_type pos = 0);
169  static std::string stringParts(const std::vector<MimeEntity>& parts, std::vector<std::string>& sparts);
170 
171  public:
172  typedef PartsType::const_iterator const_iterator;
173  typedef PartsType::iterator iterator;
174  typedef PartsType::size_type size_type;
175 
177  explicit MimeMultipart(const std::string& data);
178 
181  MimeMultipart(const MimeEntity& mimeEntity);
182 
184  explicit MimeMultipart(Type type_ = typeMixed)
185  { setType(type_); }
186 
188  size_type size() const
189  { return parts.size(); }
192  { return parts[n]; }
195  { return parts[n]; }
198  { return parts.begin(); }
201  { return parts.end(); }
204  { return parts.begin(); }
207  { return parts.end(); }
208 
209  void setType(Type type_);
210  void setType(const std::string& type_)
211  { type = type_; }
212 
215  { parts.resize(parts.size() + 1); return parts.back(); }
216 
218  MimeEntity& addObject(const MimeEntity& mimeEntity)
219  { parts.push_back(mimeEntity); return parts.back(); }
220 
222  MimeEntity& addObject(const std::string& data, const std::string& contentType = "text/plain; charset=UTF-8",
223  ContentTransferEncoding contentTransferEncoding = MimeEntity::quotedPrintable);
224 
226  MimeEntity& addObject(std::istream& in, const std::string& contentType = "text/plain; charset=UTF-8",
227  ContentTransferEncoding contentTransferEncoding = MimeEntity::quotedPrintable);
228 
229  MimeEntity& addObject(const MimeMultipart& mimeMultipart);
230 
232  MimeEntity& attachTextFile(const std::string& data, const std::string& filename, const std::string& contentType = "text/plain; charset=UTF-8")
233  {
235  mimeEntity.addHeader("Content-Disposition", "attachment; filename=" + filename);
236  return mimeEntity;
237  }
238 
240  MimeEntity& attachTextFile(std::istream& in, const std::string& filename, const std::string& contentType = "text/plain; charset=UTF-8")
241  {
243  mimeEntity.addHeader("Content-Disposition", "attachment; filename=" + filename);
244  return mimeEntity;
245  }
246 
248  MimeEntity& attachBinaryFile(const std::string& data, const std::string& filename, const std::string& contentType = "application/x-binary")
249  {
250  MimeEntity& mimeEntity = addObject(data, contentType, MimeEntity::base64);
251  mimeEntity.addHeader("Content-Disposition", "attachment; filename=" + filename);
252  return mimeEntity;
253  }
254 
256  MimeEntity& attachBinaryFile(std::istream& in, const std::string& filename, const std::string& contentType = "application/x-binary")
257  {
259  mimeEntity.addHeader("Content-Disposition", "attachment; filename=" + filename);
260  return mimeEntity;
261  }
262 
263 };
264 
265 std::ostream& operator<< (std::ostream& out, const MimeHeader& mimeHeader);
266 std::ostream& operator<< (std::ostream& out, const MimeEntity& mimeEntity);
267 std::ostream& operator<< (std::ostream& out, const MimeMultipart& mime);
268 
269 }
270 
271 #endif