MCPcopy Create free account
hub / github.com/fastify/fastify / buildRequestWithTrustProxy

Function buildRequestWithTrustProxy

lib/request.js:102–150  ·  view source on GitHub ↗
(R, trustProxy)

Source from the content-addressed store, hash-verified

100}
101
102function buildRequestWithTrustProxy (R, trustProxy) {
103 const _Request = buildRegularRequest(R)
104 const proxyFn = getTrustProxyFn(trustProxy)
105
106 // This is a more optimized version of decoration
107 _Request[kHasBeenDecorated] = true
108
109 Object.defineProperties(_Request.prototype, {
110 ip: {
111 get () {
112 const addrs = proxyAddr.all(this.raw, proxyFn)
113 return addrs[addrs.length - 1]
114 }
115 },
116 ips: {
117 get () {
118 return proxyAddr.all(this.raw, proxyFn)
119 }
120 },
121 host: {
122 get () {
123 const socket = this.raw.socket
124 if (this.headers['x-forwarded-host'] && socket != null && proxyFn(socket.remoteAddress, 0)) {
125 return getLastEntryInMultiHeaderValue(this.headers['x-forwarded-host'])
126 }
127 /**
128 * The last fallback supports the following cases:
129 * 1. http.requireHostHeader === false
130 * 2. HTTP/1.0 without a Host Header
131 * 3. Headers schema that may remove the Host Header
132 */
133 return this.headers.host ?? this.headers[':authority'] ?? ''
134 }
135 },
136 protocol: {
137 get () {
138 const socket = this.raw.socket
139 if (this.headers['x-forwarded-proto'] && socket != null && proxyFn(socket.remoteAddress, 0)) {
140 return getLastEntryInMultiHeaderValue(this.headers['x-forwarded-proto'])
141 }
142 if (this.socket) {
143 return this.socket.encrypted ? 'https' : 'http'
144 }
145 }
146 }
147 })
148
149 return _Request
150}
151
152function assertsRequestDecoration (request, name) {
153 if (!decorators.hasKey(request, name) && !decorators.exist(request, name)) {

Callers 1

buildRequestFunction · 0.85

Calls 2

buildRegularRequestFunction · 0.85
getTrustProxyFnFunction · 0.85

Tested by

no test coverage detected