MCPcopy
hub / github.com/vercel/next.js / handleRequestImpl

Function handleRequestImpl

packages/next/src/server/base-server.ts:967–1622  ·  view source on GitHub ↗
(
    req: ServerRequest,
    res: ServerResponse,
    parsedUrl?: NextUrlWithParsedQuery
  )

Source from the content-addressed store, hash-verified

965 }
966
967 private async handleRequestImpl(
968 req: ServerRequest,
969 res: ServerResponse,
970 parsedUrl?: NextUrlWithParsedQuery
971 ): Promise<void> {
972 try {
973 // Wait for the matchers to be ready.
974 await this.matchers.waitTillReady()
975
976 // ensure cookies set in middleware are merged and
977 // not overridden by API routes/getServerSideProps
978 patchSetHeaderWithCookieSupport(
979 req,
980 isNodeNextResponse(res) ? res.originalResponse : res
981 )
982
983 const urlParts = (req.url || '').split('?', 1)
984 const urlNoQuery = urlParts[0]
985
986 // this normalizes repeated slashes in the path e.g. hello//world ->
987 // hello/world or backslashes to forward slashes, this does not
988 // handle trailing slash as that is handled the same as a next.config.js
989 // redirect
990 if (urlNoQuery?.match(/(\\|\/\/)/)) {
991 const cleanUrl = normalizeRepeatedSlashes(req.url!)
992 res.redirect(cleanUrl, 308).body(cleanUrl).send()
993 return
994 }
995
996 // Parse url if parsedUrl not provided
997 if (!parsedUrl || typeof parsedUrl !== 'object') {
998 if (!req.url) {
999 throw new Error('Invariant: url can not be undefined')
1000 }
1001
1002 parsedUrl = parseUrl(req.url)
1003 }
1004
1005 if (!parsedUrl.pathname) {
1006 throw new Error("Invariant: pathname can't be empty")
1007 }
1008
1009 // Parse the querystring ourselves if the user doesn't handle querystring parsing
1010 if (typeof parsedUrl.query === 'string') {
1011 parsedUrl.query = Object.fromEntries(
1012 new URLSearchParams(parsedUrl.query)
1013 )
1014 }
1015
1016 // Update the `x-forwarded-*` headers.
1017 const { originalRequest = null } = isNodeNextRequest(req) ? req : {}
1018 const xForwardedProto = originalRequest?.headers['x-forwarded-proto']
1019 const isHttps = xForwardedProto
1020 ? xForwardedProto === 'https'
1021 : !!(originalRequest?.socket as TLSSocket)?.encrypted
1022
1023 req.headers['x-forwarded-host'] ??= req.headers['host'] ?? this.hostname
1024 req.headers['x-forwarded-port'] ??= this.port

Callers

nothing calls this directly

Calls 15

isNodeNextResponseFunction · 0.90
normalizeRepeatedSlashesFunction · 0.90
parseUrlFunction · 0.90
isNodeNextRequestFunction · 0.90
getHostnameFunction · 0.90
addRequestMetaFunction · 0.90
getNextPathnameInfoFunction · 0.90
removePathPrefixFunction · 0.90
fixMojibakeFunction · 0.90
getMaxPostponedStateSizeFunction · 0.90
readBodyWithSizeLimitFunction · 0.90

Tested by

no test coverage detected