aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llmime.h
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llmessage/llmime.h')
-rw-r--r--linden/indra/llmessage/llmime.h293
1 files changed, 293 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llmime.h b/linden/indra/llmessage/llmime.h
new file mode 100644
index 0000000..7cb2b74
--- /dev/null
+++ b/linden/indra/llmessage/llmime.h
@@ -0,0 +1,293 @@
1/**
2 * @file llmime.h
3 * @author Phoenix
4 * @date 2006-12-20
5 * @brief Declaration of mime tools.
6 *
7 * Copyright (c) 2006-2007, Linden Research, Inc.
8 *
9 * The source code in this file ("Source Code") is provided by Linden Lab
10 * to you under the terms of the GNU General Public License, version 2.0
11 * ("GPL"), unless you have obtained a separate licensing agreement
12 * ("Other License"), formally executed by you and Linden Lab. Terms of
13 * the GPL can be found in doc/GPL-license.txt in this distribution, or
14 * online at http://secondlife.com/developers/opensource/gplv2
15 *
16 * There are special exceptions to the terms and conditions of the GPL as
17 * it is applied to this Source Code. View the full text of the exception
18 * in the file doc/FLOSS-exception.txt in this software distribution, or
19 * online at http://secondlife.com/developers/opensource/flossexception
20 *
21 * By copying, modifying or distributing this software, you acknowledge
22 * that you have read and understood your obligations described above,
23 * and agree to abide by those obligations.
24 *
25 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
26 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
27 * COMPLETENESS OR PERFORMANCE.
28 */
29
30#ifndef LL_LLMIME_H
31#define LL_LLMIME_H
32
33#include <string>
34#include "llsd.h"
35
36/**
37 * This file declares various tools for parsing and creating MIME
38 * objects as described in RFCs 2045, 2046, 2047, 2048, and 2049.
39 */
40
41/**
42 * @class LLMimeIndex
43 * @brief Skeletal information useful for handling mime packages.
44 * @see LLMimeParser
45 *
46 * An instance of this class is the parsed output from a LLMimeParser
47 * which then allows for easy access into a data stream to find and
48 * get what you want out of it.
49 *
50 * This class meant as a tool to quickly find what you seek in a
51 * parsed mime entity. As such, it does not have useful support for
52 * modification of a mime entity and specializes the interface toward
53 * querying data from a fixed mime entity. Modifying an instance of
54 * LLMimeIndx does not alter a mime entity and changes to a mime
55 * entity itself are not propogated into an instance of a LLMimeIndex.
56 *
57 * Usage:<br>
58 * LLMimeIndex mime_index;<br>
59 * std::ifstream fstr("package.mime", ios::binary);<br>
60 * LLMimeParser parser;<br>
61 * if(parser.parseIndex(fstr, mime_index))<br>
62 * {<br>
63 * std::vector<U8> content;<br>
64 * content.resize(mime_index.contentLength());<br>
65 * fstr.seekg(mime_index.offset(), ios::beg);<br>
66 * // ...do work on fstr and content<br>
67 * }<br>
68 */
69class LLMimeIndex
70{
71public:
72 /* @name Client interface.
73 */
74 //@{
75 /**
76 * @brief Get the full parsed headers for this.
77 *
78 * If there are any headers, it will be a map of header name to
79 * the value found on the line. The name is everything before the
80 * colon, and the value is the string found after the colon to the
81 * end of the line after trimming leading whitespace. So, for
82 * example:
83 * Content-Type: text/plain
84 * would become an entry in the headers of:
85 * headers["Content-Type"] == "text/plain"
86 *
87 * If this instance of an index was generated by the
88 * LLMimeParser::parseIndex() call, all header names in rfc2045
89 * will be capitalized as in rfc, eg Content-Length and
90 * MIME-Version, not content-length and mime-version.
91 * @return Returns an LLSD map of header name to value. Returns
92 * undef if there are no headers.
93 */
94 LLSD headers() const;
95
96 /**
97 * @brief Get the content offset.
98 *
99 * @return Returns the number of bytes to the start of the data
100 * segment from the start of serialized mime entity. Returns -1 if
101 * offset is not known.
102 */
103 S32 offset() const;
104
105 /**
106 * @brief Get the length of the data segment for this mime part.
107 *
108 * @return Returns the content length in bytes. Returns -1 if
109 * length is not known.
110 */
111 S32 contentLength() const;
112
113 /**
114 * @brief Get the mime type associated with this node.
115 *
116 * @return Returns the mimetype.
117 */
118 std::string contentType() const;
119
120 /**
121 * @brief Helper method which simplifies parsing the return from type()
122 *
123 * @return Returns true if this is a multipart mime, and therefore
124 * getting subparts will succeed.
125 */
126 bool isMultipart() const;
127
128 /**
129 * @brief Get the number of atachments.
130 *
131 * @return Returns the number of sub-parts for this.
132 */
133 S32 subPartCount() const;
134
135 /**
136 * @brief Get the indicated attachment.
137 *
138 * @param index Value from 0 to (subPartCount() - 1).
139 * @return Returns the indicated sub-part, or an invalid mime
140 * index on failure.
141 */
142 LLMimeIndex subPart(S32 index) const;
143 //@}
144
145 /* @name Interface for building, testing, and helpers for typical use.
146 */
147 //@{
148 /**
149 * @brief Default constructor - creates a useless LLMimeIndex.
150 */
151 LLMimeIndex();
152
153 /**
154 * @brief Full constructor.
155 *
156 * @param headers The complete headers.
157 * @param content_offset The number of bytes to the start of the
158 * data segment of this mime entity from the start of the stream
159 * or buffer.
160 */
161 LLMimeIndex(LLSD headers, S32 content_offset);
162
163 /**
164 * @brief Copy constructor.
165 *
166 * @param mime The other mime object.
167 */
168 LLMimeIndex(const LLMimeIndex& mime);
169
170 // @brief Destructor.
171 ~LLMimeIndex();
172
173 /*
174 * @breif Assignment operator.
175 *
176 * @param mime The other mime object.
177 * @return Returns this after assignment.
178 */
179 LLMimeIndex& operator=(const LLMimeIndex& mime);
180
181 /**
182 * @brief Add attachment information as a sub-part to a multipart mime.
183 *
184 * @param sub_part the part to attach.
185 * @return Returns true on success, false on failure.
186 */
187 bool attachSubPart(LLMimeIndex sub_part);
188 //@}
189
190protected:
191 // Implementation.
192 class Impl;
193 Impl* mImpl;
194};
195
196
197/**
198 * @class LLMimeParser
199 * @brief This class implements a MIME parser and verifier.
200 *
201 * THOROUGH_DESCRIPTION
202 */
203class LLMimeParser
204{
205public:
206 // @brief Make a new mime parser.
207 LLMimeParser();
208
209 // @brief Mime parser Destructor.
210 ~LLMimeParser();
211
212 // @brief Reset internal state of this parser.
213 void reset();
214
215
216 /* @name Index generation interface.
217 */
218 //@{
219 /**
220 * @brief Parse a stream to find the mime index information.
221 *
222 * This method will scan the istr until a single complete mime
223 * entity is read or EOF. The istr will be modified by this
224 * parsing, so pass in a temporary stream or rewind/reset the
225 * stream after this call.
226 * @param istr An istream which contains a mime entity.
227 * @param index[out] The parsed output.
228 * @return Returns true if an index was parsed and no errors occurred.
229 */
230 bool parseIndex(std::istream& istr, LLMimeIndex& index);
231
232 /**
233 * @brief Parse a vector to find the mime index information.
234 *
235 * @param buffer A vector with data to parse.
236 * @param index[out] The parsed output.
237 * @return Returns true if an index was parsed and no errors occurred.
238 */
239 bool parseIndex(const std::vector<U8>& buffer, LLMimeIndex& index);
240
241 /**
242 * @brief Parse a stream to find the mime index information.
243 *
244 * This method will scan the istr until a single complete mime
245 * entity is read, an EOF, or limit bytes have been scanned. The
246 * istr will be modified by this parsing, so pass in a temporary
247 * stream or rewind/reset the stream after this call.
248 * @param istr An istream which contains a mime entity.
249 * @param limit The maximum number of bytes to scan.
250 * @param index[out] The parsed output.
251 * @return Returns true if an index was parsed and no errors occurred.
252 */
253 bool parseIndex(std::istream& istr, S32 limit, LLMimeIndex& index);
254
255 /**
256 * @brief Parse a memory bufffer to find the mime index information.
257 *
258 * @param buffer The start of the buffer to parse.
259 * @param buffer_length The length of the buffer.
260 * @param index[out] The parsed output.
261 * @return Returns true if an index was parsed and no errors occurred.
262 */
263 bool parseIndex(const U8* buffer, S32 buffer_length, LLMimeIndex& index);
264 //@}
265
266 /**
267 * @brief
268 *
269 * @return
270 */
271 //bool verify(std::istream& istr, LLMimeIndex& index) const;
272
273 /**
274 * @brief
275 *
276 * @return
277 */
278 //bool verify(U8* buffer, S32 buffer_length, LLMimeIndex& index) const;
279
280protected:
281 // Implementation.
282 class Impl;
283 Impl& mImpl;
284
285private:
286 // @brief Not implemneted to prevent copy consturction.
287 LLMimeParser(const LLMimeParser& parser);
288
289 // @brief Not implemneted to prevent assignment.
290 LLMimeParser& operator=(const LLMimeParser& mime);
291};
292
293#endif // LL_LLMIME_H