HTTP Client: add fix to allow TCP connection to reconnect.
authorRyan <ry@tinyclouds.org>
Tue, 19 May 2009 19:53:26 +0000 (21:53 +0200)
committerRyan <ry@tinyclouds.org>
Tue, 19 May 2009 19:53:26 +0000 (21:53 +0200)
src/http.cc
src/net.cc
src/net.h
test/test-reconnecting-socket.js [new file with mode: 0644]

index be0f478..4ebc639 100644 (file)
@@ -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<String>
+static inline Local<String>
 GetMethod (int method)
 {
   switch (method) {
@@ -169,11 +169,13 @@ HTTPConnection::on_headers_complete (http_parser *parser)
   Local<Object> 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];
index fbed1af..e8bfffc 100644 (file)
@@ -137,11 +137,17 @@ Connection::Connection (Handle<Object> 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<Object> 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();
 
index a8c056a..3dec42a 100644 (file)
--- 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 (file)
index 0000000..307afb7
--- /dev/null
@@ -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);
+}