-
Notifications
You must be signed in to change notification settings - Fork 521
Use status codes defined in HttpAbstractions #1285
Conversation
It's harmless but we use hardcoded status codes all over. Either let's leave it as it is, or change other places to use those consts. Given numeric status codes are nothing cryptic (i.e. people are familiar with them), and not bound to change, I don't really see the need to do this other than to avoid typos (but tests should catch those). We do have dupe code for reason phrases - https://github.com/aspnet/KestrelHttpServer/blob/49ff98f8cbcf511c343a7076ecdaece2b5306462/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ReasonPhrases.cs. Consolidating that would be a more interesting change. |
@Tratcher We could pass in the strings from HttpAbstractions to the code that pre-encodes the reason phrases, so we don't have duplicate strings in the two repos. Would have to make the dict in HttpAbstractions public though. |
I do like this change. I also don't think it needs to be all-or-none. We can gradually make our codebase better every day! 😄 |
@CesarBS @Tratcher Before I make the suggested changes, let's work out what to do regarding some of the status codes.
|
A) Avoid removing any existing definitions where possible. Limit compiler breaking changes. |
41490a6
to
ba2d237
Compare
🆙📅 |
statusCode != 304; | ||
return statusCode != StatusCodes.Status204NoContent && | ||
statusCode != StatusCodes.Status205ResetContent && | ||
statusCode != StatusCodes.Status304NotModified; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this part.
private static readonly byte[] _bytesStatus511 = CreateStatusBytes(StatusCodes.Status511NetworkAuthenticationRequired); | ||
|
||
private static byte[] CreateStatusBytes(int statusCode) => | ||
Encoding.ASCII.GetBytes(statusCode.ToString(CultureInfo.InvariantCulture) + " " + WebUtilities.ReasonPhrases.GetReasonPhrase(statusCode)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd want at least an assert here that GetReasonPhrase returned a non-empty value in case they get out of sync. Or tests for each of these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I'm also considering other ways to keep them in sync since we might forget to add a status code here. Potentially we'll use reflection to scan for all the status codes defined and generate code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a good candidate for Microsoft.AspNetCore.Server.Kestrel.GeneratedCode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That still requires us to maintain a list of all the status codes somewhere since the codes are defined as consts. It would be easier if StatusCodes were an enum instead and we can use Enum.GetValues(). That'd be a rather dramatic change though.
f32a13b
to
2ee8c96
Compare
2ee8c96
to
57b3685
Compare
Reaction to aspnet/HttpAbstractions#748
cc @CesarBS