forked from jstedfast/MimeKit
/
ContentDisposition.cs
830 lines (723 loc) · 28.9 KB
/
ContentDisposition.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//
// ContentDisposition.cs
//
// Author: Jeffrey Stedfast <jeff@xamarin.com>
//
// Copyright (c) 2013-2014 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Text;
#if PORTABLE
using Encoding = Portable.Text.Encoding;
#endif
using MimeKit.Utils;
namespace MimeKit {
/// <summary>
/// A class representing a Content-Disposition header value.
/// </summary>
/// <remarks>
/// The Content-Disposition header is a way for the originating client to
/// suggest to the receiving client whether to present the part to the user
/// as an attachment or as part of the content (inline).
/// </remarks>
public class ContentDisposition
{
/// <summary>
/// The attachment disposition.
/// </summary>
/// <remarks>
/// Indicates that the <see cref="MimePart"/> should be treated as an attachment.
/// </remarks>
public const string Attachment = "attachment";
/// <summary>
/// The inline disposition.
/// </summary>
/// <remarks>
/// Indicates that the <see cref="MimePart"/> should be rendered inline.
/// </remarks>
public const string Inline = "inline";
static readonly StringComparer icase = StringComparer.OrdinalIgnoreCase;
ParameterList parameters;
string disposition;
/// <summary>
/// Initializes a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// The disposition should either be <see cref="ContentDisposition.Attachment"/>
/// or <see cref="ContentDisposition.Inline"/>.
/// </remarks>
/// <param name="disposition">The disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="disposition"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="disposition"/> is not <c>"attachment"</c> or <c>"inline"</c>.
/// </exception>
public ContentDisposition (string disposition)
{
Parameters = new ParameterList ();
Disposition = disposition;
}
/// <summary>
/// Initializes a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// This is identical to <see cref="ContentDisposition(string)"/> with a disposition
/// value of <see cref="ContentDisposition.Attachment"/>.
/// </remarks>
public ContentDisposition () : this (Attachment)
{
}
/// <summary>
/// Gets or sets the disposition.
/// </summary>
/// <remarks>
/// The disposition is typically either <c>"attachment"</c> or <c>"inline"</c>.
/// </remarks>
/// <value>The disposition.</value>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="value"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="value"/> is not <c>"attachment"</c> or <c>"inline"</c>.
/// </exception>
public string Disposition {
get { return disposition; }
set {
if (value == null)
throw new ArgumentNullException ("value");
if (value.Length == 0)
throw new ArgumentException ("The disposition is not allowed to be empty.", "value");
if (icase.Compare ("attachment", value) != 0 && icase.Compare ("inline", value) != 0)
throw new ArgumentException ("The disposition is only allowed to be either 'attachment' or 'inline'.", "value");
if (disposition == value)
return;
disposition = value;
OnChanged ();
}
}
/// <summary>
/// Gets or sets a value indicating whether the <see cref="MimePart"/> is an attachment.
/// </summary>
/// <remarks>
/// A convenience property to determine if the entity should be considered an attachment or not.
/// </remarks>
/// <value><c>true</c> if the <see cref="MimePart"/> is an attachment; otherwise, <c>false</c>.</value>
public bool IsAttachment {
get { return icase.Compare (disposition, Attachment) == 0; }
set { disposition = value ? Attachment : Inline; }
}
/// <summary>
/// Gets the parameters.
/// </summary>
/// <remarks>
/// In addition to specifying whether the entity should be treated as an
/// attachment vs displayed inline, the Content-Disposition header may also
/// contain parameters to provide further information to the receiving client
/// such as the file attributes.
/// </remarks>
/// <value>The parameters.</value>
public ParameterList Parameters {
get { return parameters; }
private set {
if (parameters != null)
parameters.Changed -= OnParametersChanged;
value.Changed += OnParametersChanged;
parameters = value;
}
}
/// <summary>
/// Gets or sets the name of the file.
/// </summary>
/// <remarks>
/// When set, this can provide a useful hint for a default file name for the
/// content when the user decides to save it to disk.
/// </remarks>
/// <value>The name of the file.</value>
public string FileName {
get { return Parameters["filename"]; }
set {
if (value != null)
Parameters["filename"] = value;
else
Parameters.Remove ("filename");
}
}
/// <summary>
/// Gets or sets the creation-date parameter.
/// </summary>
/// <remarks>
/// Refers to the date and time that the content file was created on the
/// originating system. This parameter serves little purpose and is
/// typically not used by mail clients.
/// </remarks>
/// <value>The creation date.</value>
public DateTimeOffset? CreationDate {
get {
var value = Parameters["creation-date"];
if (string.IsNullOrWhiteSpace (value))
return null;
var buffer = Encoding.UTF8.GetBytes (value);
DateTimeOffset ctime;
if (!DateUtils.TryParseDateTime (buffer, 0, buffer.Length, out ctime))
return null;
return ctime;
}
set {
if (value.HasValue)
Parameters["creation-date"] = DateUtils.FormatDate (value.Value);
else
Parameters.Remove ("creation-date");
}
}
/// <summary>
/// Gets or sets the modification-date parameter.
/// </summary>
/// <remarks>
/// Refers to the date and time that the content file was last modified on
/// the originating system. This parameter serves little purpose and is
/// typically not used by mail clients.
/// </remarks>
/// <value>The modification date.</value>
public DateTimeOffset? ModificationDate {
get {
var value = Parameters["modification-date"];
if (string.IsNullOrWhiteSpace (value))
return null;
var buffer = Encoding.UTF8.GetBytes (value);
DateTimeOffset mtime;
if (!DateUtils.TryParseDateTime (buffer, 0, buffer.Length, out mtime))
return null;
return mtime;
}
set {
if (value.HasValue)
Parameters["modification-date"] = DateUtils.FormatDate (value.Value);
else
Parameters.Remove ("modification-date");
}
}
/// <summary>
/// Gets or sets the read-date parameter.
/// </summary>
/// <remarks>
/// Refers to the date and time that the content file was last read on the
/// originating system. This parameter serves little purpose and is typically
/// not used by mail clients.
/// </remarks>
/// <value>The read date.</value>
public DateTimeOffset? ReadDate {
get {
var value = Parameters["read-date"];
if (string.IsNullOrWhiteSpace (value))
return null;
var buffer = Encoding.UTF8.GetBytes (value);
DateTimeOffset atime;
if (!DateUtils.TryParseDateTime (buffer, 0, buffer.Length, out atime))
return null;
return atime;
}
set {
if (value.HasValue)
Parameters["read-date"] = DateUtils.FormatDate (value.Value);
else
Parameters.Remove ("read-date");
}
}
/// <summary>
/// Gets or sets the size parameter.
/// </summary>
/// <remarks>
/// When set, the size parameter typically refers to the original size of the
/// content on disk. This parameter is rarely used by mail clients as it serves
/// little purpose.
/// </remarks>
/// <value>The size.</value>
public long? Size {
get {
var value = Parameters["size"];
if (string.IsNullOrWhiteSpace (value))
return null;
long size;
if (!long.TryParse (value, out size))
return null;
return size;
}
set {
if (value.HasValue)
Parameters["size"] = value.Value.ToString ();
else
Parameters.Remove ("size");
}
}
internal string Encode (FormatOptions options, Encoding charset)
{
int lineLength = "Content-Disposition: ".Length;
var value = new StringBuilder (" ");
value.Append (disposition);
Parameters.Encode (options, value, ref lineLength, charset);
value.Append (options.NewLine);
return value.ToString ();
}
/// <summary>
/// Serializes the <see cref="MimeKit.ContentDisposition"/> to a string,
/// optionally encoding the parameters.
/// </summary>
/// <remarks>
/// Creates a string-representation of the <see cref="ContentDisposition"/>,
/// optionally encoding the parameters as they would be encoded for trabsport.
/// </remarks>
/// <returns>The serialized string.</returns>
/// <param name="charset">The charset to be used when encoding the parameter values.</param>
/// <param name="encode">If set to <c>true</c>, the parameter values will be encoded.</param>
public string ToString (Encoding charset, bool encode)
{
if (charset == null)
throw new ArgumentNullException ("charset");
var value = new StringBuilder ("Content-Disposition: ");
value.Append (disposition);
if (encode) {
int lineLength = value.Length;
Parameters.Encode (FormatOptions.Default, value, ref lineLength, charset);
} else {
value.Append (Parameters.ToString ());
}
return value.ToString ();
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current
/// <see cref="MimeKit.ContentDisposition"/>.
/// </summary>
/// <remarks>
/// Creates a string-representation of the <see cref="ContentDisposition"/>.
/// </remarks>
/// <returns>A <see cref="System.String"/> that represents the current
/// <see cref="MimeKit.ContentDisposition"/>.</returns>
public override string ToString ()
{
return ToString (Encoding.UTF8, false);
}
internal event EventHandler Changed;
void OnParametersChanged (object sender, EventArgs e)
{
OnChanged ();
}
void OnChanged ()
{
if (Changed != null)
Changed (this, EventArgs.Empty);
}
internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out ContentDisposition disposition)
{
string type;
int atom;
disposition = null;
if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
return false;
atom = index;
if (!ParseUtils.SkipAtom (text, ref index, endIndex)) {
if (throwOnError)
throw new ParseException (string.Format ("Invalid atom token at position {0}", atom), atom, index);
return false;
}
type = Encoding.ASCII.GetString (text, atom, index - atom);
if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
return false;
disposition = new ContentDisposition ();
disposition.disposition = type;
if (index >= endIndex)
return true;
if (text[index] != (byte) ';') {
if (throwOnError)
throw new ParseException (string.Format ("Expected ';' at position {0}", index), index, index);
return false;
}
index++;
if (!ParseUtils.SkipCommentsAndWhiteSpace (text, ref index, endIndex, throwOnError))
return false;
if (index >= endIndex)
return true;
ParameterList parameters;
if (!ParameterList.TryParse (options, text, ref index, endIndex, throwOnError, out parameters))
return false;
disposition.Parameters = parameters;
return true;
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the given index
/// and spanning across the specified number of bytes.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The starting index of the input buffer.</param>
/// <param name="length">The number of bytes in the input buffer to parse.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, int length, out ContentDisposition disposition)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (startIndex < 0 || startIndex > buffer.Length)
throw new ArgumentOutOfRangeException ("startIndex");
if (length < 0 || length > (buffer.Length - startIndex))
throw new ArgumentOutOfRangeException ("length");
int index = startIndex;
return TryParse (options, buffer, ref index, startIndex + length, false, out disposition);
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the given index
/// and spanning across the specified number of bytes.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The starting index of the input buffer.</param>
/// <param name="length">The number of bytes in the input buffer to parse.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
public static bool TryParse (byte[] buffer, int startIndex, int length, out ContentDisposition disposition)
{
return TryParse (ParserOptions.Default, buffer, startIndex, length, out disposition);
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the specified index.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The starting index of the input buffer.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, out ContentDisposition disposition)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (startIndex < 0 || startIndex >= buffer.Length)
throw new ArgumentOutOfRangeException ("startIndex");
int index = startIndex;
return TryParse (options, buffer, ref index, buffer.Length, false, out disposition);
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the specified index.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The starting index of the input buffer.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
public static bool TryParse (byte[] buffer, int startIndex, out ContentDisposition disposition)
{
return TryParse (ParserOptions.Default, buffer, startIndex, out disposition);
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the specified buffer.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
public static bool TryParse (ParserOptions options, byte[] buffer, out ContentDisposition disposition)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
int index = 0;
return TryParse (options, buffer, ref index, buffer.Length, false, out disposition);
}
/// <summary>
/// Tries to parse the given input buffer into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the specified buffer.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="buffer">The input buffer.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
public static bool TryParse (byte[] buffer, out ContentDisposition disposition)
{
return TryParse (ParserOptions.Default, buffer, out disposition);
}
/// <summary>
/// Tries to parse the given text into a new <see cref="MimeKit.ContentDisposition"/> instance.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied text.
/// </remarks>
/// <returns><c>true</c>, if the disposition was successfully parsed, <c>false</c> otherwise.</returns>
/// <param name="text">The text to parse.</param>
/// <param name="disposition">The parsed disposition.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="text"/> is <c>null</c>.
/// </exception>
public static bool TryParse (string text, out ContentDisposition disposition)
{
if (text == null)
throw new ArgumentNullException ("text");
var buffer = Encoding.UTF8.GetBytes (text);
int index = 0;
return TryParse (ParserOptions.Default, buffer, ref index, buffer.Length, false, out disposition);
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the given index
/// and spanning across the specified number of bytes.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The start index of the buffer.</param>
/// <param name="length">The length of the buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (ParserOptions options, byte[] buffer, int startIndex, int length)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (startIndex < 0 || startIndex > buffer.Length)
throw new ArgumentOutOfRangeException ("startIndex");
if (length < 0 || length > (buffer.Length - startIndex))
throw new ArgumentOutOfRangeException ("length");
ContentDisposition disposition;
int index = startIndex;
TryParse (options, buffer, ref index, startIndex + length, true, out disposition);
return disposition;
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the given index
/// and spanning across the specified number of bytes.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The start index of the buffer.</param>
/// <param name="length">The length of the buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
/// a valid range in the byte array.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (byte[] buffer, int startIndex, int length)
{
return Parse (ParserOptions.Default, buffer, startIndex, length);
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the specified index.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The start index of the buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (ParserOptions options, byte[] buffer, int startIndex)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (startIndex < 0 || startIndex > buffer.Length)
throw new ArgumentOutOfRangeException ("startIndex");
ContentDisposition disposition;
int index = startIndex;
TryParse (options, buffer, ref index, buffer.Length, true, out disposition);
return disposition;
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer starting at the specified index.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="buffer">The input buffer.</param>
/// <param name="startIndex">The start index of the buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is out of range.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (byte[] buffer, int startIndex)
{
return Parse (ParserOptions.Default, buffer, startIndex);
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="options">The parser options.</param>
/// <param name="buffer">The input buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="buffer"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (ParserOptions options, byte[] buffer)
{
if (options == null)
throw new ArgumentNullException ("options");
if (buffer == null)
throw new ArgumentNullException ("buffer");
ContentDisposition disposition;
int index = 0;
TryParse (options, buffer, ref index, buffer.Length, true, out disposition);
return disposition;
}
/// <summary>
/// Parse the specified input buffer into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the supplied buffer.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="buffer">The input buffer.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="buffer"/> is <c>null</c>.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="buffer"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (byte[] buffer)
{
return Parse (ParserOptions.Default, buffer);
}
/// <summary>
/// Parse the specified text into a new instance of the <see cref="MimeKit.ContentDisposition"/> class.
/// </summary>
/// <remarks>
/// Parses a Content-Disposition value from the specified text.
/// </remarks>
/// <returns>The parsed <see cref="MimeKit.ContentDisposition"/>.</returns>
/// <param name="text">The input text.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="text"/> is <c>null</c>.
/// </exception>
/// <exception cref="MimeKit.ParseException">
/// The <paramref name="text"/> could not be parsed.
/// </exception>
public static ContentDisposition Parse (string text)
{
if (text == null)
throw new ArgumentNullException ("text");
var buffer = Encoding.UTF8.GetBytes (text);
ContentDisposition disposition;
int index = 0;
TryParse (ParserOptions.Default, buffer, ref index, buffer.Length, true, out disposition);
return disposition;
}
}
}