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  { }
75 
77 
79  const Partheader& getHeader() const { return header; }
81  std::string getHeader(const std::string& key) const;
82 
84  const std::string& getType() const
85  { return header.getContentDisposition().getType(); }
87  std::string getMimetype() const
88  { return header.getMimetype(); }
90  const std::string& getName() const
91  { return header.getContentDisposition().getName(); }
93  const std::string& getFilename() const
94  { return header.getContentDisposition().getFilename(); }
97  { return bodyBegin; }
100  { return bodyEnd; }
102  std::string getBody() const
103  { return std::string(getBodyBegin(), getBodyEnd()); }
104  bool isEmpty() const
105  { return getBodyBegin() == getBodyEnd(); }
107  { return getBodyEnd() - getBodyBegin(); }
108 
109  // stl-style accessors
110  const_iterator begin() const { return getBodyBegin(); }
111  const_iterator end() const { return getBodyEnd(); }
112  size_type size() const { return getSize(); }
113  bool empty() const { return isEmpty(); }
114  };
115 
117  class Multipart
118  {
119  public:
120  typedef Part part_type;
121  typedef std::vector<part_type> parts_type;
122  typedef parts_type::const_iterator const_iterator;
123  typedef parts_type::value_type value_type;
124 
125  private:
126  std::string body;
127  parts_type parts;
128 
129  public:
131  { }
132  void set(const std::string& boundary, const std::string& body);
133 
134  const_iterator begin() const { return parts.begin(); }
135  const_iterator end() const { return parts.end(); }
136  const_iterator find(const std::string& partName,
137  const_iterator start) const;
138  const_iterator find(const std::string& partName) const
139  { return find(partName, begin()); }
140  };
141 
142 }
143 
144 #endif // TNT_MULTIPART_H
145