Skip to content

Kestrel Content-Length handling changes#43103

Merged
adityamandaleeka merged 6 commits intodotnet:mainfrom
adityamandaleeka:kestrel_content_length_nonbody
Aug 5, 2022
Merged

Kestrel Content-Length handling changes#43103
adityamandaleeka merged 6 commits intodotnet:mainfrom
adityamandaleeka:kestrel_content_length_nonbody

Conversation

@adityamandaleeka
Copy link
Copy Markdown
Member

This includes two changes to how we handle Content-Length on responses:

  • Forbid the inclusion of the Content-Length header on 1xx, 204 responses, or any 2xx responses to a CONNECT request.
  • Forbid non-zero Content-Length for 205 responses.

Fixes #42956

// with a status code of 1xx (Informational) or 204 (No Content).
return false;
}
else if (Method == HttpMethod.Connect && Is2xxCode(StatusCode))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: single if with additional || (Method == HttpMethod.Connect && Is2xxCode(StatusCode)) ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only if Method == HttpMethod.Connect && Is2xxCode(StatusCode) is moved to a local Is2xxConnect() local function or something. I don't like nested parens in conditions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yea, I chose to do it this way because of readability (and it makes it clear which comments belong to each part). Will keep it as-is unless there's strong objection.

Copy link
Copy Markdown
Member

@HaoK HaoK left a comment

Choose a reason for hiding this comment

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

Looks good except some minor nits

// with a status code of 1xx (Informational) or 204 (No Content).
return false;
}
else if (Method == HttpMethod.Connect && Is2xxCode(StatusCode))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only if Method == HttpMethod.Connect && Is2xxCode(StatusCode) is moved to a local Is2xxConnect() local function or something. I don't like nested parens in conditions.

Comment on lines +1253 to +1254
static bool Is1xxCode(int code) => code >= StatusCodes.Status100Continue && code < StatusCodes.Status200OK;
static bool Is2xxCode(int code) => code >= StatusCodes.Status200OK && code < StatusCodes.Status300MultipleChoices;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
static bool Is1xxCode(int code) => code >= StatusCodes.Status100Continue && code < StatusCodes.Status200OK;
static bool Is2xxCode(int code) => code >= StatusCodes.Status200OK && code < StatusCodes.Status300MultipleChoices;
static bool Is1xxCode(int code) => code is >= StatusCodes.Status100Continue and < StatusCodes.Status200OK;
static bool Is2xxCode(int code) => code is >= StatusCodes.Status200OK and < StatusCodes.Status300MultipleChoices;

Cf. dotnet/roslyn#60534 -- Roslyn can produce optimized code with the pattern matching by using an uint-cast that saves one comparison at runtime. Hopefully this will be implemented soon 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix Kestrel's Content-Length handling for 1xx, 204 and 205 responses and CONNECT requests

5 participants