Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions impit-node/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,10 @@ impl<'env> ImpitResponse {
.to_string();
let mut headers_vec: Vec<(String, String)> = Vec::new();
for (k, v) in response.headers().iter() {
match v.to_str() {
Ok(val) => headers_vec.push((k.as_str().to_string(), val.to_string())),
Err(e) => {
return Err(napi::Error::new(
napi::Status::GenericFailure,
format!("Failed to parse header value for '{}': {:?}", k.as_str(), e),
));
}
}
headers_vec.push((
k.as_str().to_string(),
v.as_bytes().iter().map(|&b| b as char).collect(),
));
}
let headers = Headers(headers_vec);
let ok = response.status().is_success();
Expand Down
5 changes: 5 additions & 0 deletions impit-node/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ describe.each([
t.expect(text).toContain(routes.charsetMetaHttpEquiv.bodyString);
});

test('non-ASCII header values are decoded as ISO-8859-1', async (t) => {
const response = await impit.fetch(new URL(routes.nonAsciiHeader.path, "http://127.0.0.1:3001").href);
t.expect(response.headers.get('x-non-ascii')).toBe(routes.nonAsciiHeader.headerValue);
});

test('.json() method works', async (t) => {
const response = await impit.fetch(getHttpBinUrl('/json'));
const json = await response.json();
Expand Down
19 changes: 19 additions & 0 deletions impit-node/test/mock.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const routes = {
path: '/charset/meta-http-equiv',
bodyString: WIN1250_STRING,
},
nonAsciiHeader: {
path: '/non-ascii-header',
headerValue: 'Dienstag, 31. März 2026',
},
}

export async function runServer(port: number): Promise<Server> {
Expand Down Expand Up @@ -50,6 +54,21 @@ export async function runServer(port: number): Promise<Server> {
res.end(html);
});

app.get(routes.nonAsciiHeader.path, (req, res) => {
const socket = res.socket!;
socket.write('HTTP/1.1 200 OK\r\n');
socket.write('Content-Type: text/plain\r\n');
socket.write(Buffer.concat([
Buffer.from('X-Non-Ascii: Dienstag, 31. M'),
Buffer.from([0xE4]), // ä in ISO-8859-1
Buffer.from('rz 2026\r\n'),
]));
socket.write('Content-Length: 2\r\n');
socket.write('\r\n');
socket.write('ok');
socket.end();
});

app.get('/socket', (req, res) => {
const socket = req.socket;
const clientAddress = socket.remoteAddress;
Expand Down
2 changes: 1 addition & 1 deletion impit-python/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl ImpitPyResponse {
let headers = HashMap::from_iter(val.headers().iter().map(|(k, v)| {
(
k.as_str().to_string(),
v.to_str().unwrap_or_default().to_string(),
v.as_bytes().iter().map(|&b| b as char).collect::<String>(),
)
}));

Expand Down
Loading