HttpRequestStream.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace SocketHttpListener.Net
  6. {
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // See the LICENSE file in the project root for more information.
  9. //
  10. // System.Net.ResponseStream
  11. //
  12. // Author:
  13. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  14. //
  15. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. internal partial class HttpRequestStream : Stream
  37. {
  38. public override bool CanSeek => false;
  39. public override bool CanWrite => false;
  40. public override bool CanRead => true;
  41. public override int Read(byte[] buffer, int offset, int size)
  42. {
  43. if (buffer == null)
  44. {
  45. throw new ArgumentNullException(nameof(buffer));
  46. }
  47. if (offset < 0 || offset > buffer.Length)
  48. {
  49. throw new ArgumentOutOfRangeException(nameof(offset));
  50. }
  51. if (size < 0 || size > buffer.Length - offset)
  52. {
  53. throw new ArgumentOutOfRangeException(nameof(size));
  54. }
  55. if (size == 0 || _closed)
  56. {
  57. return 0;
  58. }
  59. return ReadCore(buffer, offset, size);
  60. }
  61. public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
  62. {
  63. if (buffer == null)
  64. {
  65. throw new ArgumentNullException(nameof(buffer));
  66. }
  67. if (offset < 0 || offset > buffer.Length)
  68. {
  69. throw new ArgumentOutOfRangeException(nameof(offset));
  70. }
  71. if (size < 0 || size > buffer.Length - offset)
  72. {
  73. throw new ArgumentOutOfRangeException(nameof(size));
  74. }
  75. return BeginReadCore(buffer, offset, size, callback, state);
  76. }
  77. public override void Flush() { }
  78. public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
  79. public override long Length => throw new NotImplementedException();
  80. public override long Position
  81. {
  82. get => throw new NotImplementedException();
  83. set => throw new NotImplementedException();
  84. }
  85. public override long Seek(long offset, SeekOrigin origin)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. public override void SetLength(long value)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. public override void Write(byte[] buffer, int offset, int count)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  98. {
  99. return base.BeginWrite(buffer, offset, count, callback, state);
  100. }
  101. public override void EndWrite(IAsyncResult asyncResult)
  102. {
  103. base.EndWrite(asyncResult);
  104. }
  105. internal bool Closed => _closed;
  106. protected override void Dispose(bool disposing)
  107. {
  108. _closed = true;
  109. base.Dispose(disposing);
  110. }
  111. }
  112. }