Refactored a few for ChunkStream.cs

This commit is contained in:
sta 2015-04-12 17:36:41 +09:00
parent b5aa5c4f06
commit 67f63654cd

View File

@ -108,19 +108,19 @@ namespace WebSocketSharp.Net
#region Private Methods #region Private Methods
private InputChunkState readCrLfFrom (byte[] buffer, ref int offset, int count) private InputChunkState readCrLfFrom (byte[] buffer, ref int offset, int length)
{ {
if (!_sawCr) { if (!_sawCr) {
if ((char) buffer[offset++] != '\r') if (buffer[offset++] != 13)
throwProtocolViolation ("Expecting \\r."); throwProtocolViolation ("CR is expected.");
_sawCr = true; _sawCr = true;
if (offset == count) if (offset == length)
return InputChunkState.BodyFinished; return InputChunkState.BodyFinished;
} }
if ((char) buffer[offset++] != '\n') if (buffer[offset++] != 10)
throwProtocolViolation ("Expecting \\n."); throwProtocolViolation ("LF is expected.");
return InputChunkState.None; return InputChunkState.None;
} }
@ -147,14 +147,12 @@ namespace WebSocketSharp.Net
return nread; return nread;
} }
private InputChunkState readTrailerFrom (byte[] buffer, ref int offset, int count) private InputChunkState readTrailerFrom (byte[] buffer, ref int offset, int length)
{ {
var c = '\0';
// Short path. // Short path.
if (_trailerState == 2 && (char) buffer[offset] == '\r' && _saved.Length == 0) { if (_trailerState == 2 && buffer[offset] == 13 && _saved.Length == 0) {
offset++; offset++;
if (offset < count && (char) buffer[offset] == '\n') { if (offset < length && buffer[offset] == 10) {
offset++; offset++;
return InputChunkState.None; return InputChunkState.None;
} }
@ -164,14 +162,14 @@ namespace WebSocketSharp.Net
var state = _trailerState; var state = _trailerState;
var stateStr = "\r\n\r"; var stateStr = "\r\n\r";
while (offset < count && state < 4) { while (offset < length && state < 4) {
c = (char) buffer[offset++]; var b = buffer[offset++];
if ((state == 0 || state == 2) && c == '\r') { if ((state == 0 || state == 2) && b == 13) {
state++; state++;
continue; continue;
} }
if ((state == 1 || state == 3) && c == '\n') { if ((state == 1 || state == 3) && b == 10) {
state++; state++;
continue; continue;
} }
@ -180,14 +178,14 @@ namespace WebSocketSharp.Net
_saved.Append (stateStr.Substring (0, _saved.Length == 0 ? state - 2 : state)); _saved.Append (stateStr.Substring (0, _saved.Length == 0 ? state - 2 : state));
state = 0; state = 0;
if (_saved.Length > 4196) if (_saved.Length > 4196)
throwProtocolViolation ("Error reading trailer (too long)."); throwProtocolViolation ("An error has occurred in reading trailer (too long).");
} }
} }
if (state < 4) { if (state < 4) {
_trailerState = state; _trailerState = state;
if (offset < count) if (offset < length)
throwProtocolViolation ("Error reading trailer."); throwProtocolViolation ("An error has occurred in reading trailer.");
return InputChunkState.Trailer; return InputChunkState.Trailer;
} }
@ -206,35 +204,35 @@ namespace WebSocketSharp.Net
return idx > -1 ? value.Substring (0, idx) : value; return idx > -1 ? value.Substring (0, idx) : value;
} }
private InputChunkState setChunkSize (byte[] buffer, ref int offset, int count) private InputChunkState setChunkSize (byte[] buffer, ref int offset, int length)
{ {
var c = '\0'; byte b = 0;
while (offset < count) { while (offset < length) {
c = (char) buffer[offset++]; b = buffer[offset++];
if (c == '\r') { if (b == 13) {
if (_sawCr) if (_sawCr)
throwProtocolViolation ("2 CR found."); throwProtocolViolation ("Two CR are found.");
_sawCr = true; _sawCr = true;
continue; continue;
} }
if (_sawCr && c == '\n') if (_sawCr && b == 10)
break; break;
if (c == ' ') if (b == 32) // SP
_gotIt = true; _gotIt = true;
if (!_gotIt) if (!_gotIt)
_saved.Append (c); _saved.Append (b);
if (_saved.Length > 20) if (_saved.Length > 20)
throwProtocolViolation ("Chunk size too long."); throwProtocolViolation ("Chunk size is too long.");
} }
if (!_sawCr || c != '\n') { if (!_sawCr || b != 10) {
if (offset < count) if (offset < length)
throwProtocolViolation ("Missing \\n."); throwProtocolViolation ("LF isn't found.");
try { try {
if (_saved.Length > 0) if (_saved.Length > 0)
@ -270,10 +268,10 @@ namespace WebSocketSharp.Net
throw new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null); throw new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
} }
private void write (byte[] buffer, ref int offset, int count) private void write (byte[] buffer, ref int offset, int length)
{ {
if (_state == InputChunkState.None) { if (_state == InputChunkState.None) {
_state = setChunkSize (buffer, ref offset, count); _state = setChunkSize (buffer, ref offset, length);
if (_state == InputChunkState.None) if (_state == InputChunkState.None)
return; return;
@ -282,22 +280,22 @@ namespace WebSocketSharp.Net
_gotIt = false; _gotIt = false;
} }
if (_state == InputChunkState.Body && offset < count) { if (_state == InputChunkState.Body && offset < length) {
_state = writeBody (buffer, ref offset, count); _state = writeBody (buffer, ref offset, length);
if (_state == InputChunkState.Body) if (_state == InputChunkState.Body)
return; return;
} }
if (_state == InputChunkState.BodyFinished && offset < count) { if (_state == InputChunkState.BodyFinished && offset < length) {
_state = readCrLfFrom (buffer, ref offset, count); _state = readCrLfFrom (buffer, ref offset, length);
if (_state == InputChunkState.BodyFinished) if (_state == InputChunkState.BodyFinished)
return; return;
_sawCr = false; _sawCr = false;
} }
if (_state == InputChunkState.Trailer && offset < count) { if (_state == InputChunkState.Trailer && offset < length) {
_state = readTrailerFrom (buffer, ref offset, count); _state = readTrailerFrom (buffer, ref offset, length);
if (_state == InputChunkState.Trailer) if (_state == InputChunkState.Trailer)
return; return;
@ -306,16 +304,16 @@ namespace WebSocketSharp.Net
_gotIt = false; _gotIt = false;
} }
if (offset < count) if (offset < length)
write (buffer, ref offset, count); write (buffer, ref offset, length);
} }
private InputChunkState writeBody (byte[] buffer, ref int offset, int count) private InputChunkState writeBody (byte[] buffer, ref int offset, int length)
{ {
if (_chunkSize == 0) if (_chunkSize == 0)
return InputChunkState.BodyFinished; return InputChunkState.BodyFinished;
var diff = count - offset; var diff = length - offset;
if (diff + _chunkRead > _chunkSize) if (diff + _chunkRead > _chunkSize)
diff = _chunkSize - _chunkRead; diff = _chunkSize - _chunkRead;
@ -347,13 +345,13 @@ namespace WebSocketSharp.Net
public void Write (byte[] buffer, int offset, int count) public void Write (byte[] buffer, int offset, int count)
{ {
write (buffer, ref offset, count); write (buffer, ref offset, offset + count);
} }
public void WriteAndReadBack (byte[] buffer, int offset, int count, ref int read) public void WriteAndReadBack (byte[] buffer, int offset, int count, ref int read)
{ {
if (offset + read > 0) if (read > 0)
Write (buffer, offset, offset + read); Write (buffer, offset, read);
read = readFromChunks (buffer, offset, count); read = readFromChunks (buffer, offset, count);
} }