Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Bugfix+Refactor: accessing HTTP connection remoteAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 15, 2009
1 parent dcf5e72 commit 870b5db
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 60 deletions.
39 changes: 8 additions & 31 deletions src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,39 +329,16 @@ HTTPServer::New (const Arguments& args)
return args.This();
}

Handle<FunctionTemplate>
HTTPServer::GetConnectionTemplate (void)
{
return HTTPConnection::server_constructor_template;
}

Connection*
HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len)
HTTPServer::UnwrapConnection (Local<Object> connection)
{
HandleScope scope;

Local<Function> connection_handler = GetConnectionHandler ();
if (connection_handler.IsEmpty()) {
Close();
return NULL;
}

TryCatch try_catch;

Local<Object> 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<Value> argv[1] = { connection_handle };

Local<Value> ret = connection_handler->Call(handle_, 1, argv);

if (ret.IsEmpty())
FatalException(try_catch);

return connection;
return NODE_UNWRAP(HTTPConnection, connection);
}

3 changes: 2 additions & 1 deletion src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class HTTPServer : public Acceptor {
v8::Handle<v8::Object> options)
: Acceptor(handle, protocol_class, options) {}

Connection* OnConnection (struct sockaddr *addr, socklen_t len);
v8::Handle<v8::FunctionTemplate> GetConnectionTemplate (void);
Connection* UnwrapConnection (v8::Local<v8::Object> connection);
};

} // namespace node
Expand Down
49 changes: 24 additions & 25 deletions src/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ Connection::SetAcceptor (Handle<Object> acceptor_handle)
{
HandleScope scope;
handle_->Set(SERVER_SYMBOL, acceptor_handle);

Attach();
}

Expand Down Expand Up @@ -544,37 +543,52 @@ SetRemoteAddress (Local<Object> connection_handle, struct sockaddr *addr)
connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address);
}

Handle<FunctionTemplate>
Acceptor::GetConnectionTemplate (void)
{
return Connection::constructor_template;
}

Connection*
Acceptor::UnwrapConnection (Local<Object> connection)
{
HandleScope scope;
return NODE_UNWRAP(Connection, connection);
}

Connection*
Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
{
HandleScope scope;

Local<Function> connection_handler = GetConnectionHandler();
if (connection_handler.IsEmpty()) {
Local<Value> connection_handler_v =
handle_->GetHiddenValue(CONNECTION_HANDLER_SYMBOL);
if (!connection_handler_v->IsFunction()) {
printf("Connection handler was empty!");
Close();
return NULL;
}
Local<Function> connection_handler =
Local<Function>::Cast(connection_handler_v);

TryCatch try_catch;

Local<Object> connection_handle =
Connection::constructor_template->GetFunction()->NewInstance(0, NULL);
Local<Object> 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<Value> argv[1] = { connection_handle };

Handle<Value> argv[1] = { js_connection };
Local<Value> ret = connection_handler->Call(handle_, 1, argv);

if (ret.IsEmpty())
Expand Down Expand Up @@ -646,18 +660,3 @@ Acceptor::Close (const Arguments& args)
acceptor->Close();
return Undefined();
}

Local<v8::Function>
Acceptor::GetConnectionHandler (void)
{
HandleScope scope;

Local<Value> connection_handler_v = handle_->GetHiddenValue(CONNECTION_HANDLER_SYMBOL);
if (connection_handler_v->IsFunction()) {
Local<Function> connection_handler = Local<Function>::Cast(connection_handler_v);
return scope.Close(connection_handler);
}

return Local<Function>();
}

6 changes: 3 additions & 3 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ class Acceptor : public ObjectWrap {
v8::Handle<v8::Object> options);
virtual ~Acceptor () { Close(); }

v8::Local<v8::Function> GetConnectionHandler (void);

int Listen (struct addrinfo *address) {
int r = oi_server_listen (&server_, address);
if(r != 0) return r;
Expand All @@ -154,9 +152,11 @@ class Acceptor : public ObjectWrap {
Detach();
}

virtual Connection* OnConnection (struct sockaddr *addr, socklen_t len);
virtual v8::Handle<v8::FunctionTemplate> GetConnectionTemplate (void);
virtual Connection* UnwrapConnection (v8::Local<v8::Object> 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<Acceptor*> (s->data);
Connection *connection = acceptor->OnConnection (addr, len);
Expand Down
2 changes: 2 additions & 0 deletions test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 870b5db

Please sign in to comment.