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

rename

parent 434d9daf
...@@ -111,7 +111,7 @@ import { ...@@ -111,7 +111,7 @@ import {
import { BaseServerSpan } from './lib/trace/constants' import { BaseServerSpan } from './lib/trace/constants'
import { I18NProvider } from './lib/i18n-provider' import { I18NProvider } from './lib/i18n-provider'
import { sendResponse } from './send-response' import { sendResponse } from './send-response'
import { normalizeNextQueryParam, decodeNodeHeaderValue } from './web/utils' import { normalizeNextQueryParam, decodeHeaderValue } from './web/utils'
import { import {
HTML_CONTENT_TYPE_HEADER, HTML_CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE_HEADER,
...@@ -953,9 +953,9 @@ export default abstract class Server< ...@@ -953,9 +953,9 @@ export default abstract class Server<
const trimmedName = headerName.trim() const trimmedName = headerName.trim()
const headerValue = req.headers[trimmedName] const headerValue = req.headers[trimmedName]
if (typeof headerValue === 'string') { if (typeof headerValue === 'string') {
req.headers[trimmedName] = decodeNodeHeaderValue(headerValue) req.headers[trimmedName] = decodeHeaderValue(headerValue)
} else if (Array.isArray(headerValue)) { } else if (Array.isArray(headerValue)) {
req.headers[trimmedName] = headerValue.map(decodeNodeHeaderValue) req.headers[trimmedName] = headerValue.map(decodeHeaderValue)
} }
} }
// Clean up the marker header // Clean up the marker header
......
...@@ -36,7 +36,7 @@ export function encodeMojibake(input: string): string { ...@@ -36,7 +36,7 @@ export function encodeMojibake(input: string): string {
*/ */
export function decodeMojibake(input: string): string { export function decodeMojibake(input: string): string {
// Check if value contains non-ASCII characters that could be Mojibake // Check if value contains non-ASCII characters that could be Mojibake
if (!/[\x80-\xFF]/.test(input)) { if (!/[\x80-\xFF]/.test(input)) {
return input return input
} }
......
...@@ -14,10 +14,7 @@ import { getCloneableBody } from '../../body-streams' ...@@ -14,10 +14,7 @@ import { getCloneableBody } from '../../body-streams'
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils' import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils'
import { stringifyQuery } from '../../server-route-utils' import { stringifyQuery } from '../../server-route-utils'
import { formatHostname } from '../format-hostname' import { formatHostname } from '../format-hostname'
import { import { toNodeOutgoingHttpHeaders, decodeHeaderValue } from '../../web/utils'
toNodeOutgoingHttpHeaders,
decodeNodeHeaderValue,
} from '../../web/utils'
import { isAbortError } from '../../pipe-readable' import { isAbortError } from '../../pipe-readable'
import { getHostname } from '../../../shared/lib/get-hostname' import { getHostname } from '../../../shared/lib/get-hostname'
import { import {
...@@ -619,8 +616,8 @@ export function getResolveRoutes( ...@@ -619,8 +616,8 @@ export function getResolveRoutes(
encodedValue == null encodedValue == null
? undefined ? undefined
: typeof encodedValue === 'string' : typeof encodedValue === 'string'
? decodeNodeHeaderValue(encodedValue) ? decodeHeaderValue(encodedValue)
: encodedValue.map(decodeNodeHeaderValue) : encodedValue.map(decodeHeaderValue)
const oldValue = req.headers[key] const oldValue = req.headers[key]
if (oldValue !== newValue) { if (oldValue !== newValue) {
......
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
toNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders,
validateURL, validateURL,
encodeHeaderValue, encodeHeaderValue,
decodeNodeHeaderValue, decodeHeaderValue,
} from '../utils' } from '../utils'
import { ReflectAdapter } from './adapters/reflect' import { ReflectAdapter } from './adapters/reflect'
...@@ -28,7 +28,7 @@ function handleMiddlewareField( ...@@ -28,7 +28,7 @@ function handleMiddlewareField(
for (const [key, value] of init.request.headers) { for (const [key, value] of init.request.headers) {
// First decode any values that were percent-encoded by fromNodeOutgoingHttpHeaders // First decode any values that were percent-encoded by fromNodeOutgoingHttpHeaders
// to preserve non-ASCII characters, then re-encode for the middleware header // to preserve non-ASCII characters, then re-encode for the middleware header
const decodedValue = decodeNodeHeaderValue(value) const decodedValue = decodeHeaderValue(value)
const encodedValue = encodeHeaderValue(decodedValue) const encodedValue = encodeHeaderValue(decodedValue)
headers.set('x-middleware-request-' + key, encodedValue) headers.set('x-middleware-request-' + key, encodedValue)
keys.push(key) keys.push(key)
......
import { import {
toNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders,
encodeHeaderValue, encodeHeaderValue,
decodeNodeHeaderValue, decodeHeaderValue,
} from './utils' } from './utils'
describe('toNodeHeaders', () => { describe('toNodeHeaders', () => {
...@@ -80,37 +80,35 @@ describe('encodeHeaderValue', () => { ...@@ -80,37 +80,35 @@ describe('encodeHeaderValue', () => {
}) })
}) })
describe('decodeNodeHeaderValue', () => { describe('decodeHeaderValue', () => {
it('should return unencoded values unchanged', () => { it('should return unencoded values unchanged', () => {
expect(decodeNodeHeaderValue('hello-world')).toBe('hello-world') expect(decodeHeaderValue('hello-world')).toBe('hello-world')
expect(decodeNodeHeaderValue('test_value')).toBe('test_value') expect(decodeHeaderValue('test_value')).toBe('test_value')
expect(decodeNodeHeaderValue('123')).toBe('123') expect(decodeHeaderValue('123')).toBe('123')
}) })
it('should decode percent-encoded values', () => { it('should decode percent-encoded values', () => {
expect(decodeNodeHeaderValue('Montr%C3%A9al')).toBe('Montréal') expect(decodeHeaderValue('Montr%C3%A9al')).toBe('Montréal')
expect(decodeNodeHeaderValue('caf%C3%A9')).toBe('café') expect(decodeHeaderValue('caf%C3%A9')).toBe('café')
expect(decodeNodeHeaderValue('%E5%8C%97%E4%BA%AC')).toBe('北京') expect(decodeHeaderValue('%E5%8C%97%E4%BA%AC')).toBe('北京')
expect( 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('русский') ).toBe('русский')
}) })
it('should handle spaces correctly', () => { it('should handle spaces correctly', () => {
expect(decodeNodeHeaderValue('hello world')).toBe('hello world') expect(decodeHeaderValue('hello world')).toBe('hello world')
expect(decodeNodeHeaderValue('Montr%C3%A9al%20city')).toBe('Montréal city') expect(decodeHeaderValue('Montr%C3%A9al%20city')).toBe('Montréal city')
}) })
it('should handle mixed encoded and unencoded values', () => { it('should handle mixed encoded and unencoded values', () => {
expect(decodeNodeHeaderValue('City%3A%20Montr%C3%A9al')).toBe( expect(decodeHeaderValue('City%3A%20Montr%C3%A9al')).toBe('City: Montréal')
'City: Montréal'
)
}) })
it('should return original value if decoding fails', () => { it('should return original value if decoding fails', () => {
expect(decodeNodeHeaderValue('invalid%')).toBe('invalid%') expect(decodeHeaderValue('invalid%')).toBe('invalid%')
expect(decodeNodeHeaderValue('invalid%2')).toBe('invalid%2') expect(decodeHeaderValue('invalid%2')).toBe('invalid%2')
expect(decodeNodeHeaderValue('%XX')).toBe('%XX') expect(decodeHeaderValue('%XX')).toBe('%XX')
}) })
it('should round-trip encode/decode correctly', () => { it('should round-trip encode/decode correctly', () => {
...@@ -124,7 +122,7 @@ describe('decodeNodeHeaderValue', () => { ...@@ -124,7 +122,7 @@ describe('decodeNodeHeaderValue', () => {
] ]
for (const value of testValues) { for (const value of testValues) {
expect(decodeNodeHeaderValue(encodeHeaderValue(value))).toBe(value) expect(decodeHeaderValue(encodeHeaderValue(value))).toBe(value)
} }
}) })
}) })
...@@ -43,21 +43,6 @@ export function fromNodeOutgoingHttpHeaders( ...@@ -43,21 +43,6 @@ export function fromNodeOutgoingHttpHeaders(
return headers 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 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. 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 { ...@@ -207,3 +192,21 @@ export function encodeHeaderValue(value: string): string {
} }
return value 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