| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Newtonsoft.Json; |
| | | 8 | | |
| | | 9 | | namespace WireMock.Util |
| | | 10 | | { |
| | | 11 | | internal static class BodyParser |
| | | 12 | | { |
| | 1 | 13 | | private static readonly string[] TextContentTypes = |
| | 1 | 14 | | { |
| | 1 | 15 | | "text/", |
| | 1 | 16 | | "application/javascript", "application/typescript", |
| | 1 | 17 | | "application/xml", "application/xhtml+xml", |
| | 1 | 18 | | "application/x-www-form-urlencoded" |
| | 1 | 19 | | }; |
| | | 20 | | |
| | | 21 | | private static async Task<Tuple<string, Encoding>> ReadStringAsync(Stream stream) |
| | 14 | 22 | | { |
| | 14 | 23 | | using (var streamReader = new StreamReader(stream)) |
| | 14 | 24 | | { |
| | 14 | 25 | | string content = await streamReader.ReadToEndAsync(); |
| | | 26 | | |
| | 14 | 27 | | return new Tuple<string, Encoding>(content, streamReader.CurrentEncoding); |
| | | 28 | | } |
| | 14 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static async Task<byte[]> ReadBytesAsync(Stream stream) |
| | 5 | 32 | | { |
| | 5 | 33 | | using (var memoryStream = new MemoryStream()) |
| | 5 | 34 | | { |
| | 5 | 35 | | await stream.CopyToAsync(memoryStream); |
| | 5 | 36 | | return memoryStream.ToArray(); |
| | | 37 | | } |
| | 5 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static async Task<BodyData> Parse([NotNull] Stream stream, [CanBeNull] string contentTypeHeaderValue) |
| | 19 | 41 | | { |
| | 19 | 42 | | var data = new BodyData(); |
| | | 43 | | |
| | 51 | 44 | | if (contentTypeHeaderValue != null && TextContentTypes.Any(t => contentTypeHeaderValue.StartsWith(t, StringC |
| | 11 | 45 | | { |
| | | 46 | | try |
| | 11 | 47 | | { |
| | 11 | 48 | | var stringData = await ReadStringAsync(stream); |
| | 11 | 49 | | data.BodyAsString = stringData.Item1; |
| | 11 | 50 | | data.Encoding = stringData.Item2; |
| | 11 | 51 | | } |
| | 0 | 52 | | catch |
| | 0 | 53 | | { |
| | | 54 | | // Reading as string failed, just get the ByteArray. |
| | 0 | 55 | | data.BodyAsBytes = await ReadBytesAsync(stream); |
| | 0 | 56 | | } |
| | 11 | 57 | | } |
| | 8 | 58 | | else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringCompa |
| | 3 | 59 | | { |
| | 3 | 60 | | var stringData = await ReadStringAsync(stream); |
| | 3 | 61 | | data.Encoding = stringData.Item2; |
| | | 62 | | |
| | | 63 | | try |
| | 3 | 64 | | { |
| | 3 | 65 | | data.BodyAsJson = JsonConvert.DeserializeObject(stringData.Item1, new JsonSerializerSettings { Forma |
| | 3 | 66 | | } |
| | 0 | 67 | | catch |
| | 0 | 68 | | { |
| | | 69 | | // JsonConvert failed, just set the Body as string. |
| | 0 | 70 | | data.BodyAsString = stringData.Item1; |
| | 0 | 71 | | } |
| | 3 | 72 | | } |
| | | 73 | | else |
| | 5 | 74 | | { |
| | 5 | 75 | | data.BodyAsBytes = await ReadBytesAsync(stream); |
| | 5 | 76 | | } |
| | | 77 | | |
| | 19 | 78 | | return data; |
| | 19 | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |