websocket-sharp/websocket-sharp/Net/ChunkStream.cs

361 lines
8.7 KiB
C#
Raw Normal View History

2014-05-17 19:52:34 +08:00
#region License
/*
* ChunkStream.cs
*
* This code is derived from ChunkStream.cs (System.Net) of Mono
2014-05-17 19:52:34 +08:00
* (http://www.mono-project.com).
*
* The MIT License
*
* Copyright (c) 2003 Ximian, Inc (http://www.ximian.com)
* Copyright (c) 2012-2015 sta.blockhead
2014-05-17 19:52:34 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
#region Authors
/*
* Authors:
* - Gonzalo Paniagua Javier <gonzalo@ximian.com>
*/
#endregion
2012-09-10 00:36:22 +08:00
using System;
2013-02-21 15:23:25 +08:00
using System.Collections.Generic;
2012-09-10 00:36:22 +08:00
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
2014-05-17 19:52:34 +08:00
namespace WebSocketSharp.Net
{
internal class ChunkStream
{
#region Private Fields
private int _chunkRead;
private int _chunkSize;
private List<Chunk> _chunks;
2015-04-01 16:18:07 +08:00
private bool _gotIt;
2014-05-17 19:52:34 +08:00
private WebHeaderCollection _headers;
private StringBuilder _saved;
2015-04-01 16:18:07 +08:00
private bool _sawCr;
2014-05-17 19:52:34 +08:00
private InputChunkState _state;
private int _trailerState;
#endregion
#region Public Constructors
public ChunkStream (WebHeaderCollection headers)
{
_headers = headers;
_chunkSize = -1;
_chunks = new List<Chunk> ();
_saved = new StringBuilder ();
}
2015-04-01 16:18:07 +08:00
public ChunkStream (byte[] buffer, int offset, int count, WebHeaderCollection headers)
: this (headers)
{
Write (buffer, offset, count);
}
2014-05-17 19:52:34 +08:00
#endregion
#region Internal Properties
internal WebHeaderCollection Headers {
get {
return _headers;
}
}
#endregion
#region Public Properties
public int ChunkLeft {
get {
return _chunkSize - _chunkRead;
}
}
public bool WantMore {
get {
2015-05-13 16:22:13 +08:00
return _state != InputChunkState.End;
2014-05-17 19:52:34 +08:00
}
}
#endregion
#region Private Methods
2015-05-13 16:22:13 +08:00
private int read (byte[] buffer, int offset, int count)
2014-05-17 19:52:34 +08:00
{
var nread = 0;
2015-05-13 16:22:13 +08:00
var cnt = _chunks.Count;
2015-04-01 16:18:07 +08:00
for (var i = 0; i < cnt; i++) {
var chunk = _chunks[i];
2014-05-17 19:52:34 +08:00
if (chunk == null)
continue;
2014-05-20 10:03:22 +08:00
if (chunk.ReadLeft == 0) {
2015-04-01 16:18:07 +08:00
_chunks[i] = null;
2014-05-17 19:52:34 +08:00
continue;
}
2015-04-01 16:18:07 +08:00
nread += chunk.Read (buffer, offset + nread, count - nread);
if (nread == count)
2014-05-17 19:52:34 +08:00
break;
}
return nread;
}
2015-04-01 16:18:07 +08:00
private static string removeChunkExtension (string value)
2014-05-17 19:52:34 +08:00
{
2015-04-01 16:18:07 +08:00
var idx = value.IndexOf (';');
return idx > -1 ? value.Substring (0, idx) : value;
2014-05-17 19:52:34 +08:00
}
2015-04-14 16:34:08 +08:00
private InputChunkState seekCrLf (byte[] buffer, ref int offset, int length)
{
if (!_sawCr) {
if (buffer[offset++] != 13)
throwProtocolViolation ("CR is expected.");
_sawCr = true;
if (offset == length)
2015-05-12 15:29:55 +08:00
return InputChunkState.DataEnded;
2015-04-14 16:34:08 +08:00
}
if (buffer[offset++] != 10)
throwProtocolViolation ("LF is expected.");
return InputChunkState.None;
}
2015-04-12 16:36:41 +08:00
private InputChunkState setChunkSize (byte[] buffer, ref int offset, int length)
2014-05-17 19:52:34 +08:00
{
2015-04-12 16:36:41 +08:00
byte b = 0;
while (offset < length) {
b = buffer[offset++];
2015-04-17 15:48:15 +08:00
if (_sawCr) {
2015-04-20 13:53:31 +08:00
if (b != 10)
throwProtocolViolation ("LF is expected.");
2015-04-17 15:48:15 +08:00
2015-04-20 13:53:31 +08:00
break;
2015-04-17 15:48:15 +08:00
}
if (b == 13) {
2015-04-01 16:18:07 +08:00
_sawCr = true;
2014-05-17 19:52:34 +08:00
continue;
}
2015-04-01 16:18:07 +08:00
2015-04-17 15:48:15 +08:00
if (b == 10)
2015-04-15 14:39:50 +08:00
throwProtocolViolation ("LF is unexpected.");
2015-04-14 16:34:08 +08:00
2015-04-12 16:36:41 +08:00
if (b == 32) // SP
2015-04-01 16:18:07 +08:00
_gotIt = true;
2014-05-17 19:52:34 +08:00
2015-04-01 16:18:07 +08:00
if (!_gotIt)
_saved.Append ((char) b);
2014-05-17 19:52:34 +08:00
if (_saved.Length > 20)
2015-04-13 20:32:42 +08:00
throwProtocolViolation ("The chunk size is too long.");
2014-05-17 19:52:34 +08:00
}
2015-04-14 16:34:08 +08:00
if (!_sawCr || b != 10)
2014-05-17 19:52:34 +08:00
return InputChunkState.None;
_chunkRead = 0;
try {
_chunkSize = Int32.Parse (
removeChunkExtension (_saved.ToString ()), NumberStyles.HexNumber);
}
catch {
2015-04-13 20:32:42 +08:00
throwProtocolViolation ("The chunk size cannot be parsed.");
2014-05-17 19:52:34 +08:00
}
2015-04-01 16:18:07 +08:00
2014-05-17 19:52:34 +08:00
if (_chunkSize == 0) {
_trailerState = 2;
return InputChunkState.Trailer;
}
2015-05-12 15:29:55 +08:00
return InputChunkState.Data;
2014-05-17 19:52:34 +08:00
}
2015-05-13 16:22:13 +08:00
private InputChunkState setTrailer (byte[] buffer, ref int offset, int length)
2015-04-13 20:32:42 +08:00
{
2015-05-13 16:22:13 +08:00
// Check if no trailer.
2015-04-13 20:32:42 +08:00
if (_trailerState == 2 && buffer[offset] == 13 && _saved.Length == 0) {
offset++;
if (offset < length && buffer[offset] == 10) {
offset++;
2015-05-13 16:22:13 +08:00
return InputChunkState.End;
2015-04-13 20:32:42 +08:00
}
offset--;
}
while (offset < length && _trailerState < 4) {
var b = buffer[offset++];
_saved.Append ((char) b);
2015-04-13 20:32:42 +08:00
if (_saved.Length > 4196)
2015-04-14 16:34:08 +08:00
throwProtocolViolation ("The trailer is too long.");
2015-04-13 20:32:42 +08:00
2015-04-17 15:48:15 +08:00
if (_trailerState == 1 || _trailerState == 3) {
2015-04-20 13:53:31 +08:00
if (b != 10)
throwProtocolViolation ("LF is expected.");
2015-04-17 15:48:15 +08:00
2015-04-20 13:53:31 +08:00
_trailerState++;
continue;
2015-04-13 20:32:42 +08:00
}
2015-04-17 15:48:15 +08:00
if (b == 13) {
2015-04-13 20:32:42 +08:00
_trailerState++;
continue;
}
2015-04-17 15:48:15 +08:00
if (b == 10)
2015-04-15 14:39:50 +08:00
throwProtocolViolation ("LF is unexpected.");
2015-04-14 16:34:08 +08:00
2015-04-13 20:32:42 +08:00
_trailerState = 0;
}
if (_trailerState < 4)
return InputChunkState.Trailer;
_saved.Length -= 2;
var reader = new StringReader (_saved.ToString ());
2015-04-17 15:48:15 +08:00
2015-04-13 20:32:42 +08:00
string line;
2015-04-14 16:34:08 +08:00
while ((line = reader.ReadLine ()) != null && line.Length > 0)
2015-04-13 20:32:42 +08:00
_headers.Add (line);
2015-05-13 16:22:13 +08:00
return InputChunkState.End;
2015-04-13 20:32:42 +08:00
}
2014-05-17 19:52:34 +08:00
private static void throwProtocolViolation (string message)
{
2015-04-01 16:18:07 +08:00
throw new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);
2014-05-17 19:52:34 +08:00
}
2015-04-12 16:36:41 +08:00
private void write (byte[] buffer, ref int offset, int length)
2014-05-17 19:52:34 +08:00
{
2015-05-13 16:22:13 +08:00
if (_state == InputChunkState.End)
throwProtocolViolation ("The chunks were ended.");
2014-05-17 19:52:34 +08:00
if (_state == InputChunkState.None) {
2015-04-12 16:36:41 +08:00
_state = setChunkSize (buffer, ref offset, length);
2014-05-17 19:52:34 +08:00
if (_state == InputChunkState.None)
return;
_saved.Length = 0;
2015-04-01 16:18:07 +08:00
_sawCr = false;
_gotIt = false;
2014-05-17 19:52:34 +08:00
}
2015-05-12 15:29:55 +08:00
if (_state == InputChunkState.Data && offset < length) {
_state = writeData (buffer, ref offset, length);
if (_state == InputChunkState.Data)
2014-05-17 19:52:34 +08:00
return;
}
2015-05-12 15:29:55 +08:00
if (_state == InputChunkState.DataEnded && offset < length) {
2015-04-14 16:34:08 +08:00
_state = seekCrLf (buffer, ref offset, length);
2015-05-12 15:29:55 +08:00
if (_state == InputChunkState.DataEnded)
2014-05-17 19:52:34 +08:00
return;
2015-04-01 16:18:07 +08:00
_sawCr = false;
2014-05-17 19:52:34 +08:00
}
2015-04-01 16:18:07 +08:00
2015-04-12 16:36:41 +08:00
if (_state == InputChunkState.Trailer && offset < length) {
2015-05-13 16:22:13 +08:00
_state = setTrailer (buffer, ref offset, length);
2014-05-17 19:52:34 +08:00
if (_state == InputChunkState.Trailer)
return;
_saved.Length = 0;
}
2015-04-12 16:36:41 +08:00
if (offset < length)
write (buffer, ref offset, length);
2014-05-17 19:52:34 +08:00
}
2015-05-12 15:29:55 +08:00
private InputChunkState writeData (byte[] buffer, ref int offset, int length)
2014-05-17 19:52:34 +08:00
{
2015-04-13 20:32:42 +08:00
var cnt = length - offset;
var left = _chunkSize - _chunkRead;
if (cnt > left)
cnt = left;
2014-05-17 19:52:34 +08:00
2015-05-12 15:29:55 +08:00
var data = new byte[cnt];
Buffer.BlockCopy (buffer, offset, data, 0, cnt);
_chunks.Add (new Chunk (data));
2014-05-17 19:52:34 +08:00
2015-04-13 20:32:42 +08:00
offset += cnt;
_chunkRead += cnt;
2014-05-17 19:52:34 +08:00
2015-05-12 15:29:55 +08:00
return _chunkRead == _chunkSize ? InputChunkState.DataEnded : InputChunkState.Data;
2014-05-17 19:52:34 +08:00
}
#endregion
2015-05-13 16:22:13 +08:00
#region Internal Methods
2014-05-17 19:52:34 +08:00
2015-05-13 16:22:13 +08:00
internal void ResetBuffer ()
2014-05-17 19:52:34 +08:00
{
2015-05-13 16:22:13 +08:00
_chunkRead = 0;
_chunkSize = -1;
_chunks.Clear ();
2014-05-17 19:52:34 +08:00
}
2015-05-13 16:22:13 +08:00
internal int WriteAndReadBack (byte[] buffer, int offset, int writeCount, int readCount)
2014-05-17 19:52:34 +08:00
{
2015-05-13 16:22:13 +08:00
Write (buffer, offset, writeCount);
return Read (buffer, offset, readCount);
2014-05-17 19:52:34 +08:00
}
2015-05-13 16:22:13 +08:00
#endregion
#region Public Methods
public int Read (byte[] buffer, int offset, int count)
2014-05-17 19:52:34 +08:00
{
2015-04-14 16:34:08 +08:00
if (count <= 0)
2015-05-13 16:22:13 +08:00
return 0;
2015-04-14 16:34:08 +08:00
2015-05-13 16:22:13 +08:00
return read (buffer, offset, count);
2014-05-17 19:52:34 +08:00
}
2015-05-13 16:22:13 +08:00
public void Write (byte[] buffer, int offset, int count)
2014-05-17 19:52:34 +08:00
{
2015-05-13 16:22:13 +08:00
if (count <= 0)
return;
write (buffer, ref offset, offset + count);
2014-05-17 19:52:34 +08:00
}
#endregion
}
2012-09-10 00:36:22 +08:00
}