Skip to content

fix: Do not response Content-Length if Transfer-Encoding is defined#1562

Closed
charlyzeng wants to merge 2 commits intokoajs:masterfrom
charlyzeng:fix/transfer-encoding
Closed

fix: Do not response Content-Length if Transfer-Encoding is defined#1562
charlyzeng wants to merge 2 commits intokoajs:masterfrom
charlyzeng:fix/transfer-encoding

Conversation

@charlyzeng
Copy link

Background

Nodejs http module can not handle response that contains both Content-Length and Transfer-Encoding header, you can see it at https://github.com/nodejs/node/blob/v16.6.1/deps/llhttp/src/llhttp.c#L5811.

Problem

Koa will auto set Content-Length response header when use ctx.body = ..., but if Transfer-Encoding response header is defined, Content-Length should not be set. Otherwise, the response can not be handled by nodejs http module.

How To Appear

// server.js
const Koa = require('koa');

const app = new Koa();
app.use((ctx) => {
  ctx.set('Transfer-Encoding', 'chunked');
  ctx.body = 'hello world';
});
app.listen(9000);

// client.js
const http = require('http');

http.get('http://127.0.0.1:9000');

As above, run node server.js to start server, then run node client.js to send a request. Finally, you'll get an error as follow:
image

How To Resolve

Don't set Content-Length header if Transfer-Encoding is defined.

Copy link
Member

@3imed-jaberi 3imed-jaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@xcatliu
Copy link

xcatliu commented Oct 19, 2021

Hi @dead-horse , Why the latest version 2.13.3 does not contain these code?

@siakc
Copy link

siakc commented Jun 11, 2023

Hi @dead-horse , Why the latest version 2.13.3 does not contain these code?

koa/lib
/response.js:199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants