-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipartContent.cs
598 lines (553 loc) · 21.3 KB
/
MultipartContent.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
// Decompiled with JetBrains decompiler
// Type: System.Net.Http.MultipartContent
// Assembly: System.Net.Http, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: D8D12B97-8555-48F6-96B3-BBC6BF89FCBA
// Assembly location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Net.Http.dll
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http
{
/// <summary>提供 <see cref="T:System.Net.Http.HttpContent" /> 对象的集合,其可通过使用多部分/* 内容类型规范序列化。</summary>
public class MultipartContent : HttpContent, IEnumerable<HttpContent>, IEnumerable
{
private static readonly int s_crlfLength = MultipartContent.GetEncodedLength("\r\n");
private static readonly int s_dashDashLength = MultipartContent.GetEncodedLength("--");
private static readonly int s_colonSpaceLength = MultipartContent.GetEncodedLength(": ");
private static readonly int s_commaSpaceLength = MultipartContent.GetEncodedLength(", ");
private readonly List<HttpContent> _nestedContent;
private readonly string _boundary;
/// <summary>创建 <see cref="T:System.Net.Http.MultipartContent" /> 类的新实例。</summary>
public MultipartContent()
: this("mixed", MultipartContent.GetDefaultBoundary())
{
}
/// <summary>创建 <see cref="T:System.Net.Http.MultipartContent" /> 类的新实例。</summary>
/// <param name="subtype">多部分内容的子类型。</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="subtype" /> 为 <see langword="null" /> 或者只包含空格字符。</exception>
public MultipartContent(string subtype)
: this(subtype, MultipartContent.GetDefaultBoundary())
{
}
/// <summary>创建 <see cref="T:System.Net.Http.MultipartContent" /> 类的新实例。</summary>
/// <param name="subtype">多部分内容的子类型。</param>
/// <param name="boundary">多部分内容的边界字符串。</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="subtype" /> 是 <see langword="null" /> 或空字符串。
/// <paramref name="boundary" /> 为 <see langword="null" /> 或者只包含空格字符。
///
/// 或 -
/// <paramref name="boundary" /> 以空格字符结尾。</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="boundary" /> 的长度大于 70。</exception>
public MultipartContent(string subtype, string boundary)
{
if (string.IsNullOrWhiteSpace(subtype))
throw new ArgumentException(SR.net_http_argument_empty_string, nameof (subtype));
MultipartContent.ValidateBoundary(boundary);
this._boundary = boundary;
string str = boundary;
if (!str.StartsWith("\"", StringComparison.Ordinal))
str = "\"" + str + "\"";
this.Headers.ContentType = new MediaTypeHeaderValue("multipart/" + subtype)
{
Parameters = {
new NameValueHeaderValue(nameof (boundary), str)
}
};
this._nestedContent = new List<HttpContent>();
}
private static void ValidateBoundary(string boundary)
{
if (string.IsNullOrWhiteSpace(boundary))
throw new ArgumentException(SR.net_http_argument_empty_string, nameof (boundary));
if (boundary.Length > 70)
throw new ArgumentOutOfRangeException(nameof (boundary), (object) boundary, SR.Format((IFormatProvider) CultureInfo.InvariantCulture, SR.net_http_content_field_too_long, (object) 70));
if (boundary.EndsWith(" ", StringComparison.Ordinal))
throw new ArgumentException(SR.Format((IFormatProvider) CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, (object) boundary), nameof (boundary));
foreach (char ch in boundary)
{
if (('0' > ch || ch > '9') && ('a' > ch || ch > 'z') && (('A' > ch || ch > 'Z') && !"'()+_,-./:=? ".Contains(ch)))
throw new ArgumentException(SR.Format((IFormatProvider) CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, (object) boundary), nameof (boundary));
}
}
private static string GetDefaultBoundary()
{
return Guid.NewGuid().ToString();
}
/// <summary>添加多部分 HTTP 内容到 <see cref="T:System.Net.Http.HttpContent" /> 对象的集合,其可通过使用多部分/* 内容类型规范获取序列化。</summary>
/// <param name="content">要添加到集合中的 HTTP 内容。</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="content" /> 是 <see langword="null" />。</exception>
public virtual void Add(HttpContent content)
{
if (content == null)
throw new ArgumentNullException(nameof (content));
this._nestedContent.Add(content);
}
/// <summary>释放由 <see cref="T:System.Net.Http.MultipartContent" /> 使用的非托管资源,并可根据需要释放托管资源。</summary>
/// <param name="disposing">如果释放托管资源和非托管资源,则为 <see langword="true" />;如果仅释放非托管资源,则为 <see langword="false" />。</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
foreach (HttpContent httpContent in this._nestedContent)
httpContent.Dispose();
this._nestedContent.Clear();
}
base.Dispose(disposing);
}
/// <summary>返回循环访问 <see cref="T:System.Net.Http.HttpContent" /> 对象集合的枚举器,该对象集合使用多部分/* 内容类型规范进行序列化。</summary>
/// <returns>一个可用于循环访问集合的对象。</returns>
public IEnumerator<HttpContent> GetEnumerator()
{
return (IEnumerator<HttpContent>) this._nestedContent.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) this._nestedContent.GetEnumerator();
}
/// <summary>以异步操作方式,将多部分 HTTP 内容序列化到流。</summary>
/// <param name="stream">目标流。</param>
/// <param name="context">有关传输的信息(例如信道绑定令牌)。 此参数可以为 <see langword="null" />。</param>
/// <returns>表示异步操作的任务对象。</returns>
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return this.SerializeToStreamAsyncCore(stream, context, new CancellationToken());
}
internal override Task SerializeToStreamAsync(
Stream stream,
TransportContext context,
CancellationToken cancellationToken)
{
return !(this.GetType() == typeof (MultipartContent)) ? base.SerializeToStreamAsync(stream, context, cancellationToken) : this.SerializeToStreamAsyncCore(stream, context, cancellationToken);
}
private protected async Task SerializeToStreamAsyncCore(
Stream stream,
TransportContext context,
CancellationToken cancellationToken)
{
MultipartContent multipartContent = this;
try
{
ConfiguredValueTaskAwaitable valueTaskAwaitable = MultipartContent.EncodeStringToStreamAsync(stream, "--" + multipartContent._boundary + "\r\n", cancellationToken).ConfigureAwait(false);
await valueTaskAwaitable;
StringBuilder output = new StringBuilder();
for (int contentIndex = 0; contentIndex < multipartContent._nestedContent.Count; ++contentIndex)
{
HttpContent content = multipartContent._nestedContent[contentIndex];
valueTaskAwaitable = MultipartContent.EncodeStringToStreamAsync(stream, multipartContent.SerializeHeadersToString(output, contentIndex, content), cancellationToken).ConfigureAwait(false);
await valueTaskAwaitable;
await content.CopyToAsync(stream, context, cancellationToken).ConfigureAwait(false);
content = (HttpContent) null;
}
valueTaskAwaitable = MultipartContent.EncodeStringToStreamAsync(stream, "\r\n--" + multipartContent._boundary + "--\r\n", cancellationToken).ConfigureAwait(false);
await valueTaskAwaitable;
output = (StringBuilder) null;
}
catch (Exception ex)
{
if (NetEventSource.IsEnabled)
NetEventSource.Error((object) multipartContent, (object) ex, nameof (SerializeToStreamAsyncCore));
throw;
}
}
protected override async Task<Stream> CreateContentReadStreamAsync()
{
MultipartContent multipartContent = this;
try
{
Stream[] streams = new Stream[2 + multipartContent._nestedContent.Count * 2];
StringBuilder scratch = new StringBuilder();
int streamIndex = 0;
streams[streamIndex++] = MultipartContent.EncodeStringToNewStream("--" + multipartContent._boundary + "\r\n");
for (int contentIndex = 0; contentIndex < multipartContent._nestedContent.Count; ++contentIndex)
{
HttpContent content = multipartContent._nestedContent[contentIndex];
streams[streamIndex++] = MultipartContent.EncodeStringToNewStream(multipartContent.SerializeHeadersToString(scratch, contentIndex, content));
ConfiguredTaskAwaitable<Stream> configuredTaskAwaitable = content.ReadAsStreamAsync().ConfigureAwait(false);
Stream stream = await configuredTaskAwaitable ?? (Stream) new MemoryStream();
if (!stream.CanSeek)
{
// ISSUE: reference to a compiler-generated method
configuredTaskAwaitable = multipartContent.\u003C\u003En__0().ConfigureAwait(false);
return await configuredTaskAwaitable;
}
streams[streamIndex++] = stream;
}
streams[streamIndex] = MultipartContent.EncodeStringToNewStream("\r\n--" + multipartContent._boundary + "--\r\n");
return (Stream) new MultipartContent.ContentReadStream(streams);
}
catch (Exception ex)
{
if (NetEventSource.IsEnabled)
NetEventSource.Error((object) multipartContent, (object) ex, nameof (CreateContentReadStreamAsync));
throw;
}
}
private string SerializeHeadersToString(
StringBuilder scratch,
int contentIndex,
HttpContent content)
{
scratch.Clear();
if (contentIndex != 0)
{
scratch.Append("\r\n--");
scratch.Append(this._boundary);
scratch.Append("\r\n");
}
foreach (KeyValuePair<string, IEnumerable<string>> header in (HttpHeaders) content.Headers)
{
scratch.Append(header.Key);
scratch.Append(": ");
string str1 = string.Empty;
foreach (string str2 in header.Value)
{
scratch.Append(str1);
scratch.Append(str2);
str1 = ", ";
}
scratch.Append("\r\n");
}
scratch.Append("\r\n");
return scratch.ToString();
}
private static ValueTask EncodeStringToStreamAsync(
Stream stream,
string input,
CancellationToken cancellationToken)
{
byte[] bytes = HttpRuleParser.DefaultHttpEncoding.GetBytes(input);
return stream.WriteAsync(new ReadOnlyMemory<byte>(bytes), cancellationToken);
}
private static Stream EncodeStringToNewStream(string input)
{
return (Stream) new MemoryStream(HttpRuleParser.DefaultHttpEncoding.GetBytes(input), false);
}
internal override bool AllowDuplex
{
get
{
return false;
}
}
/// <summary>确定 HTTP 多部分内容的长度是否有效(以字节为单位)。</summary>
/// <param name="length">HTTP 内容的长度(以字节为单位)。</param>
/// <returns>如果 <paramref name="length" /> 是有效长度,则为 <see langword="true" />;否则为 <see langword="false" />。</returns>
protected internal override bool TryComputeLength(out long length)
{
int encodedLength = MultipartContent.GetEncodedLength(this._boundary);
long num1 = 0;
long num2 = (long) (MultipartContent.s_crlfLength + MultipartContent.s_dashDashLength + encodedLength + MultipartContent.s_crlfLength);
long num3 = num1 + (long) (MultipartContent.s_dashDashLength + encodedLength + MultipartContent.s_crlfLength);
bool flag = true;
foreach (HttpContent httpContent in this._nestedContent)
{
if (flag)
flag = false;
else
num3 += num2;
foreach (KeyValuePair<string, IEnumerable<string>> header in (HttpHeaders) httpContent.Headers)
{
num3 += (long) (MultipartContent.GetEncodedLength(header.Key) + MultipartContent.s_colonSpaceLength);
int num4 = 0;
foreach (string input in header.Value)
{
num3 += (long) MultipartContent.GetEncodedLength(input);
++num4;
}
if (num4 > 1)
num3 += (long) ((num4 - 1) * MultipartContent.s_commaSpaceLength);
num3 += (long) MultipartContent.s_crlfLength;
}
num3 += (long) MultipartContent.s_crlfLength;
long length1 = 0;
if (!httpContent.TryComputeLength(out length1))
{
length = 0L;
return false;
}
num3 += length1;
}
long num5 = num3 + (long) (MultipartContent.s_crlfLength + MultipartContent.s_dashDashLength + encodedLength + MultipartContent.s_dashDashLength + MultipartContent.s_crlfLength);
length = num5;
return true;
}
private static int GetEncodedLength(string input)
{
return HttpRuleParser.DefaultHttpEncoding.GetByteCount(input);
}
private sealed class ContentReadStream : Stream
{
private readonly Stream[] _streams;
private readonly long _length;
private int _next;
private Stream _current;
private long _position;
internal ContentReadStream(Stream[] streams)
{
this._streams = streams;
foreach (Stream stream in streams)
this._length += stream.Length;
}
protected override void Dispose(bool disposing)
{
if (!disposing)
return;
foreach (Stream stream in this._streams)
stream.Dispose();
}
public override async ValueTask DisposeAsync()
{
Stream[] streamArray = this._streams;
for (int index = 0; index < streamArray.Length; ++index)
await streamArray[index].DisposeAsync().ConfigureAwait(false);
streamArray = (Stream[]) null;
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanSeek
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
MultipartContent.ContentReadStream.ValidateReadArgs(buffer, offset, count);
if (count == 0)
return 0;
int num;
while (true)
{
if (this._current != null)
{
num = this._current.Read(buffer, offset, count);
if (num == 0)
this._current = (Stream) null;
else
break;
}
if (this._next < this._streams.Length)
this._current = this._streams[this._next++];
else
goto label_7;
}
this._position += (long) num;
return num;
label_7:
return 0;
}
public override int Read(Span<byte> buffer)
{
if (buffer.Length == 0)
return 0;
int num;
while (true)
{
if (this._current != null)
{
num = this._current.Read(buffer);
if (num == 0)
this._current = (Stream) null;
else
break;
}
if (this._next < this._streams.Length)
this._current = this._streams[this._next++];
else
goto label_7;
}
this._position += (long) num;
return num;
label_7:
return 0;
}
public override Task<int> ReadAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken)
{
MultipartContent.ContentReadStream.ValidateReadArgs(buffer, offset, count);
return this.ReadAsyncPrivate(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
public override ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default (CancellationToken))
{
return this.ReadAsyncPrivate(buffer, cancellationToken);
}
public override IAsyncResult BeginRead(
byte[] array,
int offset,
int count,
AsyncCallback asyncCallback,
object asyncState)
{
return TaskToApm.Begin((Task) this.ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState);
}
public override int EndRead(IAsyncResult asyncResult)
{
return TaskToApm.End<int>(asyncResult);
}
public async ValueTask<int> ReadAsyncPrivate(
Memory<byte> buffer,
CancellationToken cancellationToken)
{
MultipartContent.ContentReadStream contentReadStream1 = this;
if (buffer.Length == 0)
return 0;
int num1;
while (true)
{
if (contentReadStream1._current != null)
{
num1 = await contentReadStream1._current.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (num1 == 0)
contentReadStream1._current = (Stream) null;
else
break;
}
if (contentReadStream1._next < contentReadStream1._streams.Length)
{
MultipartContent.ContentReadStream contentReadStream2 = contentReadStream1;
Stream[] streams = contentReadStream1._streams;
MultipartContent.ContentReadStream contentReadStream3 = contentReadStream1;
int next = contentReadStream1._next;
int num2 = next + 1;
contentReadStream3._next = num2;
int index = next;
Stream stream = streams[index];
contentReadStream2._current = stream;
}
else
goto label_7;
}
contentReadStream1._position += (long) num1;
return num1;
label_7:
return 0;
}
public override long Position
{
get
{
return this._position;
}
set
{
if (value < 0L)
throw new ArgumentOutOfRangeException(nameof (value));
long num = 0;
for (int index1 = 0; index1 < this._streams.Length; ++index1)
{
Stream stream = this._streams[index1];
long length = stream.Length;
if (value < num + length)
{
this._current = stream;
int index2 = index1 + 1;
this._next = index2;
stream.Position = value - num;
for (; index2 < this._streams.Length; ++index2)
this._streams[index2].Position = 0L;
this._position = value;
return;
}
num += length;
}
this._current = (Stream) null;
this._next = this._streams.Length;
this._position = value;
}
}
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
this.Position = offset;
break;
case SeekOrigin.Current:
this.Position += offset;
break;
case SeekOrigin.End:
this.Position = this._length + offset;
break;
default:
throw new ArgumentOutOfRangeException(nameof (origin));
}
return this.Position;
}
public override long Length
{
get
{
return this._length;
}
}
private static void ValidateReadArgs(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException(nameof (buffer));
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof (offset));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof (count));
if (offset > buffer.Length - count)
throw new ArgumentException(SR.net_http_buffer_insufficient_length, nameof (buffer));
}
public override void Flush()
{
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Write(ReadOnlySpan<byte> buffer)
{
throw new NotSupportedException();
}
public override Task WriteAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
public override ValueTask WriteAsync(
ReadOnlyMemory<byte> buffer,
CancellationToken cancellationToken = default (CancellationToken))
{
throw new NotSupportedException();
}
}
}
}