2
0

HttpResponseStream.Managed.cs 16 KB

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