secret
Created

An unexpected HEAD request behaviour.

  • Download Gist
README.md
Markdown

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.
coffee server.coffee
  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.
coffee client.coffee

The error message I got

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

  /Users/simsicon/sites/debug_path/client.coffee:40
      return console.log(res.headers);
                            ^
  TypeError: Cannot read property 'headers' of undefined
      at /Users/simsicon/sites/debug_path/client.coffee:40:29
      at ClientRequest.<anonymous> (/Users/simsicon/sites/debug_path/client.coffee:16:14)
      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)
client.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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)
res.setEncoding('utf-8')
callback null, res
 
req.write '0\r\n\r\n'
req.end
 
location = 'http://127.0.0.1:3001/files/c97070c2030d4f3985c34209f9fc5105'
 
opts =
path: url.parse(location).path
hostname: '127.0.0.1'
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
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
{
"name": "debug_head",
"version": "0.0.1",
"author": "simsicon <simsicon@gmail.com>",
"description": "An unexpected HEAD request behaviour.",
"contributors": [
{
"name": "simsicon",
"email": "simsicon@gmail.com"
}
],
"dependencies" : {
"underscore" : ">=1.4.4"
},
"devDependencies": {
"vows" : "0.5.x"
},
"license": "MIT",
"engines": {
"node": ">=0.8.22"
}
}
server.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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'
res.end
 
server.timeout = 2000
 
server.listen 3001, '127.0.0.1'
state.json
JSON
1
{"uuid":"c97070c2030d4f3985c34209f9fc5105","content-length":"10485760","content-range":"bytes */10485760","content-type":"text/plain","content-disposition":"attachment; filename=\"file\"","host":"127.0.0.1:3001","connection":"keep-alive"}
utils.coffee
CoffeeScript
1 2 3 4
exports.get_json = (callback) ->
fs = require 'fs'
fs.readFile './state.json', (err, data) ->
callback JSON.parse(data)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.