| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | // using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | // using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | #if !NETSTANDARD |
| | | 9 | | using Microsoft.Owin; |
| | | 10 | | #else |
| | | 11 | | using Microsoft.AspNetCore.Http; |
| | | 12 | | using Microsoft.AspNetCore.Http.Extensions; |
| | | 13 | | #endif |
| | | 14 | | |
| | | 15 | | namespace WireMock.Owin |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// OwinRequestMapper |
| | | 19 | | /// </summary> |
| | | 20 | | internal class OwinRequestMapper |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// MapAsync IOwinRequest to RequestMessage |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="request"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public async Task<RequestMessage> MapAsync( |
| | | 28 | | #if !NETSTANDARD |
| | | 29 | | IOwinRequest request |
| | | 30 | | #else |
| | | 31 | | HttpRequest request |
| | | 32 | | #endif |
| | | 33 | | ) |
| | 58 | 34 | | { |
| | | 35 | | #if !NETSTANDARD |
| | 58 | 36 | | Uri url = request.Uri; |
| | 58 | 37 | | string clientIP = request.RemoteIpAddress; |
| | | 38 | | #else |
| | | 39 | | Uri url = new Uri(request.GetEncodedUrl()); |
| | | 40 | | var connection = request.HttpContext.Connection; |
| | | 41 | | string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6 |
| | | 42 | | ? connection.RemoteIpAddress.MapToIPv4().ToString() |
| | | 43 | | : connection.RemoteIpAddress.ToString(); |
| | | 44 | | #endif |
| | 58 | 45 | | string method = request.Method; |
| | | 46 | | |
| | 58 | 47 | | Dictionary<string, string[]> headers = null; |
| | 58 | 48 | | if (request.Headers.Any()) |
| | 58 | 49 | | { |
| | 58 | 50 | | headers = new Dictionary<string, string[]>(); |
| | 456 | 51 | | foreach (var header in request.Headers) |
| | 141 | 52 | | { |
| | 141 | 53 | | headers.Add(header.Key, header.Value); |
| | 141 | 54 | | } |
| | 58 | 55 | | } |
| | | 56 | | |
| | 58 | 57 | | IDictionary<string, string> cookies = null; |
| | 58 | 58 | | if (request.Cookies.Any()) |
| | 2 | 59 | | { |
| | 2 | 60 | | cookies = new Dictionary<string, string>(); |
| | 10 | 61 | | foreach (var cookie in request.Cookies) |
| | 2 | 62 | | { |
| | 2 | 63 | | cookies.Add(cookie.Key, cookie.Value); |
| | 2 | 64 | | } |
| | 2 | 65 | | } |
| | | 66 | | |
| | 58 | 67 | | BodyData body = null; |
| | 58 | 68 | | if (request.Body != null && ShouldParseBody(method)) |
| | 10 | 69 | | { |
| | 10 | 70 | | body = await BodyParser.Parse(request.Body, request.ContentType); |
| | 10 | 71 | | } |
| | | 72 | | |
| | 58 | 73 | | return new RequestMessage(url, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now }; |
| | 58 | 74 | | } |
| | | 75 | | |
| | | 76 | | private bool ShouldParseBody(string method) |
| | 58 | 77 | | { |
| | | 78 | | /* |
| | | 79 | | HEAD - No defined body semantics. |
| | | 80 | | GET - No defined body semantics. |
| | | 81 | | PUT - Body supported. |
| | | 82 | | POST - Body supported. |
| | | 83 | | DELETE - No defined body semantics. |
| | | 84 | | TRACE - Body not supported. |
| | | 85 | | OPTIONS - Body supported but no semantics on usage (maybe in the future). |
| | | 86 | | CONNECT - No defined body semantics |
| | | 87 | | PATCH - Body supported. |
| | | 88 | | */ |
| | 58 | 89 | | return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper()); |
| | 58 | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |