1 #define EMBEDDED_IN_1_0
4 // System.Net.ChunkStream
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (C) 2003 Ximian, Inc (http://www.ximian.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Globalization;
38 using System; using System.Net; namespace MonoHttp
53 public Chunk (byte [] chunk)
58 public int Read (byte [] buffer, int offset, int size)
60 int nread = (size > Bytes.Length - Offset) ? Bytes.Length - Offset : size;
61 Buffer.BlockCopy (Bytes, Offset, buffer, offset, nread);
67 internal WebHeaderCollection headers;
78 public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
81 Write (buffer, offset, size);
84 public ChunkStream (WebHeaderCollection headers)
86 this.headers = headers;
87 saved = new StringBuilder ();
88 chunks = new ArrayList ();
92 public void ResetBuffer ()
99 public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
101 if (offset + read > 0)
102 Write (buffer, offset, offset+read);
103 read = Read (buffer, offset, size);
106 public int Read (byte [] buffer, int offset, int size)
108 return ReadFromChunks (buffer, offset, size);
111 int ReadFromChunks (byte [] buffer, int offset, int size)
113 int count = chunks.Count;
115 for (int i = 0; i < count; i++) {
116 Chunk chunk = (Chunk) chunks [i];
120 if (chunk.Offset == chunk.Bytes.Length) {
125 nread += chunk.Read (buffer, offset + nread, size - nread);
133 public void Write (byte [] buffer, int offset, int size)
135 InternalWrite (buffer, ref offset, size);
138 void InternalWrite (byte [] buffer, ref int offset, int size)
140 if (state == State.None) {
141 state = GetChunkSize (buffer, ref offset, size);
142 if (state == State.None)
150 if (state == State.Body && offset < size) {
151 state = ReadBody (buffer, ref offset, size);
152 if (state == State.Body)
156 if (state == State.BodyFinished && offset < size) {
157 state = ReadCRLF (buffer, ref offset, size);
158 if (state == State.BodyFinished)
164 if (state == State.Trailer && offset < size) {
165 state = ReadTrailer (buffer, ref offset, size);
166 if (state == State.Trailer)
175 InternalWrite (buffer, ref offset, size);
178 public bool WantMore {
179 get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
182 public int ChunkLeft {
183 get { return chunkSize - chunkRead; }
186 State ReadBody (byte [] buffer, ref int offset, int size)
189 return State.BodyFinished;
191 int diff = size - offset;
192 if (diff + chunkRead > chunkSize)
193 diff = chunkSize - chunkRead;
195 byte [] chunk = new byte [diff];
196 Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
197 chunks.Add (new Chunk (chunk));
200 return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
204 State GetChunkSize (byte [] buffer, ref int offset, int size)
207 while (offset < size) {
208 c = (char) buffer [offset++];
211 ThrowProtocolViolation ("2 CR found");
217 if (sawCR && c == '\n')
226 if (saved.Length > 20)
227 ThrowProtocolViolation ("chunk size too long.");
230 if (!sawCR || c != '\n') {
232 ThrowProtocolViolation ("Missing \\n");
235 if (saved.Length > 0)
236 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
237 } catch (Exception) {
238 ThrowProtocolViolation ("Cannot parse chunk size.");
246 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
247 } catch (Exception) {
248 ThrowProtocolViolation ("Cannot parse chunk size.");
251 if (chunkSize == 0) {
253 return State.Trailer;
259 State ReadCRLF (byte [] buffer, ref int offset, int size)
262 if ((char) buffer [offset++] != '\r')
263 ThrowProtocolViolation ("Expecting \\r");
267 return State.BodyFinished;
270 if (sawCR && (char) buffer [offset++] != '\n')
271 ThrowProtocolViolation ("Expecting \\n");
276 State ReadTrailer (byte [] buffer, ref int offset, int size)
281 if (trailerState == 2 && (char) buffer [offset] == '\r' && saved.Length == 0) {
283 if (offset < size && (char) buffer [offset] == '\n') {
290 int st = trailerState;
291 string stString = "\r\n\r";
292 while (offset < size && st < 4) {
293 c = (char) buffer [offset++];
294 if ((st == 0 || st == 2) && c == '\r') {
299 if ((st == 1 || st == 3) && c == '\n') {
305 saved.Append (stString.Substring (0, saved.Length == 0? st-2: st));
313 ThrowProtocolViolation ("Error reading trailer.");
315 return State.Trailer;
318 StringReader reader = new StringReader (saved.ToString ());
320 while ((line = reader.ReadLine ()) != null && line != "")
326 static void ThrowProtocolViolation (string message)
328 WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);