Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Use enum for method rather than string compares#2027

Closed
benaadams wants to merge 2 commits intoaspnet:devfrom
benaadams:method
Closed

Use enum for method rather than string compares#2027
benaadams wants to merge 2 commits intoaspnet:devfrom
benaadams:method

Conversation

@benaadams
Copy link
Contributor

@benaadams benaadams commented Aug 27, 2017

For GET method IsHead test is performed on the string Method which since GET isn't reference equal to HEAD drops through to String.Equals -> OrdinalIgnoreCaseComparer.Equals

However the Parser starts with it as an Enum, which then gets converted to a string; so it can just be tested with the enum.

Http/2 change isn't so good; but ti gets method from header collection, so that probably needs a more general revisit

Resolves #2020

@mariuszkochanowski
Copy link
Contributor

I think that enum types should inherit from lowest possible type. HttpMethod can inherit from sbyte.

@benaadams
Copy link
Contributor Author

I think that enum types should inherit from lowest possible type. HttpMethod can inherit from sbyte.

Rather than byte as it does currently? what advantage would it confer?

@blowdart
Copy link
Member

It'd mean adding values is an even bigger breaking change than normal if people have decided to store it in a database somewhere.


Custom,

Unknown = byte.MaxValue
Copy link
Member

Choose a reason for hiding this comment

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

It's best practice to make Unknown/None the zero value, the default for uninitialized fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its used as the index into _methodNames array and does some pretty weird things based on with the number/order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could just reset it to Get rather than Unknown; isn't a path where it can't get set to something

}
set
{
CustomMethod = HttpUtilities.GetKnownMethod(value, out var method) ? null : value.ToUpperInvariant();
Copy link
Member

Choose a reason for hiding this comment

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

This mirrors the Http.Sys APIs

@cesarblum
Copy link
Contributor

cesarblum commented Aug 28, 2017

Http/2 change isn't so good; but ti gets method from header collection, so that probably needs a more general revisit

Yep, what's there now is just what's needed to make it work. Still needs to be optimized (not just the method but all the pseudo-header field reads).

var firstUppercaseChar = (char)(value[0] & ~0x20);
if (length == 3)
{
if (firstUppercaseChar == 'G' && string.Equals(value, "GET", StringComparison.OrdinalIgnoreCase))
Copy link
Contributor

Choose a reason for hiding this comment

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

OrdinalIgnoreCase

https://tools.ietf.org/html/rfc7230#section-3.1.1:

The request method is case-sensitive.

Copy link
Contributor Author

@benaadams benaadams Aug 28, 2017

Choose a reason for hiding this comment

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

Sometimes wonder if they flip coins to determine what is case sensitive in the specs. Will change

Copy link
Member

@Tratcher Tratcher Aug 28, 2017

Choose a reason for hiding this comment

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

When it doubt use case in-sensitive for parsing, regardless of the spec.

Copy link
Member

Choose a reason for hiding this comment

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

@Tratcher I would go with stricter parsing when in doubt. The risk is that Kestrel might interpret a request differently than a proxy. For instance, imagine a proxy is configured with rules that should be applied to all POST requests to a given endpoint. With case-insensitive parsing in Kestrel, these rules could possibly be bypassed by sending a pOsT request.

@ghost ghost removed the cla-already-signed label Nov 14, 2017
@davidfowl
Copy link
Member

This PR needs to do over. We refactored everything for http2

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants