Skip to content

Commit

Permalink
Merge 732d08e into 57186ea
Browse files Browse the repository at this point in the history
  • Loading branch information
dnik2000 committed Mar 9, 2020
2 parents 57186ea + 732d08e commit c42d4e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
12 changes: 7 additions & 5 deletions Source/HttpMultipartParser/MultipartFormDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ private void ParseStream(Stream stream, string boundary, Encoding encoding, int
var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize);
streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart);

streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes) =>
streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNo) =>
{
if (Files.Count == 0 || name != Files[Files.Count - 1].Name)
if (partNo == 0)
{
// create file with first partNo
Files.Add(new FilePart(name, fileName, Utilities.MemoryStreamManager.GetStream(), type, disposition));
}
Expand Down Expand Up @@ -601,10 +602,11 @@ private async Task ParseStreamAsync(Stream stream, string boundary, Encoding enc
var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize);
streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart);

streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes) =>
streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber) =>
{
if (Files.Count == 0 || name != Files[Files.Count - 1].Name)
if (partNo == 0)
{
// create file with first partNo
Files.Add(new FilePart(name, fileName, Utilities.MemoryStreamManager.GetStream(), type, disposition));
}
Expand All @@ -622,4 +624,4 @@ private async Task ParseStreamAsync(Stream stream, string boundary, Encoding enc

#endregion
}
}
}
19 changes: 13 additions & 6 deletions Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
/// <param name="contentDisposition">The content disposition of the multipart data.</param>
/// <param name="buffer">Some of the data from the file (not neccecarily all of the data).</param>
/// <param name="bytes">The length of data in buffer.</param>
/// <param name="partNo">Serial number of long file part (begins from 0).</param>
public delegate void FileStreamDelegate(
string name, string fileName, string contentType, string contentDisposition, byte[] buffer, int bytes);
string name, string fileName, string contentType, string contentDisposition, byte[] buffer, int bytes, int partNumber);

/// <summary>
/// The StreamClosedDelegate defining functions that can handle stream being closed.
Expand Down Expand Up @@ -551,6 +552,8 @@ private async Task ParseAsync(RebufferableBinaryReader reader, CancellationToken
/// </param>
private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBinaryReader reader)
{
int partNumber = 0; // begins count parts of file from 0

string name = parameters["name"];
string filename = parameters["filename"];

Expand Down Expand Up @@ -652,7 +655,7 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
// We also want to chop off the newline that is inserted by the protocl.
// We can do this by reducing endPos by the length of newline in this environment
// and encoding
FileHandler(name, filename, contentType, contentDisposition, fullBuffer, endPos - bufferNewlineLength);
FileHandler(name, filename, contentType, contentDisposition, fullBuffer, endPos - bufferNewlineLength, partNumber);

int writeBackOffset = endPos + endPosLength + boundaryNewlineOffset;
int writeBackAmount = (prevLength + curLength) - writeBackOffset;
Expand All @@ -662,7 +665,8 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
}

// No end, consume the entire previous buffer
FileHandler(name, filename, contentType, contentDisposition, prevBuffer, prevLength);
FileHandler(name, filename, contentType, contentDisposition, prevBuffer, prevLength, partNumber);
partNumber++; // increase part counter

// Now we want to swap the two buffers, we don't care
// what happens to the data from prevBuffer so we set
Expand Down Expand Up @@ -700,6 +704,8 @@ private void ParseFilePart(Dictionary<string, string> parameters, RebufferableBi
/// </returns>
private async Task ParseFilePartAsync(Dictionary<string, string> parameters, RebufferableBinaryReader reader, CancellationToken cancellationToken = default)
{
int partNumber = 0; // begins count parts of file from 0

string name = parameters["name"];
string filename = parameters["filename"];

Expand Down Expand Up @@ -801,7 +807,7 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
// We also want to chop off the newline that is inserted by the protocl.
// We can do this by reducing endPos by the length of newline in this environment
// and encoding
FileHandler(name, filename, contentType, contentDisposition, fullBuffer, endPos - bufferNewlineLength);
FileHandler(name, filename, contentType, contentDisposition, fullBuffer, endPos - bufferNewlineLength, partNumber);

int writeBackOffset = endPos + endPosLength + boundaryNewlineOffset;
int writeBackAmount = (prevLength + curLength) - writeBackOffset;
Expand All @@ -811,7 +817,8 @@ private async Task ParseFilePartAsync(Dictionary<string, string> parameters, Reb
}

// No end, consume the entire previous buffer
FileHandler(name, filename, contentType, contentDisposition, prevBuffer, prevLength);
FileHandler(name, filename, contentType, contentDisposition, prevBuffer, prevLength, partNumber);
partNumber++; // increase part counter

// Now we want to swap the two buffers, we don't care
// what happens to the data from prevBuffer so we set
Expand Down Expand Up @@ -1155,4 +1162,4 @@ private IEnumerable<string> SplitBySemicolonIgnoringSemicolonsInQuotes(string li

#endregion
}
}
}

0 comments on commit c42d4e0

Please sign in to comment.