http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Missing HTTP Response Header "Content-Encoding: gzip"

Open GhostCore07 opened this issue 3 years ago • 8 comments

-g option will enable content-type: application/gzip; charset=utf-8 but this is not enough, it has to have "Content-Encoding: gzip" also in response header here is an example of error message when attempting to serve gzip content: image

GhostCore07 avatar Dec 27 '21 07:12 GhostCore07

Did a little investigating, the issue is in this block of code shown below, the test for file === gzippedFile always fails. I replaced the test with if (file.includes(".gz")) and it works correctly for me now. This is just a hack and probably not the intended way things should work but it gets it done for now. cheers.

if (file === gzippedFile) { // is .gz picked up
  res.setHeader('Content-Encoding', 'gzip');
  // strip gz ending and lookup mime type
  contentType = mime.lookup(path.basename(file, '.gz'), defaultType);
} else if (file === brotliFile) { // is .br picked up
 res.setHeader('Content-Encoding', 'br');
  // strip br ending and lookup mime type
  contentType = mime.lookup(path.basename(file, '.br'), defaultType);
}

GhostCore07 avatar Dec 27 '21 08:12 GhostCore07

Just a little added thought. Perhaps the script checks for a regular file and then sees if there is also a .gz file version of the same file? In my case there is only a .gz file, no regular file exists. So I think it is attempting to see if there is a .gz.gz file which of course will fail, when in fact it should just serve the .gz file without checking if there is a gz and a non-gz file.

GhostCore07 avatar Dec 27 '21 08:12 GhostCore07

This issue has been inactive for 180 days

github-actions[bot] avatar Jun 25 '22 12:06 github-actions[bot]

Did a little investigating, the issue is in this block of code shown below, the test for file === gzippedFile always fails. I replaced the test with if (file.includes(".gz")) and it works correctly for me now. This is just a hack and probably not the intended way things should work but it gets it done for now. cheers.

if (file === gzippedFile) { // is .gz picked up
  res.setHeader('Content-Encoding', 'gzip');
  // strip gz ending and lookup mime type
  contentType = mime.lookup(path.basename(file, '.gz'), defaultType);
} else if (file === brotliFile) { // is .br picked up
 res.setHeader('Content-Encoding', 'br');
  // strip br ending and lookup mime type
  contentType = mime.lookup(path.basename(file, '.br'), defaultType);
}

Thanks! This worked for me, replacing the "if (file === gzippedFile) {" line (location: http-server\lib\core\index.js)

Develobba avatar Jul 23 '22 09:07 Develobba

Bump. Can we get that fix into the repo ? This also fixes the problem of serving compressed unity player WebGL builds via node.js Thanks !

ostryhub avatar Sep 20 '22 22:09 ostryhub

Bump. Can we get that fix into the repo ? This also fixes the problem of serving compressed unity player WebGL builds via node.js Thanks !

I've started a pull request but it probably still needs some work #834

GhostCore07 avatar Sep 28 '22 13:09 GhostCore07

Bump. Would be great to get the fix pulled into the repo. Thanks.

InvivoLuke avatar Nov 29 '22 17:11 InvivoLuke

-g option will enable content-type: application/gzip; charset=utf-8 but this is not enough, it has to have "Content-Encoding: gzip" also in response header here is an example of error message when attempting to serve gzip content: image

Where can you find this file to change the if?

Kbeanstudios avatar Jan 09 '23 18:01 Kbeanstudios