ChunkStream.cs 7.2 KB

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