| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net.Http.Headers; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http |
| | | 11 | | { |
| | | 12 | | public class MultipartFormDataContent : MultipartContent |
| | | 13 | | { |
| | | 14 | | private const string formData = "form-data"; |
| | | 15 | | |
| | | 16 | | public MultipartFormDataContent() |
| | 0 | 17 | | : base(formData) |
| | 0 | 18 | | { |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public MultipartFormDataContent(string boundary) |
| | 0 | 22 | | : base(formData, boundary) |
| | 0 | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public override void Add(HttpContent content) |
| | 0 | 27 | | { |
| | 0 | 28 | | ArgumentNullException.ThrowIfNull(content); |
| | | 29 | | |
| | 0 | 30 | | content.Headers.ContentDisposition ??= new ContentDispositionHeaderValue(formData); |
| | | 31 | | |
| | 0 | 32 | | base.Add(content); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public void Add(HttpContent content, string name) |
| | 0 | 36 | | { |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 38 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 39 | | |
| | 0 | 40 | | AddInternal(content, name, null); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public void Add(HttpContent content, string name, string fileName) |
| | 0 | 44 | | { |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(content); |
| | 0 | 46 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 0 | 47 | | ArgumentException.ThrowIfNullOrWhiteSpace(fileName); |
| | | 48 | | |
| | 0 | 49 | | AddInternal(content, name, fileName); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private void AddInternal(HttpContent content, string name, string? fileName) |
| | 0 | 53 | | { |
| | 0 | 54 | | if (content.Headers.ContentDisposition == null) |
| | 0 | 55 | | { |
| | 0 | 56 | | ContentDispositionHeaderValue header = new ContentDispositionHeaderValue(formData); |
| | 0 | 57 | | header.Name = name; |
| | 0 | 58 | | header.FileName = fileName; |
| | 0 | 59 | | header.FileNameStar = fileName; |
| | | 60 | | |
| | 0 | 61 | | content.Headers.ContentDisposition = header; |
| | 0 | 62 | | } |
| | 0 | 63 | | base.Add(content); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | | 67 | | // Only skip the original protected virtual SerializeToStreamAsync if this |
| | | 68 | | // isn't a derived type that may have overridden the behavior. |
| | 0 | 69 | | GetType() == typeof(MultipartFormDataContent) ? SerializeToStreamAsyncCore(stream, context, cancellationToke |
| | 0 | 70 | | base.SerializeToStreamAsync(stream, context, cancellationToken); |
| | | 71 | | } |
| | | 72 | | } |