Skip to content

Commit

Permalink
made sure that request and response params or deProxied, otherwise Re…
Browse files Browse the repository at this point in the history
…quest() and Response() constructors may misinterpret them
  • Loading branch information
poef committed Jan 8, 2025
1 parent 4439d7e commit 20c0651
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/metro.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,28 @@ function getRequestParams(req, current)
for(let prop of ['method','headers','body','mode','credentials','cache','redirect',
'referrer','referrerPolicy','integrity','keepalive','signal',
'priority','url']) {
if (typeof req[prop] == 'function') {
req[prop](params[prop], params)
} else if (typeof req[prop] != 'undefined') {
let value = req[prop]
if (typeof value=='undefined' || value == null) {
continue
}
if (value?.[Symbol.metroProxy]) {
value = value[Symbol.metroSource]
}
if (typeof value == 'function') {
value(params[prop], params)
} else {
if (prop == 'url') {
params.url = url(params.url, req.url)
params.url = url(params.url, value)
} else if (prop == 'headers') {
params.headers = new Headers(current.headers)
if (!(req.headers instanceof Headers)) {
req.headers = new Headers(req.headers)
if (!(value instanceof Headers)) {
value = new Headers(req.headers)
}
for (let [key, value] of req.headers.entries()) {
params.headers.set(key, value)
for (let [key, val] of value.entries()) {
params.headers.set(key, val)
}
} else {
params[prop] = req[prop]
params[prop] = value
}
}
}
Expand Down Expand Up @@ -453,13 +460,20 @@ function getResponseParams(res, current)
params.url = current.url
}
for(let prop of ['status','statusText','headers','body','url','type','redirected']) {
if (typeof res[prop] == 'function') {
res[prop](params[prop], params)
} else if (typeof res[prop] != 'undefined') {
let value = res[prop]
if (typeof value == 'undefined' || value == null) {
continue
}
if (value?.[Symbol.metroProxy]) {
value = value[Symbol.metroSource]
}
if (typeof value == 'function') {
value(params[prop], params)
} else {
if (prop == 'url') {
params.url = new URL(res.url, params.url || 'https://localhost/')
params.url = new URL(value, params.url || 'https://localhost/')
} else {
params[prop] = res[prop]
params[prop] = value
}
}
}
Expand Down

0 comments on commit 20c0651

Please sign in to comment.