1 | // |
---|
2 | // MultipartReader.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/MultipartReader.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Messages |
---|
8 | // Module: MultipartReader |
---|
9 | // |
---|
10 | // Definition of the MultipartReader class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_MultipartReader_INCLUDED |
---|
40 | #define Net_MultipartReader_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/BufferedStreamBuf.h" |
---|
45 | #include <istream> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | namespace Net { |
---|
50 | |
---|
51 | |
---|
52 | class MessageHeader; |
---|
53 | |
---|
54 | |
---|
55 | class Net_API MultipartStreamBuf: public Poco::BufferedStreamBuf |
---|
56 | /// This is the streambuf class used for reading from a multipart message stream. |
---|
57 | { |
---|
58 | public: |
---|
59 | MultipartStreamBuf(std::istream& istr, const std::string& boundary); |
---|
60 | ~MultipartStreamBuf(); |
---|
61 | bool lastPart() const; |
---|
62 | |
---|
63 | protected: |
---|
64 | int readFromDevice(char* buffer, std::streamsize length); |
---|
65 | |
---|
66 | private: |
---|
67 | enum |
---|
68 | { |
---|
69 | STREAM_BUFFER_SIZE = 1024 |
---|
70 | }; |
---|
71 | |
---|
72 | std::istream& _istr; |
---|
73 | std::string _boundary; |
---|
74 | bool _lastPart; |
---|
75 | }; |
---|
76 | |
---|
77 | |
---|
78 | class Net_API MultipartIOS: public virtual std::ios |
---|
79 | /// The base class for MultipartInputStream. |
---|
80 | { |
---|
81 | public: |
---|
82 | MultipartIOS(std::istream& istr, const std::string& boundary); |
---|
83 | ~MultipartIOS(); |
---|
84 | MultipartStreamBuf* rdbuf(); |
---|
85 | bool lastPart() const; |
---|
86 | |
---|
87 | protected: |
---|
88 | MultipartStreamBuf _buf; |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | class Net_API MultipartInputStream: public MultipartIOS, public std::istream |
---|
93 | /// This class is for internal use by MultipartReader only. |
---|
94 | { |
---|
95 | public: |
---|
96 | MultipartInputStream(std::istream& istr, const std::string& boundary); |
---|
97 | ~MultipartInputStream(); |
---|
98 | }; |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | class Net_API MultipartReader |
---|
103 | /// This class is used to split a MIME multipart |
---|
104 | /// message into its single parts. |
---|
105 | /// |
---|
106 | /// The format of multipart messages is described |
---|
107 | /// in section 5.1 of RFC 2046. |
---|
108 | /// |
---|
109 | /// To split a multipart message into its parts, |
---|
110 | /// do the following: |
---|
111 | /// - Create a MultipartReader object, passing it |
---|
112 | /// an input stream and optionally a boundary string. |
---|
113 | /// - while hasNextPart() returns true, call nextPart() |
---|
114 | /// and read the part from stream(). |
---|
115 | /// |
---|
116 | /// Always ensure that you read all data from the part |
---|
117 | /// stream, otherwise the MultipartReader will fail to |
---|
118 | /// find the next part. |
---|
119 | { |
---|
120 | public: |
---|
121 | explicit MultipartReader(std::istream& istr); |
---|
122 | /// Creates the MultipartReader and attaches it to the |
---|
123 | /// given input stream. |
---|
124 | /// |
---|
125 | /// The boundary string is determined from the input |
---|
126 | /// stream. The message must not contain a preamble |
---|
127 | /// preceding the first encapsulation boundary. |
---|
128 | |
---|
129 | MultipartReader(std::istream& istr, const std::string& boundary); |
---|
130 | /// Creates the MultipartReader and attaches it to the |
---|
131 | /// given input stream. The given boundary string is |
---|
132 | /// used to find message boundaries. |
---|
133 | |
---|
134 | ~MultipartReader(); |
---|
135 | /// Destroys the MultipartReader. |
---|
136 | |
---|
137 | void nextPart(MessageHeader& messageHeader); |
---|
138 | /// Moves to the next part in the message and stores the |
---|
139 | /// part's header fields in messageHeader. |
---|
140 | /// |
---|
141 | /// Throws an MultipartException if there are no more parts |
---|
142 | /// available, or if no boundary line can be found in |
---|
143 | /// the input stream. |
---|
144 | |
---|
145 | bool hasNextPart(); |
---|
146 | /// Returns true iff more parts are available. |
---|
147 | /// |
---|
148 | /// Before the first call to nextPart(), returns |
---|
149 | /// always true. |
---|
150 | |
---|
151 | std::istream& stream() const; |
---|
152 | /// Returns a reference to the reader's stream that |
---|
153 | /// can be used to read the current part. |
---|
154 | /// |
---|
155 | /// The returned reference will be valid until |
---|
156 | /// nextPart() is called or the MultipartReader |
---|
157 | /// object is destroyed. |
---|
158 | |
---|
159 | const std::string& boundary() const; |
---|
160 | /// Returns the multipart boundary used by this reader. |
---|
161 | |
---|
162 | protected: |
---|
163 | void findFirstBoundary(); |
---|
164 | void guessBoundary(); |
---|
165 | void parseHeader(MessageHeader& messageHeader); |
---|
166 | bool readLine(std::string& line, std::string::size_type n); |
---|
167 | |
---|
168 | private: |
---|
169 | MultipartReader(); |
---|
170 | MultipartReader(const MultipartReader&); |
---|
171 | MultipartReader& operator = (const MultipartReader&); |
---|
172 | |
---|
173 | std::istream& _istr; |
---|
174 | std::string _boundary; |
---|
175 | MultipartInputStream* _pMPI; |
---|
176 | }; |
---|
177 | |
---|
178 | |
---|
179 | } } // namespace Poco::Net |
---|
180 | |
---|
181 | |
---|
182 | #endif // Net_MultipartReader_INCLUDED |
---|