diff --git a/src/http.cc b/src/http.cc index 5e0deff2fc..4adc7a93aa 100644 --- a/src/http.cc +++ b/src/http.cc @@ -329,39 +329,16 @@ HTTPServer::New (const Arguments& args) return args.This(); } +Handle +HTTPServer::GetConnectionTemplate (void) +{ + return HTTPConnection::server_constructor_template; +} + Connection* -HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len) +HTTPServer::UnwrapConnection (Local connection) { HandleScope scope; - - Local connection_handler = GetConnectionHandler (); - if (connection_handler.IsEmpty()) { - Close(); - return NULL; - } - - TryCatch try_catch; - - Local connection_handle = - HTTPConnection::server_constructor_template->GetFunction()->NewInstance(0, NULL); - - if (connection_handle.IsEmpty()) { - FatalException(try_catch); - return NULL; - } - - HTTPConnection *connection = NODE_UNWRAP(HTTPConnection, connection_handle); - if (!connection) return NULL; - - connection->SetAcceptor(handle_); - - Handle argv[1] = { connection_handle }; - - Local ret = connection_handler->Call(handle_, 1, argv); - - if (ret.IsEmpty()) - FatalException(try_catch); - - return connection; + return NODE_UNWRAP(HTTPConnection, connection); } diff --git a/src/http.h b/src/http.h index 044d555b1a..ab207a3c59 100644 --- a/src/http.h +++ b/src/http.h @@ -49,7 +49,8 @@ protected: v8::Handle options) : Acceptor(handle, protocol_class, options) {} - Connection* OnConnection (struct sockaddr *addr, socklen_t len); + v8::Handle GetConnectionTemplate (void); + Connection* UnwrapConnection (v8::Local connection); }; } // namespace node diff --git a/src/net.cc b/src/net.cc index 4f5cadf322..c06989af75 100644 --- a/src/net.cc +++ b/src/net.cc @@ -140,7 +140,6 @@ Connection::SetAcceptor (Handle acceptor_handle) { HandleScope scope; handle_->Set(SERVER_SYMBOL, acceptor_handle); - Attach(); } @@ -544,37 +543,52 @@ SetRemoteAddress (Local connection_handle, struct sockaddr *addr) connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address); } +Handle +Acceptor::GetConnectionTemplate (void) +{ + return Connection::constructor_template; +} + +Connection* +Acceptor::UnwrapConnection (Local connection) +{ + HandleScope scope; + return NODE_UNWRAP(Connection, connection); +} + Connection* Acceptor::OnConnection (struct sockaddr *addr, socklen_t len) { HandleScope scope; - Local connection_handler = GetConnectionHandler(); - if (connection_handler.IsEmpty()) { + Local connection_handler_v = + handle_->GetHiddenValue(CONNECTION_HANDLER_SYMBOL); + if (!connection_handler_v->IsFunction()) { printf("Connection handler was empty!"); Close(); return NULL; } + Local connection_handler = + Local::Cast(connection_handler_v); TryCatch try_catch; - Local connection_handle = - Connection::constructor_template->GetFunction()->NewInstance(0, NULL); + Local js_connection = + GetConnectionTemplate()->GetFunction()->NewInstance(0, NULL); - if (connection_handle.IsEmpty()) { + if (js_connection.IsEmpty()) { FatalException(try_catch); return NULL; } - SetRemoteAddress(connection_handle, addr); + SetRemoteAddress(js_connection, addr); - Connection *connection = NODE_UNWRAP(Connection, connection_handle); + Connection *connection = UnwrapConnection(js_connection); if (!connection) return NULL; connection->SetAcceptor(handle_); - Handle argv[1] = { connection_handle }; - + Handle argv[1] = { js_connection }; Local ret = connection_handler->Call(handle_, 1, argv); if (ret.IsEmpty()) @@ -646,18 +660,3 @@ Acceptor::Close (const Arguments& args) acceptor->Close(); return Undefined(); } - -Local -Acceptor::GetConnectionHandler (void) -{ - HandleScope scope; - - Local connection_handler_v = handle_->GetHiddenValue(CONNECTION_HANDLER_SYMBOL); - if (connection_handler_v->IsFunction()) { - Local connection_handler = Local::Cast(connection_handler_v); - return scope.Close(connection_handler); - } - - return Local(); -} - diff --git a/src/net.h b/src/net.h index 40e8e6a9ce..85049042a6 100644 --- a/src/net.h +++ b/src/net.h @@ -139,8 +139,6 @@ protected: v8::Handle options); virtual ~Acceptor () { Close(); } - v8::Local GetConnectionHandler (void); - int Listen (struct addrinfo *address) { int r = oi_server_listen (&server_, address); if(r != 0) return r; @@ -154,9 +152,11 @@ protected: Detach(); } - virtual Connection* OnConnection (struct sockaddr *addr, socklen_t len); + virtual v8::Handle GetConnectionTemplate (void); + virtual Connection* UnwrapConnection (v8::Local connection); private: + Connection* OnConnection (struct sockaddr *addr, socklen_t len); static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) { Acceptor *acceptor = static_cast (s->data); Connection *connection = acceptor->OnConnection (addr, len); diff --git a/test/test-http.js b/test/test-http.js index 7d30d4d00a..10ed9511af 100644 --- a/test/test-http.js +++ b/test/test-http.js @@ -25,6 +25,8 @@ function onLoad () { res.finish(); responses_sent += 1; }; + + assertEquals("127.0.0.1", res.connection.remoteAddress); }).listen(PORT); var client = new node.http.Client(PORT);