}
}
- WebHeaderCollection headers;
+ internal WebHeaderCollection headers;
int chunkSize;
int chunkRead;
State state;
StringBuilder saved;
bool sawCR;
bool gotit;
+ int trailerState;
ArrayList chunks;
public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
+ : this (headers)
+ {
+ Write (buffer, offset, size);
+ }
+
+ public ChunkStream (WebHeaderCollection headers)
{
this.headers = headers;
saved = new StringBuilder ();
chunks = new ArrayList ();
chunkSize = -1;
- Write (buffer, offset, size);
}
public void ResetBuffer ()
public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
{
- Write (buffer, offset, offset+read);
+ if (offset + read > 0)
+ Write (buffer, offset, offset+read);
read = Read (buffer, offset, size);
}
public void Write (byte [] buffer, int offset, int size)
{
- InternalWrite (buffer, ref offset, size);
+ if (offset < size)
+ InternalWrite (buffer, ref offset, size);
}
void InternalWrite (byte [] buffer, ref int offset, int size)
get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
}
+ public bool DataAvailable {
+ get {
+ int count = chunks.Count;
+ for (int i = 0; i < count; i++) {
+ Chunk ch = (Chunk) chunks [i];
+ if (ch == null || ch.Bytes == null)
+ continue;
+ if (ch.Bytes.Length > 0 && ch.Offset < ch.Bytes.Length)
+ return (state != State.Body);
+ }
+ return false;
+ }
+ }
+
public int ChunkLeft {
get { return chunkSize - chunkRead; }
}
State GetChunkSize (byte [] buffer, ref int offset, int size)
{
+ chunkRead = 0;
+ chunkSize = 0;
char c = '\0';
while (offset < size) {
c = (char) buffer [offset++];
if (c == '\r') {
if (sawCR)
- throw new ProtocolViolationException ("2 CR found");
+ ThrowProtocolViolation ("2 CR found");
sawCR = true;
continue;
if (!gotit)
saved.Append (c);
+
+ if (saved.Length > 20)
+ ThrowProtocolViolation ("chunk size too long.");
}
- if (!sawCR || c != '\n')
+ if (!sawCR || c != '\n') {
+ if (offset < size)
+ ThrowProtocolViolation ("Missing \\n");
+
+ try {
+ if (saved.Length > 0) {
+ chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
+ }
+ } catch (Exception) {
+ ThrowProtocolViolation ("Cannot parse chunk size.");
+ }
+
return State.None;
+ }
chunkRead = 0;
- chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
- if (chunkSize == 0)
+ try {
+ chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
+ } catch (Exception) {
+ ThrowProtocolViolation ("Cannot parse chunk size.");
+ }
+
+ if (chunkSize == 0) {
+ trailerState = 2;
return State.Trailer;
+ }
return State.Body;
}
+ static string RemoveChunkExtension (string input)
+ {
+ int idx = input.IndexOf (';');
+ if (idx == -1)
+ return input;
+ return input.Substring (0, idx);
+ }
+
State ReadCRLF (byte [] buffer, ref int offset, int size)
{
if (!sawCR) {
if ((char) buffer [offset++] != '\r')
- throw new ProtocolViolationException ("Expecting \\r");
+ ThrowProtocolViolation ("Expecting \\r");
sawCR = true;
if (offset == size)
return State.BodyFinished;
}
- if ((char) buffer [offset++] != '\n')
- throw new ProtocolViolationException ("Expecting \\n");
+ if (sawCR && (char) buffer [offset++] != '\n')
+ ThrowProtocolViolation ("Expecting \\n");
return State.None;
}
char c = '\0';
// short path
- if ((char) buffer [offset] == '\r') {
+ if (trailerState == 2 && (char) buffer [offset] == '\r' && saved.Length == 0) {
offset++;
- if ((char) buffer [offset] == '\n') {
+ if (offset < size && (char) buffer [offset] == '\n') {
offset++;
return State.None;
}
offset--;
}
- int st = 0;
+ int st = trailerState;
string stString = "\r\n\r";
while (offset < size && st < 4) {
c = (char) buffer [offset++];
}
if (st > 0) {
- saved.Append (stString.Substring (0, st));
+ saved.Append (stString.Substring (0, saved.Length == 0? st-2: st));
st = 0;
+ if (saved.Length > 4196)
+ ThrowProtocolViolation ("Error reading trailer (too long).");
}
}
- if (st < 4)
+ if (st < 4) {
+ trailerState = st;
+ if (offset < size)
+ ThrowProtocolViolation ("Error reading trailer.");
+
return State.Trailer;
+ }
StringReader reader = new StringReader (saved.ToString ());
string line;
return State.None;
}
+
+ static void ThrowProtocolViolation (string message)
+ {
+ WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
+ throw we;
+ }
}
}