forked from smadep/MimeTools.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharpMimeHeader.cs
554 lines (512 loc) · 19.1 KB
/
SharpMimeHeader.cs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// -----------------------------------------------------------------------
//
// Copyright (C) 2003-2006 Angel Marin
//
// This file is part of SharpMimeTools
//
// SharpMimeTools is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// SharpMimeTools is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with SharpMimeTools; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// -----------------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
namespace anmar.SharpMimeTools
{
/// <summary>
/// rfc 2822 header of a rfc 2045 entity
/// </summary>
public class SharpMimeHeader : IEnumerable
{
private struct HeaderInfo
{
public StringDictionary contenttype;
public StringDictionary contentdisposition;
public StringDictionary contentlocation;
public MimeTopLevelMediaType TopLevelMediaType;
public Encoding enc;
public String subtype;
public HeaderInfo(HybridDictionary headers)
{
TopLevelMediaType = new MimeTopLevelMediaType();
enc = null;
try
{
contenttype = SharpMimeTools.parseHeaderFieldBody("Content-Type", headers["Content-Type"].ToString());
TopLevelMediaType = (MimeTopLevelMediaType)Enum.Parse(TopLevelMediaType.GetType(),
contenttype["Content-Type"].Split('/')[0].Trim(),
true);
subtype = contenttype["Content-Type"].Split('/')[1].Trim();
enc = SharpMimeTools.parseCharSet(contenttype["charset"]);
}
catch (Exception)
{
enc = SharpMimeHeader.default_encoding;
contenttype = SharpMimeTools.parseHeaderFieldBody("Content-Type", String.Concat("text/plain; charset=", enc.BodyName));
TopLevelMediaType = MimeTopLevelMediaType.text;
subtype = "plain";
}
if (enc == null)
{
enc = SharpMimeHeader.default_encoding;
}
// TODO: rework this
try
{
contentdisposition = SharpMimeTools.parseHeaderFieldBody("Content-Disposition", headers["Content-Disposition"].ToString());
}
catch (Exception)
{
contentdisposition = new StringDictionary();
}
try
{
contentlocation = SharpMimeTools.parseHeaderFieldBody("Content-Location", headers["Content-Location"].ToString());
}
catch (Exception)
{
contentlocation = new StringDictionary();
}
}
}
private static Encoding default_encoding = Encoding.ASCII;
private readonly SharpMimeMessageStream message;
private readonly HybridDictionary headers;
private String _cached_headers;
private readonly long startpoint;
private long endpoint;
private long startbody;
private HeaderInfo mt;
/// <summary>
/// Initializes a new instance of the <see cref="anmar.SharpMimeTools.SharpMimeHeader"/> class from a <see cref="System.IO.Stream"/>
/// </summary>
/// <param name="message"><see cref="System.IO.Stream"/> to read headers from</param>
public SharpMimeHeader(System.IO.Stream message)
: this(new SharpMimeMessageStream(message), 0)
{
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public SharpMimeHeader(Byte[] message)
: this(new SharpMimeMessageStream(message), 0)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="anmar.SharpMimeTools.SharpMimeHeader"/> class from a <see cref="System.IO.Stream"/> starting from the specified point
/// </summary>
/// <param name="message">the <see cref="System.IO.Stream" /> to read headers from</param>
/// <param name="startpoint">initial point of the <see cref="System.IO.Stream"/> where the headers start</param>
public SharpMimeHeader(System.IO.Stream message, long startpoint)
: this(new SharpMimeMessageStream(message), startpoint)
{
}
internal SharpMimeHeader(SharpMimeMessageStream message) : this(message, 0) { }
internal SharpMimeHeader(SharpMimeMessageStream message, long startpoint)
{
this.startpoint = startpoint;
this.message = message;
if (this.startpoint == 0)
{
String line = this.message.ReadLine();
// Perhaps there is part of the POP3 response
if (line != null && line.Length > 3 && line[0] == '+' && line[1] == 'O' && line[2] == 'K')
{
this.startpoint = this.message.Position;
}
else
this.message.ReadLine_Undo(line);
}
headers = new HybridDictionary(2, true);
parse();
}
/// <summary>
/// Gets header fields
/// </summary>
/// <param name="name">field name</param>
/// <remarks>Field names is case insentitive</remarks>
public String this[string name]
{
get
{
return getProperty(name);
}
}
/// <summary>
/// Gets the point where the headers end
/// </summary>
/// <value>Point where the headers end</value>
public long BodyPosition
{
get
{
return startbody;
}
}
/// <summary>
/// Gets CC header field
/// </summary>
/// <value>CC header body</value>
public String Cc
{
get { return GetHeaderField("Cc", String.Empty, true, false); }
}
/// <summary>
/// Gets the number of header fields found
/// </summary>
public int Count
{
get
{
return headers.Count;
}
}
/// <summary>
/// Gets Content-Disposition header field
/// </summary>
/// <value>Content-Disposition header body</value>
public String ContentDisposition
{
get { return GetHeaderField("Content-Disposition", String.Empty, true, false); }
}
/// <summary>
/// Gets the elements found in the Content-Disposition header body
/// </summary>
/// <value><see cref="System.Collections.Specialized.StringDictionary"/> with the elements found in the header body</value>
public StringDictionary ContentDispositionParameters
{
get
{
return mt.contentdisposition;
}
}
/// <summary>
/// Gets Content-ID header field
/// </summary>
/// <value>Content-ID header body</value>
public String ContentID
{
get { return GetHeaderField("Content-ID", String.Empty, true, false); }
}
/// <summary>
/// Gets Content-Location header field
/// </summary>
/// <value>Content-Location header body</value>
public String ContentLocation
{
get { return GetHeaderField("Content-Location", String.Empty, true, false); }
}
/// <summary>
/// Gets the elements found in the Content-Location header body
/// </summary>
/// <value><see cref="System.Collections.Specialized.StringDictionary"/> with the elements found in the header body</value>
public StringDictionary ContentLocationParameters
{
get
{
return mt.contentlocation;
}
}
/// <summary>
/// Gets Content-Transfer-Encoding header field
/// </summary>
/// <value>Content-Transfer-Encoding header body</value>
public String ContentTransferEncoding
{
get
{
String tmp = GetHeaderField("Content-Transfer-Encoding", null, false, false);
if (tmp != null)
{
tmp = tmp.ToLower();
}
return tmp;
}
}
/// <summary>
/// Gets Content-Type header field
/// </summary>
/// <value>Content-Type header body</value>
public String ContentType
{
get { return GetHeaderField("Content-Type", String.Concat("text/plain; charset=", mt.enc.BodyName), false, false); }
}
/// <summary>
/// Gets the elements found in the Content-Type header body
/// </summary>
/// <value><see cref="System.Collections.Specialized.StringDictionary"/> with the elements found in the header body</value>
public StringDictionary ContentTypeParameters
{
get
{
return mt.contenttype;
}
}
/// <summary>
/// Gets Date header field
/// </summary>
/// <value>Date header body</value>
public String Date
{
get { return GetHeaderField("Date", String.Empty, true, false); }
}
/// <summary>
/// Gets <see cref="System.Text.Encoding"/> found on the headers and applies to the body
/// </summary>
/// <value><see cref="System.Text.Encoding"/> for the body</value>
public Encoding Encoding
{
get
{
parse();
return mt.enc;
}
}
/// <summary>
/// Gets or sets the default <see cref="System.Text.Encoding" /> used when it isn't defined otherwise.
/// </summary>
/// <value>The <see cref="System.Text.Encoding" /> used when it isn't defined otherwise</value>
/// <remarks>The default value is <see cref="System.Text.ASCIIEncoding" /> as defined in RFC 2045 section 5.2.<br />
/// If you change this value you'll be violating this rfc section.</remarks>
public static Encoding EncodingDefault
{
get { return default_encoding; }
set
{
if (value != null && !value.BodyName.Equals(String.Empty))
default_encoding = value;
else
default_encoding = Encoding.ASCII;
}
}
/// <summary>
/// Gets From header field
/// </summary>
/// <value>From header body</value>
public String From
{
get { return GetHeaderField("From", String.Empty, true, false); }
}
/// <summary>
/// Gets Raw headers
/// </summary>
/// <value>From header body</value>
public String RawHeaders
{
get
{
if (_cached_headers != null)
return _cached_headers;
else
return message.ReadLines(startpoint, endpoint);
}
}
/// <summary>
/// Gets Message-ID header field
/// </summary>
/// <value>Message-ID header body</value>
public String MessageID
{
get { return GetHeaderField("Message-ID", String.Empty, true, false); }
}
/// <summary>
/// Gets reply address as defined by <c>rfc 2822</c>
/// </summary>
/// <value>Reply address</value>
public String Reply
{
get
{
if (!ReplyTo.Equals(String.Empty))
return ReplyTo;
else
return From;
}
}
/// <summary>
/// Gets Reply-To header field
/// </summary>
/// <value>Reply-To header body</value>
public String ReplyTo
{
get { return GetHeaderField("Reply-To", String.Empty, true, false); }
}
/// <summary>
/// Gets Return-Path header field
/// </summary>
/// <value>Return-Path header body</value>
public String ReturnPath
{
get { return GetHeaderField("Return-Path", String.Empty, true, false); }
}
/// <summary>
/// Gets Sender header field
/// </summary>
/// <value>Sender header body</value>
public String Sender
{
get { return GetHeaderField("Sender", String.Empty, true, false); }
}
/// <summary>
/// Gets Subject header field
/// </summary>
/// <value>Subject header body</value>
public String Subject
{
get { return GetHeaderField("Subject", String.Empty, false, false); }
}
/// <summary>
/// Gets SubType from Content-Type header field
/// </summary>
/// <value>SubType from Content-Type header field</value>
public String SubType
{
get
{
parse();
return mt.subtype;
}
}
/// <summary>
/// Gets To header field
/// </summary>
/// <value>To header body</value>
public String To
{
get { return GetHeaderField("To", String.Empty, true, false); }
}
/// <summary>
/// Gets top-level media type from Content-Type header field
/// </summary>
/// <value>Top-level media type from Content-Type header field</value>
public MimeTopLevelMediaType TopLevelMediaType
{
get
{
parse();
return mt.TopLevelMediaType;
}
}
/// <summary>
/// Gets Version header field
/// </summary>
/// <value>Version header body</value>
public String Version
{
get { return GetHeaderField("Version", "1.0", true, false); }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public void Close()
{
_cached_headers = message.ReadLines(startpoint, endpoint);
message.Close();
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool Contains(String name)
{
if (headers == null)
parse();
return headers.Contains(name);
}
/// <summary>
/// Returns an enumerator that can iterate through the header fields
/// </summary>
/// <returns>A <see cref="System.Collections.IEnumerator" /> for the header fields</returns>
public IEnumerator GetEnumerator()
{
return headers.GetEnumerator();
}
/// <summary>
/// Returns the requested header field body.
/// </summary>
/// <param name="name">Header field name</param>
/// <param name="defaultvalue">Value to return when the requested field is not present</param>
/// <param name="uncomment"><b>true</b> to uncomment using <see cref="anmar.SharpMimeTools.SharpMimeTools.uncommentString" />; <b>false</b> to return the value unchanged.</param>
/// <param name="rfc2047decode"><b>true</b> to decode <see cref="anmar.SharpMimeTools.SharpMimeTools.rfc2047decode" />; <b>false</b> to return the value unchanged.</param>
/// <returns>Header field body</returns>
public String GetHeaderField(String name, String defaultvalue, bool uncomment, bool rfc2047decode)
{
String tmp = getProperty(name);
if (tmp == null)
tmp = defaultvalue;
else
{
if (uncomment)
tmp = SharpMimeTools.uncommentString(tmp);
if (rfc2047decode)
tmp = SharpMimeTools.rfc2047decode(tmp);
}
return tmp;
}
private String getProperty(String name)
{
String Value;
name = name.ToLower();
parse();
if (headers != null && headers.Count > 0 && name != null && name.Length > 0 && headers.Contains(name))
Value = headers[name].ToString();
else
Value = null;
return Value;
}
private bool parse()
{
bool error = false;
if (headers.Count > 0)
{
return !error;
}
String line = String.Empty;
message.SeekPoint(startpoint);
message.Encoding = SharpMimeHeader.default_encoding;
for (line = message.ReadUnfoldedLine(); line != null; line = message.ReadUnfoldedLine())
{
if (line.Length == 0)
{
endpoint = message.Position_preRead;
startbody = message.Position;
message.ReadLine_Undo(line);
break;
}
else
{
String[] headerline = line.Split(new Char[] { ':' }, 2);
if (headerline.Length == 2)
{
headerline[1] = headerline[1].TrimStart(new Char[] { ' ' });
if (headers.Contains(headerline[0]))
{
headers[headerline[0]] = String.Concat(headers[headerline[0]], "\r\n", headerline[1]);
}
else
{
headers.Add(headerline[0].ToLower(), headerline[1]);
}
}
}
}
mt = new HeaderInfo(headers);
return !error;
}
}
}