Skip to content

Commit 76e45ca

Browse files
committed
Obsoleted MimeReader.OnMultipartBoundary() and OnMultipartEndBoundary()
Replaced OnMultipartBoundary() with * OnMultipartBoundaryBegin() * OnMultipartBoundaryRead() * OnMultipartBoundaryEnd() Replaced OnMultipartEndBoundary() with * OnMultipartEndBoundaryBegin() * OnMultipartEndBoundaryRead() * OnMultipartEndBoundaryEnd() This will allow implementers to clone the *exact* boundary marker that was encountered in the original stream, including any trailing whitespace.
1 parent de1c68b commit 76e45ca

File tree

3 files changed

+285
-59
lines changed

3 files changed

+285
-59
lines changed

MimeKit/AsyncMimeReader.cs

+43-23
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,50 @@ async Task StepHeadersAsync (CancellationToken cancellationToken)
291291
await OnHeadersEndAsync (headerBlockBegin, headersBeginLineNumber, headerBlockEnd, lineNumber, cancellationToken).ConfigureAwait (false);
292292
}
293293

294-
bool SkipBoundaryMarker (bool endBoundary)
294+
async Task<bool> SkipBoundaryMarkerAsync (string boundary, bool endBoundary, CancellationToken cancellationToken)
295295
{
296+
long beginOffset = GetOffset (inputIndex);
297+
int beginLineNumber = lineNumber;
298+
int startIndex = inputIndex;
299+
bool result;
300+
301+
if (endBoundary)
302+
await OnMultipartEndBoundaryBeginAsync (beginOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
303+
else
304+
await OnMultipartBoundaryBeginAsync (beginOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
305+
296306
unsafe {
297307
fixed (byte* inbuf = input) {
298-
return SkipBoundaryMarker (inbuf, endBoundary);
308+
result = SkipBoundaryMarkerInternal (inbuf, endBoundary);
299309
}
300310
}
311+
312+
int count = inputIndex - startIndex;
313+
314+
if (endBoundary)
315+
await OnMultipartEndBoundaryReadAsync (input, startIndex, count, beginOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
316+
else
317+
await OnMultipartBoundaryReadAsync (input, startIndex, count, beginOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
318+
319+
long endOffset = GetOffset (inputIndex);
320+
321+
if (endBoundary) {
322+
await OnMultipartEndBoundaryEndAsync (beginOffset, beginLineNumber, endOffset, cancellationToken).ConfigureAwait (false);
323+
324+
#pragma warning disable 618
325+
// Obsolete
326+
await OnMultipartEndBoundaryAsync (boundary, beginOffset, endOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
327+
#pragma warning restore 618
328+
} else {
329+
await OnMultipartBoundaryEndAsync (beginOffset, beginLineNumber, endOffset, cancellationToken).ConfigureAwait (false);
330+
331+
#pragma warning disable 618
332+
// Obsolete
333+
await OnMultipartBoundaryAsync (boundary, beginOffset, endOffset, beginLineNumber, cancellationToken).ConfigureAwait (false);
334+
#pragma warning restore 618
335+
}
336+
337+
return result;
301338
}
302339

303340
async Task<MimeParserState> StepAsync (CancellationToken cancellationToken)
@@ -497,18 +534,13 @@ async Task MultipartScanEpilogueAsync (CancellationToken cancellationToken)
497534

498535
async Task MultipartScanSubpartsAsync (ContentType multipartContentType, int depth, CancellationToken cancellationToken)
499536
{
500-
var boundaryOffset = GetOffset (inputIndex);
501-
502537
do {
503538
// skip over the boundary marker
504-
if (!SkipBoundaryMarker (endBoundary: false)) {
505-
await OnMultipartBoundaryAsync (multipartContentType.Boundary, boundaryOffset, GetOffset (inputIndex), lineNumber, cancellationToken).ConfigureAwait (false);
539+
if (!await SkipBoundaryMarkerAsync (multipartContentType.Boundary, endBoundary: false, cancellationToken).ConfigureAwait (false)) {
506540
boundary = BoundaryType.Eos;
507541
return;
508542
}
509543

510-
await OnMultipartBoundaryAsync (multipartContentType.Boundary, boundaryOffset, GetOffset (inputIndex), lineNumber - 1, cancellationToken).ConfigureAwait (false);
511-
512544
var beginLineNumber = lineNumber;
513545

514546
// Note: When parsing non-toplevel parts, the header parser will never result in the Error state.
@@ -517,10 +549,9 @@ async Task MultipartScanSubpartsAsync (ContentType multipartContentType, int dep
517549

518550
if (state == MimeParserState.Boundary) {
519551
if (headerCount == 0) {
520-
if (boundary == BoundaryType.ImmediateBoundary) {
521-
//beginOffset = GetOffset (inputIndex);
552+
if (boundary == BoundaryType.ImmediateBoundary)
522553
continue;
523-
}
554+
524555
return;
525556
}
526557

@@ -565,8 +596,6 @@ async Task MultipartScanSubpartsAsync (ContentType multipartContentType, int dep
565596
await OnMimePartEndAsync (type, currentBeginOffset, beginLineNumber, currentHeadersEndOffset, endOffset, lines, cancellationToken).ConfigureAwait (false);
566597
break;
567598
}
568-
569-
boundaryOffset = endOffset;
570599
} while (boundary == BoundaryType.ImmediateBoundary);
571600
}
572601

@@ -598,14 +627,7 @@ async Task<int> ConstructMultipartAsync (ContentType contentType, int depth, Can
598627

599628
if (boundary == BoundaryType.ImmediateEndBoundary) {
600629
// consume the end boundary and read the epilogue (if there is one)
601-
// FIXME: multipart.WriteEndBoundary = true;
602-
603-
var boundaryOffset = GetOffset (inputIndex);
604-
var boundaryLineNumber = lineNumber;
605-
606-
SkipBoundaryMarker (endBoundary: true);
607-
608-
await OnMultipartEndBoundaryAsync (marker, boundaryOffset, GetOffset (inputIndex), boundaryLineNumber, cancellationToken).ConfigureAwait (false);
630+
await SkipBoundaryMarkerAsync (marker, endBoundary: true, cancellationToken).ConfigureAwait (false);
609631

610632
PopBoundary ();
611633

@@ -616,8 +638,6 @@ async Task<int> ConstructMultipartAsync (ContentType contentType, int depth, Can
616638
return GetLineCount (beginLineNumber, beginOffset, endOffset);
617639
}
618640

619-
// FIXME: multipart.WriteEndBoundary = false;
620-
621641
// We either found the end of the stream or we found a parent's boundary
622642
PopBoundary ();
623643

MimeKit/ExperimentalMimeParser.cs

+2-18
Original file line numberDiff line numberDiff line change
@@ -593,33 +593,17 @@ protected override void OnMultipartBegin (ContentType contentType, long beginOff
593593
depth++;
594594
}
595595

596-
/// <summary>
597-
/// Called when a multipart boundary is encountered in the stream.
598-
/// </summary>
599-
/// <remarks>
600-
/// Called when a multipart boundary is encountered in the stream.
601-
/// </remarks>
602-
/// <param name="boundary">The multipart boundary string.</param>
603-
/// <param name="beginOffset">The offset into the stream where the boundary marker began.</param>
604-
/// <param name="endOffset">The offset into the stream where the boundary marker ended.</param>
605-
/// <param name="lineNumber">The line number where the boundary marker was found in the stream.</param>
606-
/// <param name="cancellationToken">The cancellation token.</param>
607-
protected override void OnMultipartBoundary (string boundary, long beginOffset, long endOffset, int lineNumber, CancellationToken cancellationToken)
608-
{
609-
}
610-
611596
/// <summary>
612597
/// Called when a multipart end boundary is encountered in the stream.
613598
/// </summary>
614599
/// <remarks>
615600
/// Called when a multipart end boundary is encountered in the stream.
616601
/// </remarks>
617-
/// <param name="boundary">The multipart boundary string.</param>
618602
/// <param name="beginOffset">The offset into the stream where the boundary marker began.</param>
619-
/// <param name="endOffset">The offset into the stream where the boundary marker ended.</param>
620603
/// <param name="lineNumber">The line number where the boundary marker was found in the stream.</param>
604+
/// <param name="endOffset">The offset into the stream where the boundary marker ended.</param>
621605
/// <param name="cancellationToken">The cancellation token.</param>
622-
protected override void OnMultipartEndBoundary (string boundary, long beginOffset, long endOffset, int lineNumber, CancellationToken cancellationToken)
606+
protected override void OnMultipartEndBoundaryEnd (long beginOffset, int lineNumber, long endOffset, CancellationToken cancellationToken)
623607
{
624608
var multipart = (Multipart) stack.Peek ();
625609

0 commit comments

Comments
 (0)