Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions functions/http/Http.Tests/ParseContentTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ public class ParseContentTypeTest : FunctionTestBase<ParseContentType.Function>
{ new FormUrlEncodedContent(new[] { KeyValuePair.Create("name", "Ginger") }), "Ginger" },
{ new FormUrlEncodedContent(new[] { KeyValuePair.Create("not-name", "foo") }), null} ,
{ new StringContent("other-text", Encoding.UTF8, "application/other-content"), null },
// ByteArrayContent doesn't set a Content-Type of its own, so this
// one arrives with no header at all.
{ new ByteArrayContent(Encoding.UTF8.GetBytes("{\"name\":\"John\"}")), null },
{ BodyWithContentType("text", "{\"name\":\"John\"}"), null },
};

// A media type with no subtype, which the framework can't parse.
private static HttpContent BodyWithContentType(string contentType, string body)
{
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(body));
content.Headers.TryAddWithoutValidation("Content-Type", contentType);
return content;
}

[Theory]
[MemberData(nameof(TestData))]
public async Task Process(HttpContent content, string expectedName)
Expand Down
14 changes: 13 additions & 1 deletion functions/http/ParseContentType/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public async Task HandleAsync(HttpContext context)
HttpResponse response = context.Response;

string name = null;
ContentType contentType = new ContentType(request.ContentType);

ContentType contentType;
try
{
contentType = new ContentType(request.ContentType);
}
catch (Exception e) when (e is ArgumentException or FormatException)
{
// Missing Content-Type header, or one that isn't a valid media type
// (e.g. "text"). Same handling as the unrecognized types below.
response.StatusCode = (int) HttpStatusCode.BadRequest;
return;
}
Comment on lines +38 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using exceptions for expected control flow (such as a missing or empty Content-Type header) is a performance anti-pattern in .NET. By checking string.IsNullOrEmpty(request.ContentType) first, we can handle the missing header case gracefully without throwing and catching an ArgumentException (or ArgumentNullException). This also allows us to simplify the catch block to only handle FormatException for malformed content types.

        if (string.IsNullOrEmpty(request.ContentType))
        {
            response.StatusCode = (int) HttpStatusCode.BadRequest;
            return;
        }

        ContentType contentType;
        try
        {
            contentType = new ContentType(request.ContentType);
        }
        catch (FormatException)
        {
            // One that isn't a valid media type (e.g. "text"). Same handling as the unrecognized types below.
            response.StatusCode = (int) HttpStatusCode.BadRequest;
            return;
        }


switch (contentType.MediaType)
{
Expand Down
Loading