Minor improvements to network code

This commit is contained in:
Bond-009
2019-11-28 17:46:06 +01:00
parent f3ca4631c3
commit 6f45d95951
4 changed files with 39 additions and 96 deletions

View File

@@ -14,9 +14,9 @@ namespace Emby.Server.Implementations.SocketSharp
{
public class WebSocketSharpRequest : IHttpRequest
{
public const string FormUrlEncoded = "application/x-www-form-urlencoded";
public const string MultiPartFormData = "multipart/form-data";
public const string Soap11 = "text/xml; charset=utf-8";
private const string FormUrlEncoded = "application/x-www-form-urlencoded";
private const string MultiPartFormData = "multipart/form-data";
private const string Soap11 = "text/xml; charset=utf-8";
private string _remoteIp;
private Dictionary<string, object> _items;
@@ -77,7 +77,7 @@ namespace Emby.Server.Implementations.SocketSharp
get =>
_responseContentType
?? (_responseContentType = GetResponseContentType(Request));
set => this._responseContentType = value;
set => _responseContentType = value;
}
public string PathInfo => Request.Path.Value;
@@ -90,7 +90,6 @@ namespace Emby.Server.Implementations.SocketSharp
public bool IsLocal => Request.HttpContext.Connection.LocalIpAddress.Equals(Request.HttpContext.Connection.RemoteIpAddress);
public string HttpMethod => Request.Method;
public string Verb => HttpMethod;
@@ -123,24 +122,29 @@ namespace Emby.Server.Implementations.SocketSharp
return specifiedContentType;
}
const string serverDefaultContentType = "application/json";
const string ServerDefaultContentType = "application/json";
var acceptContentTypes = httpReq.Headers.GetCommaSeparatedValues(HeaderNames.Accept);
string defaultContentType = null;
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
{
defaultContentType = serverDefaultContentType;
defaultContentType = ServerDefaultContentType;
}
var acceptsAnything = false;
var hasDefaultContentType = defaultContentType != null;
if (acceptContentTypes != null)
{
foreach (var acceptsType in acceptContentTypes)
foreach (ReadOnlySpan<char> acceptsType in acceptContentTypes)
{
// TODO: @bond move to Span when Span.Split lands
// https://github.com/dotnet/corefx/issues/26528
var contentType = acceptsType?.Split(';')[0].Trim();
ReadOnlySpan<char> contentType = acceptsType;
var index = contentType.IndexOf(';');
if (index != -1)
{
contentType = contentType.Slice(0, index);
}
contentType = contentType.Trim();
acceptsAnything = contentType.Equals("*/*", StringComparison.OrdinalIgnoreCase);
if (acceptsAnything)
@@ -157,7 +161,7 @@ namespace Emby.Server.Implementations.SocketSharp
}
else
{
return serverDefaultContentType;
return ServerDefaultContentType;
}
}
}
@@ -168,7 +172,7 @@ namespace Emby.Server.Implementations.SocketSharp
}
// We could also send a '406 Not Acceptable', but this is allowed also
return serverDefaultContentType;
return ServerDefaultContentType;
}
public static bool HasAnyOfContentTypes(HttpRequest request, params string[] contentTypes)
@@ -196,12 +200,12 @@ namespace Emby.Server.Implementations.SocketSharp
private static string GetQueryStringContentType(HttpRequest httpReq)
{
ReadOnlySpan<char> format = httpReq.Query["format"].ToString().AsSpan();
ReadOnlySpan<char> format = httpReq.Query["format"].ToString();
if (format == null)
{
const int formatMaxLength = 4;
ReadOnlySpan<char> pi = httpReq.Path.ToString().AsSpan();
if (pi == null || pi.Length <= formatMaxLength)
const int FormatMaxLength = 4;
ReadOnlySpan<char> pi = httpReq.Path.ToString();
if (pi == null || pi.Length <= FormatMaxLength)
{
return null;
}
@@ -212,18 +216,18 @@ namespace Emby.Server.Implementations.SocketSharp
}
format = LeftPart(pi, '/');
if (format.Length > formatMaxLength)
if (format.Length > FormatMaxLength)
{
return null;
}
}
format = LeftPart(format, '.');
if (format.Contains("json".AsSpan(), StringComparison.OrdinalIgnoreCase))
if (format.Contains("json", StringComparison.OrdinalIgnoreCase))
{
return "application/json";
}
else if (format.Contains("xml".AsSpan(), StringComparison.OrdinalIgnoreCase))
else if (format.Contains("xml", StringComparison.OrdinalIgnoreCase))
{
return "application/xml";
}