Commit 0e5e7c3b authored by Honfika's avatar Honfika

StreamExtended updated

parent f186f9b7
......@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="StreamExtended, Version=1.0.115.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.115-beta\lib\net45\StreamExtended.dll</HintPath>
<Reference Include="StreamExtended, Version=1.0.124.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.124-beta\lib\net45\StreamExtended.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="StreamExtended" version="1.0.115-beta" targetFramework="net45" />
<package id="StreamExtended" version="1.0.124-beta" targetFramework="net45" />
</packages>
\ No newline at end of file
......@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// Holds a reference to client
/// </summary>
internal ProxyClient ProxyClient { get; set; }
internal ProxyClient ProxyClient { get; }
internal bool HasMulipartEventSubscribers => MultipartRequestPartSent != null;
......
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using StreamExtended.Network;
namespace Titanium.Web.Proxy.Extensions
{
......
......@@ -10,25 +10,22 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Helpers
{
internal class HttpWriter
internal class HttpWriter : CustomBinaryWriter
{
public int BufferSize { get; }
private readonly Stream stream;
private readonly char[] charBuffer;
private static readonly byte[] newLine = ProxyConstants.NewLine;
private static readonly Encoder encoder = Encoding.ASCII.GetEncoder();
internal HttpWriter(Stream stream, int bufferSize)
internal HttpWriter(Stream stream, int bufferSize) : base(stream)
{
BufferSize = bufferSize;
// ASCII encoder max byte count is char count + 1
charBuffer = new char[BufferSize - 1];
this.stream = stream;
}
public Task WriteLineAsync()
......@@ -88,27 +85,27 @@ namespace Titanium.Web.Proxy.Helpers
if (flush)
{
// flush the stream
await stream.FlushAsync();
await FlushAsync();
}
}
public async Task WriteAsync(byte[] data, bool flush = false)
{
await stream.WriteAsync(data, 0, data.Length);
await WriteAsync(data, 0, data.Length);
if (flush)
{
// flush the stream
await stream.FlushAsync();
await FlushAsync();
}
}
public async Task WriteAsync(byte[] data, int offset, int count, bool flush = false)
public async Task WriteAsync(byte[] data, int offset, int count, bool flush)
{
await stream.WriteAsync(data, offset, count);
await WriteAsync(data, offset, count);
if (flush)
{
// flush the stream
await stream.FlushAsync();
await FlushAsync();
}
}
......@@ -240,7 +237,7 @@ namespace Titanium.Web.Proxy.Helpers
remainingBytes -= bytesRead;
await stream.WriteAsync(buffer, 0, bytesRead);
await WriteAsync(buffer, 0, bytesRead);
}
}
}
......
......@@ -614,17 +614,19 @@ namespace Titanium.Web.Proxy
{
// End the operation
var request = args.WebSession.Request;
var reader = args.ProxyClient.ClientStreamReader;
var writer = args.WebSession.ServerConnection.StreamWriter;
//send the request body bytes to server
if (request.ContentLength > 0 && args.HasMulipartEventSubscribers && request.IsMultipartFormData)
{
var inputStream = args.ProxyClient.ClientStream;
string boundary = HttpHelper.GetBoundaryFromContentType(request.ContentType);
long bytesToSend = request.ContentLength;
while (bytesToSend > 0)
{
var outputStream = args.WebSession.ServerConnection.StreamWriter;
long read = await SendUntilBoundaryAsync(args.ProxyClient.ClientStream, outputStream, bytesToSend, boundary);
long read = await SendUntilBoundaryAsync(reader, writer, bytesToSend, boundary);
if (read == 0)
{
break;
......@@ -634,7 +636,9 @@ namespace Titanium.Web.Proxy
if (bytesToSend > 0)
{
var headers = new HeaderCollection();
var stream = new CustomBufferedPeekStream(args.ProxyClient.ClientStream);
//var stream = new CustomBufferedPeekStream(inputStream);
//await HeaderParser.ReadHeaders(new CustomBinaryReader(stream, BufferSize), headers);
var stream = new CopyStream(reader, writer, BufferSize);
await HeaderParser.ReadHeaders(new CustomBinaryReader(stream, BufferSize), headers);
args.OnMultipartRequestPartSent(boundary, headers);
}
......@@ -642,8 +646,7 @@ namespace Titanium.Web.Proxy
}
else
{
await args.WebSession.ServerConnection.StreamWriter.CopyBodyAsync(args.ProxyClient.ClientStreamReader,
request.IsChunked, request.ContentLength, false);
await writer.CopyBodyAsync(args.ProxyClient.ClientStreamReader, request.IsChunked, request.ContentLength, false);
}
}
......@@ -651,7 +654,7 @@ namespace Titanium.Web.Proxy
/// Read a line from the byte stream
/// </summary>
/// <returns></returns>
private async Task<long> SendUntilBoundaryAsync(CustomBufferedStream inputStream, HttpRequestWriter outputStream, long totalBytesToRead, string boundary)
private async Task<long> SendUntilBoundaryAsync(CustomBinaryReader reader, HttpRequestWriter writer, long totalBytesToRead, string boundary)
{
int bufferDataLength = 0;
......@@ -662,9 +665,9 @@ namespace Titanium.Web.Proxy
int boundaryLength = boundary.Length + 4;
long bytesRead = 0;
while (bytesRead < totalBytesToRead && (inputStream.DataAvailable || await inputStream.FillBufferAsync()))
while (bytesRead < totalBytesToRead && (reader.DataAvailable || await reader.FillBufferAsync()))
{
byte newChar = inputStream.ReadByteFromBuffer();
byte newChar = reader.ReadByteFromBuffer();
buffer[bufferDataLength] = newChar;
bufferDataLength++;
......@@ -698,7 +701,7 @@ namespace Titanium.Web.Proxy
{
//boundary is not longer than 70 bytes according to the specification, so keeping the last 100 (minimum 74) bytes is enough
const int bytesToKeep = 100;
await outputStream.WriteAsync(buffer, 0, buffer.Length - bytesToKeep);
await writer.WriteAsync(buffer, 0, buffer.Length - bytesToKeep);
Buffer.BlockCopy(buffer, buffer.Length - bytesToKeep, buffer, 0, bytesToKeep);
bufferDataLength = bytesToKeep;
}
......@@ -706,7 +709,7 @@ namespace Titanium.Web.Proxy
if (bytesRead > 0)
{
await outputStream.WriteAsync(buffer, 0, bufferDataLength);
await writer.WriteAsync(buffer, 0, bufferDataLength);
}
return bytesRead;
......
......@@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
<PackageReference Include="StreamExtended" Version="1.0.115-beta" />
<PackageReference Include="StreamExtended" Version="1.0.124-beta" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
......@@ -21,7 +21,7 @@
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Principal.Windows">
<Version>4.4.0</Version>
<Version>4.4.1</Version>
</PackageReference>
</ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment