//
using System.Collections;
+using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
{
enum State {
None,
+ PartialSize,
Body,
BodyFinished,
Trailer
internal WebHeaderCollection headers;
int chunkSize;
int chunkRead;
+ int totalWritten;
State state;
//byte [] waitBuffer;
StringBuilder saved;
saved = new StringBuilder ();
chunks = new ArrayList ();
chunkSize = -1;
+ totalWritten = 0;
}
public void ResetBuffer ()
{
chunkSize = -1;
chunkRead = 0;
+ totalWritten = 0;
chunks.Clear ();
}
{
int count = chunks.Count;
int nread = 0;
+
+ var chunksForRemoving = new List<Chunk>(count);
for (int i = 0; i < count; i++) {
Chunk chunk = (Chunk) chunks [i];
- if (chunk == null)
- continue;
if (chunk.Offset == chunk.Bytes.Length) {
- chunks [i] = null;
+ chunksForRemoving.Add(chunk);
continue;
}
break;
}
+ foreach (var chunk in chunksForRemoving)
+ chunks.Remove(chunk);
+
return nread;
}
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)
{
- if (state == State.None) {
+ if (state == State.None || state == State.PartialSize) {
state = GetChunkSize (buffer, ref offset, size);
- if (state == State.None)
+ if (state == State.PartialSize)
return;
saved.Length = 0;
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 TotalDataSize {
+ get { return totalWritten; }
+ }
+
public int ChunkLeft {
get { return chunkSize - chunkRead; }
}
chunks.Add (new Chunk (chunk));
offset += diff;
chunkRead += diff;
+ totalWritten += diff;
return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
}
State GetChunkSize (byte [] buffer, ref int offset, int size)
{
+ chunkRead = 0;
+ chunkSize = 0;
char c = '\0';
while (offset < size) {
c = (char) buffer [offset++];
ThrowProtocolViolation ("Missing \\n");
try {
- if (saved.Length > 0)
- chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
+ if (saved.Length > 0) {
+ chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
+ }
} catch (Exception) {
ThrowProtocolViolation ("Cannot parse chunk size.");
}
- return State.None;
+ return State.PartialSize;
}
chunkRead = 0;
try {
- chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
+ 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) {
// short path
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;
}
if (st > 0) {
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) {
trailerState = st;
- if (offset < size)
+ if (offset < size)
ThrowProtocolViolation ("Error reading trailer.");
return State.Trailer;