diff options
Diffstat (limited to 'Emby.Server.Implementations/SocketSharp')
| -rw-r--r-- | Emby.Server.Implementations/SocketSharp/RequestMono.cs | 39 | ||||
| -rw-r--r-- | Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs | 62 |
2 files changed, 60 insertions, 41 deletions
diff --git a/Emby.Server.Implementations/SocketSharp/RequestMono.cs b/Emby.Server.Implementations/SocketSharp/RequestMono.cs index 82bcf2f04..5e29e4058 100644 --- a/Emby.Server.Implementations/SocketSharp/RequestMono.cs +++ b/Emby.Server.Implementations/SocketSharp/RequestMono.cs @@ -13,9 +13,9 @@ namespace Emby.Server.Implementations.SocketSharp { public partial class WebSocketSharpRequest : IHttpRequest { - internal static string GetParameter(string header, string attr) + internal static string GetParameter(ReadOnlySpan<char> header, string attr) { - int ap = header.IndexOf(attr, StringComparison.Ordinal); + int ap = header.IndexOf(attr.AsSpan(), StringComparison.Ordinal); if (ap == -1) { return null; @@ -33,18 +33,19 @@ namespace Emby.Server.Implementations.SocketSharp ending = ' '; } - int end = header.IndexOf(ending, ap + 1); + var slice = header.Slice(ap + 1); + int end = slice.IndexOf(ending); if (end == -1) { - return ending == '"' ? null : header.Substring(ap); + return ending == '"' ? null : header.Slice(ap).ToString(); } - return header.Substring(ap + 1, end - ap - 1); + return slice.Slice(0, end - ap - 1).ToString(); } private async Task LoadMultiPart(WebROCollection form) { - string boundary = GetParameter(ContentType, "; boundary="); + string boundary = GetParameter(ContentType.AsSpan(), "; boundary="); if (boundary == null) { return; @@ -377,17 +378,17 @@ namespace Emby.Server.Implementations.SocketSharp } var elem = new Element(); - string header; - while ((header = ReadHeaders()) != null) + ReadOnlySpan<char> header; + while ((header = ReadHeaders().AsSpan()) != null) { - if (header.StartsWith("Content-Disposition:", StringComparison.OrdinalIgnoreCase)) + if (header.StartsWith("Content-Disposition:".AsSpan(), StringComparison.OrdinalIgnoreCase)) { elem.Name = GetContentDispositionAttribute(header, "name"); elem.Filename = StripPath(GetContentDispositionAttributeWithEncoding(header, "filename")); } - else if (header.StartsWith("Content-Type:", StringComparison.OrdinalIgnoreCase)) + else if (header.StartsWith("Content-Type:".AsSpan(), StringComparison.OrdinalIgnoreCase)) { - elem.ContentType = header.Substring("Content-Type:".Length).Trim(); + elem.ContentType = header.Slice("Content-Type:".Length).Trim().ToString(); elem.Encoding = GetEncoding(elem.ContentType); } } @@ -435,16 +436,16 @@ namespace Emby.Server.Implementations.SocketSharp return sb.ToString(); } - private static string GetContentDispositionAttribute(string l, string name) + private static string GetContentDispositionAttribute(ReadOnlySpan<char> l, string name) { - int idx = l.IndexOf(name + "=\"", StringComparison.Ordinal); + int idx = l.IndexOf((name + "=\"").AsSpan(), StringComparison.Ordinal); if (idx < 0) { return null; } int begin = idx + name.Length + "=\"".Length; - int end = l.IndexOf('"', begin); + int end = l.Slice(begin).IndexOf('"'); if (end < 0) { return null; @@ -455,19 +456,19 @@ namespace Emby.Server.Implementations.SocketSharp return string.Empty; } - return l.Substring(begin, end - begin); + return l.Slice(begin, end - begin).ToString(); } - private string GetContentDispositionAttributeWithEncoding(string l, string name) + private string GetContentDispositionAttributeWithEncoding(ReadOnlySpan<char> l, string name) { - int idx = l.IndexOf(name + "=\"", StringComparison.Ordinal); + int idx = l.IndexOf((name + "=\"").AsSpan(), StringComparison.Ordinal); if (idx < 0) { return null; } int begin = idx + name.Length + "=\"".Length; - int end = l.IndexOf('"', begin); + int end = l.Slice(begin).IndexOf('"'); if (end < 0) { return null; @@ -478,7 +479,7 @@ namespace Emby.Server.Implementations.SocketSharp return string.Empty; } - string temp = l.Substring(begin, end - begin); + ReadOnlySpan<char> temp = l.Slice(begin, end - begin); byte[] source = new byte[temp.Length]; for (int i = temp.Length - 1; i >= 0; i--) { diff --git a/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs b/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs index 976f2ec06..bc002dc4c 100644 --- a/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs +++ b/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs @@ -56,19 +56,37 @@ namespace Emby.Server.Implementations.SocketSharp public string XRealIp => StringValues.IsNullOrEmpty(request.Headers["X-Real-IP"]) ? null : request.Headers["X-Real-IP"].ToString(); private string remoteIp; + public string RemoteIp + { + get + { + if (remoteIp != null) + { + return remoteIp; + } - public string RemoteIp => - remoteIp ?? - (remoteIp = CheckBadChars(XForwardedFor) ?? - NormalizeIp(CheckBadChars(XRealIp) ?? - (string.IsNullOrEmpty(request.HttpContext.Connection.RemoteIpAddress.ToString()) ? null : NormalizeIp(request.HttpContext.Connection.RemoteIpAddress.ToString())))); + var temp = CheckBadChars(XForwardedFor.AsSpan()); + if (temp.Length != 0) + { + return remoteIp = temp.ToString(); + } + + temp = CheckBadChars(XRealIp.AsSpan()); + if (temp.Length != 0) + { + return remoteIp = NormalizeIp(temp).ToString(); + } + + return remoteIp = NormalizeIp(request.HttpContext.Connection.RemoteIpAddress.ToString().AsSpan()).ToString(); + } + } 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) + internal static ReadOnlySpan<char> CheckBadChars(ReadOnlySpan<char> name) { - if (name == null || name.Length == 0) + if (name.Length == 0) { return name; } @@ -99,7 +117,7 @@ namespace Emby.Server.Implementations.SocketSharp } else if (c == 127 || (c < ' ' && c != '\t')) { - throw new ArgumentException("net_WebHeaderInvalidControlChars"); + throw new ArgumentException("net_WebHeaderInvalidControlChars", nameof(name)); } break; @@ -113,7 +131,7 @@ namespace Emby.Server.Implementations.SocketSharp break; } - throw new ArgumentException("net_WebHeaderInvalidCRLFChars"); + throw new ArgumentException("net_WebHeaderInvalidCRLFChars", nameof(name)); } case 2: @@ -124,29 +142,29 @@ namespace Emby.Server.Implementations.SocketSharp break; } - throw new ArgumentException("net_WebHeaderInvalidCRLFChars"); + throw new ArgumentException("net_WebHeaderInvalidCRLFChars", nameof(name)); } } } if (crlf != 0) { - throw new ArgumentException("net_WebHeaderInvalidCRLFChars"); + throw new ArgumentException("net_WebHeaderInvalidCRLFChars", nameof(name)); } return name; } - private string NormalizeIp(string ip) + private ReadOnlySpan<char> NormalizeIp(ReadOnlySpan<char> ip) { - if (!string.IsNullOrWhiteSpace(ip)) + if (ip.Length != 0 && !ip.IsWhiteSpace()) { // Handle ipv4 mapped to ipv6 const string srch = "::ffff:"; - var index = ip.IndexOf(srch, StringComparison.OrdinalIgnoreCase); + var index = ip.IndexOf(srch.AsSpan(), StringComparison.OrdinalIgnoreCase); if (index == 0) { - ip = ip.Substring(srch.Length); + ip = ip.Slice(srch.Length); } } @@ -324,7 +342,7 @@ namespace Emby.Server.Implementations.SocketSharp } this.pathInfo = WebUtility.UrlDecode(pathInfo); - this.pathInfo = NormalizePathInfo(pathInfo, mode); + this.pathInfo = NormalizePathInfo(pathInfo, mode).ToString(); } return this.pathInfo; @@ -436,7 +454,7 @@ namespace Emby.Server.Implementations.SocketSharp public static Encoding GetEncoding(string contentTypeHeader) { - var param = GetParameter(contentTypeHeader, "charset="); + var param = GetParameter(contentTypeHeader.AsSpan(), "charset="); if (param == null) { return null; @@ -488,18 +506,18 @@ namespace Emby.Server.Implementations.SocketSharp } } - public static string NormalizePathInfo(string pathInfo, string handlerPath) + public static ReadOnlySpan<char> NormalizePathInfo(string pathInfo, string handlerPath) { if (handlerPath != null) { - var trimmed = pathInfo.TrimStart('/'); - if (trimmed.StartsWith(handlerPath, StringComparison.OrdinalIgnoreCase)) + var trimmed = pathInfo.AsSpan().TrimStart('/'); + if (trimmed.StartsWith(handlerPath.AsSpan(), StringComparison.OrdinalIgnoreCase)) { - return trimmed.Substring(handlerPath.Length); + return trimmed.Slice(handlerPath.Length).ToString().AsSpan(); } } - return pathInfo; + return pathInfo.AsSpan(); } } } |
