Commit e946e11c authored by Honfika's avatar Honfika

multipart form data reading fixed

parent 57667e1b
...@@ -51,8 +51,8 @@ ...@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="StreamExtended, Version=1.0.124.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL"> <Reference Include="StreamExtended, Version=1.0.127.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.124-beta\lib\net45\StreamExtended.dll</HintPath> <HintPath>..\..\packages\StreamExtended.1.0.127-beta\lib\net45\StreamExtended.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="StreamExtended" version="1.0.124-beta" targetFramework="net45" /> <package id="StreamExtended" version="1.0.127-beta" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
...@@ -620,29 +620,28 @@ namespace Titanium.Web.Proxy ...@@ -620,29 +620,28 @@ namespace Titanium.Web.Proxy
//send the request body bytes to server //send the request body bytes to server
if (request.ContentLength > 0 && args.HasMulipartEventSubscribers && request.IsMultipartFormData) if (request.ContentLength > 0 && args.HasMulipartEventSubscribers && request.IsMultipartFormData)
{ {
var inputStream = args.ProxyClient.ClientStream;
string boundary = HttpHelper.GetBoundaryFromContentType(request.ContentType); string boundary = HttpHelper.GetBoundaryFromContentType(request.ContentType);
long bytesToSend = request.ContentLength; 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) if (read == 0)
{ {
break; break;
} }
bytesToSend -= read; if (bytesToSend > copyStream.ReadBytes)
if (bytesToSend > 0)
{ {
var headers = new HeaderCollection(); var headers = new HeaderCollection();
//var stream = new CustomBufferedPeekStream(inputStream); await HeaderParser.ReadHeaders(copyStreamReader, headers);
//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); args.OnMultipartRequestPartSent(boundary, headers);
} }
} }
await copyStream.FlushAsync();
} }
else else
{ {
...@@ -654,11 +653,10 @@ namespace Titanium.Web.Proxy ...@@ -654,11 +653,10 @@ namespace Titanium.Web.Proxy
/// Read a line from the byte stream /// Read a line from the byte stream
/// </summary> /// </summary>
/// <returns></returns> /// <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; int bufferDataLength = 0;
// try to use the thread static buffer
var buffer = BufferPool.GetBuffer(BufferSize); var buffer = BufferPool.GetBuffer(BufferSize);
try try
{ {
...@@ -676,8 +674,7 @@ namespace Titanium.Web.Proxy ...@@ -676,8 +674,7 @@ namespace Titanium.Web.Proxy
if (bufferDataLength >= boundaryLength) if (bufferDataLength >= boundaryLength)
{ {
int startIdx = bufferDataLength - boundaryLength; int startIdx = bufferDataLength - boundaryLength;
if (buffer[startIdx] == '-' && buffer[startIdx + 1] == '-' if (buffer[startIdx] == '-' && buffer[startIdx + 1] == '-')
&& buffer[bufferDataLength - 2] == '\r' && buffer[bufferDataLength - 1] == '\n')
{ {
startIdx += 2; startIdx += 2;
bool ok = true; bool ok = true;
...@@ -701,17 +698,11 @@ namespace Titanium.Web.Proxy ...@@ -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 //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; const int bytesToKeep = 100;
await writer.WriteAsync(buffer, 0, buffer.Length - bytesToKeep);
Buffer.BlockCopy(buffer, buffer.Length - bytesToKeep, buffer, 0, bytesToKeep); Buffer.BlockCopy(buffer, buffer.Length - bytesToKeep, buffer, 0, bytesToKeep);
bufferDataLength = bytesToKeep; bufferDataLength = bytesToKeep;
} }
} }
if (bytesRead > 0)
{
await writer.WriteAsync(buffer, 0, bufferDataLength);
}
return bytesRead; return bytesRead;
} }
finally finally
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" /> <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>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <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