8000 docs(request): clarify request host functionality (#5904) · fastify/fastify@48ceb9d · GitHub
[go: up one dir, main page]

Skip to content

Commit 48ceb9d

Browse files
authored
docs(request): clarify request host functionality (#5904)
1 parent 0cb4273 commit 48ceb9d

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ listed in alphabetical order.
334334
<https://www.npmjs.com/~jean-michelet>
335335
* [__Maksim Sinik__](https://github.com/fox1t),
336336
<https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
337+
* [__Frazer Smith__](https://github.com/Fdawgs), <https://www.npmjs.com/~fdawgs>
337338
* [__Manuel Spigolon__](https://github.com/eomm),
338339
<https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
339-
* [__Frazer Smith__](https://github.com/Fdawgs), <https://www.npmjs.com/~fdawgs>
340340

341341
### Emeritus Contributors
342342
Great contributors on a specific area in the Fastify ecosystem will be invited
@@ -360,14 +360,14 @@ active contributors group.
360360
<https://twitter.com/serayaeryn>, <https://www.npmjs.com/~serayaeryn>
361361
* [__Rafael Gonzaga__](https://github.com/rafaelgss),
362362
<https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
363+
* [__Trivikram Kamat__](https://github.com/trivikr),
364+
<https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
363365
* [__Ayoub El Khattabi__](https://github.com/AyoubElk),
364366
<https://twitter.com/ayoubelkh>, <https://www.npmjs.com/~ayoubelk>
365367
* [__Cemre Mengu__](https://github.com/cemremengu),
366368
<https://twitter.com/cemremengu>, <https://www.npmjs.com/~cemremengu>
367369
* [__Salman Mitha__](https://github.com/salmanm),
368370
<https://www.npmjs.com/~salmanm>
369-
* [__Trivikram Kamat__](https://github.com/trivikr),
370-
<https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
371371
* [__Nathan Woltman__](https://github.com/nwoltman),
372372
<https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
373373

docs/Reference/Request.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ Request is a core Fastify object containing the following fields:
2323
- `host` - the host of the incoming request (derived from `X-Forwarded-Host`
2424
header when the [`trustProxy`](./Server.md#factory-trust-proxy) option is
2525
enabled). For HTTP/2 compatibility it returns `:authority` if no host header
26-
exists. The host header may return empty string when using
27-
`requireHostHeader = false`, not suppied when connected with `HTTP/1.0` or
28-
using schema validation that remove the extra headers.
29-
- `hostname` - the host of the `host` property, it may refers the incoming
30-
request hostname
31-
- `port` - the port of the `host` property, it may refers the port thats
32-
the server is listening on
26+
exists. The host header may return an empty string if `requireHostHeader`
27+
is false, not provided with HTTP/1.0, or removed by schema validation.
28+
- `hostname` - the hostname derived from the `host` property of
29+
the incoming request
30+
- `port` - the port from the `host` property, which may refer to
31+
the port the server is listening on
3332
- `protocol` - the protocol of the incoming request (`https` or `http`)
3433
- `method` - the method of the incoming request
3534
- `url` - the URL of the incoming request
@@ -88,8 +87,8 @@ request's headers with the `request.raw.headers` property.
8887
> Note: For performance reason on `not found` route, you may see that we will
8988
add an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
9089

91-
> Note: When using schema, it may mutate the `request.headers` and
92-
`request.raw.headers` object. So, you may found the headers becomes empty.
90+
> Note: Using schema validation may mutate the `request.headers` and
91+
`request.raw.headers` objects, causing the headers to become empty.
9392

9493
```js
9594
fastify.post('/:params', options, function (request, reply) {

lib/request.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ function buildRequestWithTrustProxy (R, trustProxy) {
117117
if (this.ip !== undefined && this.headers['x-forwarded-host']) {
118118
return getLastEntryInMultiHeaderValue(this.headers['x-forwarded-host'])
119119
}
120-
// the last fallback is used to support the following cases:
121-
// 1. support http.requireHostHeader === false
122-
// 2. support HTTP/1.0 without Host Header
123-
// 3. support headers schema which may remove the Host Header
120+
/**
121+
* The last fallback supports the following cases:
122+
* 1. http.requireHostHeader === false
123+
* 2. HTTP/1.0 without a Host Header
124+
* 3. Headers schema that may remove the Host Header
125+
*/
124126
return this.headers.host ?? this.headers[':authority'] ?? ''
125127
}
126128
},
@@ -213,10 +215,12 @@ Object.defineProperties(Request.prototype, {
213215
},
214216
host: {
215217
get () {
216-
// the last fallback is used to support the following cases:
217-
// 1. support http.requireHostHeader === false
218-
// 2. support HTTP/1.0 without Host Header
219-
// 3. support headers schema which may remove the Host Header
218+
/**
219+
* The last fallback supports the following cases:
220+
* 1. http.requireHostHeader === false
221+
* 2. HTTP/1.0 without a Host Header
222+
* 3. Headers schema that may remove the Host Header
223+
*/
220224
return this.raw.headers.host ?? this.raw.headers[':authority'] ?? ''
221225
}
222226
},

0 commit comments

Comments
 (0)
0