websocket-sharp/websocket-sharp/Net/ResponseStream.cs

404 lines
8.8 KiB
C#
Raw Normal View History

#region License
/*
* ResponseStream.cs
*
* This code is derived from ResponseStream.cs (System.Net) of Mono
* (http://www.mono-project.com).
*
* The MIT License
*
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
2020-02-16 20:06:10 +08:00
* Copyright (c) 2012-2020 sta.blockhead
*
* 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:
2014-05-12 18:53:30 +08:00
* - Gonzalo Paniagua Javier <gonzalo@novell.com>
*/
#endregion
2012-09-10 00:36:22 +08:00
using System;
using System.IO;
using System.Text;
namespace WebSocketSharp.Net
{
internal class ResponseStream : Stream
{
#region Private Fields
2020-02-10 19:39:38 +08:00
private MemoryStream _bodyBuffer;
2020-02-01 20:32:52 +08:00
private static readonly byte[] _crlf;
2015-06-03 15:35:53 +08:00
private bool _disposed;
2020-02-09 16:40:52 +08:00
private Stream _innerStream;
2020-02-02 16:28:22 +08:00
private static readonly byte[] _lastChunk;
2020-02-13 20:00:29 +08:00
private static readonly int _maxHeadersLength;
2015-06-03 15:35:53 +08:00
private HttpListenerResponse _response;
2015-06-22 13:40:23 +08:00
private bool _sendChunked;
2015-06-03 15:35:53 +08:00
private Action<byte[], int, int> _write;
2015-06-04 17:18:07 +08:00
private Action<byte[], int, int> _writeBody;
private Action<byte[], int, int> _writeChunked;
#endregion
2020-02-01 20:32:52 +08:00
#region Static Constructor
static ResponseStream ()
{
2020-02-02 16:30:03 +08:00
_crlf = new byte[] { 13, 10 }; // "\r\n"
2020-02-02 16:28:22 +08:00
_lastChunk = new byte[] { 48, 13, 10, 13, 10 }; // "0\r\n\r\n"
2020-02-13 20:00:29 +08:00
_maxHeadersLength = 32768;
2020-02-01 20:32:52 +08:00
}
#endregion
#region Internal Constructors
2015-06-01 19:53:45 +08:00
internal ResponseStream (
2020-02-09 16:40:52 +08:00
Stream innerStream,
2020-01-29 18:47:33 +08:00
HttpListenerResponse response,
bool ignoreWriteExceptions
)
{
2020-02-09 16:40:52 +08:00
_innerStream = innerStream;
_response = response;
2015-06-04 17:18:07 +08:00
if (ignoreWriteExceptions) {
_write = writeWithoutThrowingException;
_writeChunked = writeChunkedWithoutThrowingException;
}
else {
2020-02-09 16:40:52 +08:00
_write = innerStream.Write;
2015-06-04 17:18:07 +08:00
_writeChunked = writeChunked;
}
2015-06-02 20:40:29 +08:00
2020-02-10 19:39:38 +08:00
_bodyBuffer = new MemoryStream ();
}
#endregion
#region Public Properties
public override bool CanRead {
get {
return false;
}
}
public override bool CanSeek {
get {
return false;
}
}
public override bool CanWrite {
get {
2015-06-01 19:53:45 +08:00
return !_disposed;
}
}
public override long Length {
get {
throw new NotSupportedException ();
}
}
public override long Position {
get {
throw new NotSupportedException ();
}
set {
throw new NotSupportedException ();
}
}
#endregion
#region Private Methods
2015-06-12 16:14:36 +08:00
private bool flush (bool closing)
2015-05-31 17:14:08 +08:00
{
2015-06-02 20:40:29 +08:00
if (!_response.HeadersSent) {
2020-02-12 18:49:44 +08:00
if (!flushHeaders ())
2015-06-12 16:14:36 +08:00
return false;
2020-02-12 18:46:19 +08:00
_response.HeadersSent = true;
2015-06-22 13:40:23 +08:00
_sendChunked = _response.SendChunked;
_writeBody = _sendChunked ? _writeChunked : _write;
2015-06-02 20:40:29 +08:00
}
2015-05-31 17:14:08 +08:00
2015-06-04 17:18:07 +08:00
flushBody (closing);
2020-02-14 18:38:13 +08:00
2015-06-12 16:14:36 +08:00
return true;
2015-06-03 15:35:53 +08:00
}
2015-06-04 17:18:07 +08:00
private void flushBody (bool closing)
2015-06-03 15:35:53 +08:00
{
2020-02-10 19:39:38 +08:00
using (_bodyBuffer) {
var len = _bodyBuffer.Length;
2020-02-06 20:46:18 +08:00
2015-06-02 20:40:29 +08:00
if (len > Int32.MaxValue) {
2020-02-10 19:39:38 +08:00
_bodyBuffer.Position = 0;
2020-02-06 20:46:18 +08:00
2015-06-02 20:40:29 +08:00
var buffLen = 1024;
var buff = new byte[buffLen];
var nread = 0;
2020-02-06 20:46:18 +08:00
while (true) {
2020-02-10 19:39:38 +08:00
nread = _bodyBuffer.Read (buff, 0, buffLen);
2020-02-06 20:46:18 +08:00
if (nread <= 0)
break;
2015-06-04 17:18:07 +08:00
_writeBody (buff, 0, nread);
2020-02-06 20:46:18 +08:00
}
2015-06-02 20:40:29 +08:00
}
2015-06-03 15:35:53 +08:00
else if (len > 0) {
2020-02-10 19:39:38 +08:00
_writeBody (_bodyBuffer.GetBuffer (), 0, (int) len);
2015-06-02 20:40:29 +08:00
}
}
2015-06-01 19:53:45 +08:00
2020-02-08 15:56:10 +08:00
if (!closing) {
2020-02-10 19:39:38 +08:00
_bodyBuffer = new MemoryStream ();
2020-02-08 15:56:10 +08:00
return;
2020-02-07 20:48:19 +08:00
}
2020-02-08 15:56:10 +08:00
if (_sendChunked)
_write (_lastChunk, 0, 5);
2020-02-10 19:39:38 +08:00
_bodyBuffer = null;
2015-06-01 19:53:45 +08:00
}
2020-02-12 18:49:44 +08:00
private bool flushHeaders ()
2015-06-12 16:14:36 +08:00
{
2019-12-11 18:54:20 +08:00
if (!_response.SendChunked) {
2020-02-10 19:39:38 +08:00
if (_response.ContentLength64 != _bodyBuffer.Length)
2015-06-12 16:14:36 +08:00
return false;
2019-12-11 18:54:20 +08:00
}
var statusLine = _response.StatusLine;
var headers = _response.FullHeaders;
var buff = new MemoryStream ();
var enc = Encoding.UTF8;
2015-06-12 16:14:36 +08:00
2019-12-11 18:54:20 +08:00
using (var writer = new StreamWriter (buff, enc, 256)) {
writer.Write (statusLine);
writer.Write (headers.ToStringMultiValue (true));
writer.Flush ();
var start = enc.GetPreamble ().Length;
var len = buff.Length - start;
2020-02-13 20:03:09 +08:00
if (len > _maxHeadersLength)
2015-06-12 16:14:36 +08:00
return false;
2019-12-11 18:54:20 +08:00
_write (buff.GetBuffer (), start, (int) len);
2015-06-12 16:14:36 +08:00
}
2019-12-11 18:54:20 +08:00
_response.CloseConnection = headers["Connection"] == "close";
2020-02-13 20:04:54 +08:00
2015-06-12 16:14:36 +08:00
return true;
}
2020-02-03 20:37:04 +08:00
private static byte[] getChunkSizeBytes (int size)
{
var chunkSize = String.Format ("{0:x}\r\n", size);
2020-02-14 18:41:32 +08:00
2020-02-03 20:37:04 +08:00
return Encoding.ASCII.GetBytes (chunkSize);
}
2015-06-03 15:35:53 +08:00
private void writeChunked (byte[] buffer, int offset, int count)
{
2020-02-03 20:38:54 +08:00
var size = getChunkSizeBytes (count);
2020-01-31 18:43:34 +08:00
2020-02-09 16:40:52 +08:00
_innerStream.Write (size, 0, size.Length);
_innerStream.Write (buffer, offset, count);
_innerStream.Write (_crlf, 0, 2);
2015-06-03 15:35:53 +08:00
}
2020-01-30 18:40:48 +08:00
private void writeChunkedWithoutThrowingException (
byte[] buffer, int offset, int count
)
2015-06-04 17:18:07 +08:00
{
try {
writeChunked (buffer, offset, count);
}
catch {
}
}
2020-01-30 18:38:01 +08:00
private void writeWithoutThrowingException (
byte[] buffer, int offset, int count
)
2015-06-04 17:18:07 +08:00
{
try {
2020-02-09 16:40:52 +08:00
_innerStream.Write (buffer, offset, count);
2015-06-04 17:18:07 +08:00
}
catch {
}
}
2015-06-01 19:53:45 +08:00
#endregion
#region Internal Methods
internal void Close (bool force)
{
2015-06-01 19:53:45 +08:00
if (_disposed)
return;
2015-06-01 19:53:45 +08:00
_disposed = true;
2020-01-29 18:39:51 +08:00
if (!force) {
if (flush (true)) {
_response.Close ();
_response = null;
2020-02-09 16:40:52 +08:00
_innerStream = null;
2020-01-29 18:39:51 +08:00
return;
}
2020-02-11 21:03:08 +08:00
_response.CloseConnection = true;
2020-01-29 18:39:51 +08:00
}
2015-06-12 16:14:36 +08:00
2020-02-04 20:46:03 +08:00
if (_sendChunked)
_write (_lastChunk, 0, 5);
2020-02-10 19:39:38 +08:00
_bodyBuffer.Dispose ();
2020-01-29 18:39:51 +08:00
_response.Abort ();
2020-02-10 19:39:38 +08:00
_bodyBuffer = null;
2015-06-01 19:53:45 +08:00
_response = null;
2020-02-09 16:40:52 +08:00
_innerStream = null;
2015-06-01 19:53:45 +08:00
}
2015-05-30 17:16:32 +08:00
internal void InternalWrite (byte[] buffer, int offset, int count)
{
2015-06-04 17:18:07 +08:00
_write (buffer, offset, count);
}
#endregion
#region Public Methods
public override IAsyncResult BeginRead (
2020-01-28 20:15:25 +08:00
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state
)
{
throw new NotSupportedException ();
}
public override IAsyncResult BeginWrite (
2020-01-27 20:13:21 +08:00
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state
)
{
2020-01-27 20:15:16 +08:00
if (_disposed) {
var name = GetType ().ToString ();
2020-02-15 16:38:25 +08:00
2020-01-27 20:15:16 +08:00
throw new ObjectDisposedException (name);
}
2020-02-10 19:39:38 +08:00
return _bodyBuffer.BeginWrite (buffer, offset, count, callback, state);
}
public override void Close ()
{
2015-06-01 19:53:45 +08:00
Close (false);
}
2015-06-01 19:53:45 +08:00
protected override void Dispose (bool disposing)
{
Close (!disposing);
}
public override int EndRead (IAsyncResult asyncResult)
{
throw new NotSupportedException ();
}
public override void EndWrite (IAsyncResult asyncResult)
{
2020-01-27 20:08:25 +08:00
if (_disposed) {
var name = GetType ().ToString ();
2020-02-15 16:39:37 +08:00
2020-01-27 20:08:25 +08:00
throw new ObjectDisposedException (name);
}
2020-02-10 19:39:38 +08:00
_bodyBuffer.EndWrite (asyncResult);
}
public override void Flush ()
{
2020-01-26 18:54:48 +08:00
if (_disposed)
return;
var sendChunked = _sendChunked || _response.SendChunked;
if (!sendChunked)
return;
flush (false);
}
2015-05-30 17:16:32 +08:00
public override int Read (byte[] buffer, int offset, int count)
{
throw new NotSupportedException ();
}
public override long Seek (long offset, SeekOrigin origin)
{
throw new NotSupportedException ();
}
public override void SetLength (long value)
{
throw new NotSupportedException ();
}
2015-05-30 17:16:32 +08:00
public override void Write (byte[] buffer, int offset, int count)
{
2020-01-25 21:51:22 +08:00
if (_disposed) {
var name = GetType ().ToString ();
2020-02-15 16:40:52 +08:00
2020-01-25 21:51:22 +08:00
throw new ObjectDisposedException (name);
}
2020-02-10 19:39:38 +08:00
_bodyBuffer.Write (buffer, offset, count);
}
#endregion
}
2012-09-10 00:36:22 +08:00
}