make additional classes portable

This commit is contained in:
Luke Pulverenti 2016-11-10 20:58:20 -05:00
parent 01fc207b62
commit 836e1fdc30
5 changed files with 48 additions and 112 deletions

View File

@ -79,8 +79,10 @@
<Compile Include="HttpServer\Security\AuthorizationContext.cs" /> <Compile Include="HttpServer\Security\AuthorizationContext.cs" />
<Compile Include="HttpServer\Security\AuthService.cs" /> <Compile Include="HttpServer\Security\AuthService.cs" />
<Compile Include="HttpServer\Security\SessionContext.cs" /> <Compile Include="HttpServer\Security\SessionContext.cs" />
<Compile Include="HttpServer\SocketSharp\RequestMono.cs" />
<Compile Include="HttpServer\SocketSharp\SharpWebSocket.cs" /> <Compile Include="HttpServer\SocketSharp\SharpWebSocket.cs" />
<Compile Include="HttpServer\SocketSharp\WebSocketSharpListener.cs" /> <Compile Include="HttpServer\SocketSharp\WebSocketSharpListener.cs" />
<Compile Include="HttpServer\SocketSharp\WebSocketSharpRequest.cs" />
<Compile Include="HttpServer\SocketSharp\WebSocketSharpResponse.cs" /> <Compile Include="HttpServer\SocketSharp\WebSocketSharpResponse.cs" />
<Compile Include="HttpServer\StreamWriter.cs" /> <Compile Include="HttpServer\StreamWriter.cs" />
<Compile Include="HttpServer\SwaggerService.cs" /> <Compile Include="HttpServer\SwaggerService.cs" />

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@ -7,7 +8,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp namespace Emby.Server.Implementations.HttpServer.SocketSharp
{ {
public partial class WebSocketSharpRequest : IHttpRequest public partial class WebSocketSharpRequest : IHttpRequest
{ {
@ -68,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
input.Position = e.Start; input.Position = e.Start;
input.Read(copy, 0, (int)e.Length); input.Read(copy, 0, (int)e.Length);
form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy)); form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy, 0, copy.Length));
} }
else else
{ {
@ -76,7 +77,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
// We use a substream, as in 2.x we will support large uploads streamed to disk, // We use a substream, as in 2.x we will support large uploads streamed to disk,
// //
HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length); HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
files.AddFile(e.Name, sub); files[e.Name] = sub;
} }
} }
} }
@ -89,7 +90,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (form == null) if (form == null)
{ {
form = new WebROCollection(); form = new WebROCollection();
files = new HttpFileCollection(); files = new Dictionary<string, HttpPostedFile>();
if (IsContentType("multipart/form-data", true)) if (IsContentType("multipart/form-data", true))
{ {
@ -224,7 +225,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (starts_with) if (starts_with)
return StrUtils.StartsWith(ContentType, ct, true); return StrUtils.StartsWith(ContentType, ct, true);
return String.Compare(ContentType, ct, true, Helpers.InvariantCulture) == 0; return string.Equals(ContentType, ct, StringComparison.OrdinalIgnoreCase);
} }
async Task LoadWwwForm() async Task LoadWwwForm()
@ -287,67 +288,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
WebROCollection form; WebROCollection form;
HttpFileCollection files; Dictionary<string, HttpPostedFile> files;
public sealed class HttpFileCollection : NameObjectCollectionBase
{
internal HttpFileCollection()
{
}
internal void AddFile(string name, HttpPostedFile file)
{
BaseAdd(name, file);
}
public void CopyTo(Array dest, int index)
{
/* XXX this is kind of gross and inefficient
* since it makes a copy of the superclass's
* list */
object[] values = BaseGetAllValues();
values.CopyTo(dest, index);
}
public string GetKey(int index)
{
return BaseGetKey(index);
}
public HttpPostedFile Get(int index)
{
return (HttpPostedFile)BaseGet(index);
}
public HttpPostedFile Get(string key)
{
return (HttpPostedFile)BaseGet(key);
}
public HttpPostedFile this[string key]
{
get
{
return Get(key);
}
}
public HttpPostedFile this[int index]
{
get
{
return Get(index);
}
}
public string[] AllKeys
{
get
{
return BaseGetAllKeys();
}
}
}
class WebROCollection : QueryParamCollection class WebROCollection : QueryParamCollection
{ {
bool got_id; bool got_id;
@ -589,29 +531,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
internal sealed class StrUtils internal sealed class StrUtils
{ {
StrUtils() { }
public static bool StartsWith(string str1, string str2)
{
return StartsWith(str1, str2, false);
}
public static bool StartsWith(string str1, string str2, bool ignore_case) public static bool StartsWith(string str1, string str2, bool ignore_case)
{ {
int l2 = str2.Length; if (string.IsNullOrWhiteSpace(str1))
if (l2 == 0) {
return true;
int l1 = str1.Length;
if (l2 > l1)
return false; return false;
}
return 0 == String.Compare(str1, 0, str2, 0, l2, ignore_case, Helpers.InvariantCulture); var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
} return str1.IndexOf(str2, comparison) == 0;
public static bool EndsWith(string str1, string str2)
{
return EndsWith(str1, str2, false);
} }
public static bool EndsWith(string str1, string str2, bool ignore_case) public static bool EndsWith(string str1, string str2, bool ignore_case)
@ -624,7 +552,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (l2 > l1) if (l2 > l1)
return false; return false;
return 0 == String.Compare(str1, l1 - l2, str2, 0, l2, ignore_case, Helpers.InvariantCulture); var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return str1.IndexOf(str2, comparison) == str1.Length - str2.Length - 1;
} }
} }
@ -742,7 +671,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
for (int i = temp.Length - 1; i >= 0; i--) for (int i = temp.Length - 1; i >= 0; i--)
source[i] = (byte)temp[i]; source[i] = (byte)temp[i];
return encoding.GetString(source); return encoding.GetString(source, 0, source.Length);
} }
bool ReadBoundary() bool ReadBoundary()

View File

@ -1,20 +1,20 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net;
using System.Text; using System.Text;
using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer;
using Emby.Server.Implementations.HttpServer.SocketSharp; using Emby.Server.Implementations.HttpServer.SocketSharp;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
using ServiceStack;
using SocketHttpListener.Net; using SocketHttpListener.Net;
using IHttpFile = MediaBrowser.Model.Services.IHttpFile; using IHttpFile = MediaBrowser.Model.Services.IHttpFile;
using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest; using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse; using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
using IResponse = MediaBrowser.Model.Services.IResponse; using IResponse = MediaBrowser.Model.Services.IResponse;
namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp namespace Emby.Server.Implementations.HttpServer.SocketSharp
{ {
public partial class WebSocketSharpRequest : IHttpRequest public partial class WebSocketSharpRequest : IHttpRequest
{ {
@ -327,19 +327,30 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
var pi = httpReq.PathInfo; var pi = httpReq.PathInfo;
if (pi == null || pi.Length <= formatMaxLength) return null; if (pi == null || pi.Length <= formatMaxLength) return null;
if (pi[0] == '/') pi = pi.Substring(1); if (pi[0] == '/') pi = pi.Substring(1);
format = pi.LeftPart('/'); format = LeftPart(pi, '/');
if (format.Length > formatMaxLength) return null; if (format.Length > formatMaxLength) return null;
} }
format = format.LeftPart('.').ToLower(); format = LeftPart(format, '.').ToLower();
if (format.Contains("json")) return "application/json"; if (format.Contains("json")) return "application/json";
if (format.Contains("xml")) return Xml; if (format.Contains("xml")) return Xml;
return null; return null;
} }
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);
}
public bool HasExplicitResponseContentType { get; private set; } public bool HasExplicitResponseContentType { get; private set; }
public static string HandlerFactoryPath;
private string pathInfo; private string pathInfo;
public string PathInfo public string PathInfo
{ {
@ -347,7 +358,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{ {
if (this.pathInfo == null) if (this.pathInfo == null)
{ {
var mode = HttpListenerHost.HandlerFactoryPath; var mode = HandlerFactoryPath;
var pos = request.RawUrl.IndexOf("?"); var pos = request.RawUrl.IndexOf("?");
if (pos != -1) if (pos != -1)
@ -363,7 +374,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
this.pathInfo = request.RawUrl; this.pathInfo = request.RawUrl;
} }
this.pathInfo = this.pathInfo.UrlDecode(); this.pathInfo = WebUtility.UrlDecode(pathInfo);
this.pathInfo = NormalizePathInfo(pathInfo, mode); this.pathInfo = NormalizePathInfo(pathInfo, mode);
} }
return this.pathInfo; return this.pathInfo;
@ -427,9 +438,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (cookies == null) if (cookies == null)
{ {
cookies = new Dictionary<string, System.Net.Cookie>(); cookies = new Dictionary<string, System.Net.Cookie>();
for (var i = 0; i < this.request.Cookies.Count; i++) foreach (var cookie in this.request.Cookies)
{ {
var httpCookie = this.request.Cookies[i]; var httpCookie = (Cookie) cookie;
cookies[httpCookie.Name] = new System.Net.Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, httpCookie.Domain); cookies[httpCookie.Name] = new System.Net.Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, httpCookie.Domain);
} }
} }
@ -539,10 +550,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return httpFiles = new IHttpFile[0]; return httpFiles = new IHttpFile[0];
httpFiles = new IHttpFile[files.Count]; httpFiles = new IHttpFile[files.Count];
for (var i = 0; i < files.Count; i++) var i = 0;
foreach (var pair in files)
{ {
var reqFile = files[i]; var reqFile = pair.Value;
httpFiles[i] = new HttpFile httpFiles[i] = new HttpFile
{ {
ContentType = reqFile.ContentType, ContentType = reqFile.ContentType,
@ -550,6 +561,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
FileName = reqFile.FileName, FileName = reqFile.FileName,
InputStream = reqFile.InputStream, InputStream = reqFile.InputStream,
}; };
i++;
} }
} }
return httpFiles; return httpFiles;
@ -561,14 +573,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
if (stream is MemoryStream) if (stream is MemoryStream)
{ {
var other = (MemoryStream)stream; var other = (MemoryStream)stream;
try
byte[] buffer;
if (streamProvider.TryGetBuffer(other, out buffer))
{ {
return new MemoryStream(other.GetBuffer(), 0, (int)other.Length, false, true); return streamProvider.CreateNew(buffer);
}
catch (UnauthorizedAccessException)
{
return new MemoryStream(other.ToArray(), 0, (int)other.Length, false, true);
} }
return streamProvider.CreateNew(other.ToArray());
} }
return stream; return stream;
@ -577,7 +588,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
public static string GetHandlerPathIfAny(string listenerUrl) public static string GetHandlerPathIfAny(string listenerUrl)
{ {
if (listenerUrl == null) return null; if (listenerUrl == null) return null;
var pos = listenerUrl.IndexOf("://", StringComparison.InvariantCultureIgnoreCase); var pos = listenerUrl.IndexOf("://", StringComparison.OrdinalIgnoreCase);
if (pos == -1) return null; if (pos == -1) return null;
var startHostUrl = listenerUrl.Substring(pos + "://".Length); var startHostUrl = listenerUrl.Substring(pos + "://".Length);
var endPos = startHostUrl.IndexOf('/'); var endPos = startHostUrl.IndexOf('/');
@ -589,7 +600,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
public static string NormalizePathInfo(string pathInfo, string handlerPath) public static string NormalizePathInfo(string pathInfo, string handlerPath)
{ {
if (handlerPath != null && pathInfo.TrimStart('/').StartsWith( if (handlerPath != null && pathInfo.TrimStart('/').StartsWith(
handlerPath, StringComparison.InvariantCultureIgnoreCase)) handlerPath, StringComparison.OrdinalIgnoreCase))
{ {
return pathInfo.TrimStart('/').Substring(handlerPath.Length); return pathInfo.TrimStart('/').Substring(handlerPath.Length);
} }

View File

@ -2,10 +2,8 @@
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Implementations.HttpServer.SocketSharp;
using ServiceStack; using ServiceStack;
using ServiceStack.Host; using ServiceStack.Host;
using ServiceStack.Web;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -158,14 +156,12 @@ namespace MediaBrowser.Server.Implementations.HttpServer
return this; return this;
} }
public static string HandlerFactoryPath;
/// <summary> /// <summary>
/// Starts the Web Service /// Starts the Web Service
/// </summary> /// </summary>
private void StartListener() private void StartListener()
{ {
HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes.First()); WebSocketSharpRequest.HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes.First());
_listener = GetListener(); _listener = GetListener();

View File

@ -111,8 +111,6 @@
<Compile Include="EntryPoints\ExternalPortForwarding.cs" /> <Compile Include="EntryPoints\ExternalPortForwarding.cs" />
<Compile Include="HttpServer\HttpListenerHost.cs" /> <Compile Include="HttpServer\HttpListenerHost.cs" />
<Compile Include="HttpServer\ServerFactory.cs" /> <Compile Include="HttpServer\ServerFactory.cs" />
<Compile Include="HttpServer\SocketSharp\RequestMono.cs" />
<Compile Include="HttpServer\SocketSharp\WebSocketSharpRequest.cs" />
<Compile Include="IO\LibraryMonitor.cs" /> <Compile Include="IO\LibraryMonitor.cs" />
<Compile Include="IO\MemoryStreamProvider.cs" /> <Compile Include="IO\MemoryStreamProvider.cs" />
<Compile Include="LiveTv\TunerHosts\SatIp\ChannelScan.cs" /> <Compile Include="LiveTv\TunerHosts\SatIp\ChannelScan.cs" />