ChunkedInputStream.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Net.ChunkedInputStream
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. #if NET_2_0
  28. using System.Net.Sockets;
  29. using System.Runtime.InteropServices;
  30. namespace System.Net {
  31. class ChunkedInputStream : RequestStream {
  32. bool disposed;
  33. ChunkStream decoder;
  34. HttpListenerContext context;
  35. bool no_more_data;
  36. class ReadBufferState {
  37. public byte [] Buffer;
  38. public int Offset;
  39. public int Count;
  40. public int InitialCount;
  41. public HttpStreamAsyncResult Ares;
  42. public ReadBufferState (byte [] buffer, int offset, int count,
  43. HttpStreamAsyncResult ares)
  44. {
  45. Buffer = buffer;
  46. Offset = offset;
  47. Count = count;
  48. InitialCount = count;
  49. Ares = ares;
  50. }
  51. }
  52. public ChunkedInputStream (HttpListenerContext context, Socket sock,
  53. byte [] buffer, int offset, int length)
  54. : base (sock, buffer, offset, length)
  55. {
  56. this.context = context;
  57. WebHeaderCollection coll = (WebHeaderCollection) context.Request.Headers;
  58. decoder = new ChunkStream (coll);
  59. }
  60. public ChunkStream Decoder {
  61. get { return decoder; }
  62. set { decoder = value; }
  63. }
  64. public override int Read ([In,Out] byte [] buffer, int offset, int count)
  65. {
  66. IAsyncResult ares = BeginRead (buffer, offset, count, null, null);
  67. return EndRead (ares);
  68. }
  69. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  70. AsyncCallback cback, object state)
  71. {
  72. if (disposed)
  73. throw new ObjectDisposedException (GetType ().ToString ());
  74. if (buffer == null)
  75. throw new ArgumentNullException ("buffer");
  76. int len = buffer.Length;
  77. if (offset < 0 || offset > len)
  78. throw new ArgumentOutOfRangeException ("offset exceeds the size of buffer");
  79. if (count < 0 || offset > len - count)
  80. throw new ArgumentOutOfRangeException ("offset+size exceeds the size of buffer");
  81. HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
  82. ares.Callback = cback;
  83. ares.State = state;
  84. if (no_more_data) {
  85. ares.Complete ();
  86. return ares;
  87. }
  88. ares.Buffer = new byte [8192];
  89. ares.Offset = 0;
  90. ares.Count = 8192;
  91. ReadBufferState rb = new ReadBufferState (buffer, offset, count, ares);
  92. base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
  93. return ares;
  94. }
  95. void OnRead (IAsyncResult base_ares)
  96. {
  97. ReadBufferState rb = (ReadBufferState) base_ares.AsyncState;
  98. HttpStreamAsyncResult ares = rb.Ares;
  99. try {
  100. int nread = base.EndRead (base_ares);
  101. decoder.Write (ares.Buffer, ares.Offset, nread);
  102. nread = decoder.Read (rb.Buffer, rb.Offset, rb.Count);
  103. rb.Offset += nread;
  104. rb.Count -= nread;
  105. if (rb.Count == 0 || !decoder.WantMore) {
  106. no_more_data = !decoder.WantMore;
  107. ares.Count = rb.InitialCount - rb.Count;
  108. ares.Complete ();
  109. return;
  110. }
  111. ares.Offset = 0;
  112. ares.Count = Math.Min (8192, decoder.ChunkLeft + 6);
  113. base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
  114. } catch (Exception e) {
  115. context.Connection.SendError (e.Message, 400);
  116. ares.Complete (e);
  117. }
  118. }
  119. public override int EndRead (IAsyncResult ares)
  120. {
  121. if (disposed)
  122. throw new ObjectDisposedException (GetType ().ToString ());
  123. HttpStreamAsyncResult my_ares = ares as HttpStreamAsyncResult;
  124. if (ares == null)
  125. throw new ArgumentException ("Invalid IAsyncResult", "ares");
  126. if (!ares.IsCompleted)
  127. ares.AsyncWaitHandle.WaitOne ();
  128. if (my_ares.Error != null)
  129. throw new HttpListenerException (400, "I/O operation aborted.");
  130. return my_ares.Count;
  131. }
  132. public override void Close ()
  133. {
  134. if (!disposed) {
  135. disposed = true;
  136. base.Close ();
  137. }
  138. }
  139. }
  140. }
  141. #endif