// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
#if USE_ASPNETCORE
using System.Security.Cryptography.X509Certificates;
#endif
using Stef.Validation;
using WireMock.Models;
using WireMock.Owin;
using WireMock.Types;
using WireMock.Util;
namespace WireMock;
///
/// The RequestMessage.
///
public class RequestMessage : IRequestMessage
{
///
public string ClientIP { get; }
///
public string Url { get; }
///
public string AbsoluteUrl { get; }
///
public string? ProxyUrl { get; set; }
///
public DateTime DateTime { get; set; }
///
public string Path { get; }
///
public string AbsolutePath { get; }
///
public string[] PathSegments { get; }
///
public string[] AbsolutePathSegments { get; }
///
public string Method { get; }
///
public string HttpVersion { get; }
///
public IDictionary>? Headers { get; }
///
public IDictionary? Cookies { get; }
///
public IDictionary>? Query { get; }
///
public IDictionary>? QueryIgnoreCase { get; }
///
public string RawQuery { get; }
///
public IBodyData? BodyData { get; }
///
public string? Body { get; }
///
public object? BodyAsJson { get; set; }
///
public byte[]? BodyAsBytes { get; }
#if MIMEKIT
///
[Newtonsoft.Json.JsonIgnore] // Issue 1001
public object? BodyAsMimeMessage { get; }
#endif
///
public string? DetectedBodyType { get; }
///
public string? DetectedBodyTypeFromContentType { get; }
///
public string? DetectedCompression { get; }
///
public string Host { get; }
///
public string Protocol { get; }
///
public int Port { get; }
///
public string Origin { get; }
#if USE_ASPNETCORE
///
public X509Certificate2? ClientCertificate { get; }
#endif
///
/// Used for Unit Testing
///
internal RequestMessage(
UrlDetails urlDetails,
string method,
string clientIP,
IBodyData? bodyData = null,
IDictionary? headers = null,
IDictionary? cookies = null) : this(null, urlDetails, method, clientIP, bodyData, headers, cookies)
{
}
internal RequestMessage(
IWireMockMiddlewareOptions? options,
UrlDetails urlDetails,
string method,
string clientIP,
IBodyData? bodyData = null,
IDictionary? headers = null,
IDictionary? cookies = null,
string httpVersion = "1.1"
#if USE_ASPNETCORE
, X509Certificate2? clientCertificate = null
#endif
)
{
Guard.NotNull(urlDetails);
Guard.NotNull(method);
Guard.NotNull(clientIP);
AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
Url = urlDetails.Url.ToString();
Protocol = urlDetails.Url.Scheme;
Host = urlDetails.Url.Host;
Port = urlDetails.Url.Port;
Origin = $"{Protocol}://{Host}:{Port}";
AbsolutePath = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
Path = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
PathSegments = Path.Split('/').Skip(1).ToArray();
AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();
Method = method;
HttpVersion = httpVersion;
ClientIP = clientIP;
BodyData = bodyData;
// Convenience getters for e.g. Handlebars
Body = BodyData?.BodyAsString;
BodyAsJson = BodyData?.BodyAsJson;
BodyAsBytes = BodyData?.BodyAsBytes;
DetectedBodyType = BodyData?.DetectedBodyType.ToString();
DetectedBodyTypeFromContentType = BodyData?.DetectedBodyTypeFromContentType.ToString();
DetectedCompression = BodyData?.DetectedCompression;
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList(header.Value));
Cookies = cookies;
RawQuery = urlDetails.Url.Query;
Query = QueryStringParser.Parse(RawQuery, options?.QueryParameterMultipleValueSupport);
QueryIgnoreCase = new Dictionary>(Query, StringComparer.OrdinalIgnoreCase);
#if USE_ASPNETCORE
ClientCertificate = clientCertificate;
#endif
#if MIMEKIT
try
{
if (MimeKitUtils.TryGetMimeMessage(this, out var mimeMessage))
{
BodyAsMimeMessage = mimeMessage;
}
}
catch
{
// Ignore exception from MimeMessage.Load
}
#endif
}
///
public WireMockList? GetParameter(string key, bool ignoreCase = false)
{
if (Query == null)
{
return null;
}
var query = !ignoreCase ? Query : new Dictionary>(Query, StringComparer.OrdinalIgnoreCase);
return query.ContainsKey(key) ? query[key] : null;
}
}