| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/WebUtilities/src |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.IO; |
| | 8 | | using System.Text; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | #pragma warning disable CA1806 // Didn't check TryParse |
| | 12 | | #pragma warning disable IDE0018 // Inline declaration |
| | 13 | |
|
| | 14 | | namespace Azure.Core.Http.Multipart |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Various extension methods for dealing with the section body stream |
| | 18 | | /// </summary> |
| | 19 | | internal static class MultipartSectionStreamExtensions |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Reads the body of the section as a string |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="section">The section to read from</param> |
| | 25 | | /// <returns>The body steam as string</returns> |
| | 26 | | public static async Task<string> ReadAsStringAsync(this MultipartSection section) |
| | 27 | | { |
| 0 | 28 | | if (section == null) |
| | 29 | | { |
| 0 | 30 | | throw new ArgumentNullException(nameof(section)); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | MediaTypeHeaderValue sectionMediaType; |
| 0 | 34 | | MediaTypeHeaderValue.TryParse(section.ContentType, out sectionMediaType); |
| | 35 | |
|
| 0 | 36 | | Encoding streamEncoding = sectionMediaType?.Encoding; |
| 0 | 37 | | if (streamEncoding == null || streamEncoding == Encoding.UTF7) |
| | 38 | | { |
| 0 | 39 | | streamEncoding = Encoding.UTF8; |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | using (var reader = new StreamReader( |
| 0 | 43 | | section.Body, |
| 0 | 44 | | streamEncoding, |
| 0 | 45 | | detectEncodingFromByteOrderMarks: true, |
| 0 | 46 | | bufferSize: 1024, |
| 0 | 47 | | leaveOpen: true)) |
| | 48 | | { |
| 0 | 49 | | return await reader.ReadToEndAsync().ConfigureAwait(false); |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | | } |
| | 53 | | } |