2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2017-05-24 19:12:55 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
namespace SocketHttpListener.Net
|
|
|
|
{
|
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
//
|
|
|
|
// System.Net.ResponseStream
|
|
|
|
//
|
|
|
|
// Author:
|
2019-01-07 23:24:34 +00:00
|
|
|
// Gonzalo Paniagua Javier (gonzalo@novell.com)
|
2017-05-24 19:12:55 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
|
|
|
|
//
|
|
|
|
// 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:
|
2019-01-07 23:27:46 +00:00
|
|
|
//
|
2017-05-24 19:12:55 +00:00
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
2019-01-07 23:27:46 +00:00
|
|
|
//
|
2017-05-24 19:12:55 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
internal sealed class ChunkedInputStream : HttpRequestStream
|
|
|
|
{
|
|
|
|
private ChunkStream _decoder;
|
|
|
|
private readonly HttpListenerContext _context;
|
|
|
|
private bool _no_more_data;
|
|
|
|
|
|
|
|
private class ReadBufferState
|
|
|
|
{
|
|
|
|
public byte[] Buffer;
|
|
|
|
public int Offset;
|
|
|
|
public int Count;
|
|
|
|
public int InitialCount;
|
|
|
|
public HttpStreamAsyncResult Ares;
|
|
|
|
public ReadBufferState(byte[] buffer, int offset, int count, HttpStreamAsyncResult ares)
|
|
|
|
{
|
|
|
|
Buffer = buffer;
|
|
|
|
Offset = offset;
|
|
|
|
Count = count;
|
|
|
|
InitialCount = count;
|
|
|
|
Ares = ares;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChunkedInputStream(HttpListenerContext context, Stream stream, byte[] buffer, int offset, int length)
|
|
|
|
: base(stream, buffer, offset, length)
|
|
|
|
{
|
|
|
|
_context = context;
|
2019-01-13 20:37:13 +00:00
|
|
|
var coll = (WebHeaderCollection)context.Request.Headers;
|
2017-05-24 19:12:55 +00:00
|
|
|
_decoder = new ChunkStream(coll);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChunkStream Decoder
|
|
|
|
{
|
2019-01-13 20:31:14 +00:00
|
|
|
get => _decoder;
|
|
|
|
set => _decoder = value;
|
2017-05-24 19:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override int ReadCore(byte[] buffer, int offset, int count)
|
|
|
|
{
|
2019-01-18 15:48:01 +00:00
|
|
|
IAsyncResult ares = BeginReadCore(buffer, offset, count, null, null);
|
2017-05-24 19:12:55 +00:00
|
|
|
return EndRead(ares);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IAsyncResult BeginReadCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
|
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var ares = new HttpStreamAsyncResult(this);
|
2017-05-24 19:12:55 +00:00
|
|
|
ares._callback = cback;
|
|
|
|
ares._state = state;
|
|
|
|
if (_no_more_data || size == 0 || _closed)
|
|
|
|
{
|
|
|
|
ares.Complete();
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
int nread = _decoder.Read(buffer, offset, size);
|
|
|
|
offset += nread;
|
|
|
|
size -= nread;
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
// got all we wanted, no need to bother the decoder yet
|
|
|
|
ares._count = nread;
|
|
|
|
ares.Complete();
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
if (!_decoder.WantMore)
|
|
|
|
{
|
|
|
|
_no_more_data = nread == 0;
|
|
|
|
ares._count = nread;
|
|
|
|
ares.Complete();
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
ares._buffer = new byte[8192];
|
|
|
|
ares._offset = 0;
|
|
|
|
ares._count = 8192;
|
2019-01-13 20:37:13 +00:00
|
|
|
var rb = new ReadBufferState(buffer, offset, size, ares);
|
2017-05-24 19:12:55 +00:00
|
|
|
rb.InitialCount += nread;
|
|
|
|
base.BeginReadCore(ares._buffer, ares._offset, ares._count, OnRead, rb);
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnRead(IAsyncResult base_ares)
|
|
|
|
{
|
2019-01-18 15:48:01 +00:00
|
|
|
ReadBufferState rb = (ReadBufferState)base_ares.AsyncState;
|
2019-01-13 20:37:13 +00:00
|
|
|
var ares = rb.Ares;
|
2017-05-24 19:12:55 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
int nread = base.EndRead(base_ares);
|
2017-06-21 14:50:54 +00:00
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
_no_more_data = true;
|
|
|
|
ares._count = rb.InitialCount - rb.Count;
|
|
|
|
ares.Complete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:12:55 +00:00
|
|
|
_decoder.Write(ares._buffer, ares._offset, nread);
|
|
|
|
nread = _decoder.Read(rb.Buffer, rb.Offset, rb.Count);
|
|
|
|
rb.Offset += nread;
|
|
|
|
rb.Count -= nread;
|
2017-06-21 14:50:54 +00:00
|
|
|
if (rb.Count == 0 || !_decoder.WantMore)
|
2017-05-24 19:12:55 +00:00
|
|
|
{
|
|
|
|
_no_more_data = !_decoder.WantMore && nread == 0;
|
|
|
|
ares._count = rb.InitialCount - rb.Count;
|
|
|
|
ares.Complete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ares._offset = 0;
|
|
|
|
ares._count = Math.Min(8192, _decoder.ChunkLeft + 6);
|
|
|
|
base.BeginReadCore(ares._buffer, ares._offset, ares._count, OnRead, rb);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_context.Connection.SendError(e.Message, 400);
|
|
|
|
ares.Complete(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int EndRead(IAsyncResult asyncResult)
|
|
|
|
{
|
|
|
|
if (asyncResult == null)
|
|
|
|
throw new ArgumentNullException(nameof(asyncResult));
|
|
|
|
|
2019-01-13 20:37:13 +00:00
|
|
|
var ares = asyncResult as HttpStreamAsyncResult;
|
2017-05-24 19:12:55 +00:00
|
|
|
if (ares == null || !ReferenceEquals(this, ares._parent))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid async result");
|
|
|
|
}
|
|
|
|
if (ares._endCalled)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Invalid end call");
|
|
|
|
}
|
|
|
|
ares._endCalled = true;
|
|
|
|
|
|
|
|
if (!asyncResult.IsCompleted)
|
|
|
|
asyncResult.AsyncWaitHandle.WaitOne();
|
|
|
|
|
|
|
|
if (ares._error != null)
|
2017-06-21 14:50:54 +00:00
|
|
|
throw new HttpListenerException((int)HttpStatusCode.BadRequest, "Operation aborted");
|
2017-05-24 19:12:55 +00:00
|
|
|
|
|
|
|
return ares._count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|