Commit e946e11c authored by Honfika's avatar Honfika

multipart form data reading fixed

parent 57667e1b
......@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<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 Include="StreamExtended, Version=1.0.127.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.127-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.124-beta" targetFramework="net45" />
<package id="StreamExtended" version="1.0.127-beta" targetFramework="net45" />
</packages>
\ No newline at end of file
......@@ -620,29 +620,28 @@ namespace Titanium.Web.Proxy
//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 copyStream = new CopyStream(reader, writer, BufferSize);
var copyStreamReader = new CustomBinaryReader(copyStream, BufferSize);
while (bytesToSend > copyStream.ReadBytes)
{
long read = await SendUntilBoundaryAsync(reader, writer, bytesToSend, boundary);
long read = await ReadUntilBoundaryAsync(copyStreamReader, bytesToSend, boundary);
if (read == 0)
{
break;
}
bytesToSend -= read;
if (bytesToSend > 0)
if (bytesToSend > copyStream.ReadBytes)
{
var headers = new HeaderCollection();
//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);
await HeaderParser.ReadHeaders(copyStreamReader, headers);
args.OnMultipartRequestPartSent(boundary, headers);
}
}
await copyStream.FlushAsync();
}
else
{
......@@ -654,11 +653,10 @@ namespace Titanium.Web.Proxy
/// Read a line from the byte stream
/// </summary>
/// <returns></returns>
private async Task<long> SendUntilBoundaryAsync(CustomBinaryReader reader, HttpRequestWriter writer, long totalBytesToRead, string boundary)
private async Task<long> ReadUntilBoundaryAsync(CustomBinaryReader reader, long totalBytesToRead, string boundary)
{
int bufferDataLength = 0;
// try to use the thread static buffer
var buffer = BufferPool.GetBuffer(BufferSize);
try
{
......@@ -676,8 +674,7 @@ namespace Titanium.Web.Proxy
if (bufferDataLength >= boundaryLength)
{
int startIdx = bufferDataLength - boundaryLength;
if (buffer[startIdx] == '-' && buffer[startIdx + 1] == '-'
&& buffer[bufferDataLength - 2] == '\r' && buffer[bufferDataLength - 1] == '\n')
if (buffer[startIdx] == '-' && buffer[startIdx + 1] == '-')
{
startIdx += 2;
bool ok = true;
......@@ -701,17 +698,11 @@ 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 writer.WriteAsync(buffer, 0, buffer.Length - bytesToKeep);
Buffer.BlockCopy(buffer, buffer.Length - bytesToKeep, buffer, 0, bytesToKeep);
bufferDataLength = bytesToKeep;
}
}
if (bytesRead > 0)
{
await writer.WriteAsync(buffer, 0, bufferDataLength);
}
return bytesRead;
}
finally
......
......@@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
<PackageReference Include="StreamExtended" Version="1.0.124-beta" />
<PackageReference Include="StreamExtended" Version="1.0.127-beta" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
......
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