multipart.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 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 
30 #ifndef TNT_MULTIPART_H
31 #define TNT_MULTIPART_H
32 
33 #include <tnt/messageheader.h>
34 #include <tnt/contentdisposition.h>
35 #include <vector>
36 #include <iterator>
37 
38 namespace tnt
39 {
41  class Partheader : public Messageheader
42  {
44 
45  protected:
46  return_type onField(const char* name, const char* value);
47 
48  public:
50  { return _cd; }
51  std::string getMimetype() const;
52  };
53 
55  class Part
56  {
57  public:
58  typedef std::string::const_iterator const_iterator;
59  typedef std::string::const_iterator iterator;
60  typedef std::string::size_type size_type;
61  typedef std::string::value_type value_type;
62  typedef std::string::pointer pointer;
63  typedef std::string::reference reference;
64  typedef std::string::const_reference const_reference;
65  typedef std::string::difference_type difference_type;
66 
67  private:
68  Partheader _header;
69  const_iterator _bodyBegin;
70  const_iterator _bodyEnd;
71 
72  public:
73  Part() { }
74 
76 
78  const Partheader& getHeader() const { return _header; }
79 
81  std::string getHeader(const std::string& key) const;
82 
84  const std::string& getType() const
85  { return _header.getContentDisposition().getType(); }
86 
88  std::string getMimetype() const
89  { return _header.getMimetype(); }
90 
92  const std::string& getName() const
93  { return _header.getContentDisposition().getName(); }
94 
96  const std::string& getFilename() const
97  { return _header.getContentDisposition().getFilename(); }
98 
101  { return _bodyBegin; }
102 
105  { return _bodyEnd; }
106 
108  std::string getBody() const
109  { return std::string(getBodyBegin(), getBodyEnd()); }
110  bool isEmpty() const
111  { return getBodyBegin() == getBodyEnd(); }
113  { return getBodyEnd() - getBodyBegin(); }
114 
115  // stl-style accessors
116  const_iterator begin() const { return getBodyBegin(); }
117  const_iterator end() const { return getBodyEnd(); }
118  size_type size() const { return getSize(); }
119  bool empty() const { return isEmpty(); }
120  };
121 
123  class Multipart
124  {
125  public:
126  typedef Part part_type;
127  typedef std::vector<part_type> parts_type;
128  typedef parts_type::const_iterator const_iterator;
129  typedef parts_type::value_type value_type;
130 
131  private:
132  std::string _body;
133  parts_type _parts;
134 
135  public:
136  Multipart() { }
137  void set(const std::string& boundary, const std::string& body);
138 
139  const_iterator begin() const { return _parts.begin(); }
140  const_iterator end() const { return _parts.end(); }
141  const_iterator find(const std::string& partName, const_iterator start) const;
142  const_iterator find(const std::string& partName) const
143  { return find(partName, begin()); }
144  };
145 }
146 
147 #endif // TNT_MULTIPART_H
148