Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
388859ce
Commit
388859ce
authored
Jan 07, 2026
by
Jiachi Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename
parent
434d9daf
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
45 deletions
+43
-45
base-server.ts
packages/next/src/server/base-server.ts
+3
-3
mojibake.ts
packages/next/src/server/lib/mojibake.ts
+1
-1
resolve-routes.ts
packages/next/src/server/lib/router-utils/resolve-routes.ts
+3
-6
response.ts
packages/next/src/server/web/spec-extension/response.ts
+2
-2
utils.test.ts
packages/next/src/server/web/utils.test.ts
+16
-18
utils.ts
packages/next/src/server/web/utils.ts
+18
-15
No files found.
packages/next/src/server/base-server.ts
View file @
388859ce
...
...
@@ -111,7 +111,7 @@ import {
import
{
BaseServerSpan
}
from
'./lib/trace/constants'
import
{
I18NProvider
}
from
'./lib/i18n-provider'
import
{
sendResponse
}
from
'./send-response'
import
{
normalizeNextQueryParam
,
decode
Node
HeaderValue
}
from
'./web/utils'
import
{
normalizeNextQueryParam
,
decodeHeaderValue
}
from
'./web/utils'
import
{
HTML_CONTENT_TYPE_HEADER
,
JSON_CONTENT_TYPE_HEADER
,
...
...
@@ -953,9 +953,9 @@ export default abstract class Server<
const
trimmedName
=
headerName
.
trim
()
const
headerValue
=
req
.
headers
[
trimmedName
]
if
(
typeof
headerValue
===
'string'
)
{
req
.
headers
[
trimmedName
]
=
decode
Node
HeaderValue
(
headerValue
)
req
.
headers
[
trimmedName
]
=
decodeHeaderValue
(
headerValue
)
}
else
if
(
Array
.
isArray
(
headerValue
))
{
req
.
headers
[
trimmedName
]
=
headerValue
.
map
(
decode
Node
HeaderValue
)
req
.
headers
[
trimmedName
]
=
headerValue
.
map
(
decodeHeaderValue
)
}
}
// Clean up the marker header
...
...
packages/next/src/server/lib/mojibake.ts
View file @
388859ce
...
...
@@ -36,7 +36,7 @@ export function encodeMojibake(input: string): string {
*/
export
function
decodeMojibake
(
input
:
string
):
string
{
// Check if value contains non-ASCII characters that could be Mojibake
if
(
!
/
[\x
80-
\x
FF
]
/
.
test
(
input
))
{
return
input
}
...
...
packages/next/src/server/lib/router-utils/resolve-routes.ts
View file @
388859ce
...
...
@@ -14,10 +14,7 @@ import { getCloneableBody } from '../../body-streams'
import
{
filterReqHeaders
,
ipcForbiddenHeaders
}
from
'../server-ipc/utils'
import
{
stringifyQuery
}
from
'../../server-route-utils'
import
{
formatHostname
}
from
'../format-hostname'
import
{
toNodeOutgoingHttpHeaders
,
decodeNodeHeaderValue
,
}
from
'../../web/utils'
import
{
toNodeOutgoingHttpHeaders
,
decodeHeaderValue
}
from
'../../web/utils'
import
{
isAbortError
}
from
'../../pipe-readable'
import
{
getHostname
}
from
'../../../shared/lib/get-hostname'
import
{
...
...
@@ -619,8 +616,8 @@ export function getResolveRoutes(
encodedValue
==
null
?
undefined
:
typeof
encodedValue
===
'string'
?
decode
Node
HeaderValue
(
encodedValue
)
:
encodedValue
.
map
(
decode
Node
HeaderValue
)
?
decodeHeaderValue
(
encodedValue
)
:
encodedValue
.
map
(
decodeHeaderValue
)
const
oldValue
=
req
.
headers
[
key
]
if
(
oldValue
!==
newValue
)
{
...
...
packages/next/src/server/web/spec-extension/response.ts
View file @
388859ce
...
...
@@ -5,7 +5,7 @@ import {
toNodeOutgoingHttpHeaders
,
validateURL
,
encodeHeaderValue
,
decode
Node
HeaderValue
,
decodeHeaderValue
,
}
from
'../utils'
import
{
ReflectAdapter
}
from
'./adapters/reflect'
...
...
@@ -28,7 +28,7 @@ function handleMiddlewareField(
for
(
const
[
key
,
value
]
of
init
.
request
.
headers
)
{
// First decode any values that were percent-encoded by fromNodeOutgoingHttpHeaders
// to preserve non-ASCII characters, then re-encode for the middleware header
const
decodedValue
=
decode
Node
HeaderValue
(
value
)
const
decodedValue
=
decodeHeaderValue
(
value
)
const
encodedValue
=
encodeHeaderValue
(
decodedValue
)
headers
.
set
(
'x-middleware-request-'
+
key
,
encodedValue
)
keys
.
push
(
key
)
...
...
packages/next/src/server/web/utils.test.ts
View file @
388859ce
import
{
toNodeOutgoingHttpHeaders
,
encodeHeaderValue
,
decode
Node
HeaderValue
,
decodeHeaderValue
,
}
from
'./utils'
describe
(
'toNodeHeaders'
,
()
=>
{
...
...
@@ -80,37 +80,35 @@ describe('encodeHeaderValue', () => {
})
})
describe
(
'decode
Node
HeaderValue'
,
()
=>
{
describe
(
'decodeHeaderValue'
,
()
=>
{
it
(
'should return unencoded values unchanged'
,
()
=>
{
expect
(
decode
Node
HeaderValue
(
'hello-world'
)).
toBe
(
'hello-world'
)
expect
(
decode
Node
HeaderValue
(
'test_value'
)).
toBe
(
'test_value'
)
expect
(
decode
Node
HeaderValue
(
'123'
)).
toBe
(
'123'
)
expect
(
decodeHeaderValue
(
'hello-world'
)).
toBe
(
'hello-world'
)
expect
(
decodeHeaderValue
(
'test_value'
)).
toBe
(
'test_value'
)
expect
(
decodeHeaderValue
(
'123'
)).
toBe
(
'123'
)
})
it
(
'should decode percent-encoded values'
,
()
=>
{
expect
(
decode
Node
HeaderValue
(
'Montr%C3%A9al'
)).
toBe
(
'Montréal'
)
expect
(
decode
Node
HeaderValue
(
'caf%C3%A9'
)).
toBe
(
'café'
)
expect
(
decode
Node
HeaderValue
(
'%E5%8C%97%E4%BA%AC'
)).
toBe
(
'北京'
)
expect
(
decodeHeaderValue
(
'Montr%C3%A9al'
)).
toBe
(
'Montréal'
)
expect
(
decodeHeaderValue
(
'caf%C3%A9'
)).
toBe
(
'café'
)
expect
(
decodeHeaderValue
(
'%E5%8C%97%E4%BA%AC'
)).
toBe
(
'北京'
)
expect
(
decode
Node
HeaderValue
(
'%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9'
)
decodeHeaderValue
(
'%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9'
)
).
toBe
(
'русский'
)
})
it
(
'should handle spaces correctly'
,
()
=>
{
expect
(
decode
Node
HeaderValue
(
'hello world'
)).
toBe
(
'hello world'
)
expect
(
decode
Node
HeaderValue
(
'Montr%C3%A9al%20city'
)).
toBe
(
'Montréal city'
)
expect
(
decodeHeaderValue
(
'hello world'
)).
toBe
(
'hello world'
)
expect
(
decodeHeaderValue
(
'Montr%C3%A9al%20city'
)).
toBe
(
'Montréal city'
)
})
it
(
'should handle mixed encoded and unencoded values'
,
()
=>
{
expect
(
decodeNodeHeaderValue
(
'City%3A%20Montr%C3%A9al'
)).
toBe
(
'City: Montréal'
)
expect
(
decodeHeaderValue
(
'City%3A%20Montr%C3%A9al'
)).
toBe
(
'City: Montréal'
)
})
it
(
'should return original value if decoding fails'
,
()
=>
{
expect
(
decode
Node
HeaderValue
(
'invalid%'
)).
toBe
(
'invalid%'
)
expect
(
decode
Node
HeaderValue
(
'invalid%2'
)).
toBe
(
'invalid%2'
)
expect
(
decode
Node
HeaderValue
(
'%XX'
)).
toBe
(
'%XX'
)
expect
(
decodeHeaderValue
(
'invalid%'
)).
toBe
(
'invalid%'
)
expect
(
decodeHeaderValue
(
'invalid%2'
)).
toBe
(
'invalid%2'
)
expect
(
decodeHeaderValue
(
'%XX'
)).
toBe
(
'%XX'
)
})
it
(
'should round-trip encode/decode correctly'
,
()
=>
{
...
...
@@ -124,7 +122,7 @@ describe('decodeNodeHeaderValue', () => {
]
for
(
const
value
of
testValues
)
{
expect
(
decode
Node
HeaderValue
(
encodeHeaderValue
(
value
))).
toBe
(
value
)
expect
(
decodeHeaderValue
(
encodeHeaderValue
(
value
))).
toBe
(
value
)
}
})
})
packages/next/src/server/web/utils.ts
View file @
388859ce
...
...
@@ -43,21 +43,6 @@ export function fromNodeOutgoingHttpHeaders(
return
headers
}
/**
* Decodes a header value that may have been percent-encoded by fromNodeOutgoingHttpHeaders.
*/
export
function
decodeNodeHeaderValue
(
value
:
string
):
string
{
// Check if value contains percent-encoded sequences
if
(
/%
[
0-9A-Fa-f
]{2}
/
.
test
(
value
))
{
try
{
return
decodeURIComponent
(
value
)
}
catch
{
return
value
}
}
return
value
}
/*
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
that are within a single set-cookie field-value, such as in the Expires portion.
...
...
@@ -207,3 +192,21 @@ export function encodeHeaderValue(value: string): string {
}
return
value
}
/**
* Decodes a header value that may have been percent-encoded.
*
* @param value - The header value to decode
* @returns The decoded header value
*/
export
function
decodeHeaderValue
(
value
:
string
):
string
{
// Check if value contains percent-encoded sequences
if
(
/%
[
0-9A-Fa-f
]{2}
/
.
test
(
value
))
{
try
{
return
decodeURIComponent
(
value
)
}
catch
{
return
value
}
}
return
value
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment