HttpResponseStream.Managed.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.ExceptionServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Net;
  12. using MediaBrowser.Model.System;
  13. namespace SocketHttpListener.Net
  14. {
  15. // Licensed to the .NET Foundation under one or more agreements.
  16. // See the LICENSE file in the project root for more information.
  17. //
  18. // System.Net.ResponseStream
  19. //
  20. // Author:
  21. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  22. //
  23. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  24. //
  25. // Permission is hereby granted, free of charge, to any person obtaining
  26. // a copy of this software and associated documentation files (the
  27. // "Software"), to deal in the Software without restriction, including
  28. // without limitation the rights to use, copy, modify, merge, publish,
  29. // distribute, sublicense, and/or sell copies of the Software, and to
  30. // permit persons to whom the Software is furnished to do so, subject to
  31. // the following conditions:
  32. //
  33. // The above copyright notice and this permission notice shall be
  34. // included in all copies or substantial portions of the Software.
  35. //
  36. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  37. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  38. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  39. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  40. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  41. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  42. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. //
  44. internal partial class HttpResponseStream : Stream
  45. {
  46. private HttpListenerResponse _response;
  47. private bool _ignore_errors;
  48. private bool _trailer_sent;
  49. private Stream _stream;
  50. private readonly IMemoryStreamFactory _memoryStreamFactory;
  51. private readonly IAcceptSocket _socket;
  52. private readonly bool _supportsDirectSocketAccess;
  53. private readonly IEnvironmentInfo _environment;
  54. private readonly IFileSystem _fileSystem;
  55. internal HttpResponseStream(Stream stream, HttpListenerResponse response, bool ignore_errors, IMemoryStreamFactory memoryStreamFactory, IAcceptSocket socket, bool supportsDirectSocketAccess, IEnvironmentInfo environment, IFileSystem fileSystem)
  56. {
  57. _response = response;
  58. _ignore_errors = ignore_errors;
  59. _memoryStreamFactory = memoryStreamFactory;
  60. _socket = socket;
  61. _supportsDirectSocketAccess = supportsDirectSocketAccess;
  62. _environment = environment;
  63. _fileSystem = fileSystem;
  64. _stream = stream;
  65. }
  66. private void DisposeCore()
  67. {
  68. byte[] bytes = null;
  69. MemoryStream ms = GetHeaders(true);
  70. bool chunked = _response.SendChunked;
  71. if (_stream.CanWrite)
  72. {
  73. try
  74. {
  75. if (ms != null)
  76. {
  77. long start = ms.Position;
  78. if (chunked && !_trailer_sent)
  79. {
  80. bytes = GetChunkSizeBytes(0, true);
  81. ms.Position = ms.Length;
  82. ms.Write(bytes, 0, bytes.Length);
  83. }
  84. InternalWrite(ms.GetBuffer(), (int)start, (int)(ms.Length - start));
  85. _trailer_sent = true;
  86. }
  87. else if (chunked && !_trailer_sent)
  88. {
  89. bytes = GetChunkSizeBytes(0, true);
  90. InternalWrite(bytes, 0, bytes.Length);
  91. _trailer_sent = true;
  92. }
  93. }
  94. catch (HttpListenerException)
  95. {
  96. // Ignore error due to connection reset by peer
  97. }
  98. }
  99. _response.Close();
  100. }
  101. internal async Task WriteWebSocketHandshakeHeadersAsync()
  102. {
  103. if (_closed)
  104. throw new ObjectDisposedException(GetType().ToString());
  105. if (_stream.CanWrite)
  106. {
  107. MemoryStream ms = GetHeaders(closing: false, isWebSocketHandshake: true);
  108. bool chunked = _response.SendChunked;
  109. long start = ms.Position;
  110. if (chunked)
  111. {
  112. byte[] bytes = GetChunkSizeBytes(0, true);
  113. ms.Position = ms.Length;
  114. ms.Write(bytes, 0, bytes.Length);
  115. }
  116. await InternalWriteAsync(ms.GetBuffer(), (int)start, (int)(ms.Length - start)).ConfigureAwait(false);
  117. await _stream.FlushAsync().ConfigureAwait(false);
  118. }
  119. }
  120. private MemoryStream GetHeaders(bool closing, bool isWebSocketHandshake = false)
  121. {
  122. // SendHeaders works on shared headers
  123. lock (_response.headers_lock)
  124. {
  125. if (_response.HeadersSent)
  126. return null;
  127. var ms = _memoryStreamFactory.CreateNew();
  128. _response.SendHeaders(closing, ms);
  129. return ms;
  130. }
  131. //lock (_response._headersLock)
  132. //{
  133. // if (_response.SentHeaders)
  134. // {
  135. // return null;
  136. // }
  137. // MemoryStream ms = new MemoryStream();
  138. // _response.SendHeaders(closing, ms, isWebSocketHandshake);
  139. // return ms;
  140. //}
  141. }
  142. private static byte[] s_crlf = new byte[] { 13, 10 };
  143. private static byte[] GetChunkSizeBytes(int size, bool final)
  144. {
  145. string str = String.Format("{0:x}\r\n{1}", size, final ? "\r\n" : "");
  146. return Encoding.ASCII.GetBytes(str);
  147. }
  148. internal void InternalWrite(byte[] buffer, int offset, int count)
  149. {
  150. if (_ignore_errors)
  151. {
  152. try
  153. {
  154. _stream.Write(buffer, offset, count);
  155. }
  156. catch { }
  157. }
  158. else
  159. {
  160. try
  161. {
  162. _stream.Write(buffer, offset, count);
  163. }
  164. catch (IOException ex)
  165. {
  166. throw new HttpListenerException(ex.HResult, ex.Message);
  167. }
  168. }
  169. }
  170. internal Task InternalWriteAsync(byte[] buffer, int offset, int count) =>
  171. _ignore_errors ? InternalWriteIgnoreErrorsAsync(buffer, offset, count) : _stream.WriteAsync(buffer, offset, count);
  172. private async Task InternalWriteIgnoreErrorsAsync(byte[] buffer, int offset, int count)
  173. {
  174. try { await _stream.WriteAsync(buffer, offset, count).ConfigureAwait(false); }
  175. catch { }
  176. }
  177. private void WriteCore(byte[] buffer, int offset, int size)
  178. {
  179. if (size == 0)
  180. return;
  181. byte[] bytes = null;
  182. MemoryStream ms = GetHeaders(false);
  183. bool chunked = _response.SendChunked;
  184. if (ms != null)
  185. {
  186. long start = ms.Position; // After the possible preamble for the encoding
  187. ms.Position = ms.Length;
  188. if (chunked)
  189. {
  190. bytes = GetChunkSizeBytes(size, false);
  191. ms.Write(bytes, 0, bytes.Length);
  192. }
  193. int new_count = Math.Min(size, 16384 - (int)ms.Position + (int)start);
  194. ms.Write(buffer, offset, new_count);
  195. size -= new_count;
  196. offset += new_count;
  197. InternalWrite(ms.GetBuffer(), (int)start, (int)(ms.Length - start));
  198. ms.SetLength(0);
  199. ms.Capacity = 0; // 'dispose' the buffer in ms.
  200. }
  201. else if (chunked)
  202. {
  203. bytes = GetChunkSizeBytes(size, false);
  204. InternalWrite(bytes, 0, bytes.Length);
  205. }
  206. if (size > 0)
  207. InternalWrite(buffer, offset, size);
  208. if (chunked)
  209. InternalWrite(s_crlf, 0, 2);
  210. }
  211. private IAsyncResult BeginWriteCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
  212. {
  213. if (_closed)
  214. {
  215. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  216. ares._callback = cback;
  217. ares._state = state;
  218. ares.Complete();
  219. return ares;
  220. }
  221. byte[] bytes = null;
  222. MemoryStream ms = GetHeaders(false);
  223. bool chunked = _response.SendChunked;
  224. if (ms != null)
  225. {
  226. long start = ms.Position;
  227. ms.Position = ms.Length;
  228. if (chunked)
  229. {
  230. bytes = GetChunkSizeBytes(size, false);
  231. ms.Write(bytes, 0, bytes.Length);
  232. }
  233. ms.Write(buffer, offset, size);
  234. buffer = ms.GetBuffer();
  235. offset = (int)start;
  236. size = (int)(ms.Position - start);
  237. }
  238. else if (chunked)
  239. {
  240. bytes = GetChunkSizeBytes(size, false);
  241. InternalWrite(bytes, 0, bytes.Length);
  242. }
  243. try
  244. {
  245. return _stream.BeginWrite(buffer, offset, size, cback, state);
  246. }
  247. catch (IOException ex)
  248. {
  249. if (_ignore_errors)
  250. {
  251. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  252. ares._callback = cback;
  253. ares._state = state;
  254. ares.Complete();
  255. return ares;
  256. }
  257. else
  258. {
  259. throw new HttpListenerException(ex.HResult, ex.Message);
  260. }
  261. }
  262. }
  263. private void EndWriteCore(IAsyncResult asyncResult)
  264. {
  265. if (_closed)
  266. return;
  267. if (_ignore_errors)
  268. {
  269. try
  270. {
  271. _stream.EndWrite(asyncResult);
  272. if (_response.SendChunked)
  273. _stream.Write(s_crlf, 0, 2);
  274. }
  275. catch { }
  276. }
  277. else
  278. {
  279. try
  280. {
  281. _stream.EndWrite(asyncResult);
  282. if (_response.SendChunked)
  283. _stream.Write(s_crlf, 0, 2);
  284. }
  285. catch (IOException ex)
  286. {
  287. // NetworkStream wraps exceptions in IOExceptions; if the underlying socket operation
  288. // failed because of invalid arguments or usage, propagate that error. Otherwise
  289. // wrap the whole thing in an HttpListenerException. This is all to match Windows behavior.
  290. if (ex.InnerException is ArgumentException || ex.InnerException is InvalidOperationException)
  291. {
  292. throw ex.InnerException;
  293. }
  294. throw new HttpListenerException(ex.HResult, ex.Message);
  295. }
  296. }
  297. }
  298. private bool EnableSendFileWithSocket
  299. {
  300. get { return false; }
  301. }
  302. public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  303. {
  304. if (_supportsDirectSocketAccess && offset == 0 && count == 0 && !_response.SendChunked && _response.ContentLength64 > 8192)
  305. {
  306. if (EnableSendFileWithSocket)
  307. {
  308. return TransmitFileOverSocket(path, offset, count, fileShareMode, cancellationToken);
  309. }
  310. }
  311. return TransmitFileManaged(path, offset, count, fileShareMode, cancellationToken);
  312. }
  313. private readonly byte[] _emptyBuffer = new byte[] { };
  314. private Task TransmitFileOverSocket(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  315. {
  316. var ms = GetHeaders(false);
  317. byte[] preBuffer;
  318. if (ms != null)
  319. {
  320. using (var msCopy = new MemoryStream())
  321. {
  322. ms.CopyTo(msCopy);
  323. preBuffer = msCopy.ToArray();
  324. }
  325. }
  326. else
  327. {
  328. return TransmitFileManaged(path, offset, count, fileShareMode, cancellationToken);
  329. }
  330. //_logger.Info("Socket sending file {0} {1}", path, response.ContentLength64);
  331. return _socket.SendFile(path, preBuffer, _emptyBuffer, cancellationToken);
  332. }
  333. const int StreamCopyToBufferSize = 81920;
  334. private async Task TransmitFileManaged(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
  335. {
  336. var allowAsync = _environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows;
  337. var fileOpenOptions = offset > 0
  338. ? FileOpenOptions.RandomAccess
  339. : FileOpenOptions.SequentialScan;
  340. if (allowAsync)
  341. {
  342. fileOpenOptions |= FileOpenOptions.Asynchronous;
  343. }
  344. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  345. using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShareMode, fileOpenOptions))
  346. {
  347. if (offset > 0)
  348. {
  349. fs.Position = offset;
  350. }
  351. var targetStream = this;
  352. if (count > 0)
  353. {
  354. if (allowAsync)
  355. {
  356. await CopyToInternalAsync(fs, targetStream, count, cancellationToken).ConfigureAwait(false);
  357. }
  358. else
  359. {
  360. await CopyToInternalAsyncWithSyncRead(fs, targetStream, count, cancellationToken).ConfigureAwait(false);
  361. }
  362. }
  363. else
  364. {
  365. if (allowAsync)
  366. {
  367. await fs.CopyToAsync(targetStream, StreamCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  368. }
  369. else
  370. {
  371. fs.CopyTo(targetStream, StreamCopyToBufferSize);
  372. }
  373. }
  374. }
  375. }
  376. private static async Task CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
  377. {
  378. var array = new byte[StreamCopyToBufferSize];
  379. int bytesRead;
  380. while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
  381. {
  382. var bytesToWrite = Math.Min(bytesRead, copyLength);
  383. if (bytesToWrite > 0)
  384. {
  385. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  386. }
  387. copyLength -= bytesToWrite;
  388. if (copyLength <= 0)
  389. {
  390. break;
  391. }
  392. }
  393. }
  394. private static async Task CopyToInternalAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
  395. {
  396. var array = new byte[StreamCopyToBufferSize];
  397. int bytesRead;
  398. while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
  399. {
  400. var bytesToWrite = Math.Min(bytesRead, copyLength);
  401. if (bytesToWrite > 0)
  402. {
  403. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  404. }
  405. copyLength -= bytesToWrite;
  406. if (copyLength <= 0)
  407. {
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. }