aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-11-08 14:55:32 -0500
committerGitHub <noreply@github.com>2016-11-08 14:55:32 -0500
commit82c46a84e4ce7419a92e6c5674de92d69b6ae11a (patch)
treec7263a8e6aaf43680a2de2d3ae00be15be6cd1ad /MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs
parenta86e9fa73dd159db89efd2ae3e206f45a53c784c (diff)
parente8c70da2b6044243d8352af8358dd701afe570e5 (diff)
Merge pull request #2277 from MediaBrowser/dev
Dev
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs')
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs231
1 files changed, 162 insertions, 69 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs
index c56f62bec..6f44fcce7 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
-using System.Collections.Specialized;
using System.IO;
using System.Text;
using Emby.Server.Implementations.HttpServer.SocketSharp;
using Funq;
-using MediaBrowser.Common.IO;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Services;
using ServiceStack;
using ServiceStack.Host;
-using ServiceStack.Web;
using SocketHttpListener.Net;
using IHttpFile = MediaBrowser.Model.Services.IHttpFile;
using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
@@ -25,12 +22,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
public Container Container { get; set; }
private readonly HttpListenerRequest request;
private readonly IHttpResponse response;
- private readonly IMemoryStreamProvider _memoryStreamProvider;
+ private readonly IMemoryStreamFactory _memoryStreamProvider;
- public WebSocketSharpRequest(HttpListenerContext httpContext, string operationName, RequestAttributes requestAttributes, ILogger logger, IMemoryStreamProvider memoryStreamProvider)
+ public WebSocketSharpRequest(HttpListenerContext httpContext, string operationName, ILogger logger, IMemoryStreamFactory memoryStreamProvider)
{
this.OperationName = operationName;
- this.RequestAttributes = requestAttributes;
_memoryStreamProvider = memoryStreamProvider;
this.request = httpContext.Request;
this.response = new WebSocketSharpResponse(logger, httpContext.Response, this);
@@ -56,38 +52,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
get { return response; }
}
- public RequestAttributes RequestAttributes { get; set; }
-
- public T TryResolve<T>()
- {
- if (typeof(T) == typeof(IHttpRequest))
- throw new Exception("You don't need to use IHttpRequest.TryResolve<IHttpRequest> to resolve itself");
-
- if (typeof(T) == typeof(IHttpResponse))
- throw new Exception("Resolve IHttpResponse with 'Response' property instead of IHttpRequest.TryResolve<IHttpResponse>");
-
- return Container == null
- ? HostContext.TryResolve<T>()
- : Container.TryResolve<T>();
- }
-
public string OperationName { get; set; }
public object Dto { get; set; }
- public string GetRawBody()
- {
- if (bufferedStream != null)
- {
- return bufferedStream.ToArray().FromUtf8Bytes();
- }
-
- using (var reader = new StreamReader(InputStream))
- {
- return reader.ReadToEnd();
- }
- }
-
public string RawUrl
{
get { return request.RawUrl; }
@@ -107,7 +75,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
get
{
- return String.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedFor]) ? null : request.Headers[HttpHeaders.XForwardedFor];
+ return String.IsNullOrEmpty(request.Headers["X-Forwarded-For"]) ? null : request.Headers["X-Forwarded-For"];
}
}
@@ -115,7 +83,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
get
{
- return string.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedPort]) ? (int?)null : int.Parse(request.Headers[HttpHeaders.XForwardedPort]);
+ return string.IsNullOrEmpty(request.Headers["X-Forwarded-Port"]) ? (int?)null : int.Parse(request.Headers["X-Forwarded-Port"]);
}
}
@@ -123,7 +91,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
get
{
- return string.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedProtocol]) ? null : request.Headers[HttpHeaders.XForwardedProtocol];
+ return string.IsNullOrEmpty(request.Headers["X-Forwarded-Proto"]) ? null : request.Headers["X-Forwarded-Proto"];
}
}
@@ -131,7 +99,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
get
{
- return String.IsNullOrEmpty(request.Headers[HttpHeaders.XRealIp]) ? null : request.Headers[HttpHeaders.XRealIp];
+ return String.IsNullOrEmpty(request.Headers["X-Real-IP"]) ? null : request.Headers["X-Real-IP"];
}
}
@@ -143,7 +111,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return remoteIp ??
(remoteIp = (CheckBadChars(XForwardedFor)) ??
(NormalizeIp(CheckBadChars(XRealIp)) ??
- (request.RemoteEndPoint != null ? NormalizeIp(request.RemoteEndPoint.Address.ToString()) : null)));
+ (request.RemoteEndPoint != null ? NormalizeIp(request.RemoteEndPoint.IpAddress.ToString()) : null)));
}
}
@@ -262,7 +230,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
get
{
return responseContentType
- ?? (responseContentType = this.GetResponseContentType());
+ ?? (responseContentType = GetResponseContentType(this));
}
set
{
@@ -271,6 +239,98 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
}
}
+ private static string GetResponseContentType(IRequest httpReq)
+ {
+ var specifiedContentType = GetQueryStringContentType(httpReq);
+ if (!string.IsNullOrEmpty(specifiedContentType)) return specifiedContentType;
+
+ var acceptContentTypes = httpReq.AcceptTypes;
+ var defaultContentType = httpReq.ContentType;
+ if (httpReq.HasAnyOfContentTypes(MimeTypes.FormUrlEncoded, MimeTypes.MultiPartFormData))
+ {
+ defaultContentType = HostContext.Config.DefaultContentType;
+ }
+
+ var customContentTypes = ContentTypes.Instance.ContentTypeFormats.Values;
+ 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)
+ {
+ var contentType = ContentFormat.GetRealContentType(acceptsType);
+ 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;
+ if (HostContext.Config.DefaultContentType != null)
+ return HostContext.Config.DefaultContentType;
+ }
+
+ foreach (var contentType in acceptContentTypes)
+ {
+ foreach (var customContentType in customContentTypes)
+ {
+ if (contentType.StartsWith(customContentType, StringComparison.OrdinalIgnoreCase))
+ return customContentType;
+ }
+ }
+ }
+
+ if (acceptContentTypes == null && httpReq.ContentType == MimeTypes.Soap11)
+ {
+ return MimeTypes.Soap11;
+ }
+
+ //We could also send a '406 Not Acceptable', but this is allowed also
+ return HostContext.Config.DefaultContentType;
+ }
+
+ private static string GetQueryStringContentType(IRequest httpReq)
+ {
+ var format = httpReq.QueryString["format"];
+ 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);
+ format = pi.LeftPart('/');
+ if (format.Length > formatMaxLength) return null;
+ }
+
+ format = format.LeftPart('.').ToLower();
+ if (format.Contains("json")) return "application/json";
+ if (format.Contains("xml")) return MimeTypes.Xml;
+
+ string contentType;
+ ContentTypes.Instance.ContentTypeFormats.TryGetValue(format, out contentType);
+
+ return contentType;
+ }
+
public bool HasExplicitResponseContentType { get; private set; }
private string pathInfo;
@@ -286,7 +346,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (pos != -1)
{
var path = request.RawUrl.Substring(0, pos);
- this.pathInfo = HttpRequestExtensions.GetPathInfo(
+ this.pathInfo = GetPathInfo(
path,
mode,
mode ?? "");
@@ -303,6 +363,55 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
}
}
+ 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('/') : "/";
+ }
+
private Dictionary<string, System.Net.Cookie> cookies;
public IDictionary<string, System.Net.Cookie> Cookies
{
@@ -327,10 +436,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
get { return request.UserAgent; }
}
- private QueryParamCollection headers;
public QueryParamCollection Headers
{
- get { return headers ?? (headers = ToQueryParams(request.Headers)); }
+ get { return request.Headers; }
}
private QueryParamCollection queryString;
@@ -345,18 +453,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
get { return formData ?? (formData = this.Form); }
}
- private QueryParamCollection ToQueryParams(NameValueCollection collection)
- {
- var result = new QueryParamCollection();
-
- foreach (var key in collection.AllKeys)
- {
- result[key] = collection[key];
- }
-
- return result;
- }
-
public bool IsLocal
{
get { return request.IsLocal; }
@@ -416,21 +512,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
}
}
- public bool UseBufferedStream
- {
- get { return bufferedStream != null; }
- set
- {
- bufferedStream = value
- ? bufferedStream ?? _memoryStreamProvider.CreateNew(request.InputStream.ReadFully())
- : null;
- }
- }
-
- private MemoryStream bufferedStream;
public Stream InputStream
{
- get { return bufferedStream ?? request.InputStream; }
+ get { return request.InputStream; }
}
public long ContentLength
@@ -466,7 +550,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
}
}
- static Stream GetSubStream(Stream stream, IMemoryStreamProvider streamProvider)
+ static Stream GetSubStream(Stream stream, IMemoryStreamFactory streamProvider)
{
if (stream is MemoryStream)
{
@@ -507,4 +591,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return pathInfo;
}
}
+
+ 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; }
+ }
}