aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llmime.h
blob: bcc284821f68c2df767c3135f6f7a94c32c15c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/** 
 * @file llmime.h
 * @author Phoenix
 * @date 2006-12-20
 * @brief Declaration of mime tools.
 *
 * $LicenseInfo:firstyear=2006&license=viewergpl$
 * 
 * Copyright (c) 2006-2008, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#ifndef LL_LLMIME_H
#define LL_LLMIME_H

#include <string>
#include "llsd.h"

/**
 * This file declares various tools for parsing and creating MIME
 * objects as described in RFCs 2045, 2046, 2047, 2048, and 2049.
 */

/** 
 * @class LLMimeIndex
 * @brief Skeletal information useful for handling mime packages.
 * @see LLMimeParser
 *
 * An instance of this class is the parsed output from a LLMimeParser
 * which then allows for easy access into a data stream to find and
 * get what you want out of it.
 *
 * This class meant as a tool to quickly find what you seek in a
 * parsed mime entity. As such, it does not have useful support for
 * modification of a mime entity and specializes the interface toward
 * querying data from a fixed mime entity. Modifying an instance of
 * LLMimeIndx does not alter a mime entity and changes to a mime
 * entity itself are not propogated into an instance of a LLMimeIndex.
 *
 * Usage:<br>
 *  LLMimeIndex mime_index;<br>
 *  std::ifstream fstr("package.mime", ios::binary);<br>
 *  LLMimeParser parser;<br>
 *  if(parser.parseIndex(fstr, mime_index))<br>
 *  {<br>
 *    std::vector<U8> content;<br>
 *    content.resize(mime_index.contentLength());<br>
 *    fstr.seekg(mime_index.offset(), ios::beg);<br>
 *    // ...do work on fstr and content<br>
 *  }<br>
 */
class LLMimeIndex
{
public:
	/* @name Client interface.
	 */
	//@{
	/** 
	 * @brief Get the full parsed headers for this.
	 *
	 * If there are any headers, it will be a map of header name to
	 * the value found on the line. The name is everything before the
	 * colon, and the value is the string found after the colon to the
	 * end of the line after trimming leading whitespace. So, for
	 * example:
	 * Content-Type:  text/plain
	 * would become an entry in the headers of:
	 * headers["Content-Type"] == "text/plain"
	 *
	 * If this instance of an index was generated by the
	 * LLMimeParser::parseIndex() call, all header names in rfc2045
	 * will be capitalized as in rfc, eg Content-Length and
	 * MIME-Version, not content-length and mime-version.
	 * @return Returns an LLSD map of header name to value. Returns
	 * undef if there are no headers.
	 */
	LLSD headers() const;

	/** 
	 * @brief Get the content offset.
	 *
	 * @return Returns the number of bytes to the start of the data
	 * segment from the start of serialized mime entity. Returns -1 if
	 * offset is not known.
	 */
	S32 offset() const;

	/** 
	 * @brief Get the length of the data segment for this mime part.
	 *
	 * @return Returns the content length in bytes. Returns -1 if
	 * length is not known.
	 */
	S32 contentLength() const;

	/** 
	 * @brief Get the mime type associated with this node.
	 *
	 * @return Returns the mimetype.
	 */
	std::string contentType() const;

	/** 
	 * @brief Helper method which simplifies parsing the return from type()
	 *
	 * @return Returns true if this is a multipart mime, and therefore
	 * getting subparts will succeed.
	 */
	bool isMultipart() const;

	/** 
	 * @brief Get the number of atachments.
	 *
	 * @return Returns the number of sub-parts for this.
	 */
	S32 subPartCount() const;

	/** 
	 * @brief Get the indicated attachment.
	 *
	 * @param index Value from 0 to (subPartCount() - 1).
	 * @return Returns the indicated sub-part, or an invalid mime
	 * index on failure.
	 */
	LLMimeIndex subPart(S32 index) const;
	//@}

	/* @name Interface for building, testing, and helpers for typical use.
	 */
	//@{
	/**
	 * @brief Default constructor - creates a useless LLMimeIndex.
	 */
	LLMimeIndex();

	/**
	 * @brief Full constructor.
	 *
	 * @param headers The complete headers.
	 * @param content_offset The number of bytes to the start of the
	 * data segment of this mime entity from the start of the stream
	 * or buffer.
	 */
	LLMimeIndex(LLSD headers, S32 content_offset);

	/**
	 * @brief Copy constructor.
	 *
	 * @param mime The other mime object.
	 */
	LLMimeIndex(const LLMimeIndex& mime);

	// @brief Destructor.
	~LLMimeIndex();

	/*
	 * @breif Assignment operator.
	 *
	 * @param mime The other mime object.
	 * @return Returns this after assignment.
	 */
	LLMimeIndex& operator=(const LLMimeIndex& mime);

	/** 
	 * @brief Add attachment information as a sub-part to a multipart mime.
	 *
	 * @param sub_part the part to attach.
	 * @return Returns true on success, false on failure.
	 */
	bool attachSubPart(LLMimeIndex sub_part);
	//@}

protected:
	// Implementation.
	class Impl;
	Impl* mImpl;
};


/** 
 * @class LLMimeParser
 * @brief This class implements a MIME parser and verifier.
 *
 * THOROUGH_DESCRIPTION
 */
class LLMimeParser
{
public:
	// @brief Make a new mime parser.
	LLMimeParser();
	
	// @brief Mime parser Destructor.
	~LLMimeParser();

	// @brief Reset internal state of this parser.
	void reset();

	
	/* @name Index generation interface.
	 */
	//@{
	/** 
	 * @brief Parse a stream to find the mime index information.
	 *
	 * This method will scan the istr until a single complete mime
	 * entity is read or EOF. The istr will be modified by this
	 * parsing, so pass in a temporary stream or rewind/reset the
	 * stream after this call.
	 * @param istr An istream which contains a mime entity.
	 * @param index[out] The parsed output.
	 * @return Returns true if an index was parsed and no errors occurred.
	 */
	bool parseIndex(std::istream& istr, LLMimeIndex& index);

	/** 
	 * @brief Parse a vector to find the mime index information.
	 *
	 * @param buffer A vector with data to parse.
	 * @param index[out] The parsed output.
	 * @return Returns true if an index was parsed and no errors occurred.
	 */
	bool parseIndex(const std::vector<U8>& buffer, LLMimeIndex& index);

	/** 
	 * @brief Parse a stream to find the mime index information.
	 *
	 * This method will scan the istr until a single complete mime
	 * entity is read, an EOF, or limit bytes have been scanned. The
	 * istr will be modified by this parsing, so pass in a temporary
	 * stream or rewind/reset the stream after this call.
	 * @param istr An istream which contains a mime entity.
	 * @param limit The maximum number of bytes to scan.
	 * @param index[out] The parsed output.
	 * @return Returns true if an index was parsed and no errors occurred.
	 */
	bool parseIndex(std::istream& istr, S32 limit, LLMimeIndex& index);

	/** 
	 * @brief Parse a memory bufffer to find the mime index information.
	 *
	 * @param buffer The start of the buffer to parse.
	 * @param buffer_length The length of the buffer.
	 * @param index[out] The parsed output.
	 * @return Returns true if an index was parsed and no errors occurred.
	 */
	bool parseIndex(const U8* buffer, S32 buffer_length, LLMimeIndex& index);
	//@}

	/** 
	 * @brief 
	 *
	 * @return
	 */
	//bool verify(std::istream& istr, LLMimeIndex& index) const;

	/** 
	 * @brief 
	 *
	 * @return
	 */
	//bool verify(U8* buffer, S32 buffer_length, LLMimeIndex& index) const;

protected:
	// Implementation.
	class Impl;
	Impl& mImpl;

private:
	// @brief Not implemneted to prevent copy consturction.
	LLMimeParser(const LLMimeParser& parser);

	// @brief Not implemneted to prevent assignment.
	LLMimeParser& operator=(const LLMimeParser& mime);
};

#endif // LL_LLMIME_H