Microsoft.AspNetCore.WebUtilities Line length limi

2020-04-12 00:48发布

问题:

I've have been trying to automate a test in to upload a text file to a web api which uses flowjshandler, all running in c# .net core and linux docker containers. The test program uploads the file without exceptions when run from visual studio. However if I run the test program and web api inside a container I get this exception in the asp .net core web api framework:

Line length limit 100 exceeded., Microsoft.AspNetCore.WebUtilities,
at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.d__39.MoveNext()

I've look at Microsoft.AspNetCore.WebUtilities and found where the error is coming from but not sure how to avoid 100 character limit. I have tried to vary the upload files.

It is a multi part message with content

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarym45GFZc25WVhjtVB
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowChunkNumber"

1
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowChunkSize"

1048576
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowCurrentChunkSize"

440
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowTotalSize"

440
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowIdentifier"

440-Boundarystxt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowFilename"

Boundarys.txt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowRelativePath"

Boundarys.txt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowTotalChunks"

1
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="file"; filename="Boundarys.txt"

回答1:

I was using Environment.Newline when running on linux containers does not recognize the end of line. Changed Newline to be \r\n in the Content-Disposition: form-data.

i.e

var nl = "\r\n";
sb.AppendFormat($"{nl}{BOUNDARY}{nl}Content-Disposition: form-data; name=\"flowChunkNumber\"{nl}{nl}{flowFileUpload.flowChunkNumber}{nl}{BOUNDARY}{nl}Content-Disposition: form-data; 


回答2:

It's important to know why this failed without the "\r\n". Have a look at the following spec: https://tools.ietf.org/html/rfc2046#section-4.1.1

The canonical form of any MIME "text" subtype MUST always represent a
line break as a CRLF sequence.  Similarly, any occurrence of CRLF in
MIME "text" MUST represent a line break.  Use of CR and LF outside of
line break sequences is also forbidden.

So it depends on how you build this string. If you used a StringBuilder and built your request line by line with AppendLine(), that would work on Windows but not Linux. AppendLine uses the Environment.NewLine, which is CRLF for Windows and LF for Linux. Under the covers, BufferedReadStream is a stickler about new lines being delineated by CRLF as per the spec. So in this case, you need to be explicit and use \r\n.