ChunkStream.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Text;
  33. namespace System.Net
  34. {
  35. class ChunkStream
  36. {
  37. enum State {
  38. None,
  39. Body,
  40. BodyFinished,
  41. Trailer
  42. }
  43. class Chunk {
  44. public byte [] Bytes;
  45. public int Offset;
  46. public Chunk (byte [] chunk)
  47. {
  48. this.Bytes = chunk;
  49. }
  50. public int Read (byte [] buffer, int offset, int size)
  51. {
  52. int nread = (size > Bytes.Length - Offset) ? Bytes.Length - Offset : size;
  53. Buffer.BlockCopy (Bytes, Offset, buffer, offset, nread);
  54. Offset += nread;
  55. return nread;
  56. }
  57. }
  58. WebHeaderCollection headers;
  59. int chunkSize;
  60. int chunkRead;
  61. State state;
  62. //byte [] waitBuffer;
  63. StringBuilder saved;
  64. bool sawCR;
  65. bool gotit;
  66. ArrayList chunks;
  67. public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
  68. {
  69. this.headers = headers;
  70. saved = new StringBuilder ();
  71. chunks = new ArrayList ();
  72. chunkSize = -1;
  73. Write (buffer, offset, size);
  74. }
  75. public void ResetBuffer ()
  76. {
  77. chunkSize = -1;
  78. chunkRead = 0;
  79. chunks.Clear ();
  80. }
  81. public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
  82. {
  83. Write (buffer, offset, offset+read);
  84. read = Read (buffer, offset, size);
  85. }
  86. public int Read (byte [] buffer, int offset, int size)
  87. {
  88. return ReadFromChunks (buffer, offset, size);
  89. }
  90. int ReadFromChunks (byte [] buffer, int offset, int size)
  91. {
  92. int count = chunks.Count;
  93. int nread = 0;
  94. for (int i = 0; i < count; i++) {
  95. Chunk chunk = (Chunk) chunks [i];
  96. if (chunk == null)
  97. continue;
  98. if (chunk.Offset == chunk.Bytes.Length) {
  99. chunks [i] = null;
  100. continue;
  101. }
  102. nread += chunk.Read (buffer, offset + nread, size - nread);
  103. if (nread == size)
  104. break;
  105. }
  106. return nread;
  107. }
  108. public void Write (byte [] buffer, int offset, int size)
  109. {
  110. InternalWrite (buffer, ref offset, size);
  111. }
  112. void InternalWrite (byte [] buffer, ref int offset, int size)
  113. {
  114. if (state == State.None) {
  115. state = GetChunkSize (buffer, ref offset, size);
  116. if (state == State.None)
  117. return;
  118. saved.Length = 0;
  119. sawCR = false;
  120. gotit = false;
  121. }
  122. if (state == State.Body && offset < size) {
  123. state = ReadBody (buffer, ref offset, size);
  124. if (state == State.Body)
  125. return;
  126. }
  127. if (state == State.BodyFinished && offset < size) {
  128. state = ReadCRLF (buffer, ref offset, size);
  129. if (state == State.BodyFinished)
  130. return;
  131. sawCR = false;
  132. }
  133. if (state == State.Trailer && offset < size) {
  134. state = ReadTrailer (buffer, ref offset, size);
  135. if (state == State.Trailer)
  136. return;
  137. saved.Length = 0;
  138. sawCR = false;
  139. gotit = false;
  140. }
  141. if (offset < size)
  142. InternalWrite (buffer, ref offset, size);
  143. }
  144. public bool WantMore {
  145. get { return (chunkRead != chunkSize || chunkSize != 0); }
  146. }
  147. public int ChunkLeft {
  148. get { return chunkSize - chunkRead; }
  149. }
  150. State ReadBody (byte [] buffer, ref int offset, int size)
  151. {
  152. if (chunkSize == 0)
  153. return State.BodyFinished;
  154. int diff = size - offset;
  155. if (diff + chunkRead > chunkSize)
  156. diff = chunkSize - chunkRead;
  157. byte [] chunk = new byte [diff];
  158. Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
  159. chunks.Add (new Chunk (chunk));
  160. offset += diff;
  161. chunkRead += diff;
  162. return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
  163. }
  164. State GetChunkSize (byte [] buffer, ref int offset, int size)
  165. {
  166. char c = '\0';
  167. while (offset < size) {
  168. c = (char) buffer [offset++];
  169. if (c == '\r') {
  170. if (sawCR)
  171. throw new ProtocolViolationException ("2 CR found");
  172. sawCR = true;
  173. continue;
  174. }
  175. if (sawCR && c == '\n')
  176. break;
  177. if (c == ' ')
  178. gotit = true;
  179. if (!gotit)
  180. saved.Append (c);
  181. }
  182. if (!sawCR || c != '\n')
  183. return State.None;
  184. chunkRead = 0;
  185. chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
  186. if (chunkSize == 0)
  187. return State.Trailer;
  188. return State.Body;
  189. }
  190. State ReadCRLF (byte [] buffer, ref int offset, int size)
  191. {
  192. if (!sawCR) {
  193. if ((char) buffer [offset++] != '\r')
  194. throw new ProtocolViolationException ("Expecting \\r");
  195. sawCR = true;
  196. if (offset == size)
  197. return State.BodyFinished;
  198. }
  199. if ((char) buffer [offset++] != '\n')
  200. throw new ProtocolViolationException ("Expecting \\n");
  201. return State.None;
  202. }
  203. State ReadTrailer (byte [] buffer, ref int offset, int size)
  204. {
  205. char c = '\0';
  206. // short path
  207. if ((char) buffer [offset] == '\r') {
  208. offset++;
  209. if ((char) buffer [offset] == '\n') {
  210. offset++;
  211. return State.None;
  212. }
  213. offset--;
  214. }
  215. int st = 0;
  216. string stString = "\r\n\r";
  217. while (offset < size && st < 4) {
  218. c = (char) buffer [offset++];
  219. if ((st == 0 || st == 2) && c == '\r') {
  220. st++;
  221. continue;
  222. }
  223. if ((st == 1 || st == 3) && c == '\n') {
  224. st++;
  225. continue;
  226. }
  227. if (st > 0) {
  228. saved.Append (stString.Substring (0, st));
  229. st = 0;
  230. }
  231. }
  232. if (st < 4)
  233. return State.Trailer;
  234. StringReader reader = new StringReader (saved.ToString ());
  235. string line;
  236. while ((line = reader.ReadLine ()) != null && line != "")
  237. headers.Add (line);
  238. return State.None;
  239. }
  240. }
  241. }