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