Refactored a few for ChunkStream.cs
This commit is contained in:
parent
d959e0f437
commit
f7ca668c02
@ -53,10 +53,10 @@ namespace WebSocketSharp.Net
|
|||||||
private int _chunkRead;
|
private int _chunkRead;
|
||||||
private int _chunkSize;
|
private int _chunkSize;
|
||||||
private List<Chunk> _chunks;
|
private List<Chunk> _chunks;
|
||||||
private bool _gotit;
|
private bool _gotIt;
|
||||||
private WebHeaderCollection _headers;
|
private WebHeaderCollection _headers;
|
||||||
private StringBuilder _saved;
|
private StringBuilder _saved;
|
||||||
private bool _sawCR;
|
private bool _sawCr;
|
||||||
private InputChunkState _state;
|
private InputChunkState _state;
|
||||||
private int _trailerState;
|
private int _trailerState;
|
||||||
|
|
||||||
@ -64,12 +64,6 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#region Public Constructors
|
#region Public Constructors
|
||||||
|
|
||||||
public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
|
|
||||||
: this (headers)
|
|
||||||
{
|
|
||||||
Write (buffer, offset, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChunkStream (WebHeaderCollection headers)
|
public ChunkStream (WebHeaderCollection headers)
|
||||||
{
|
{
|
||||||
_headers = headers;
|
_headers = headers;
|
||||||
@ -78,6 +72,12 @@ namespace WebSocketSharp.Net
|
|||||||
_saved = new StringBuilder ();
|
_saved = new StringBuilder ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChunkStream (byte[] buffer, int offset, int count, WebHeaderCollection headers)
|
||||||
|
: this (headers)
|
||||||
|
{
|
||||||
|
Write (buffer, offset, count);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Internal Properties
|
#region Internal Properties
|
||||||
@ -108,53 +108,53 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
|
||||||
private InputChunkState readCRLF (byte [] buffer, ref int offset, int size)
|
private InputChunkState readCrLfFrom (byte[] buffer, ref int offset, int count)
|
||||||
{
|
{
|
||||||
if (!_sawCR) {
|
if (!_sawCr) {
|
||||||
if ((char) buffer [offset++] != '\r')
|
if ((char) buffer[offset++] != '\r')
|
||||||
throwProtocolViolation ("Expecting \\r.");
|
throwProtocolViolation ("Expecting \\r.");
|
||||||
|
|
||||||
_sawCR = true;
|
_sawCr = true;
|
||||||
if (offset == size)
|
if (offset == count)
|
||||||
return InputChunkState.BodyFinished;
|
return InputChunkState.BodyFinished;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((char) buffer [offset++] != '\n')
|
if ((char) buffer[offset++] != '\n')
|
||||||
throwProtocolViolation ("Expecting \\n.");
|
throwProtocolViolation ("Expecting \\n.");
|
||||||
|
|
||||||
return InputChunkState.None;
|
return InputChunkState.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int readFromChunks (byte [] buffer, int offset, int size)
|
private int readFromChunks (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
var count = _chunks.Count;
|
var cnt = _chunks.Count;
|
||||||
var nread = 0;
|
var nread = 0;
|
||||||
for (int i = 0; i < count; i++) {
|
for (var i = 0; i < cnt; i++) {
|
||||||
var chunk = _chunks [i];
|
var chunk = _chunks[i];
|
||||||
if (chunk == null)
|
if (chunk == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (chunk.ReadLeft == 0) {
|
if (chunk.ReadLeft == 0) {
|
||||||
_chunks [i] = null;
|
_chunks[i] = null;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
nread += chunk.Read (buffer, offset + nread, size - nread);
|
nread += chunk.Read (buffer, offset + nread, count - nread);
|
||||||
if (nread == size)
|
if (nread == count)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nread;
|
return nread;
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputChunkState readTrailer (byte [] buffer, ref int offset, int size)
|
private InputChunkState readTrailerFrom (byte[] buffer, ref int offset, int count)
|
||||||
{
|
{
|
||||||
var c = '\0';
|
var c = '\0';
|
||||||
|
|
||||||
// Short path
|
// Short path.
|
||||||
if (_trailerState == 2 && (char) buffer [offset] == '\r' && _saved.Length == 0) {
|
if (_trailerState == 2 && (char) buffer[offset] == '\r' && _saved.Length == 0) {
|
||||||
offset++;
|
offset++;
|
||||||
if (offset < size && (char) buffer [offset] == '\n') {
|
if (offset < count && (char) buffer[offset] == '\n') {
|
||||||
offset++;
|
offset++;
|
||||||
return InputChunkState.None;
|
return InputChunkState.None;
|
||||||
}
|
}
|
||||||
@ -162,31 +162,31 @@ namespace WebSocketSharp.Net
|
|||||||
offset--;
|
offset--;
|
||||||
}
|
}
|
||||||
|
|
||||||
var st = _trailerState;
|
var state = _trailerState;
|
||||||
var stString = "\r\n\r";
|
var stateStr = "\r\n\r";
|
||||||
while (offset < size && st < 4) {
|
while (offset < count && state < 4) {
|
||||||
c = (char) buffer [offset++];
|
c = (char) buffer[offset++];
|
||||||
if ((st == 0 || st == 2) && c == '\r') {
|
if ((state == 0 || state == 2) && c == '\r') {
|
||||||
st++;
|
state++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((st == 1 || st == 3) && c == '\n') {
|
if ((state == 1 || state == 3) && c == '\n') {
|
||||||
st++;
|
state++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st > 0) {
|
if (state > 0) {
|
||||||
_saved.Append (stString.Substring (0, _saved.Length == 0 ? st - 2 : st));
|
_saved.Append (stateStr.Substring (0, _saved.Length == 0 ? state - 2 : state));
|
||||||
st = 0;
|
state = 0;
|
||||||
if (_saved.Length > 4196)
|
if (_saved.Length > 4196)
|
||||||
throwProtocolViolation ("Error reading trailer (too long).");
|
throwProtocolViolation ("Error reading trailer (too long).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st < 4) {
|
if (state < 4) {
|
||||||
_trailerState = st;
|
_trailerState = state;
|
||||||
if (offset < size)
|
if (offset < count)
|
||||||
throwProtocolViolation ("Error reading trailer.");
|
throwProtocolViolation ("Error reading trailer.");
|
||||||
|
|
||||||
return InputChunkState.Trailer;
|
return InputChunkState.Trailer;
|
||||||
@ -200,42 +200,40 @@ namespace WebSocketSharp.Net
|
|||||||
return InputChunkState.None;
|
return InputChunkState.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string removeChunkExtension (string input)
|
private static string removeChunkExtension (string value)
|
||||||
{
|
{
|
||||||
var idx = input.IndexOf (';');
|
var idx = value.IndexOf (';');
|
||||||
return idx > -1
|
return idx > -1 ? value.Substring (0, idx) : value;
|
||||||
? input.Substring (0, idx)
|
|
||||||
: input;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputChunkState setChunkSize (byte [] buffer, ref int offset, int size)
|
private InputChunkState setChunkSize (byte[] buffer, ref int offset, int count)
|
||||||
{
|
{
|
||||||
var c = '\0';
|
var c = '\0';
|
||||||
while (offset < size) {
|
while (offset < count) {
|
||||||
c = (char) buffer [offset++];
|
c = (char) buffer[offset++];
|
||||||
if (c == '\r') {
|
if (c == '\r') {
|
||||||
if (_sawCR)
|
if (_sawCr)
|
||||||
throwProtocolViolation ("2 CR found.");
|
throwProtocolViolation ("2 CR found.");
|
||||||
|
|
||||||
_sawCR = true;
|
_sawCr = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_sawCR && c == '\n')
|
if (_sawCr && c == '\n')
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (c == ' ')
|
if (c == ' ')
|
||||||
_gotit = true;
|
_gotIt = true;
|
||||||
|
|
||||||
if (!_gotit)
|
if (!_gotIt)
|
||||||
_saved.Append (c);
|
_saved.Append (c);
|
||||||
|
|
||||||
if (_saved.Length > 20)
|
if (_saved.Length > 20)
|
||||||
throwProtocolViolation ("Chunk size too long.");
|
throwProtocolViolation ("Chunk size too long.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_sawCR || c != '\n') {
|
if (!_sawCr || c != '\n') {
|
||||||
if (offset < size)
|
if (offset < count)
|
||||||
throwProtocolViolation ("Missing \\n.");
|
throwProtocolViolation ("Missing \\n.");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -269,78 +267,75 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private static void throwProtocolViolation (string message)
|
private static void throwProtocolViolation (string message)
|
||||||
{
|
{
|
||||||
var ex = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
|
throw new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void write (byte [] buffer, ref int offset, int size)
|
private void write (byte[] buffer, ref int offset, int count)
|
||||||
{
|
{
|
||||||
if (_state == InputChunkState.None) {
|
if (_state == InputChunkState.None) {
|
||||||
_state = setChunkSize (buffer, ref offset, size);
|
_state = setChunkSize (buffer, ref offset, count);
|
||||||
if (_state == InputChunkState.None)
|
if (_state == InputChunkState.None)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_saved.Length = 0;
|
_saved.Length = 0;
|
||||||
_sawCR = false;
|
_sawCr = false;
|
||||||
_gotit = false;
|
_gotIt = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_state == InputChunkState.Body && offset < size) {
|
if (_state == InputChunkState.Body && offset < count) {
|
||||||
_state = writeBody (buffer, ref offset, size);
|
_state = writeBody (buffer, ref offset, count);
|
||||||
if (_state == InputChunkState.Body)
|
if (_state == InputChunkState.Body)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_state == InputChunkState.BodyFinished && offset < size) {
|
if (_state == InputChunkState.BodyFinished && offset < count) {
|
||||||
_state = readCRLF (buffer, ref offset, size);
|
_state = readCrLfFrom (buffer, ref offset, count);
|
||||||
if (_state == InputChunkState.BodyFinished)
|
if (_state == InputChunkState.BodyFinished)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_sawCR = false;
|
_sawCr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_state == InputChunkState.Trailer && offset < size) {
|
if (_state == InputChunkState.Trailer && offset < count) {
|
||||||
_state = readTrailer (buffer, ref offset, size);
|
_state = readTrailerFrom (buffer, ref offset, count);
|
||||||
if (_state == InputChunkState.Trailer)
|
if (_state == InputChunkState.Trailer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_saved.Length = 0;
|
_saved.Length = 0;
|
||||||
_sawCR = false;
|
_sawCr = false;
|
||||||
_gotit = false;
|
_gotIt = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset < size)
|
if (offset < count)
|
||||||
write (buffer, ref offset, size);
|
write (buffer, ref offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputChunkState writeBody (byte [] buffer, ref int offset, int size)
|
private InputChunkState writeBody (byte[] buffer, ref int offset, int count)
|
||||||
{
|
{
|
||||||
if (_chunkSize == 0)
|
if (_chunkSize == 0)
|
||||||
return InputChunkState.BodyFinished;
|
return InputChunkState.BodyFinished;
|
||||||
|
|
||||||
var diff = size - offset;
|
var diff = count - offset;
|
||||||
if (diff + _chunkRead > _chunkSize)
|
if (diff + _chunkRead > _chunkSize)
|
||||||
diff = _chunkSize - _chunkRead;
|
diff = _chunkSize - _chunkRead;
|
||||||
|
|
||||||
var body = new byte [diff];
|
var body = new byte[diff];
|
||||||
Buffer.BlockCopy (buffer, offset, body, 0, diff);
|
Buffer.BlockCopy (buffer, offset, body, 0, diff);
|
||||||
_chunks.Add (new Chunk (body));
|
_chunks.Add (new Chunk (body));
|
||||||
|
|
||||||
offset += diff;
|
offset += diff;
|
||||||
_chunkRead += diff;
|
_chunkRead += diff;
|
||||||
|
|
||||||
return _chunkRead == _chunkSize
|
return _chunkRead == _chunkSize ? InputChunkState.BodyFinished : InputChunkState.Body;
|
||||||
? InputChunkState.BodyFinished
|
|
||||||
: InputChunkState.Body;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
|
|
||||||
public int Read (byte [] buffer, int offset, int size)
|
public int Read (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
return readFromChunks (buffer, offset, size);
|
return readFromChunks (buffer, offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetBuffer ()
|
public void ResetBuffer ()
|
||||||
@ -350,17 +345,17 @@ namespace WebSocketSharp.Net
|
|||||||
_chunks.Clear ();
|
_chunks.Clear ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write (byte [] buffer, int offset, int size)
|
public void Write (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
write (buffer, ref offset, size);
|
write (buffer, ref offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
|
public void WriteAndReadBack (byte[] buffer, int offset, int count, ref int read)
|
||||||
{
|
{
|
||||||
if (offset + read > 0)
|
if (offset + read > 0)
|
||||||
Write (buffer, offset, offset + read);
|
Write (buffer, offset, offset + read);
|
||||||
|
|
||||||
read = readFromChunks (buffer, offset, size);
|
read = readFromChunks (buffer, offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user