WebSocketSharpRequest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Text;
  6. using Emby.Server.Implementations.HttpServer.SocketSharp;
  7. using Funq;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Services;
  12. using ServiceStack;
  13. using ServiceStack.Host;
  14. using ServiceStack.Web;
  15. using SocketHttpListener.Net;
  16. using IHttpFile = MediaBrowser.Model.Services.IHttpFile;
  17. using IHttpRequest = MediaBrowser.Model.Services.IHttpRequest;
  18. using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
  19. using IResponse = MediaBrowser.Model.Services.IResponse;
  20. namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
  21. {
  22. public partial class WebSocketSharpRequest : IHttpRequest
  23. {
  24. public Container Container { get; set; }
  25. private readonly HttpListenerRequest request;
  26. private readonly IHttpResponse response;
  27. private readonly IMemoryStreamProvider _memoryStreamProvider;
  28. public WebSocketSharpRequest(HttpListenerContext httpContext, string operationName, RequestAttributes requestAttributes, ILogger logger, IMemoryStreamProvider memoryStreamProvider)
  29. {
  30. this.OperationName = operationName;
  31. this.RequestAttributes = requestAttributes;
  32. _memoryStreamProvider = memoryStreamProvider;
  33. this.request = httpContext.Request;
  34. this.response = new WebSocketSharpResponse(logger, httpContext.Response, this);
  35. }
  36. public HttpListenerRequest HttpRequest
  37. {
  38. get { return request; }
  39. }
  40. public object OriginalRequest
  41. {
  42. get { return request; }
  43. }
  44. public IResponse Response
  45. {
  46. get { return response; }
  47. }
  48. public IHttpResponse HttpResponse
  49. {
  50. get { return response; }
  51. }
  52. public RequestAttributes RequestAttributes { get; set; }
  53. public T TryResolve<T>()
  54. {
  55. if (typeof(T) == typeof(IHttpRequest))
  56. throw new Exception("You don't need to use IHttpRequest.TryResolve<IHttpRequest> to resolve itself");
  57. if (typeof(T) == typeof(IHttpResponse))
  58. throw new Exception("Resolve IHttpResponse with 'Response' property instead of IHttpRequest.TryResolve<IHttpResponse>");
  59. return Container == null
  60. ? HostContext.TryResolve<T>()
  61. : Container.TryResolve<T>();
  62. }
  63. public string OperationName { get; set; }
  64. public object Dto { get; set; }
  65. public string GetRawBody()
  66. {
  67. if (bufferedStream != null)
  68. {
  69. return bufferedStream.ToArray().FromUtf8Bytes();
  70. }
  71. using (var reader = new StreamReader(InputStream))
  72. {
  73. return reader.ReadToEnd();
  74. }
  75. }
  76. public string RawUrl
  77. {
  78. get { return request.RawUrl; }
  79. }
  80. public string AbsoluteUri
  81. {
  82. get { return request.Url.AbsoluteUri.TrimEnd('/'); }
  83. }
  84. public string UserHostAddress
  85. {
  86. get { return request.UserHostAddress; }
  87. }
  88. public string XForwardedFor
  89. {
  90. get
  91. {
  92. return String.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedFor]) ? null : request.Headers[HttpHeaders.XForwardedFor];
  93. }
  94. }
  95. public int? XForwardedPort
  96. {
  97. get
  98. {
  99. return string.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedPort]) ? (int?)null : int.Parse(request.Headers[HttpHeaders.XForwardedPort]);
  100. }
  101. }
  102. public string XForwardedProtocol
  103. {
  104. get
  105. {
  106. return string.IsNullOrEmpty(request.Headers[HttpHeaders.XForwardedProtocol]) ? null : request.Headers[HttpHeaders.XForwardedProtocol];
  107. }
  108. }
  109. public string XRealIp
  110. {
  111. get
  112. {
  113. return String.IsNullOrEmpty(request.Headers[HttpHeaders.XRealIp]) ? null : request.Headers[HttpHeaders.XRealIp];
  114. }
  115. }
  116. private string remoteIp;
  117. public string RemoteIp
  118. {
  119. get
  120. {
  121. return remoteIp ??
  122. (remoteIp = (CheckBadChars(XForwardedFor)) ??
  123. (NormalizeIp(CheckBadChars(XRealIp)) ??
  124. (request.RemoteEndPoint != null ? NormalizeIp(request.RemoteEndPoint.Address.ToString()) : null)));
  125. }
  126. }
  127. private static readonly char[] HttpTrimCharacters = new char[] { (char)0x09, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20 };
  128. //
  129. // CheckBadChars - throws on invalid chars to be not found in header name/value
  130. //
  131. internal static string CheckBadChars(string name)
  132. {
  133. if (name == null || name.Length == 0)
  134. {
  135. return name;
  136. }
  137. // VALUE check
  138. //Trim spaces from both ends
  139. name = name.Trim(HttpTrimCharacters);
  140. //First, check for correctly formed multi-line value
  141. //Second, check for absenece of CTL characters
  142. int crlf = 0;
  143. for (int i = 0; i < name.Length; ++i)
  144. {
  145. char c = (char)(0x000000ff & (uint)name[i]);
  146. switch (crlf)
  147. {
  148. case 0:
  149. if (c == '\r')
  150. {
  151. crlf = 1;
  152. }
  153. else if (c == '\n')
  154. {
  155. // Technically this is bad HTTP. But it would be a breaking change to throw here.
  156. // Is there an exploit?
  157. crlf = 2;
  158. }
  159. else if (c == 127 || (c < ' ' && c != '\t'))
  160. {
  161. throw new ArgumentException("net_WebHeaderInvalidControlChars");
  162. }
  163. break;
  164. case 1:
  165. if (c == '\n')
  166. {
  167. crlf = 2;
  168. break;
  169. }
  170. throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
  171. case 2:
  172. if (c == ' ' || c == '\t')
  173. {
  174. crlf = 0;
  175. break;
  176. }
  177. throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
  178. }
  179. }
  180. if (crlf != 0)
  181. {
  182. throw new ArgumentException("net_WebHeaderInvalidCRLFChars");
  183. }
  184. return name;
  185. }
  186. internal static bool ContainsNonAsciiChars(string token)
  187. {
  188. for (int i = 0; i < token.Length; ++i)
  189. {
  190. if ((token[i] < 0x20) || (token[i] > 0x7e))
  191. {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. private string NormalizeIp(string ip)
  198. {
  199. if (!string.IsNullOrWhiteSpace(ip))
  200. {
  201. // Handle ipv4 mapped to ipv6
  202. const string srch = "::ffff:";
  203. var index = ip.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  204. if (index == 0)
  205. {
  206. ip = ip.Substring(srch.Length);
  207. }
  208. }
  209. return ip;
  210. }
  211. public bool IsSecureConnection
  212. {
  213. get { return request.IsSecureConnection || XForwardedProtocol == "https"; }
  214. }
  215. public string[] AcceptTypes
  216. {
  217. get { return request.AcceptTypes; }
  218. }
  219. private Dictionary<string, object> items;
  220. public Dictionary<string, object> Items
  221. {
  222. get { return items ?? (items = new Dictionary<string, object>()); }
  223. }
  224. private string responseContentType;
  225. public string ResponseContentType
  226. {
  227. get
  228. {
  229. return responseContentType
  230. ?? (responseContentType = this.GetResponseContentType());
  231. }
  232. set
  233. {
  234. this.responseContentType = value;
  235. HasExplicitResponseContentType = true;
  236. }
  237. }
  238. public bool HasExplicitResponseContentType { get; private set; }
  239. private string pathInfo;
  240. public string PathInfo
  241. {
  242. get
  243. {
  244. if (this.pathInfo == null)
  245. {
  246. var mode = HostContext.Config.HandlerFactoryPath;
  247. var pos = request.RawUrl.IndexOf("?");
  248. if (pos != -1)
  249. {
  250. var path = request.RawUrl.Substring(0, pos);
  251. this.pathInfo = HttpRequestExtensions.GetPathInfo(
  252. path,
  253. mode,
  254. mode ?? "");
  255. }
  256. else
  257. {
  258. this.pathInfo = request.RawUrl;
  259. }
  260. this.pathInfo = this.pathInfo.UrlDecode();
  261. this.pathInfo = NormalizePathInfo(pathInfo, mode);
  262. }
  263. return this.pathInfo;
  264. }
  265. }
  266. private Dictionary<string, System.Net.Cookie> cookies;
  267. public IDictionary<string, System.Net.Cookie> Cookies
  268. {
  269. get
  270. {
  271. if (cookies == null)
  272. {
  273. cookies = new Dictionary<string, System.Net.Cookie>();
  274. for (var i = 0; i < this.request.Cookies.Count; i++)
  275. {
  276. var httpCookie = this.request.Cookies[i];
  277. cookies[httpCookie.Name] = new System.Net.Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, httpCookie.Domain);
  278. }
  279. }
  280. return cookies;
  281. }
  282. }
  283. public string UserAgent
  284. {
  285. get { return request.UserAgent; }
  286. }
  287. private QueryParamCollection headers;
  288. public QueryParamCollection Headers
  289. {
  290. get { return headers ?? (headers = ToQueryParams(request.Headers)); }
  291. }
  292. private QueryParamCollection queryString;
  293. public QueryParamCollection QueryString
  294. {
  295. get { return queryString ?? (queryString = MyHttpUtility.ParseQueryString(request.Url.Query)); }
  296. }
  297. private QueryParamCollection formData;
  298. public QueryParamCollection FormData
  299. {
  300. get { return formData ?? (formData = this.Form); }
  301. }
  302. private QueryParamCollection ToQueryParams(NameValueCollection collection)
  303. {
  304. var result = new QueryParamCollection();
  305. foreach (var key in collection.AllKeys)
  306. {
  307. result[key] = collection[key];
  308. }
  309. return result;
  310. }
  311. public bool IsLocal
  312. {
  313. get { return request.IsLocal; }
  314. }
  315. private string httpMethod;
  316. public string HttpMethod
  317. {
  318. get
  319. {
  320. return httpMethod
  321. ?? (httpMethod = Param(HttpHeaders.XHttpMethodOverride)
  322. ?? request.HttpMethod);
  323. }
  324. }
  325. public string Verb
  326. {
  327. get { return HttpMethod; }
  328. }
  329. public string Param(string name)
  330. {
  331. return Headers[name]
  332. ?? QueryString[name]
  333. ?? FormData[name];
  334. }
  335. public string ContentType
  336. {
  337. get { return request.ContentType; }
  338. }
  339. public Encoding contentEncoding;
  340. public Encoding ContentEncoding
  341. {
  342. get { return contentEncoding ?? request.ContentEncoding; }
  343. set { contentEncoding = value; }
  344. }
  345. public Uri UrlReferrer
  346. {
  347. get { return request.UrlReferrer; }
  348. }
  349. public static Encoding GetEncoding(string contentTypeHeader)
  350. {
  351. var param = GetParameter(contentTypeHeader, "charset=");
  352. if (param == null) return null;
  353. try
  354. {
  355. return Encoding.GetEncoding(param);
  356. }
  357. catch (ArgumentException)
  358. {
  359. return null;
  360. }
  361. }
  362. public bool UseBufferedStream
  363. {
  364. get { return bufferedStream != null; }
  365. set
  366. {
  367. bufferedStream = value
  368. ? bufferedStream ?? _memoryStreamProvider.CreateNew(request.InputStream.ReadFully())
  369. : null;
  370. }
  371. }
  372. private MemoryStream bufferedStream;
  373. public Stream InputStream
  374. {
  375. get { return bufferedStream ?? request.InputStream; }
  376. }
  377. public long ContentLength
  378. {
  379. get { return request.ContentLength64; }
  380. }
  381. private IHttpFile[] httpFiles;
  382. public IHttpFile[] Files
  383. {
  384. get
  385. {
  386. if (httpFiles == null)
  387. {
  388. if (files == null)
  389. return httpFiles = new IHttpFile[0];
  390. httpFiles = new IHttpFile[files.Count];
  391. for (var i = 0; i < files.Count; i++)
  392. {
  393. var reqFile = files[i];
  394. httpFiles[i] = new HttpFile
  395. {
  396. ContentType = reqFile.ContentType,
  397. ContentLength = reqFile.ContentLength,
  398. FileName = reqFile.FileName,
  399. InputStream = reqFile.InputStream,
  400. };
  401. }
  402. }
  403. return httpFiles;
  404. }
  405. }
  406. static Stream GetSubStream(Stream stream, IMemoryStreamProvider streamProvider)
  407. {
  408. if (stream is MemoryStream)
  409. {
  410. var other = (MemoryStream)stream;
  411. try
  412. {
  413. return new MemoryStream(other.GetBuffer(), 0, (int)other.Length, false, true);
  414. }
  415. catch (UnauthorizedAccessException)
  416. {
  417. return new MemoryStream(other.ToArray(), 0, (int)other.Length, false, true);
  418. }
  419. }
  420. return stream;
  421. }
  422. public static string GetHandlerPathIfAny(string listenerUrl)
  423. {
  424. if (listenerUrl == null) return null;
  425. var pos = listenerUrl.IndexOf("://", StringComparison.InvariantCultureIgnoreCase);
  426. if (pos == -1) return null;
  427. var startHostUrl = listenerUrl.Substring(pos + "://".Length);
  428. var endPos = startHostUrl.IndexOf('/');
  429. if (endPos == -1) return null;
  430. var endHostUrl = startHostUrl.Substring(endPos + 1);
  431. return String.IsNullOrEmpty(endHostUrl) ? null : endHostUrl.TrimEnd('/');
  432. }
  433. public static string NormalizePathInfo(string pathInfo, string handlerPath)
  434. {
  435. if (handlerPath != null && pathInfo.TrimStart('/').StartsWith(
  436. handlerPath, StringComparison.InvariantCultureIgnoreCase))
  437. {
  438. return pathInfo.TrimStart('/').Substring(handlerPath.Length);
  439. }
  440. return pathInfo;
  441. }
  442. }
  443. }