简体   繁体   中英

Alter a stream with trumpet, how to get content-length header in http.Response right?

I have a nodejs http server running and I want to alter content on specific sites (kind of my own private ad-blocker). I decided to do this with trumpet . Now the problem is, when trumpet injects the new content and pipes it to the http Response (http.IncomingMessage), the headers['content-length'] value is no more valid.

Some browsers do not seem to care (Chrome / Firefox on W7), others crash immediatly (Safari on iPad). I tried erasing the content-length header alltogether, with similar outcome (Safari waiting forever).

How can I set the content-length of the response right, after switching contents? I found harmon , but I just need this last bit, so harmon felt like too much overhead. Or is it not that trivial?

If you do not know how big your response will be when you send the headers (the case here), you need to use chunked transfer encoding .

Node should default to chunked transfer encoding if you do not specify any Content-Length . Either remove the code that's setting the Content-Length header or try res.removeHeader('Content-Length') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM