Skip to content

Commit b796e73

Browse files
committed
refactor(routing): rewrite HEAD to GET at request edge
- Remember original HEAD method and strip body only at response end - Remove HEAD-to-GET route lookup retry fallback - Rewrite incoming HEAD request to GET before routing
1 parent 04d7b1c commit b796e73

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/routing/Handler.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ export class Handler {
187187
return middlewareResult
188188
}
189189
}
190-
let routeResult = this.routerInstance.find(req.method, holder.parsedUrl.pathname)
191-
if (!routeResult && req.method === 'HEAD') {
192-
routeResult = this.routerInstance.find('GET', holder.parsedUrl.pathname)
193-
}
190+
const routeResult = this.routerInstance.find(req.method, holder.parsedUrl.pathname)
194191
if (routeResult) {
195192
const routeEntry = 'data' in routeResult ? routeResult.data : null
196193
if (!routeEntry) {
@@ -255,6 +252,10 @@ export class Handler {
255252
}
256253
}
257254
return async (req: Request, info?: Deno.ServeHandlerInfo) => {
255+
const isHead = req.method === 'HEAD'
256+
if (isHead) {
257+
req = new Core.API.Request(req, { method: 'GET' })
258+
}
258259
const observe = this.events.hasListeners()
259260
const requestStart = observe ? performance.now() : 0
260261
const holder: Types.RequestHolder = {
@@ -314,7 +315,7 @@ export class Handler {
314315
timedOut
315316
)
316317
}
317-
if (req.method === 'HEAD') {
318+
if (isHead) {
318319
return await Routing.Respond.toHeadResponse(finalResponse)
319320
}
320321
return finalResponse

0 commit comments

Comments
 (0)