diff --git a/src/http.cc b/src/http.cc index be0f478130a..4ebc639457e 100644 --- a/src/http.cc +++ b/src/http.cc @@ -136,7 +136,7 @@ DEFINE_PARSER_CALLBACK(on_uri, ON_URI_SYMBOL) DEFINE_PARSER_CALLBACK(on_header_field, ON_HEADER_FIELD_SYMBOL) DEFINE_PARSER_CALLBACK(on_header_value, ON_HEADER_VALUE_SYMBOL) -static Local +static inline Local GetMethod (int method) { switch (method) { @@ -169,11 +169,13 @@ HTTPConnection::on_headers_complete (http_parser *parser) Local message_handler = message_handler_v->ToObject(); // METHOD - message_handler->Set(METHOD_SYMBOL, GetMethod(connection->parser_.method)); + if (connection->parser_.type == HTTP_REQUEST) + message_handler->Set(METHOD_SYMBOL, GetMethod(connection->parser_.method)); // STATUS - message_handler->Set(STATUS_CODE_SYMBOL, - Integer::New(connection->parser_.status_code)); + if (connection->parser_.type == HTTP_RESPONSE) + message_handler->Set(STATUS_CODE_SYMBOL, + Integer::New(connection->parser_.status_code)); // VERSION char version[10]; diff --git a/src/net.cc b/src/net.cc index fbed1afebb9..e8bfffc6ec0 100644 --- a/src/net.cc +++ b/src/net.cc @@ -137,11 +137,17 @@ Connection::Connection (Handle handle) : ObjectWrap(handle) { encoding_ = RAW; - double timeout = 60.0; // default host_ = NULL; port_ = NULL; + Init(); +} + +void +Connection::Init (void) +{ + double timeout = 60.0; // default oi_socket_init(&socket_, timeout); socket_.on_connect = Connection::on_connect; socket_.on_read = Connection::on_read; @@ -151,6 +157,7 @@ Connection::Connection (Handle handle) socket_.data = this; } + Connection::~Connection () { static int i = 0; @@ -190,6 +197,13 @@ Connection::Connect (const Arguments& args) HandleScope scope; + if (connection->ReadyState() != CLOSED) { + return ThrowException(String::New("Socket is already connected.")); + } else { + // XXX ugly. + connection->Init(); // in case we're reusing the socket... ? + } + if (args.Length() == 0 || args[0]->IsInt32() == false) return ThrowException(String::New("Must specify a port.")); @@ -258,6 +272,8 @@ Connection::AfterResolve (eio_req *req) return 0; } + puts("net.cc: resolve failed"); + connection->OnDisconnect(); connection->Detach(); diff --git a/src/net.h b/src/net.h index a8c056ae4eb..3dec42a143c 100644 --- a/src/net.h +++ b/src/net.h @@ -115,6 +115,8 @@ private: connection->OnTimeout(); } + void Init (void); // constructor helper. + static int Resolve (eio_req *req); static int AfterResolve (eio_req *req); char *host_; diff --git a/test/test-reconnecting-socket.js b/test/test-reconnecting-socket.js new file mode 100644 index 00000000000..307afb7ab4f --- /dev/null +++ b/test/test-reconnecting-socket.js @@ -0,0 +1,42 @@ +include("mjsunit"); +var port = 8921; + +function onLoad () { + + new node.tcp.Server(function (socket) { + puts("new connection"); + socket.onConnect = function () { + socket.send("hello\r\n"); + }; + + socket.onEOF = function () { + socket.close(); + }; + + socket.onDisconnect = function () { + socket.server.close(); + }; + }).listen(port); + + var count = 0; + var client = new node.tcp.Connection(); + + client.encoding = "UTF8"; + client.onConnect = function () { + puts("client connected"); + }; + + client.onReceive = function (chunk) { + puts("got msg"); + assertEquals("hello\r\n", chunk); + client.fullClose(); + }; + + client.onDisconnect = function () { + puts("client disconnected"); + if (count++ < 5) + client.connect(port); + }; + + client.connect(port); +}