Fix a few for RequestStream.cs

This commit is contained in:
sta 2014-05-11 15:38:30 +09:00
parent 091b45c8b0
commit bd0e925d67

View File

@ -8,7 +8,7 @@
* The MIT License * The MIT License
* *
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com) * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -33,7 +33,7 @@
#region Authors #region Authors
/* /*
* Authors: * Authors:
* Gonzalo Paniagua Javier <gonzalo@novell.com> * - Gonzalo Paniagua Javier <gonzalo@novell.com>
*/ */
#endregion #endregion
@ -57,8 +57,7 @@ namespace WebSocketSharp.Net
#region Internal Constructors #region Internal Constructors
internal RequestStream ( internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
Stream stream, byte [] buffer, int offset, int length)
: this (stream, buffer, offset, length, -1) : this (stream, buffer, offset, length, -1)
{ {
} }
@ -167,24 +166,20 @@ 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)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException (GetType ().ToString ()); throw new ObjectDisposedException (GetType ().ToString ());
var read = fillFromBuffer (buffer, offset, count); var nread = fillFromBuffer (buffer, offset, count);
if (read > 0 || read == -1) { if (nread > 0 || nread == -1) {
var ares = new HttpStreamAsyncResult (); var ares = new HttpStreamAsyncResult ();
ares.Buffer = buffer; ares.Buffer = buffer;
ares.Offset = offset; ares.Offset = offset;
ares.Count = count; ares.Count = count;
ares.Callback = callback; ares.Callback = callback;
ares.State = state; ares.State = state;
ares.SyncRead = read; ares.SyncRead = nread;
ares.Complete (); ares.Complete ();
return ares; return ares;
@ -225,11 +220,11 @@ namespace WebSocketSharp.Net
} }
// Close on exception? // Close on exception?
var read = _stream.EndRead (asyncResult); var nread = _stream.EndRead (asyncResult);
if (read > 0 && _remainingBody > 0) if (nread > 0 && _remainingBody > 0)
_remainingBody -= read; _remainingBody -= nread;
return read; return nread;
} }
public override void EndWrite (IAsyncResult asyncResult) public override void EndWrite (IAsyncResult asyncResult)
@ -248,17 +243,18 @@ namespace WebSocketSharp.Net
// Call fillFromBuffer to check for buffer boundaries even when // Call fillFromBuffer to check for buffer boundaries even when
// _remainingBody is 0. // _remainingBody is 0.
var read = fillFromBuffer (buffer, offset, count); var nread = fillFromBuffer (buffer, offset, count);
if (read == -1) // No more bytes available (Content-Length). if (nread == -1) // No more bytes available (Content-Length).
return 0; return 0;
else if (read > 0)
return read;
read = _stream.Read (buffer, offset, count); if (nread > 0)
if (read > 0 && _remainingBody > 0) return nread;
_remainingBody -= read;
return read; nread = _stream.Read (buffer, offset, count);
if (nread > 0 && _remainingBody > 0)
_remainingBody -= nread;
return nread;
} }
public override long Seek (long offset, SeekOrigin origin) public override long Seek (long offset, SeekOrigin origin)