Commit 388859ce authored by Jiachi Liu's avatar Jiachi Liu

rename

parent 434d9daf
......@@ -111,7 +111,7 @@ import {
import { BaseServerSpan } from './lib/trace/constants'
import { I18NProvider } from './lib/i18n-provider'
import { sendResponse } from './send-response'
import { normalizeNextQueryParam, decodeNodeHeaderValue } 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] = decodeNodeHeaderValue(headerValue)
req.headers[trimmedName] = decodeHeaderValue(headerValue)
} else if (Array.isArray(headerValue)) {
req.headers[trimmedName] = headerValue.map(decodeNodeHeaderValue)
req.headers[trimmedName] = headerValue.map(decodeHeaderValue)
}
}
// Clean up the marker header
......
......@@ -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 (!/[\x80-\xFF]/.test(input)) {
return input
}
......
......@@ -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'
? decodeNodeHeaderValue(encodedValue)
: encodedValue.map(decodeNodeHeaderValue)
? decodeHeaderValue(encodedValue)
: encodedValue.map(decodeHeaderValue)
const oldValue = req.headers[key]
if (oldValue !== newValue) {
......
......@@ -5,7 +5,7 @@ import {
toNodeOutgoingHttpHeaders,
validateURL,
encodeHeaderValue,
decodeNodeHeaderValue,
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 = decodeNodeHeaderValue(value)
const decodedValue = decodeHeaderValue(value)
const encodedValue = encodeHeaderValue(decodedValue)
headers.set('x-middleware-request-' + key, encodedValue)
keys.push(key)
......
import {
toNodeOutgoingHttpHeaders,
encodeHeaderValue,
decodeNodeHeaderValue,
decodeHeaderValue,
} from './utils'
describe('toNodeHeaders', () => {
......@@ -80,37 +80,35 @@ describe('encodeHeaderValue', () => {
})
})
describe('decodeNodeHeaderValue', () => {
describe('decodeHeaderValue', () => {
it('should return unencoded values unchanged', () => {
expect(decodeNodeHeaderValue('hello-world')).toBe('hello-world')
expect(decodeNodeHeaderValue('test_value')).toBe('test_value')
expect(decodeNodeHeaderValue('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(decodeNodeHeaderValue('Montr%C3%A9al')).toBe('Montréal')
expect(decodeNodeHeaderValue('caf%C3%A9')).toBe('café')
expect(decodeNodeHeaderValue('%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(
decodeNodeHeaderValue('%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(decodeNodeHeaderValue('hello world')).toBe('hello world')
expect(decodeNodeHeaderValue('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(decodeNodeHeaderValue('invalid%')).toBe('invalid%')
expect(decodeNodeHeaderValue('invalid%2')).toBe('invalid%2')
expect(decodeNodeHeaderValue('%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(decodeNodeHeaderValue(encodeHeaderValue(value))).toBe(value)
expect(decodeHeaderValue(encodeHeaderValue(value))).toBe(value)
}
})
})
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment