hyperium / http

Rust HTTP types
Apache License 2.0
1.12k stars 283 forks source link

Why does canonical_reason() return a Option? #663

Closed danechitoaie closed 5 months ago

danechitoaie commented 5 months ago

Why does canonical_reason() return a Option?

It feels a bit weird that if I do:

StatusCode::BAD_REQUEST.canonical_reason()

I also have to check if it's Some, or None to actually get the "Bad Request" string when I know for sure it exists as otherwise I wouldn't been able to do StatusCode::BAD_REQUEST.

Is there any other cleaner way to get that value?

seanmonstar commented 5 months ago

That's because all status codes do not have a canonical reason. For instance, StatusCode::from_u16(220) would be valid, but it doesn't have a known reason phrase.

danechitoaie commented 5 months ago

Yes, makes sense. But still feels weird when using the normal, default, well established status codes. 99% of the cases people will work with those statuses, when for sure they will have a canonical reason. And just for those 1% of the cases when people build some unusual statuses we have to add additional boiler plate. Maybe there can be a way so that when using a default predefined status to have a method to get the text/canonical reason that returns its &str directly? And only when someone builds a custom one ::from_u16(...) to have option result? Just an idea, but if it's not possible and it is what it is then we'll have to learn deal with it.

seanmonstar commented 5 months ago

That would require a separate type, which can only represent known status codes.

Regarding the reason phrase, for what it's worth, the HTTP specifications have deprecated them, and they do not exist in HTTP/2 or 3.