Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@simsicon / secret

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
An unexpected HEAD request behaviour.

How to reproduce the problem

  1. Put those files into your play ground.
  2. Choose your node version to 0.10.5 and install dependencies.
npm install
  1. Start the server.
  1. Run client script, two requests will be sent, a PUT request and a HEAD request, the PUT request will get response as expected, but the HEAD request will not.

The error message I got

  { [Error: socket hang up] code: 'ECONNRESET' }

      return console.log(res.headers);
  TypeError: Cannot read property 'headers' of undefined
      at /Users/simsicon/sites/debug_path/
      at ClientRequest.<anonymous> (/Users/simsicon/sites/debug_path/
      at ClientRequest.EventEmitter.emit (events.js:95:17)
      at Socket.socketOnEnd [as onend] (http.js:1520:9)
      at Socket.g (events.js:175:14)
      at Socket.EventEmitter.emit (events.js:117:20)
      at _stream_readable.js:895:16
      at process._tickCallback (node.js:415:13)
http = require 'http'
fs = require 'fs'
url = require 'url'
_ = require 'underscore'
send_request = (opts, callback) ->
req = http.request opts
req.on 'error', (err) ->
callback err
req.on 'response', (res) ->
console.log 'STATUS:' + res.statusCode
console.log 'HEADERS:' + JSON.stringify(res.headers)
callback null, res
req.write '0\r\n\r\n'
location = ''
opts =
path: url.parse(location).path
hostname: ''
port: 3001
send_request _.extend(opts, {method: 'PUT'}), (err, res) ->
console.log err
console.log res.headers
setTimeout (->
send_request _.extend(opts, {method: 'HEAD'}), (err, res) ->
console.log err
console.log res.headers
), 3000
"name": "debug_head",
"version": "0.0.1",
"author": "simsicon <>",
"description": "An unexpected HEAD request behaviour.",
"contributors": [
"name": "simsicon",
"email": ""
"dependencies" : {
"underscore" : ">=1.4.4"
"devDependencies": {
"vows" : "0.5.x"
"license": "MIT",
"engines": {
"node": ">=0.8.22"
http = require 'http'
url = require 'url'
fs = require 'fs'
utils = require './utils'
server = http.createServer (req, res) ->
if req.method.toUpperCase() == 'HEAD' or req.method.toUpperCase() == 'PUT'
utils.get_json (file) ->
headers =
'Content-Type': file['content-type']
'Content-Length': file['content-length']
'Content-Disposition': file['content-disposition']
'Range': 0 + '-' + 10000
console.log "Request Headers: " + req.method
console.log req.headers
console.log "Response Headers: "
console.log headers
res.writeHead 200, headers
res.write '\n'
server.timeout = 2000
server.listen 3001, ''
{"uuid":"c97070c2030d4f3985c34209f9fc5105","content-length":"10485760","content-range":"bytes */10485760","content-type":"text/plain","content-disposition":"attachment; filename=\"file\"","host":"","connection":"keep-alive"}
exports.get_json = (callback) ->
fs = require 'fs'
fs.readFile './state.json', (err, data) ->
callback JSON.parse(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.