baalexander
baalexander

Reputation: 2589

Socket Hang Up when using https.request in node.js

When using https.request with node.js v04.7, I get the following error:

Error: socket hang up
    at CleartextStream.<anonymous> (http.js:1272:45)
    at CleartextStream.emit (events.js:61:17)
    at Array.<anonymous> (tls.js:617:22)
    at EventEmitter._tickCallback (node.js:126:26)

Simplified code that will generate the error:

var https = require('https')
  , fs    = require('fs')

var options = {
  host: 'localhost'
, port: 8000
, key: fs.readFileSync('../../test-key.pem')
, cert: fs.readFileSync('../../test-cert.pem')
}

// Set up server and start listening
https.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'})
  res.end('success')
}).listen(options.port, options.host)

// Wait a second to let the server start up
setTimeout(function() {
  var clientRequest = https.request(options, function(res) {
    res.on('data', function (chunk) {
      console.log('Called')
    })
  })

  clientRequest.write('')
  clientRequest.end()
}, 1000)

I get the error even with the server and client running on different node instances and have tested with port 8000, 3000, and 443 and with and without the SSL certificates. I do have libssl and libssl-dev on my Ubuntu machine.

Any ideas on what could be the cause?

Upvotes: 2

Views: 9075

Answers (4)

Quentin
Quentin

Reputation: 269

In

https.createServer(function (req, res) {

you are missing options when you create the server, should be:

https.createServer(options, function (req, res) {

with your key and cert inside

Upvotes: 1

murvinlai
murvinlai

Reputation: 50345

I had a similar problem and i think i got a fix. but then I have another socket problem. See my solution here: http://groups.google.com/group/nodejs/browse_thread/thread/9189df2597aa199e/b83b16c08a051706?lnk=gst&q=hang+up#b83b16c08a051706

key point: use 0.4.8, http.request instead of http.createClient.

However, the new problem is, if I let the program running for long time, (I actually left the program running but no activity during weekend), then I will get socket hang up error when I send a request to http Server. (not even reach the http.request). I don't know if it is because of my code, or it is different problem with http Server

Upvotes: 0

Allan
Allan

Reputation: 1715

I had a very similar problem where the response's end event never fired.

Adding this line fixed the problem:

// Hack to emit end on close because of a core bug that never fires end
response.on('close', function () {response.emit('end')});

I found an example of this in the request library mentioned in the previous answer.

Upvotes: 1

700 Software
700 Software

Reputation: 87763

Short answer: Use the the latest source code instead of the one you have. Store it where you will and then require it, you are good to go.


In the request 1.2.0 source code, main.js line 76, I see

http.createClient(options.uri.port, options.uri.hostname, options.uri.protocol === 'https:');

Looking at the http.js source code, I see

exports.createClient = function(port, host) {
  var c = new Client();
  c.port = port;
  c.host = host;
  return c;
};

It is requesting with 3 params but the actual function only has 2. The functionality is replaced with a separate module for https.

Looking at the latest main.js source code, I see dramatic changes. The most important is the addition of require('https').

It appears that request has been fixed but never re-released. Fortunately, the fix seems to work if you just copy manually from the raw view of the latest main.js source code and use it instead.

Upvotes: 0

Related Questions