You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Emby.Server.Implementations/SocketSharp/WebSocketSharpRequest.cs

251 lines
8.1 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
5 years ago
using System.Net.Mime;
using MediaBrowser.Common.Net;
6 years ago
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
6 years ago
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
namespace Emby.Server.Implementations.SocketSharp
{
public class WebSocketSharpRequest : IHttpRequest
6 years ago
{
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;
6 years ago
public WebSocketSharpRequest(HttpRequest httpRequest, HttpResponse httpResponse, string operationName, ILogger logger)
6 years ago
{
this.OperationName = operationName;
this.Request = httpRequest;
this.Response = httpResponse;
6 years ago
}
public string Accept => StringValues.IsNullOrEmpty(Request.Headers[HeaderNames.Accept]) ? null : Request.Headers[HeaderNames.Accept].ToString();
6 years ago
public string Authorization => StringValues.IsNullOrEmpty(Request.Headers[HeaderNames.Authorization]) ? null : Request.Headers[HeaderNames.Authorization].ToString();
6 years ago
public HttpRequest Request { get; }
6 years ago
public HttpResponse Response { get; }
6 years ago
public string OperationName { get; set; }
6 years ago
public string RawUrl => Request.GetEncodedPathAndQuery();
6 years ago
public string AbsoluteUri => Request.GetDisplayUrl().TrimEnd('/');
6 years ago
public string RemoteIp
{
get
{
if (_remoteIp != null)
{
return _remoteIp;
}
6 years ago
IPAddress ip;
// "Real" remote ip might be in X-Forwarded-For of X-Real-Ip
// (if the server is behind a reverse proxy for example)
if (!IPAddress.TryParse(GetHeader(CustomHeaderNames.XForwardedFor), out ip))
{
if (!IPAddress.TryParse(GetHeader(CustomHeaderNames.XRealIP), out ip))
6 years ago
{
ip = Request.HttpContext.Connection.RemoteIpAddress;
6 years ago
}
}
return _remoteIp = NormalizeIp(ip).ToString();
6 years ago
}
}
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));
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.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)
6 years ago
{
if (ip.IsIPv4MappedToIPv6)
6 years ago
{
return ip.MapToIPv4();
6 years ago
}
return ip;
}
public static string GetResponseContentType(HttpRequest httpReq)
{
var specifiedContentType = GetQueryStringContentType(httpReq);
if (!string.IsNullOrEmpty(specifiedContentType))
{
return specifiedContentType;
}
const string ServerDefaultContentType = MediaTypeNames.Application.Json;
6 years ago
var acceptContentTypes = httpReq.Headers.GetCommaSeparatedValues(HeaderNames.Accept);
6 years ago
string defaultContentType = null;
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
{
defaultContentType = ServerDefaultContentType;
6 years ago
}
var acceptsAnything = false;
var hasDefaultContentType = defaultContentType != null;
if (acceptContentTypes != null)
{
foreach (ReadOnlySpan<char> acceptsType in acceptContentTypes)
6 years ago
{
ReadOnlySpan<char> contentType = acceptsType;
var index = contentType.IndexOf(';');
if (index != -1)
{
contentType = contentType.Slice(0, index);
}
contentType = contentType.Trim();
6 years ago
acceptsAnything = contentType.Equals("*/*", StringComparison.OrdinalIgnoreCase);
if (acceptsAnything)
{
break;
}
}
if (acceptsAnything)
{
if (hasDefaultContentType)
{
return defaultContentType;
}
else
{
return ServerDefaultContentType;
6 years ago
}
}
}
if (acceptContentTypes == null && httpReq.ContentType == Soap11)
{
return Soap11;
}
// We could also send a '406 Not Acceptable', but this is allowed also
return ServerDefaultContentType;
6 years ago
}
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)
{
ReadOnlySpan<char> format = httpReq.Query["format"].ToString();
6 years ago
if (format == null)
{
const int FormatMaxLength = 4;
ReadOnlySpan<char> pi = httpReq.Path.ToString();
if (pi == null || pi.Length <= FormatMaxLength)
6 years ago
{
return null;
}
if (pi[0] == '/')
{
pi = pi.Slice(1);
6 years ago
}
format = LeftPart(pi, '/');
if (format.Length > FormatMaxLength)
6 years ago
{
return null;
}
}
format = LeftPart(format, '.');
if (format.Contains("json", StringComparison.OrdinalIgnoreCase))
6 years ago
{
return "application/json";
}
else if (format.Contains("xml", StringComparison.OrdinalIgnoreCase))
6 years ago
{
return "application/xml";
}
return null;
}
public static ReadOnlySpan<char> LeftPart(ReadOnlySpan<char> strVal, char needle)
6 years ago
{
if (strVal == null)
{
return null;
}
var pos = strVal.IndexOf(needle);
6 years ago
return pos == -1 ? strVal : strVal.Slice(0, pos);
}
}
}