Bug 18800 - IndexOutOfRangeException when MultipartFormDataContent filename value contains whitespace
Summary: IndexOutOfRangeException when MultipartFormDataContent filename value contain...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: master
Hardware: Macintosh Windows
: Normal normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-04-03 12:44 UTC by leankitryan
Modified: 2015-03-25 10:18 UTC (History)
6 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description leankitryan 2014-04-03 12:44:57 UTC
var fileName = "~stacktrace & stuff.txt";
var content = new MultipartFormDataContent();
var fileStream = new FileStream(String.Format(@"/mnt/sdcard/Download/{0}", fileName), FileMode.Open, FileAccess.Read);
content.Add(new StreamContent(fileStream), "attachmentFile", fileName);

throws the following exception at content.Add 

System.IndexOutOfRangeException: Array index is out of range.
  at System.Net.Http.Headers.Lexer.IsValidToken (System.String input) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.ContentDispositionHeaderValue.EncodeBase64Value (System.String value) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.ContentDispositionHeaderValue.set_FileName (System.String value) [0x00000] in <filename unknown>:0 

If I remove spaces and special characters no exception is thrown. I've tried wrapping the value in quotes but that doesn't seem to help either. Inspecting the HTTP headers this code generates shows mono.droid is not wrapping the filename value in quotes however both standard.net and mono properly wrap this value in quotes and do not throw an exception.
Comment 2 Udham Singh 2014-04-04 06:40:37 UTC
Exception log : https://gist.github.com/saurabh360/6ca2d61c63db589e1287
Comment 3 Jonathan Pryor 2014-04-04 11:56:17 UTC
This is a Mono BCL bug:

$ csharp -r:System.Net.Http
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> using System.Net.Http;
csharp> using System.IO;
csharp> var content = new MultipartFormDataContent();
csharp> var source  = new StreamContent(new MemoryStream());
csharp> content.Add(source, "attachmentFile", "~stacktrace & stuff.txt");

> System.IndexOutOfRangeException: Array index is out of range.
>   at System.Net.Http.Headers.Lexer.IsValidToken (System.String input) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.Headers.ContentDispositionHeaderValue.EncodeBase64Value (System.String value) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.Headers.ContentDispositionHeaderValue.set_FileName (System.String value) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.MultipartFormDataContent.AddContentDisposition (System.Net.Http.HttpContent content, System.String name, System.String fileName) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.MultipartFormDataContent.Add (System.Net.Http.HttpContent content, System.String name, System.String fileName) [0x00000] in <filename unknown>:0 
>   at <InteractiveExpressionClass>.Host (System.Object& $retval) [0x00000] in <filename unknown>:0 
>   at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <filename unknown>:0 
>   at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <filename unknown>:0
Comment 4 Marek Safar 2014-11-27 05:59:23 UTC
Fixed in master
Comment 5 Ruben Buniatyan 2015-03-25 09:46:27 UTC
It still throws for the method Add(content, name, fileName) when the 'fileName' parameter contains double quotes.
Comment 6 Marek Safar 2015-03-25 10:18:30 UTC
content.Add(source, "attachmentFile", "\"");

fails for me with mono 4 as expected

Note You need to log in before you can comment on or make changes to this bug.