Fix a few for ResponseStream.cs

This commit is contained in:
sta 2014-05-12 19:53:30 +09:00
parent bd0e925d67
commit 87d48ed9ad
2 changed files with 52 additions and 60 deletions

View File

@ -646,7 +646,7 @@ namespace WebSocketSharp.Net
expect.Length > 0 && expect.Length > 0 &&
expect.ToLower () == "100-continue") { expect.ToLower () == "100-continue") {
var output = _context.Connection.GetResponseStream (); var output = _context.Connection.GetResponseStream ();
output.InternalWrite (_100continue, 0, _100continue.Length); output.WriteInternally (_100continue, 0, _100continue.Length);
} }
} }

View File

@ -33,7 +33,7 @@
#region Authors #region Authors
/* /*
* Authors: * Authors:
* Gonzalo Paniagua Javier <gonzalo@novell.com> * - Gonzalo Paniagua Javier <gonzalo@novell.com>
*/ */
#endregion #endregion
@ -67,8 +67,7 @@ namespace WebSocketSharp.Net
#region Internal Constructors #region Internal Constructors
internal ResponseStream ( internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignoreErrors)
Stream stream, HttpListenerResponse response, bool ignoreErrors)
{ {
_stream = stream; _stream = stream;
_response = response; _response = response;
@ -119,8 +118,7 @@ namespace WebSocketSharp.Net
private static byte [] getChunkSizeBytes (int size, bool final) private static byte [] getChunkSizeBytes (int size, bool final)
{ {
return Encoding.ASCII.GetBytes ( return Encoding.ASCII.GetBytes (String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : ""));
String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : ""));
} }
private MemoryStream getHeaders (bool closing) private MemoryStream getHeaders (bool closing)
@ -138,7 +136,7 @@ namespace WebSocketSharp.Net
#region Internal Methods #region Internal Methods
internal void InternalWrite (byte [] buffer, int offset, int count) internal void WriteInternally (byte [] buffer, int offset, int count)
{ {
if (_ignoreErrors) { if (_ignoreErrors) {
try { try {
@ -157,44 +155,38 @@ namespace WebSocketSharp.Net
#region Public Methods #region Public Methods
public override IAsyncResult BeginRead ( public override IAsyncResult BeginRead (
byte [] buffer, byte [] buffer, int offset, int count, AsyncCallback callback, object state)
int offset,
int count,
AsyncCallback callback,
object state)
{ {
throw new NotSupportedException (); throw new NotSupportedException ();
} }
public override IAsyncResult BeginWrite ( public override IAsyncResult BeginWrite (
byte [] buffer, byte [] buffer, int offset, int count, AsyncCallback callback, object state)
int offset,
int count,
AsyncCallback callback,
object state)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException (GetType ().ToString ()); throw new ObjectDisposedException (GetType ().ToString ());
var stream = getHeaders (false); var headers = getHeaders (false);
var chunked = _response.SendChunked; var chunked = _response.SendChunked;
byte [] bytes = null; byte [] bytes = null;
if (stream != null) { if (headers != null) {
var start = stream.Position; using (headers) {
stream.Position = stream.Length; var start = headers.Position;
if (chunked) { headers.Position = headers.Length;
bytes = getChunkSizeBytes (count, false); if (chunked) {
stream.Write (bytes, 0, bytes.Length); bytes = getChunkSizeBytes (count, false);
} headers.Write (bytes, 0, bytes.Length);
}
stream.Write (buffer, offset, count); headers.Write (buffer, offset, count);
buffer = stream.GetBuffer (); buffer = headers.GetBuffer ();
offset = (int) start; offset = (int) start;
count = (int) (stream.Position - start); count = (int) (headers.Position - start);
}
} }
else if (chunked) { else if (chunked) {
bytes = getChunkSizeBytes (count, false); bytes = getChunkSizeBytes (count, false);
InternalWrite (bytes, 0, bytes.Length); WriteInternally (bytes, 0, bytes.Length);
} }
return _stream.BeginWrite (buffer, offset, count, callback, state); return _stream.BeginWrite (buffer, offset, count, callback, state);
@ -207,24 +199,26 @@ namespace WebSocketSharp.Net
_disposed = true; _disposed = true;
var stream = getHeaders (true); var headers = getHeaders (true);
var chunked = _response.SendChunked; var chunked = _response.SendChunked;
byte [] bytes = null; byte [] bytes = null;
if (stream != null) { if (headers != null) {
var start = stream.Position; using (headers) {
if (chunked && !_trailerSent) { var start = headers.Position;
bytes = getChunkSizeBytes (0, true); if (chunked && !_trailerSent) {
stream.Position = stream.Length; bytes = getChunkSizeBytes (0, true);
stream.Write (bytes, 0, bytes.Length); headers.Position = headers.Length;
headers.Write (bytes, 0, bytes.Length);
}
WriteInternally (headers.GetBuffer (), (int) start, (int) (headers.Length - start));
} }
InternalWrite (
stream.GetBuffer (), (int) start, (int) (stream.Length - start));
_trailerSent = true; _trailerSent = true;
} }
else if (chunked && !_trailerSent) { else if (chunked && !_trailerSent) {
bytes = getChunkSizeBytes (0, true); bytes = getChunkSizeBytes (0, true);
InternalWrite (bytes, 0, bytes.Length); WriteInternally (bytes, 0, bytes.Length);
_trailerSent = true; _trailerSent = true;
} }
@ -283,38 +277,36 @@ namespace WebSocketSharp.Net
if (_disposed) if (_disposed)
throw new ObjectDisposedException (GetType ().ToString ()); throw new ObjectDisposedException (GetType ().ToString ());
var stream = getHeaders (false); var headers = getHeaders (false);
var chunked = _response.SendChunked; var chunked = _response.SendChunked;
byte [] bytes = null; byte [] bytes = null;
if (stream != null) { if (headers != null) {
// After the possible preamble for the encoding. // After the possible preamble for the encoding.
var start = stream.Position; using (headers) {
stream.Position = stream.Length; var start = headers.Position;
if (chunked) { headers.Position = headers.Length;
bytes = getChunkSizeBytes (count, false); if (chunked) {
stream.Write (bytes, 0, bytes.Length); bytes = getChunkSizeBytes (count, false);
} headers.Write (bytes, 0, bytes.Length);
}
var newCount = Math.Min ( var newCount = Math.Min (count, 16384 - (int) headers.Position + (int) start);
count, 16384 - (int) stream.Position + (int) start); headers.Write (buffer, offset, newCount);
stream.Write (buffer, offset, newCount); count -= newCount;
count -= newCount; offset += newCount;
offset += newCount; WriteInternally (headers.GetBuffer (), (int) start, (int) (headers.Length - start));
InternalWrite ( }
stream.GetBuffer (), (int) start, (int) (stream.Length - start));
stream.SetLength (0);
stream.Capacity = 0; // 'dispose' the buffer in stream.
} }
else if (chunked) { else if (chunked) {
bytes = getChunkSizeBytes (count, false); bytes = getChunkSizeBytes (count, false);
InternalWrite (bytes, 0, bytes.Length); WriteInternally (bytes, 0, bytes.Length);
} }
if (count > 0) if (count > 0)
InternalWrite (buffer, offset, count); WriteInternally (buffer, offset, count);
if (chunked) if (chunked)
InternalWrite (_crlf, 0, 2); WriteInternally (_crlf, 0, 2);
} }
#endregion #endregion