ChunkStream.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // System.Net.ChunkStream
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.Collections;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Text;
  13. namespace System.Net
  14. {
  15. class ChunkStream
  16. {
  17. enum State {
  18. None,
  19. Body,
  20. BodyFinished,
  21. Trailer
  22. }
  23. class Chunk {
  24. public byte [] Bytes;
  25. public int Offset;
  26. public Chunk (byte [] chunk)
  27. {
  28. this.Bytes = chunk;
  29. }
  30. public int Read (byte [] buffer, int offset, int size)
  31. {
  32. int nread = (size > Bytes.Length - Offset) ? Bytes.Length - Offset : size;
  33. Buffer.BlockCopy (Bytes, Offset, buffer, offset, nread);
  34. Offset += nread;
  35. return nread;
  36. }
  37. }
  38. WebHeaderCollection headers;
  39. int chunkSize;
  40. int chunkRead;
  41. State state;
  42. //byte [] waitBuffer;
  43. StringBuilder saved;
  44. bool sawCR;
  45. bool gotit;
  46. ArrayList chunks;
  47. public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
  48. {
  49. this.headers = headers;
  50. saved = new StringBuilder ();
  51. chunks = new ArrayList ();
  52. chunkSize = -1;
  53. Write (buffer, offset, size);
  54. }
  55. public void ResetBuffer ()
  56. {
  57. chunkSize = -1;
  58. chunkRead = 0;
  59. chunks.Clear ();
  60. }
  61. public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
  62. {
  63. Write (buffer, offset, offset+read);
  64. read = Read (buffer, offset, size);
  65. }
  66. public int Read (byte [] buffer, int offset, int size)
  67. {
  68. return ReadFromChunks (buffer, offset, size);
  69. }
  70. int ReadFromChunks (byte [] buffer, int offset, int size)
  71. {
  72. int count = chunks.Count;
  73. int nread = 0;
  74. for (int i = 0; i < count; i++) {
  75. Chunk chunk = (Chunk) chunks [i];
  76. if (chunk == null)
  77. continue;
  78. if (chunk.Offset == chunk.Bytes.Length) {
  79. chunks [i] = null;
  80. continue;
  81. }
  82. nread += chunk.Read (buffer, offset + nread, size - nread);
  83. if (nread == size)
  84. break;
  85. }
  86. return nread;
  87. }
  88. public void Write (byte [] buffer, int offset, int size)
  89. {
  90. InternalWrite (buffer, ref offset, size);
  91. }
  92. void InternalWrite (byte [] buffer, ref int offset, int size)
  93. {
  94. if (state == State.None) {
  95. state = GetChunkSize (buffer, ref offset, size);
  96. if (state == State.None)
  97. return;
  98. saved.Length = 0;
  99. sawCR = false;
  100. gotit = false;
  101. }
  102. if (state == State.Body && offset < size) {
  103. state = ReadBody (buffer, ref offset, size);
  104. if (state == State.Body)
  105. return;
  106. }
  107. if (state == State.BodyFinished && offset < size) {
  108. state = ReadCRLF (buffer, ref offset, size);
  109. if (state == State.BodyFinished)
  110. return;
  111. sawCR = false;
  112. }
  113. if (state == State.Trailer && offset < size) {
  114. state = ReadTrailer (buffer, ref offset, size);
  115. if (state == State.Trailer)
  116. return;
  117. saved.Length = 0;
  118. sawCR = false;
  119. gotit = false;
  120. }
  121. if (offset < size)
  122. InternalWrite (buffer, ref offset, size);
  123. }
  124. public bool WantMore {
  125. get { return (chunkRead != chunkSize || chunkSize != 0); }
  126. }
  127. State ReadBody (byte [] buffer, ref int offset, int size)
  128. {
  129. if (chunkSize == 0)
  130. return State.BodyFinished;
  131. int diff = size - offset;
  132. if (diff + chunkRead > chunkSize)
  133. diff = chunkSize - chunkRead;
  134. byte [] chunk = new byte [diff];
  135. Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
  136. chunks.Add (new Chunk (chunk));
  137. offset += diff;
  138. chunkRead += diff;
  139. return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
  140. }
  141. State GetChunkSize (byte [] buffer, ref int offset, int size)
  142. {
  143. char c = '\0';
  144. while (offset < size) {
  145. c = (char) buffer [offset++];
  146. if (c == '\r') {
  147. if (sawCR)
  148. throw new ProtocolViolationException ("2 CR found");
  149. sawCR = true;
  150. continue;
  151. }
  152. if (sawCR && c == '\n')
  153. break;
  154. if (c == ' ')
  155. gotit = true;
  156. if (!gotit)
  157. saved.Append (c);
  158. }
  159. if (!sawCR || c != '\n')
  160. return State.None;
  161. chunkRead = 0;
  162. chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
  163. if (chunkSize == 0)
  164. return State.Trailer;
  165. return State.Body;
  166. }
  167. State ReadCRLF (byte [] buffer, ref int offset, int size)
  168. {
  169. if (!sawCR) {
  170. if ((char) buffer [offset++] != '\r')
  171. throw new ProtocolViolationException ("Expecting \\r");
  172. sawCR = true;
  173. if (offset == size)
  174. return State.BodyFinished;
  175. }
  176. if ((char) buffer [offset++] != '\n')
  177. throw new ProtocolViolationException ("Expecting \\n");
  178. return State.None;
  179. }
  180. State ReadTrailer (byte [] buffer, ref int offset, int size)
  181. {
  182. char c = '\0';
  183. // short path
  184. if ((char) buffer [offset] == '\r') {
  185. offset++;
  186. if ((char) buffer [offset] == '\n') {
  187. offset++;
  188. return State.None;
  189. }
  190. offset--;
  191. }
  192. int st = 0;
  193. string stString = "\r\n\r";
  194. while (offset < size && st < 4) {
  195. c = (char) buffer [offset++];
  196. if ((st == 0 || st == 2) && c == '\r') {
  197. st++;
  198. continue;
  199. }
  200. if ((st == 1 || st == 3) && c == '\n') {
  201. st++;
  202. continue;
  203. }
  204. if (st > 0) {
  205. saved.Append (stString.Substring (0, st));
  206. st = 0;
  207. }
  208. }
  209. if (st < 4)
  210. return State.Trailer;
  211. StringReader reader = new StringReader (saved.ToString ());
  212. string line;
  213. while ((line = reader.ReadLine ()) != null && line != "")
  214. headers.Add (line);
  215. return State.None;
  216. }
  217. }
  218. }