2014-07-18 21:28:40 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2016-11-10 20:37:20 -05:00
|
|
|
|
using Emby.Server.Implementations.HttpServer;
|
2016-11-04 22:17:18 -04:00
|
|
|
|
using Emby.Server.Implementations.HttpServer.SocketSharp;
|
2016-10-23 15:14:57 -04:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2016-10-25 15:02:04 -04:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2015-01-03 14:38:22 -05:00
|
|
|
|
using SocketHttpListener.Net;
|
2016-10-25 15:02:04 -04:00
|
|
|
|
using IHttpFile = MediaBrowser.Model.Services.IHttpFile;
|
|
|
|
|
using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
|
|
|
|
|
using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
|
|
|
|
|
using IResponse = MediaBrowser.Model.Services.IResponse;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
|
2016-11-10 20:58:20 -05:00
|
|
|
|
namespace Emby.Server.Implementations.HttpServer.SocketSharp
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
|
|
|
|
public partial class WebSocketSharpRequest : IHttpRequest
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpListenerRequest request;
|
|
|
|
|
private readonly IHttpResponse response;
|
2016-11-08 13:44:23 -05:00
|
|
|
|
private readonly IMemoryStreamFactory _memoryStreamProvider;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
|
2016-11-08 13:44:23 -05:00
|
|
|
|
public WebSocketSharpRequest(HttpListenerContext httpContext, string operationName, ILogger logger, IMemoryStreamFactory memoryStreamProvider)
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
|
|
|
|
this.OperationName = operationName;
|
2016-10-06 14:55:01 -04:00
|
|
|
|
_memoryStreamProvider = memoryStreamProvider;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
this.request = httpContext.Request;
|
2016-06-19 12:53:43 -04:00
|
|
|
|
this.response = new WebSocketSharpResponse(logger, httpContext.Response, this);
|
2017-09-02 23:17:48 -04:00
|
|
|
|
|
|
|
|
|
//HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetHandlerPathIfAny(string listenerUrl)
|
|
|
|
|
{
|
|
|
|
|
if (listenerUrl == null) return null;
|
|
|
|
|
var pos = listenerUrl.IndexOf("://", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
if (pos == -1) return null;
|
|
|
|
|
var startHostUrl = listenerUrl.Substring(pos + "://".Length);
|
|
|
|
|
var endPos = startHostUrl.IndexOf('/');
|
|
|
|
|
if (endPos == -1) return null;
|
|
|
|
|
var endHostUrl = startHostUrl.Substring(endPos + 1);
|
|
|
|
|
return string.IsNullOrEmpty(endHostUrl) ? null : endHostUrl.TrimEnd('/');
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HttpListenerRequest HttpRequest
|
|
|
|
|
{
|
|
|
|
|
get { return request; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object OriginalRequest
|
|
|
|
|
{
|
|
|
|
|
get { return request; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IResponse Response
|
|
|
|
|
{
|
|
|
|
|
get { return response; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IHttpResponse HttpResponse
|
|
|
|
|
{
|
|
|
|
|
get { return response; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string OperationName { get; set; }
|
|
|
|
|
|
|
|
|
|
public object Dto { get; set; }
|
|
|
|
|
|
|
|
|
|
public string RawUrl
|
|
|
|
|
{
|
|
|
|
|
get { return request.RawUrl; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string AbsoluteUri
|
|
|
|
|
{
|
|
|
|
|
get { return request.Url.AbsoluteUri.TrimEnd('/'); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string UserHostAddress
|
|
|
|
|
{
|
|
|
|
|
get { return request.UserHostAddress; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string XForwardedFor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
return String.IsNullOrEmpty(request.Headers["X-Forwarded-For"]) ? null : request.Headers["X-Forwarded-For"];
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? XForwardedPort
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
return string.IsNullOrEmpty(request.Headers["X-Forwarded-Port"]) ? (int?)null : int.Parse(request.Headers["X-Forwarded-Port"]);
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string XForwardedProtocol
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
return string.IsNullOrEmpty(request.Headers["X-Forwarded-Proto"]) ? null : request.Headers["X-Forwarded-Proto"];
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string XRealIp
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
return String.IsNullOrEmpty(request.Headers["X-Real-IP"]) ? null : request.Headers["X-Real-IP"];
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string remoteIp;
|
|
|
|
|
public string RemoteIp
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return remoteIp ??
|
2016-07-08 14:11:23 -04:00
|
|
|
|
(remoteIp = (CheckBadChars(XForwardedFor)) ??
|
|
|
|
|
(NormalizeIp(CheckBadChars(XRealIp)) ??
|
2017-09-02 22:42:13 -04:00
|
|
|
|
(request.RemoteEndPoint != null ? NormalizeIp(request.RemoteEndPoint.Address.ToString()) : null)));
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 14:11:23 -04:00
|
|
|
|
private static readonly char[] HttpTrimCharacters = new char[] { (char)0x09, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20 };
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// CheckBadChars - throws on invalid chars to be not found in header name/value
|
|
|
|
|
//
|
|
|
|
|
internal static string CheckBadChars(string name)
|
|
|
|
|
{
|
|
|
|
|
if (name == null || name.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VALUE check
|
|
|
|
|
//Trim spaces from both ends
|
|
|
|
|
name = name.Trim(HttpTrimCharacters);
|
|
|
|
|
|
|
|
|
|
//First, check for correctly formed multi-line value
|
|
|
|
|
//Second, check for absenece of CTL characters
|
|
|
|
|
int crlf = 0;
|
|
|
|
|
for (int i = 0; i < name.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
char c = (char)(0x000000ff & (uint)name[i]);
|
|
|
|
|
switch (crlf)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
if (c == '\r')
|
|
|
|
|
{
|
|
|
|
|
crlf = 1;
|
|
|
|
|
}
|
|
|
|
|
else if (c == '\n')
|
|
|
|
|
{
|
|
|
|
|
// Technically this is bad HTTP. But it would be a breaking change to throw here.
|
|
|
|
|
// Is there an exploit?
|
|
|
|
|
crlf = 2;
|
|
|
|
|
}
|
|
|
|
|
else if (c == 127 || (c < ' ' && c != '\t'))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("net_WebHeaderInvalidControlChars");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
{
|
|
|
|
|
crlf = 2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
if (c == ' ' || c == '\t')
|
|
|
|
|
{
|
|
|
|
|
crlf = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (crlf != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool ContainsNonAsciiChars(string token)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < token.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if ((token[i] < 0x20) || (token[i] > 0x7e))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 22:02:26 -05:00
|
|
|
|
private string NormalizeIp(string ip)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(ip))
|
|
|
|
|
{
|
|
|
|
|
// Handle ipv4 mapped to ipv6
|
|
|
|
|
const string srch = "::ffff:";
|
|
|
|
|
var index = ip.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
ip = ip.Substring(srch.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ip;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:28:40 -04:00
|
|
|
|
public bool IsSecureConnection
|
|
|
|
|
{
|
|
|
|
|
get { return request.IsSecureConnection || XForwardedProtocol == "https"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] AcceptTypes
|
|
|
|
|
{
|
|
|
|
|
get { return request.AcceptTypes; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, object> items;
|
|
|
|
|
public Dictionary<string, object> Items
|
|
|
|
|
{
|
|
|
|
|
get { return items ?? (items = new Dictionary<string, object>()); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string responseContentType;
|
|
|
|
|
public string ResponseContentType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return responseContentType
|
2016-11-06 15:41:55 -05:00
|
|
|
|
?? (responseContentType = GetResponseContentType(this));
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
this.responseContentType = value;
|
|
|
|
|
HasExplicitResponseContentType = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 09:41:24 -05:00
|
|
|
|
public const string FormUrlEncoded = "application/x-www-form-urlencoded";
|
|
|
|
|
public const string MultiPartFormData = "multipart/form-data";
|
2016-11-06 15:41:55 -05:00
|
|
|
|
private static string GetResponseContentType(IRequest httpReq)
|
|
|
|
|
{
|
|
|
|
|
var specifiedContentType = GetQueryStringContentType(httpReq);
|
|
|
|
|
if (!string.IsNullOrEmpty(specifiedContentType)) return specifiedContentType;
|
|
|
|
|
|
2016-11-10 20:37:20 -05:00
|
|
|
|
var serverDefaultContentType = "application/json";
|
|
|
|
|
|
2016-11-06 15:41:55 -05:00
|
|
|
|
var acceptContentTypes = httpReq.AcceptTypes;
|
|
|
|
|
var defaultContentType = httpReq.ContentType;
|
2016-11-10 09:41:24 -05:00
|
|
|
|
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
|
2016-11-06 15:41:55 -05:00
|
|
|
|
{
|
2016-11-10 20:37:20 -05:00
|
|
|
|
defaultContentType = serverDefaultContentType;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var preferredContentTypes = new string[] {};
|
|
|
|
|
|
|
|
|
|
var acceptsAnything = false;
|
|
|
|
|
var hasDefaultContentType = !string.IsNullOrEmpty(defaultContentType);
|
|
|
|
|
if (acceptContentTypes != null)
|
|
|
|
|
{
|
|
|
|
|
var hasPreferredContentTypes = new bool[preferredContentTypes.Length];
|
|
|
|
|
foreach (var acceptsType in acceptContentTypes)
|
|
|
|
|
{
|
2016-11-10 20:37:20 -05:00
|
|
|
|
var contentType = HttpResultFactory.GetRealContentType(acceptsType);
|
2016-11-06 15:41:55 -05:00
|
|
|
|
acceptsAnything = acceptsAnything || contentType == "*/*";
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < preferredContentTypes.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (hasPreferredContentTypes[i]) continue;
|
|
|
|
|
var preferredContentType = preferredContentTypes[i];
|
|
|
|
|
hasPreferredContentTypes[i] = contentType.StartsWith(preferredContentType);
|
|
|
|
|
|
|
|
|
|
//Prefer Request.ContentType if it is also a preferredContentType
|
|
|
|
|
if (hasPreferredContentTypes[i] && preferredContentType == defaultContentType)
|
|
|
|
|
return preferredContentType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < preferredContentTypes.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (hasPreferredContentTypes[i]) return preferredContentTypes[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (acceptsAnything)
|
|
|
|
|
{
|
|
|
|
|
if (hasDefaultContentType)
|
|
|
|
|
return defaultContentType;
|
2016-11-10 20:37:20 -05:00
|
|
|
|
if (serverDefaultContentType != null)
|
|
|
|
|
return serverDefaultContentType;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 09:41:24 -05:00
|
|
|
|
if (acceptContentTypes == null && httpReq.ContentType == Soap11)
|
2016-11-06 15:41:55 -05:00
|
|
|
|
{
|
2016-11-10 09:41:24 -05:00
|
|
|
|
return Soap11;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//We could also send a '406 Not Acceptable', but this is allowed also
|
2016-11-10 20:37:20 -05:00
|
|
|
|
return serverDefaultContentType;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
}
|
2016-11-10 20:37:20 -05:00
|
|
|
|
|
2016-11-10 09:41:24 -05:00
|
|
|
|
public const string Soap11 = "text/xml; charset=utf-8";
|
2016-11-06 15:41:55 -05:00
|
|
|
|
|
2016-11-10 09:41:24 -05:00
|
|
|
|
public static bool HasAnyOfContentTypes(IRequest request, params string[] contentTypes)
|
|
|
|
|
{
|
|
|
|
|
if (contentTypes == null || request.ContentType == null) return false;
|
|
|
|
|
foreach (var contentType in contentTypes)
|
|
|
|
|
{
|
|
|
|
|
if (IsContentType(request, contentType)) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsContentType(IRequest request, string contentType)
|
|
|
|
|
{
|
|
|
|
|
return request.ContentType.StartsWith(contentType, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public const string Xml = "application/xml";
|
2016-11-06 15:41:55 -05:00
|
|
|
|
private static string GetQueryStringContentType(IRequest httpReq)
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
var format = httpReq.QueryString["format"];
|
2016-11-06 15:41:55 -05:00
|
|
|
|
if (format == null)
|
|
|
|
|
{
|
|
|
|
|
const int formatMaxLength = 4;
|
|
|
|
|
var pi = httpReq.PathInfo;
|
|
|
|
|
if (pi == null || pi.Length <= formatMaxLength) return null;
|
|
|
|
|
if (pi[0] == '/') pi = pi.Substring(1);
|
2016-11-10 20:58:20 -05:00
|
|
|
|
format = LeftPart(pi, '/');
|
2016-11-06 15:41:55 -05:00
|
|
|
|
if (format.Length > formatMaxLength) return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 20:58:20 -05:00
|
|
|
|
format = LeftPart(format, '.').ToLower();
|
2016-11-08 13:44:23 -05:00
|
|
|
|
if (format.Contains("json")) return "application/json";
|
2016-11-10 09:41:24 -05:00
|
|
|
|
if (format.Contains("xml")) return Xml;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
|
2016-11-10 20:37:20 -05:00
|
|
|
|
return null;
|
2016-11-06 15:41:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 20:58:20 -05:00
|
|
|
|
public static string LeftPart(string strVal, char needle)
|
|
|
|
|
{
|
|
|
|
|
if (strVal == null) return null;
|
|
|
|
|
var pos = strVal.IndexOf(needle);
|
|
|
|
|
return pos == -1
|
|
|
|
|
? strVal
|
|
|
|
|
: strVal.Substring(0, pos);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:28:40 -04:00
|
|
|
|
public bool HasExplicitResponseContentType { get; private set; }
|
|
|
|
|
|
2016-11-10 20:58:20 -05:00
|
|
|
|
public static string HandlerFactoryPath;
|
|
|
|
|
|
2014-07-18 21:28:40 -04:00
|
|
|
|
private string pathInfo;
|
|
|
|
|
public string PathInfo
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.pathInfo == null)
|
|
|
|
|
{
|
2016-11-10 20:58:20 -05:00
|
|
|
|
var mode = HandlerFactoryPath;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
|
|
|
|
|
var pos = request.RawUrl.IndexOf("?");
|
|
|
|
|
if (pos != -1)
|
|
|
|
|
{
|
|
|
|
|
var path = request.RawUrl.Substring(0, pos);
|
2016-11-06 15:41:55 -05:00
|
|
|
|
this.pathInfo = GetPathInfo(
|
2014-07-18 21:28:40 -04:00
|
|
|
|
path,
|
|
|
|
|
mode,
|
|
|
|
|
mode ?? "");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.pathInfo = request.RawUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 15:12:55 -04:00
|
|
|
|
this.pathInfo = System.Net.WebUtility.UrlDecode(pathInfo);
|
2014-07-18 21:28:40 -04:00
|
|
|
|
this.pathInfo = NormalizePathInfo(pathInfo, mode);
|
|
|
|
|
}
|
|
|
|
|
return this.pathInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-06 15:41:55 -05:00
|
|
|
|
private static string GetPathInfo(string fullPath, string mode, string appPath)
|
|
|
|
|
{
|
|
|
|
|
var pathInfo = ResolvePathInfoFromMappedPath(fullPath, mode);
|
|
|
|
|
if (!string.IsNullOrEmpty(pathInfo)) return pathInfo;
|
|
|
|
|
|
|
|
|
|
//Wildcard mode relies on this to work out the handlerPath
|
|
|
|
|
pathInfo = ResolvePathInfoFromMappedPath(fullPath, appPath);
|
|
|
|
|
if (!string.IsNullOrEmpty(pathInfo)) return pathInfo;
|
|
|
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string ResolvePathInfoFromMappedPath(string fullPath, string mappedPathRoot)
|
|
|
|
|
{
|
|
|
|
|
if (mappedPathRoot == null) return null;
|
|
|
|
|
|
|
|
|
|
var sbPathInfo = new StringBuilder();
|
|
|
|
|
var fullPathParts = fullPath.Split('/');
|
|
|
|
|
var mappedPathRootParts = mappedPathRoot.Split('/');
|
|
|
|
|
var fullPathIndexOffset = mappedPathRootParts.Length - 1;
|
|
|
|
|
var pathRootFound = false;
|
|
|
|
|
|
|
|
|
|
for (var fullPathIndex = 0; fullPathIndex < fullPathParts.Length; fullPathIndex++)
|
|
|
|
|
{
|
|
|
|
|
if (pathRootFound)
|
|
|
|
|
{
|
|
|
|
|
sbPathInfo.Append("/" + fullPathParts[fullPathIndex]);
|
|
|
|
|
}
|
|
|
|
|
else if (fullPathIndex - fullPathIndexOffset >= 0)
|
|
|
|
|
{
|
|
|
|
|
pathRootFound = true;
|
|
|
|
|
for (var mappedPathRootIndex = 0; mappedPathRootIndex < mappedPathRootParts.Length; mappedPathRootIndex++)
|
|
|
|
|
{
|
|
|
|
|
if (!string.Equals(fullPathParts[fullPathIndex - fullPathIndexOffset + mappedPathRootIndex], mappedPathRootParts[mappedPathRootIndex], StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
pathRootFound = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!pathRootFound) return null;
|
|
|
|
|
|
|
|
|
|
var path = sbPathInfo.ToString();
|
|
|
|
|
return path.Length > 1 ? path.TrimEnd('/') : "/";
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:28:40 -04:00
|
|
|
|
private Dictionary<string, System.Net.Cookie> cookies;
|
|
|
|
|
public IDictionary<string, System.Net.Cookie> Cookies
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (cookies == null)
|
|
|
|
|
{
|
|
|
|
|
cookies = new Dictionary<string, System.Net.Cookie>();
|
2016-11-10 20:58:20 -05:00
|
|
|
|
foreach (var cookie in this.request.Cookies)
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
var httpCookie = (System.Net.Cookie) cookie;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
cookies[httpCookie.Name] = new System.Net.Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, httpCookie.Domain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cookies;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string UserAgent
|
|
|
|
|
{
|
|
|
|
|
get { return request.UserAgent; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 15:02:04 -04:00
|
|
|
|
public QueryParamCollection Headers
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
get { return request.Headers; }
|
2016-10-25 15:02:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QueryParamCollection queryString;
|
|
|
|
|
public QueryParamCollection QueryString
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
2016-10-25 15:02:04 -04:00
|
|
|
|
get { return queryString ?? (queryString = MyHttpUtility.ParseQueryString(request.Url.Query)); }
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 15:02:04 -04:00
|
|
|
|
private QueryParamCollection formData;
|
|
|
|
|
public QueryParamCollection FormData
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
2016-10-25 15:02:04 -04:00
|
|
|
|
get { return formData ?? (formData = this.Form); }
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsLocal
|
|
|
|
|
{
|
|
|
|
|
get { return request.IsLocal; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string httpMethod;
|
|
|
|
|
public string HttpMethod
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return httpMethod
|
2016-11-10 09:41:24 -05:00
|
|
|
|
?? (httpMethod = request.HttpMethod);
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Verb
|
|
|
|
|
{
|
|
|
|
|
get { return HttpMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Param(string name)
|
|
|
|
|
{
|
|
|
|
|
return Headers[name]
|
|
|
|
|
?? QueryString[name]
|
|
|
|
|
?? FormData[name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ContentType
|
|
|
|
|
{
|
|
|
|
|
get { return request.ContentType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Encoding contentEncoding;
|
|
|
|
|
public Encoding ContentEncoding
|
|
|
|
|
{
|
|
|
|
|
get { return contentEncoding ?? request.ContentEncoding; }
|
|
|
|
|
set { contentEncoding = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Uri UrlReferrer
|
|
|
|
|
{
|
|
|
|
|
get { return request.UrlReferrer; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Encoding GetEncoding(string contentTypeHeader)
|
|
|
|
|
{
|
|
|
|
|
var param = GetParameter(contentTypeHeader, "charset=");
|
|
|
|
|
if (param == null) return null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Encoding.GetEncoding(param);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream InputStream
|
|
|
|
|
{
|
2016-11-08 13:44:23 -05:00
|
|
|
|
get { return request.InputStream; }
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long ContentLength
|
|
|
|
|
{
|
|
|
|
|
get { return request.ContentLength64; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IHttpFile[] httpFiles;
|
|
|
|
|
public IHttpFile[] Files
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (httpFiles == null)
|
|
|
|
|
{
|
|
|
|
|
if (files == null)
|
|
|
|
|
return httpFiles = new IHttpFile[0];
|
|
|
|
|
|
|
|
|
|
httpFiles = new IHttpFile[files.Count];
|
2016-11-10 20:58:20 -05:00
|
|
|
|
var i = 0;
|
|
|
|
|
foreach (var pair in files)
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
2016-11-10 20:58:20 -05:00
|
|
|
|
var reqFile = pair.Value;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
httpFiles[i] = new HttpFile
|
|
|
|
|
{
|
|
|
|
|
ContentType = reqFile.ContentType,
|
|
|
|
|
ContentLength = reqFile.ContentLength,
|
|
|
|
|
FileName = reqFile.FileName,
|
|
|
|
|
InputStream = reqFile.InputStream,
|
|
|
|
|
};
|
2016-11-10 20:58:20 -05:00
|
|
|
|
i++;
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return httpFiles;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:44:23 -05:00
|
|
|
|
static Stream GetSubStream(Stream stream, IMemoryStreamFactory streamProvider)
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
|
|
|
|
if (stream is MemoryStream)
|
|
|
|
|
{
|
|
|
|
|
var other = (MemoryStream)stream;
|
2016-11-10 20:58:20 -05:00
|
|
|
|
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
if (streamProvider.TryGetBuffer(other, out buffer))
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
2016-11-10 20:58:20 -05:00
|
|
|
|
return streamProvider.CreateNew(buffer);
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
2016-11-10 20:58:20 -05:00
|
|
|
|
return streamProvider.CreateNew(other.ToArray());
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string NormalizePathInfo(string pathInfo, string handlerPath)
|
|
|
|
|
{
|
|
|
|
|
if (handlerPath != null && pathInfo.TrimStart('/').StartsWith(
|
2016-11-10 20:58:20 -05:00
|
|
|
|
handlerPath, StringComparison.OrdinalIgnoreCase))
|
2014-07-18 21:28:40 -04:00
|
|
|
|
{
|
|
|
|
|
return pathInfo.TrimStart('/').Substring(handlerPath.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pathInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-08 13:44:23 -05:00
|
|
|
|
|
|
|
|
|
public class HttpFile : IHttpFile
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public long ContentLength { get; set; }
|
|
|
|
|
public string ContentType { get; set; }
|
|
|
|
|
public Stream InputStream { get; set; }
|
|
|
|
|
}
|
2014-07-18 21:28:40 -04:00
|
|
|
|
}
|