Skip to content

Instantly share code, notes, and snippets.

@abock
Created January 3, 2014 19:26
Show Gist options
  • Save abock/91240f26ef40964fc388 to your computer and use it in GitHub Desktop.
Save abock/91240f26ef40964fc388 to your computer and use it in GitHub Desktop.
Handle multiple filename parameters (e.g. as passed by browsers through a Content-Disposition header when uploading content) such as "filename=foo.txt; filename*=UTF-8''foo.txt" (recommended by RFC 6266 Appendix D: http://tools.ietf.org/html/rfc6266#appendix-D)
diff --git a/MimeKit/ContentDisposition.cs b/MimeKit/ContentDisposition.cs
index 4d5355a..69d80f3 100644
--- a/MimeKit/ContentDisposition.cs
+++ b/MimeKit/ContentDisposition.cs
@@ -411,7 +411,7 @@ namespace MimeKit {
return true;
ParameterList parameters;
- if (!ParameterList.TryParse (options, text, ref index, endIndex, throwOnError, out parameters))
+ if (!ParameterList.TryParse (options, text, ref index, endIndex, throwOnError, true, out parameters))
return false;
disposition.Parameters = parameters;
diff --git a/MimeKit/ContentType.cs b/MimeKit/ContentType.cs
index d15f1ed..2bda133 100644
--- a/MimeKit/ContentType.cs
+++ b/MimeKit/ContentType.cs
@@ -439,7 +439,7 @@ namespace MimeKit {
return true;
ParameterList parameters;
- if (!ParameterList.TryParse (options, text, ref index, endIndex, throwOnError, out parameters))
+ if (!ParameterList.TryParse (options, text, ref index, endIndex, throwOnError, false, out parameters))
return false;
contentType.Parameters = parameters;
diff --git a/MimeKit/ParameterList.cs b/MimeKit/ParameterList.cs
index a8b4e54..173dd92 100644
--- a/MimeKit/ParameterList.cs
+++ b/MimeKit/ParameterList.cs
@@ -710,7 +710,7 @@ namespace MimeKit {
return new string (output, 0, outLength);
}
- internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, out ParameterList paramList)
+ internal static bool TryParse (ParserOptions options, byte[] text, ref int index, int endIndex, bool throwOnError, bool respectRfc6266, out ParameterList paramList)
{
var rfc2184 = new Dictionary<string, List<NameValuePair>> (icase);
var @params = new List<NameValuePair> ();
@@ -807,6 +807,17 @@ namespace MimeKit {
value = string.Empty;
}
+ // RFC6266 (appendix D) recommends including a filename* parameter with an encoding
+ // containing the encoded filename following an ASCII filename parameter for legacy
+ // browser compatibilty; MimeKit handles the parsing of the *=<ENCODING> parameters,
+ // but does not allow multiples with the same name; in this case remove the preivous
+ // one, which should be the ASCII one; a more thorough fix for this would be to
+ // more accurately differentiate parameters with the same name but different encodings
+ // inside TryParseNameValuePair.
+ int filenameIndex = -1;
+ if (respectRfc6266 && param.Name == "filename" && (filenameIndex = paramList.IndexOf ("filename")) >= 0)
+ paramList.RemoveAt (filenameIndex);
+
paramList.Add (new Parameter (param.Name, value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment