jellyfin-server/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs

249 lines
8.2 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2019-02-25 22:34:32 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
2019-12-03 09:55:22 +00:00
using System.Net.Mime;
2020-04-19 13:18:28 +00:00
using MediaBrowser.Common.Extensions;
2019-04-12 13:25:18 +00:00
using MediaBrowser.Common.Net;
2019-02-25 22:34:32 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
2019-02-25 22:34:32 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
namespace Emby.Server.Implementations.SocketSharp
{
2019-10-25 10:47:20 +00:00
public class WebSocketSharpRequest : IHttpRequest
2019-02-25 22:34:32 +00:00
{
2019-11-28 16:46:06 +00:00
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;
private string _responseContentType;
2019-02-25 22:34:32 +00:00
2020-06-06 00:15:56 +00:00
public WebSocketSharpRequest(HttpRequest httpRequest, HttpResponse httpResponse, string operationName)
2019-02-25 22:34:32 +00:00
{
this.OperationName = operationName;
this.Request = httpRequest;
this.Response = httpResponse;
2019-02-25 22:34:32 +00:00
}
public string Accept => StringValues.IsNullOrEmpty(Request.Headers[HeaderNames.Accept]) ? null : Request.Headers[HeaderNames.Accept].ToString();
2019-02-25 22:34:32 +00:00
public string Authorization => StringValues.IsNullOrEmpty(Request.Headers[HeaderNames.Authorization]) ? null : Request.Headers[HeaderNames.Authorization].ToString();
2019-02-25 22:34:32 +00:00
public HttpRequest Request { get; }
2019-02-25 22:34:32 +00:00
public HttpResponse Response { get; }
2019-02-25 22:34:32 +00:00
public string OperationName { get; set; }
2019-02-25 22:34:32 +00:00
public string RawUrl => Request.GetEncodedPathAndQuery();
2019-02-25 22:34:32 +00:00
public string AbsoluteUri => Request.GetDisplayUrl().TrimEnd('/');
2019-02-25 22:34:32 +00:00
2019-02-16 15:54:18 +00:00
public string RemoteIp
{
get
{
if (_remoteIp != null)
2019-02-16 15:54:18 +00:00
{
return _remoteIp;
2019-02-16 15:54:18 +00:00
}
2019-02-25 22:34:32 +00:00
IPAddress ip;
2019-02-16 15:54:18 +00:00
// "Real" remote ip might be in X-Forwarded-For of X-Real-Ip
// (if the server is behind a reverse proxy for example)
2019-04-12 13:25:18 +00:00
if (!IPAddress.TryParse(GetHeader(CustomHeaderNames.XForwardedFor), out ip))
2019-02-16 15:54:18 +00:00
{
2019-04-12 13:25:18 +00:00
if (!IPAddress.TryParse(GetHeader(CustomHeaderNames.XRealIP), out ip))
2019-02-25 22:34:32 +00:00
{
ip = Request.HttpContext.Connection.RemoteIpAddress;
// Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
ip ??= IPAddress.Loopback;
2019-02-25 22:34:32 +00:00
}
}
return _remoteIp = NormalizeIp(ip).ToString();
2019-02-25 22:34:32 +00:00
}
}
public string[] AcceptTypes => Request.Headers.GetCommaSeparatedValues(HeaderNames.Accept);
public Dictionary<string, object> Items => _items ?? (_items = new Dictionary<string, object>());
public string ResponseContentType
{
get =>
_responseContentType
?? (_responseContentType = GetResponseContentType(Request));
2019-11-28 16:46:06 +00:00
set => _responseContentType = value;
}
public string PathInfo => Request.Path.Value;
public string UserAgent => Request.Headers[HeaderNames.UserAgent];
public IHeaderDictionary Headers => Request.Headers;
public IQueryCollection QueryString => Request.Query;
public bool IsLocal =>
(Request.HttpContext.Connection.LocalIpAddress == null
&& Request.HttpContext.Connection.RemoteIpAddress == null)
|| Request.HttpContext.Connection.LocalIpAddress.Equals(Request.HttpContext.Connection.RemoteIpAddress);
public string HttpMethod => Request.Method;
public string Verb => HttpMethod;
public string ContentType => Request.ContentType;
public Uri UrlReferrer => Request.GetTypedHeaders().Referer;
public Stream InputStream => Request.Body;
public long ContentLength => Request.ContentLength ?? 0;
private string GetHeader(string name) => Request.Headers[name].ToString();
private static IPAddress NormalizeIp(IPAddress ip)
2019-02-25 22:34:32 +00:00
{
if (ip.IsIPv4MappedToIPv6)
2019-02-25 22:34:32 +00:00
{
return ip.MapToIPv4();
2019-02-25 22:34:32 +00:00
}
return ip;
}
public static string GetResponseContentType(HttpRequest httpReq)
{
var specifiedContentType = GetQueryStringContentType(httpReq);
if (!string.IsNullOrEmpty(specifiedContentType))
{
return specifiedContentType;
}
const string ServerDefaultContentType = MediaTypeNames.Application.Json;
2019-02-25 22:34:32 +00:00
var acceptContentTypes = httpReq.Headers.GetCommaSeparatedValues(HeaderNames.Accept);
2019-02-25 22:34:32 +00:00
string defaultContentType = null;
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
{
2019-11-28 16:46:06 +00:00
defaultContentType = ServerDefaultContentType;
2019-02-25 22:34:32 +00:00
}
var acceptsAnything = false;
var hasDefaultContentType = defaultContentType != null;
if (acceptContentTypes != null)
{
2019-11-28 16:46:06 +00:00
foreach (ReadOnlySpan<char> acceptsType in acceptContentTypes)
2019-02-25 22:34:32 +00:00
{
2019-11-28 16:46:06 +00:00
ReadOnlySpan<char> contentType = acceptsType;
var index = contentType.IndexOf(';');
if (index != -1)
{
contentType = contentType.Slice(0, index);
}
contentType = contentType.Trim();
2019-02-25 22:34:32 +00:00
acceptsAnything = contentType.Equals("*/*", StringComparison.OrdinalIgnoreCase);
if (acceptsAnything)
{
break;
}
}
if (acceptsAnything)
{
if (hasDefaultContentType)
{
return defaultContentType;
}
else
{
2019-11-28 16:46:06 +00:00
return ServerDefaultContentType;
2019-02-25 22:34:32 +00:00
}
}
}
if (acceptContentTypes == null && httpReq.ContentType == Soap11)
{
return Soap11;
}
// We could also send a '406 Not Acceptable', but this is allowed also
2019-11-28 16:46:06 +00:00
return ServerDefaultContentType;
2019-02-25 22:34:32 +00:00
}
public static bool HasAnyOfContentTypes(HttpRequest 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(HttpRequest request, string contentType)
{
return request.ContentType.StartsWith(contentType, StringComparison.OrdinalIgnoreCase);
}
private static string GetQueryStringContentType(HttpRequest httpReq)
{
2020-05-26 17:33:20 +00:00
ReadOnlySpan<char> format = httpReq.Query["format"].ToString();
2020-05-26 18:00:37 +00:00
if (format == ReadOnlySpan<char>.Empty)
2019-02-25 22:34:32 +00:00
{
2019-11-28 16:46:06 +00:00
const int FormatMaxLength = 4;
ReadOnlySpan<char> pi = httpReq.Path.ToString();
if (pi == null || pi.Length <= FormatMaxLength)
2019-02-25 22:34:32 +00:00
{
return null;
}
if (pi[0] == '/')
{
2019-02-26 21:40:25 +00:00
pi = pi.Slice(1);
2019-02-25 22:34:32 +00:00
}
2020-04-19 13:18:28 +00:00
format = pi.LeftPart('/');
2019-11-28 16:46:06 +00:00
if (format.Length > FormatMaxLength)
2019-02-25 22:34:32 +00:00
{
return null;
}
}
2020-04-19 13:18:28 +00:00
format = format.LeftPart('.');
2019-11-28 16:46:06 +00:00
if (format.Contains("json", StringComparison.OrdinalIgnoreCase))
2019-02-25 22:34:32 +00:00
{
return "application/json";
}
2019-11-28 16:46:06 +00:00
else if (format.Contains("xml", StringComparison.OrdinalIgnoreCase))
2019-02-25 22:34:32 +00:00
{
return "application/xml";
}
return null;
}
}
}