Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do headers convert into lowercase automatically? #71

Closed
theblackfort opened this issue Apr 11, 2019 · 9 comments
Closed

Why do headers convert into lowercase automatically? #71

theblackfort opened this issue Apr 11, 2019 · 9 comments

Comments

@theblackfort
Copy link

What are you trying to achieve or the steps to reproduce?

I would like to know how to capitalize headers when sending the response. Looks like Fastify converts the headers to lowercase while sending the response.

reply.header(`X-Robots-Tag`, `noindex`);

What was the result you received?

x-robots-tag: noindex

What did you expect?

X-Robots-Tag: noindex

Context

  • node version: v10.15.3
  • fastify version: 2.1.0
  • os: Mac
  • any other relevant information: None
@jsumners
Copy link
Member

The HTTP spec defines header names as case insensitive. Fastify, and require('http'), translates header names to lowercase for consistency and ease of use. Implementations that expect headers to be case sensitive are broken per https://tools.ietf.org/html/rfc2616#section-4.2:

Each header field consists
of a name followed by a colon (":") and the field value. Field names
are case-insensitive.

@theblackfort
Copy link
Author

Fair point but I ask this from an aesthetic point of view. Headers from helmet (https://github.com/fastify/fastify-helmet) are all capitalized and look much better.

Any way to achieve the same?

X-Content-Type-Options: nosniff
X-DNS-Prefetch-Control: off
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN

@nwoltman
Copy link

Fastify handles headers this way as a performance optimization. By only handling headers in a lowercased format, Fastify has better performance and uses less memory.

Also, the Internet will eventually move to HTTP/2, which only supports lowercase headers, so it's not worth getting attached to the aesthetic look of HTTP/1-style headers.

@theblackfort
Copy link
Author

“Life can only be understood backwards; but it must be lived forwards.”

@onluiz
Copy link

onluiz commented Oct 25, 2019

No workaround?

@StingyJack
Copy link

HTTP/2, which only supports lowercase headers

That is an incorrect statement. https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2

@nwoltman
Copy link

@StingyJack Semantics ¯\_(ツ)_/¯

While uppercase headers can be used at the application level, they will always be sent as lowercase.

However, header field names MUST be converted to lowercase prior to their encoding in HTTP/2. A request or response containing uppercase header field names MUST be treated as malformed (Section 8.1.2.6).

@wongsean
Copy link

wongsean commented Jan 4, 2024

Fair point, but it would be better to mention it here https://fastify.dev/docs/latest/Reference/Request/#headers.

@Uzlopak
Copy link

Uzlopak commented Jan 4, 2024

@wongsean

Feel invited to provide a PR to improve the documentation. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants