Browse Source

Bugfix+Refactor: accessing HTTP connection remoteAddress

v0.7.4-release
Ryan 16 years ago
parent
commit
870b5db46c
  1. 39
      src/http.cc
  2. 3
      src/http.h
  3. 49
      src/net.cc
  4. 6
      src/net.h
  5. 2
      test/test-http.js

39
src/http.cc

@ -329,39 +329,16 @@ HTTPServer::New (const Arguments& args)
return args.This();
}
Connection*
HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len)
Handle<FunctionTemplate>
HTTPServer::GetConnectionTemplate (void)
{
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;
return HTTPConnection::server_constructor_template;
}
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;
Connection*
HTTPServer::UnwrapConnection (Local<Object> connection)
{
HandleScope scope;
return NODE_UNWRAP(HTTPConnection, connection);
}

3
src/http.h

@ -49,7 +49,8 @@ protected:
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

49
src/net.cc

@ -140,7 +140,6 @@ Connection::SetAcceptor (Handle<Object> acceptor_handle)
{
HandleScope scope;
handle_->Set(SERVER_SYMBOL, acceptor_handle);
Attach();
}
@ -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())
@ -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
src/net.h

@ -139,8 +139,6 @@ protected:
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;
@ -154,9 +152,11 @@ protected:
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);

2
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);

Loading…
Cancel
Save