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