| | | 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[] JsonContentTypes = |
| | 1 | 14 | | { |
| | 1 | 15 | | "application/json", |
| | 1 | 16 | | "application/vnd.api+json" |
| | 1 | 17 | | }; |
| | | 18 | | |
| | 1 | 19 | | private static readonly string[] TextContentTypes = |
| | 1 | 20 | | { |
| | 1 | 21 | | "text/", |
| | 1 | 22 | | "application/javascript", "application/typescript", |
| | 1 | 23 | | "application/xml", "application/xhtml+xml", |
| | 1 | 24 | | "application/x-www-form-urlencoded" |
| | 1 | 25 | | }; |
| | | 26 | | |
| | | 27 | | private static async Task<Tuple<string, Encoding>> ReadStringAsync(Stream stream) |
| | 10 | 28 | | { |
| | 10 | 29 | | using (var streamReader = new StreamReader(stream)) |
| | 10 | 30 | | { |
| | 10 | 31 | | string content = await streamReader.ReadToEndAsync(); |
| | | 32 | | |
| | 10 | 33 | | return new Tuple<string, Encoding>(content, streamReader.CurrentEncoding); |
| | | 34 | | } |
| | 10 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static async Task<byte[]> ReadBytesAsync(Stream stream) |
| | 0 | 38 | | { |
| | 0 | 39 | | using (var memoryStream = new MemoryStream()) |
| | 0 | 40 | | { |
| | 0 | 41 | | await stream.CopyToAsync(memoryStream); |
| | 0 | 42 | | return memoryStream.ToArray(); |
| | | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | public static async Task<BodyData> Parse([NotNull] Stream stream, [CanBeNull] string contentTypeHeaderValue) |
| | 10 | 47 | | { |
| | 10 | 48 | | var data = new BodyData(); |
| | | 49 | | |
| | 63 | 50 | | if (contentTypeHeaderValue != null && TextContentTypes.Any(text => contentTypeHeaderValue.StartsWith(text, S |
| | 2 | 51 | | { |
| | | 52 | | try |
| | 2 | 53 | | { |
| | 2 | 54 | | var stringData = await ReadStringAsync(stream); |
| | 2 | 55 | | data.BodyAsString = stringData.Item1; |
| | 2 | 56 | | data.Encoding = stringData.Item2; |
| | 2 | 57 | | } |
| | 0 | 58 | | catch |
| | 0 | 59 | | { |
| | | 60 | | // Reading as string failed, just get the ByteArray. |
| | 0 | 61 | | data.BodyAsBytes = await ReadBytesAsync(stream); |
| | 0 | 62 | | } |
| | 2 | 63 | | } |
| | 18 | 64 | | else if (contentTypeHeaderValue != null && JsonContentTypes.Any(json => contentTypeHeaderValue.StartsWith(js |
| | 8 | 65 | | { |
| | 8 | 66 | | var stringData = await ReadStringAsync(stream); |
| | 8 | 67 | | data.BodyAsString = stringData.Item1; |
| | 8 | 68 | | data.Encoding = stringData.Item2; |
| | | 69 | | |
| | | 70 | | try |
| | 8 | 71 | | { |
| | 8 | 72 | | data.BodyAsJson = JsonConvert.DeserializeObject(stringData.Item1, new JsonSerializerSettings { Forma |
| | 8 | 73 | | } |
| | 0 | 74 | | catch |
| | 0 | 75 | | { |
| | | 76 | | // JsonConvert failed, just set the Body as string. |
| | 0 | 77 | | data.BodyAsString = stringData.Item1; |
| | 0 | 78 | | } |
| | 8 | 79 | | } |
| | | 80 | | else |
| | 0 | 81 | | { |
| | 0 | 82 | | data.BodyAsBytes = await ReadBytesAsync(stream); |
| | 0 | 83 | | } |
| | | 84 | | |
| | 10 | 85 | | return data; |
| | 10 | 86 | | } |
| | | 87 | | } |
| | | 88 | | } |