ChunkStream.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. internal 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. : this (headers)
  69. {
  70. Write (buffer, offset, size);
  71. }
  72. public ChunkStream (WebHeaderCollection headers)
  73. {
  74. this.headers = headers;
  75. saved = new StringBuilder ();
  76. chunks = new ArrayList ();
  77. chunkSize = -1;
  78. }
  79. public void ResetBuffer ()
  80. {
  81. chunkSize = -1;
  82. chunkRead = 0;
  83. chunks.Clear ();
  84. }
  85. public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
  86. {
  87. if (offset + read > 0)
  88. Write (buffer, offset, offset+read);
  89. read = Read (buffer, offset, size);
  90. }
  91. public int Read (byte [] buffer, int offset, int size)
  92. {
  93. return ReadFromChunks (buffer, offset, size);
  94. }
  95. int ReadFromChunks (byte [] buffer, int offset, int size)
  96. {
  97. int count = chunks.Count;
  98. int nread = 0;
  99. for (int i = 0; i < count; i++) {
  100. Chunk chunk = (Chunk) chunks [i];
  101. if (chunk == null)
  102. continue;
  103. if (chunk.Offset == chunk.Bytes.Length) {
  104. chunks [i] = null;
  105. continue;
  106. }
  107. nread += chunk.Read (buffer, offset + nread, size - nread);
  108. if (nread == size)
  109. break;
  110. }
  111. return nread;
  112. }
  113. public void Write (byte [] buffer, int offset, int size)
  114. {
  115. InternalWrite (buffer, ref offset, size);
  116. }
  117. void InternalWrite (byte [] buffer, ref int offset, int size)
  118. {
  119. if (state == State.None) {
  120. state = GetChunkSize (buffer, ref offset, size);
  121. if (state == State.None)
  122. return;
  123. saved.Length = 0;
  124. sawCR = false;
  125. gotit = false;
  126. }
  127. if (state == State.Body && offset < size) {
  128. state = ReadBody (buffer, ref offset, size);
  129. if (state == State.Body)
  130. return;
  131. }
  132. if (state == State.BodyFinished && offset < size) {
  133. state = ReadCRLF (buffer, ref offset, size);
  134. if (state == State.BodyFinished)
  135. return;
  136. sawCR = false;
  137. }
  138. if (state == State.Trailer && offset < size) {
  139. state = ReadTrailer (buffer, ref offset, size);
  140. if (state == State.Trailer)
  141. return;
  142. saved.Length = 0;
  143. sawCR = false;
  144. gotit = false;
  145. }
  146. if (offset < size)
  147. InternalWrite (buffer, ref offset, size);
  148. }
  149. public bool WantMore {
  150. get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
  151. }
  152. public int ChunkLeft {
  153. get { return chunkSize - chunkRead; }
  154. }
  155. State ReadBody (byte [] buffer, ref int offset, int size)
  156. {
  157. if (chunkSize == 0)
  158. return State.BodyFinished;
  159. int diff = size - offset;
  160. if (diff + chunkRead > chunkSize)
  161. diff = chunkSize - chunkRead;
  162. byte [] chunk = new byte [diff];
  163. Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
  164. chunks.Add (new Chunk (chunk));
  165. offset += diff;
  166. chunkRead += diff;
  167. return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
  168. }
  169. State GetChunkSize (byte [] buffer, ref int offset, int size)
  170. {
  171. char c = '\0';
  172. while (offset < size) {
  173. c = (char) buffer [offset++];
  174. if (c == '\r') {
  175. if (sawCR)
  176. ThrowProtocolViolation ("2 CR found");
  177. sawCR = true;
  178. continue;
  179. }
  180. if (sawCR && c == '\n')
  181. break;
  182. if (c == ' ')
  183. gotit = true;
  184. if (!gotit)
  185. saved.Append (c);
  186. if (saved.Length > 20)
  187. ThrowProtocolViolation ("chunk size too long.");
  188. }
  189. if (!sawCR || c != '\n') {
  190. if (offset < size)
  191. ThrowProtocolViolation ("Missing \\n");
  192. try {
  193. if (saved.Length > 0)
  194. chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
  195. } catch (Exception) {
  196. ThrowProtocolViolation ("Cannot parse chunk size.");
  197. }
  198. return State.None;
  199. }
  200. chunkRead = 0;
  201. try {
  202. chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
  203. } catch (Exception) {
  204. ThrowProtocolViolation ("Cannot parse chunk size.");
  205. }
  206. if (chunkSize == 0)
  207. return State.Trailer;
  208. return State.Body;
  209. }
  210. State ReadCRLF (byte [] buffer, ref int offset, int size)
  211. {
  212. if (!sawCR) {
  213. if ((char) buffer [offset++] != '\r')
  214. ThrowProtocolViolation ("Expecting \\r");
  215. sawCR = true;
  216. if (offset == size)
  217. return State.BodyFinished;
  218. }
  219. if (sawCR && (char) buffer [offset++] != '\n')
  220. ThrowProtocolViolation ("Expecting \\n");
  221. return State.None;
  222. }
  223. State ReadTrailer (byte [] buffer, ref int offset, int size)
  224. {
  225. char c = '\0';
  226. // short path
  227. if ((char) buffer [offset] == '\r') {
  228. offset++;
  229. if ((char) buffer [offset] == '\n') {
  230. offset++;
  231. return State.None;
  232. }
  233. offset--;
  234. }
  235. int st = 0;
  236. string stString = "\r\n\r";
  237. while (offset < size && st < 4) {
  238. c = (char) buffer [offset++];
  239. if ((st == 0 || st == 2) && c == '\r') {
  240. st++;
  241. continue;
  242. }
  243. if ((st == 1 || st == 3) && c == '\n') {
  244. st++;
  245. continue;
  246. }
  247. if (st > 0) {
  248. saved.Append (stString.Substring (0, st));
  249. st = 0;
  250. }
  251. }
  252. if (st < 4) {
  253. if (offset < size)
  254. ThrowProtocolViolation ("Error reading trailer.");
  255. return State.Trailer;
  256. }
  257. StringReader reader = new StringReader (saved.ToString ());
  258. string line;
  259. while ((line = reader.ReadLine ()) != null && line != "")
  260. headers.Add (line);
  261. return State.None;
  262. }
  263. static void ThrowProtocolViolation (string message)
  264. {
  265. WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
  266. throw we;
  267. }
  268. }
  269. }