Skip to content

Commit

Permalink
Merge pull request #87 from tapmantwo/master
Browse files Browse the repository at this point in the history
Improve content disposition header parsing.
  • Loading branch information
holytshirt committed Oct 20, 2015
2 parents 309804c + 17353b4 commit 15e2034
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/OpenRasta.Tests.Unit/Web/HttpHeaders_Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public void the_filename_parameter_is_parsed()
ShouldBe("test");
}

[Test]
public void fields_can_contain_semi_colons()
{
var header = new ContentDispositionHeader("form-data;filename=\"test;name\"");
header.FileName.
ShouldBe("test;name");
}

[Test]
public void the_first_value_is_the_disposition()
{
Expand Down
7 changes: 6 additions & 1 deletion src/OpenRasta/Web/ContentDispositionHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using OpenRasta.Text;

namespace OpenRasta.Web
{
public class ContentDispositionHeader: IEquatable<ContentDispositionHeader>
{
private static readonly Regex SplitReg = new Regex(";(?=([^\"]*\"[^\"]*\")*[^\"]*$)", RegexOptions.Compiled);

public ContentDispositionHeader(string header)
{
var fragments = header.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var fragments = SplitReg.Split(header).Where(f => !string.IsNullOrEmpty(f)).ToArray();

if (fragments.Length == 0)
throw new FormatException("The header value {0} is invalid for Content-Disposition.".With(header));
Disposition = fragments[0].Trim();
Expand Down

0 comments on commit 15e2034

Please sign in to comment.