Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response.writeHead method is called twice #208

Open
anseki opened this issue May 24, 2018 · 1 comment
Open

response.writeHead method is called twice #208

anseki opened this issue May 24, 2018 · 1 comment

Comments

@anseki
Copy link

anseki commented May 24, 2018

Hi, thank you for the great module.

So, response.writeHead method is called twice when the request is not a GET request.

For example:

Save a content file to public/index.html:

<body>Hello, World!</body>

Run this code as a server:

'use strict';

const fileServer = new (require('node-static')).Server('./public');

require('http').createServer((request, response) => {
  request.addListener('end', () => {

    response.setHeader('foo', 'bar');
    fileServer.serve(request, response);

  }).resume();
}).listen(8080);

And run this code as a client (using request module):

'use strict';

require('request')({
  url: 'http://localhost:8080/index.html',
  method: 'HEAD'
}, (error, response, body) => {
  console.log('error: ', error);
  console.log('statusCode: ', response && response.statusCode);
  console.log('headers:');
  console.log(response && response.headers);
  console.log('body: ', body);
});

Then, an error "Can't set headers after they are sent." is thrown.
This issue is relevant to #207.

BTW, when the response.setHeader method was not called, the error is not thrown even if the response.writeHead method is called twice. This is another issue about Node.js.

In Server.prototype.respondNoGzip, the response.writeHead method is called when requested file is not cached.

res.writeHead(status, headers);

And, in Server.prototype.finish, the response.writeHead method is called again when the request is not a GET request.
res.writeHead(status, headers);

Also, a response body is made (i.e. it is piped to response) even if the request is a HEAD request. (And that body may be discarded by the server.)

}).pipe(res, { end: false });

I couldn't understand this comment:

// Don't end the request here, if we're streaming;

What is a case that the response.writeHead method must be called here?
Anyway, it should check whether headers were already sent or not by using response.headersSent or response._header.

            if (!res._header) {
              res.writeHead(status, headers);
@anseki
Copy link
Author

anseki commented May 24, 2018

BTW, it seems that these are duplicated.

for (var k in _headers) { headers[k] = _headers[k] }

for (var k in _headers) { headers[k] = _headers[k] }

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

No branches or pull requests

1 participant