Refactored a few for ResponseStream.cs

This commit is contained in:
sta 2015-06-22 14:40:23 +09:00
parent 8645185321
commit 846f380157

View File

@ -43,19 +43,15 @@ using System.Text;
namespace WebSocketSharp.Net namespace WebSocketSharp.Net
{ {
// FIXME: Does this buffer the response until close?
// Update: We send a single packet for the first non-chunked write.
// What happens when we set Content-Length to X and write X-1 bytes then close?
// What happens if we don't set Content-Length at all?
internal class ResponseStream : Stream internal class ResponseStream : Stream
{ {
#region Private Fields #region Private Fields
private MemoryStream _body; private MemoryStream _body;
private bool _chunked;
private static readonly byte[] _crlf = new byte[] { 13, 10 }; private static readonly byte[] _crlf = new byte[] { 13, 10 };
private bool _disposed; private bool _disposed;
private HttpListenerResponse _response; private HttpListenerResponse _response;
private bool _sendChunked;
private Stream _stream; private Stream _stream;
private Action<byte[], int, int> _write; private Action<byte[], int, int> _write;
private Action<byte[], int, int> _writeBody; private Action<byte[], int, int> _writeBody;
@ -135,12 +131,12 @@ namespace WebSocketSharp.Net
return false; return false;
} }
_chunked = _response.SendChunked; _sendChunked = _response.SendChunked;
_writeBody = _chunked ? _writeChunked : _write; _writeBody = _sendChunked ? _writeChunked : _write;
} }
flushBody (closing); flushBody (closing);
if (closing && _chunked) { if (closing && _sendChunked) {
var last = getChunkSizeBytes (0, true); var last = getChunkSizeBytes (0, true);
_write (last, 0, last.Length); _write (last, 0, last.Length);
} }
@ -233,7 +229,7 @@ namespace WebSocketSharp.Net
_response.Close (); _response.Close ();
} }
else { else {
if (_chunked) { if (_sendChunked) {
var last = getChunkSizeBytes (0, true); var last = getChunkSizeBytes (0, true);
_write (last, 0, last.Length); _write (last, 0, last.Length);
} }
@ -297,7 +293,7 @@ namespace WebSocketSharp.Net
public override void Flush () public override void Flush ()
{ {
if (!_disposed && (_chunked || _response.SendChunked)) if (!_disposed && (_sendChunked || _response.SendChunked))
flush (false); flush (false);
} }