6

I am creating resources on a node server and attaching an ETag to each one of them. I am using the method found in this question What encoding should I use to properly generate an ETag with crypto in nodeJS? for generating the ETags. However, when I do a HEAD request on command line using curl, like:

$ curl -i -u username:password -X HEAD http://localhost:3000/aResource/0

the answer is

HTTP/1.1 200 OK
X-Powered-By: Express
ETag: "1a37c148692f92ad6c97887f77c873e6"
Content-Type: text/plain
Date: Sat, 26 Oct 2013 01:06:28 GMT
Connection: keep-alive

^C
$

and I have to specifically press Control-C in order to see the command prompt again. On the other hand, I find this strange, because the way I am handling the HEAD request is through app.head on node and after the authentication I am calling the function:

function serveResourceEtag(req, res) {
    console.log("Following route through HEAD");
    var anID    = parseInt(req.params[resourceID], "utf8");
    if (anID < serverData.resourceEtags.length) {
        res.writeHead (200, {"ETag": "\"" + serverData.resourceEtags[anID] + "\"", "Content-Type": "text/plain"});
        res.end();
    }
    else {
        res.writeHead (404, {"Content-Type": "text/plain"});
        res.end();
    }
};

In other words, even though I have the res.end(); command in the end, the transmission for some reason has not stopped. As far as I understand a HEAD request should get the same header as a GET request. The difference with a GET is that in the case of HEAD we do not have the body of a GET request. Can you tell what seems to be the problem here?

Community
  • 1
  • 1
MightyMouse
  • 13,208
  • 8
  • 33
  • 43
  • 1
    It's not your server's fault. When I do `curl -i -X HEAD http://google.com` I have to kill curl to get back to the prompt, which is odd, because a curl GET request actually does drop me back to the prompt. Go figure. – Dan Ross Oct 26 '13 at 01:40
  • Wow! I did not think of trying something that trivial. Please post the answer and I will accept. Thanks for your time. I would have kept on reading about HEAD requests and node documentation endlessly. – MightyMouse Oct 26 '13 at 01:42
  • Haha, ya sometimes you just have to poke it with a stick. – Dan Ross Oct 26 '13 at 01:47

2 Answers2

8

You're right about the behaviour of the HTTP methods, it's just the curl that's wrong, you want:

curl -I -u username:password http://localhost:3000/aResource/0

not -X HEAD which is saying use a HEAD method of HTTP rather than a GET which only returns the headers

Matt Newman
  • 134
  • 2
  • 1
    Actually the truth seems to be somewhere in between. All I had to change for curl to return was to make the lowercase i to capital I like you have it above. Then -X HEAD works as expected. I am accepting anyway. Thanks for your time. – MightyMouse Oct 26 '13 at 01:53
  • 1
    Removing "-X HEAD" causes curl to make a 'GET' request. That is, fetch response headers and response body, although you displayed only the headers using the -I option. Like @MightyMouse said, changing -i to -I (only) is the solution. – Manu Manjunath Nov 08 '13 at 07:09
1

It's not your server's fault. When I do curl -i -X HEAD http://google.com I have to kill curl to get back to the prompt, which is odd, because a curl GET request actually does drop me back to the prompt.

Dan Ross
  • 3,596
  • 4
  • 31
  • 60
  • Sorry, Dan, I will actually accept Matt's suggestion since the capital I does make curl to return. I have to go back and read the manual. But still, as I write to Matt, if one uses -I (instead of -i) and moreover -X HEAD as I had, everything works the way I expected. Thanks again for all the help. – MightyMouse Oct 26 '13 at 01:54