2
votes

I have a http request/response question.

Let's say I have a proxy server that just forwards the client's requests to a third party server, and third party server's responses back to the client:

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {

  var opts = {
    hostname : 'www.example.com',
    port     : 80,
    path     : req.url,
    method   : req.method,
    headers  : req.headers
  };
  opts.headers.host = opts.host;

  var proxyReq = http.request(opts, function (proxyResponse) {
    res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
    proxyResponse.pipe(res);
  });

  req.pipe(proxyReq);

}).listen(3000);

Let's say the client is cache-aware, (like a browser) and it passed a if-modified-since or if-none-match (or whatever) header that caused the third party server to respond with a 304 status code.

Now, technically, the server shouldn't send a body and the client doesn't care about the response body after receiving a 304.

Would it make any sense, (and would it save any bandwidth/resources) by doing this in the proxy response handler for servers that erroneously send a response body with a 304:

var proxyReq = http.request(opts, function (proxyResponse) {

  // Terminate the request, and respond with empty body.
  if (proxyResponse.statusCode === 304) {
    proxyResponse.connection.end();
    return res.end();
  }

  /* ... */

Or is it bad to terminate sockets like that? I'm not quite familiar whether calling .end() on the socket messes with the http keep-alive connections, or has any effect on performance.

Thank you for your help!

1

1 Answers

2
votes

Now, technically, the client doesn't care about the response body after receiving a 304.

A 304 response should not contain a response body. If it does it would be a violation of the protocol and you should close the connection. The RFC2616:

The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.