2009-03-04 00:31:37 +01:00
|
|
|
#include "node.h"
|
2009-04-18 15:35:42 +02:00
|
|
|
#include "http.h"
|
2009-05-03 14:09:16 +02:00
|
|
|
#include <http_parser.h>
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-04-04 14:50:15 +02:00
|
|
|
#include <assert.h>
|
2009-05-03 14:09:16 +02:00
|
|
|
#include <stdio.h>
|
2009-04-04 14:50:15 +02:00
|
|
|
|
2009-05-03 21:06:20 +02:00
|
|
|
#define ON_MESSAGE_SYMBOL String::NewSymbol("onMessage")
|
|
|
|
#define MESSAGE_HANDLER_SYMBOL String::NewSymbol("messageHandler")
|
|
|
|
#define ON_HEADERS_COMPLETE_SYMBOL String::NewSymbol("onHeadersComplete")
|
2009-05-03 21:37:10 +02:00
|
|
|
#define ON_BODY_SYMBOL String::NewSymbol("onBody")
|
|
|
|
#define ON_MESSAGE_COMPLETE_SYMBOL String::NewSymbol("onMessageComplete")
|
2009-05-03 21:06:20 +02:00
|
|
|
|
|
|
|
#define PATH_SYMBOL String::NewSymbol("path")
|
2009-05-04 12:08:13 +02:00
|
|
|
#define QUERY_STRING_SYMBOL String::NewSymbol("query_string")
|
|
|
|
#define URI_SYMBOL String::NewSymbol("uri")
|
|
|
|
#define FRAGMENT_SYMBOL String::NewSymbol("fragment")
|
|
|
|
|
2009-05-03 21:06:20 +02:00
|
|
|
#define STATUS_CODE_SYMBOL String::NewSymbol("status_code")
|
|
|
|
#define HTTP_VERSION_SYMBOL String::NewSymbol("http_version")
|
|
|
|
|
2009-02-16 01:34:45 +01:00
|
|
|
using namespace v8;
|
2009-05-03 14:09:16 +02:00
|
|
|
using namespace node;
|
2009-02-16 01:34:45 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2009-05-04 12:08:13 +02:00
|
|
|
// Native Helper Functions
|
2009-05-03 21:06:20 +02:00
|
|
|
|
|
|
|
static Persistent<Function> _fill_field;
|
|
|
|
static Persistent<Function> _append_header_field;
|
|
|
|
static Persistent<Function> _append_header_value;
|
|
|
|
|
|
|
|
#define CATCH_NATIVE_HTTP_FUNCTION(variable, jsname) \
|
|
|
|
do { \
|
|
|
|
if (variable.IsEmpty()) { \
|
|
|
|
Local<Object> __g = Context::GetCurrent()->Global(); \
|
|
|
|
Local<Value> __node_v = __g->Get(String::NewSymbol("node")); \
|
|
|
|
Local<Object> __node = __node_v->ToObject(); \
|
|
|
|
Local<Value> __http_v = __node->Get(String::NewSymbol("http")); \
|
|
|
|
Local<Object> __http = __http_v->ToObject(); \
|
|
|
|
Local<Value> __value = __http->Get(String::NewSymbol(jsname)); \
|
|
|
|
Handle<Function> __function_handle = Handle<Function>::Cast(__value); \
|
|
|
|
variable = Persistent<Function>::New(__function_handle); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
void
|
|
|
|
fillField (Handle<Value> message, Handle<Value> field, Handle<Value> value)
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
CATCH_NATIVE_HTTP_FUNCTION(_fill_field, "fillField");
|
|
|
|
Handle<Value> argv[] = { message, field, value };
|
|
|
|
_fill_field->Call(message->ToObject(), 3, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
appendHeaderField (Handle<Value> message, Handle<Value> d)
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
CATCH_NATIVE_HTTP_FUNCTION(_append_header_field, "appendHeaderField");
|
|
|
|
Handle<Value> argv[] = { message, d };
|
|
|
|
_append_header_field->Call(message->ToObject(), 2, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
appendHeaderValue (Handle<Value> message, Handle<Value> d)
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
CATCH_NATIVE_HTTP_FUNCTION(_append_header_value, "appendHeaderValue");
|
|
|
|
Handle<Value> argv[] = { message, d };
|
|
|
|
_append_header_value->Call(message->ToObject(), 2, argv);
|
|
|
|
}
|
|
|
|
|
2009-05-04 17:19:04 +02:00
|
|
|
Persistent<FunctionTemplate> HTTPConnection::client_constructor_template;
|
|
|
|
Persistent<FunctionTemplate> HTTPConnection::server_constructor_template;
|
2009-05-04 15:39:36 +02:00
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
void
|
2009-05-03 21:06:20 +02:00
|
|
|
HTTPConnection::Initialize (Handle<Object> target)
|
2009-02-24 19:13:58 +01:00
|
|
|
{
|
2009-03-03 02:36:08 +01:00
|
|
|
HandleScope scope;
|
2009-02-23 13:48:34 +01:00
|
|
|
|
2009-05-04 17:38:17 +02:00
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(v8NewClient);
|
2009-05-04 17:19:04 +02:00
|
|
|
client_constructor_template = Persistent<FunctionTemplate>::New(t);
|
|
|
|
client_constructor_template->Inherit(Connection::constructor_template);
|
|
|
|
client_constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
target->Set(String::NewSymbol("HTTPClient"), client_constructor_template->GetFunction());
|
|
|
|
|
2009-05-04 17:38:17 +02:00
|
|
|
t = FunctionTemplate::New(v8NewServer);
|
2009-05-04 17:19:04 +02:00
|
|
|
server_constructor_template = Persistent<FunctionTemplate>::New(t);
|
|
|
|
server_constructor_template->Inherit(Connection::constructor_template);
|
|
|
|
server_constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
target->Set(String::NewSymbol("HTTPServerSideSocket"),
|
|
|
|
server_constructor_template->GetFunction());
|
2009-02-16 01:34:45 +01:00
|
|
|
}
|
|
|
|
|
2009-05-03 14:09:16 +02:00
|
|
|
Handle<Value>
|
2009-05-04 15:39:36 +02:00
|
|
|
HTTPConnection::v8NewClient (const Arguments& args)
|
2009-03-03 19:17:33 +01:00
|
|
|
{
|
|
|
|
HandleScope scope;
|
2009-05-03 14:09:16 +02:00
|
|
|
if (args[0]->IsFunction() == false)
|
|
|
|
return ThrowException(String::New("Must pass a class as the first argument."));
|
2009-05-04 12:08:13 +02:00
|
|
|
Local<Function> protocol_class = Local<Function>::Cast(args[0]);
|
|
|
|
new HTTPConnection(args.This(), protocol_class, HTTP_RESPONSE);
|
2009-05-04 15:39:36 +02:00
|
|
|
return args.This();
|
|
|
|
}
|
2009-03-03 01:56:15 +01:00
|
|
|
|
2009-05-04 15:39:36 +02:00
|
|
|
Handle<Value>
|
|
|
|
HTTPConnection::v8NewServer (const Arguments& args)
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
if (args[0]->IsFunction() == false)
|
|
|
|
return ThrowException(String::New("Must pass a class as the first argument."));
|
|
|
|
Local<Function> protocol_class = Local<Function>::Cast(args[0]);
|
|
|
|
new HTTPConnection(args.This(), protocol_class, HTTP_REQUEST);
|
2009-05-03 14:09:16 +02:00
|
|
|
return args.This();
|
2009-02-23 13:48:34 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
void
|
2009-05-03 21:06:20 +02:00
|
|
|
HTTPConnection::OnReceive (const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
http_parser_execute(&parser_, static_cast<const char*>(buf), len);
|
|
|
|
|
|
|
|
if (http_parser_has_error(&parser_)) {
|
2009-05-04 15:39:36 +02:00
|
|
|
// do something?
|
2009-05-03 21:06:20 +02:00
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
2009-05-04 15:39:36 +02:00
|
|
|
|
|
|
|
// XXX when do we close the connection?
|
2009-05-03 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HTTPConnection::on_message_begin (http_parser *parser)
|
|
|
|
{
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
Local<Object> protocol = connection->GetProtocol();
|
|
|
|
Local<Value> on_message_v = protocol->Get(ON_MESSAGE_SYMBOL);
|
|
|
|
if (!on_message_v->IsFunction()) return -1;
|
|
|
|
Handle<Function> on_message = Handle<Function>::Cast(on_message_v);
|
|
|
|
Local<Object> message_handler = on_message->NewInstance();
|
|
|
|
connection->handle_->SetHiddenValue(MESSAGE_HANDLER_SYMBOL, message_handler);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-04 12:08:13 +02:00
|
|
|
#define DEFINE_FILL_VALUE_CALLBACK(callback_name, symbol) \
|
|
|
|
int \
|
|
|
|
HTTPConnection::callback_name (http_parser *parser, const char *buf, size_t len) \
|
|
|
|
{ \
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data); \
|
|
|
|
HandleScope scope; \
|
|
|
|
Local<Value> message_handler_v = \
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL); \
|
|
|
|
fillField(message_handler_v, symbol, String::New(buf, len)); \
|
|
|
|
return 0; \
|
2009-05-03 21:06:20 +02:00
|
|
|
}
|
2009-05-04 12:08:13 +02:00
|
|
|
DEFINE_FILL_VALUE_CALLBACK(on_path, PATH_SYMBOL)
|
|
|
|
DEFINE_FILL_VALUE_CALLBACK(on_query_string, QUERY_STRING_SYMBOL)
|
|
|
|
DEFINE_FILL_VALUE_CALLBACK(on_uri, URI_SYMBOL)
|
|
|
|
DEFINE_FILL_VALUE_CALLBACK(on_fragment, FRAGMENT_SYMBOL)
|
2009-05-03 21:06:20 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
HTTPConnection::on_header_field (http_parser *parser, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
|
|
|
HandleScope scope;
|
|
|
|
Local<Value> message_handler_v =
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
appendHeaderField(message_handler_v, String::New(buf, len));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HTTPConnection::on_header_value (http_parser *parser, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
|
|
|
HandleScope scope;
|
|
|
|
Local<Value> message_handler_v =
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
appendHeaderValue(message_handler_v, String::New(buf, len));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HTTPConnection::on_headers_complete (http_parser *parser)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
2009-05-03 21:06:20 +02:00
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
2009-05-03 21:37:10 +02:00
|
|
|
HandleScope scope;
|
2009-05-03 21:06:20 +02:00
|
|
|
|
|
|
|
Local<Value> message_handler_v =
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
Local<Object> message_handler = message_handler_v->ToObject();
|
|
|
|
|
|
|
|
// STATUS
|
|
|
|
message_handler->Set(STATUS_CODE_SYMBOL,
|
|
|
|
Integer::New(connection->parser_.status_code));
|
|
|
|
|
|
|
|
// VERSION
|
|
|
|
char version[10];
|
2009-05-04 12:08:13 +02:00
|
|
|
snprintf( version
|
|
|
|
, 10
|
|
|
|
, "%d.%d"
|
|
|
|
, connection->parser_.version_major
|
|
|
|
, connection->parser_.version_minor
|
|
|
|
);
|
2009-05-03 21:06:20 +02:00
|
|
|
message_handler->Set(HTTP_VERSION_SYMBOL, String::New(version));
|
|
|
|
|
|
|
|
|
|
|
|
Local<Value> on_headers_complete_v = message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL);
|
|
|
|
if (on_headers_complete_v->IsFunction() == false) return 0;
|
|
|
|
|
|
|
|
Handle<Function> on_headers_complete = Handle<Function>::Cast(on_headers_complete_v);
|
|
|
|
|
|
|
|
|
|
|
|
on_headers_complete->Call(message_handler, 0, NULL);
|
|
|
|
|
|
|
|
return 0;
|
2009-03-03 01:56:15 +01:00
|
|
|
}
|
2009-02-16 01:34:45 +01:00
|
|
|
|
2009-05-03 21:37:10 +02:00
|
|
|
int
|
|
|
|
HTTPConnection::on_body (http_parser *parser, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
if(len == 0) return 0;
|
|
|
|
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
Local<Value> message_handler_v =
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
Local<Object> message_handler = message_handler_v->ToObject();
|
|
|
|
|
|
|
|
Local<Value> on_body_v = message_handler->Get(ON_BODY_SYMBOL);
|
|
|
|
if (on_body_v->IsFunction() == false) return 0;
|
|
|
|
Handle<Function> on_body = Handle<Function>::Cast(on_body_v);
|
|
|
|
|
|
|
|
Handle<Value> argv[1];
|
|
|
|
|
|
|
|
// XXX whose encoding should we check? each message should have their own,
|
|
|
|
// probably. it ought to default to raw.
|
|
|
|
if(connection->encoding_ == UTF8) {
|
|
|
|
// utf8 encoding
|
|
|
|
Handle<String> chunk = String::New((const char*)buf, len);
|
|
|
|
argv[0] = chunk;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// raw encoding
|
|
|
|
Local<Array> array = Array::New(len);
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
char val = static_cast<const char*>(buf)[i];
|
|
|
|
array->Set(Integer::New(i), Integer::New(val));
|
|
|
|
}
|
|
|
|
argv[0] = array;
|
|
|
|
}
|
|
|
|
on_body->Call(message_handler, 1, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HTTPConnection::on_message_complete (http_parser *parser)
|
|
|
|
{
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data);
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
Local<Value> message_handler_v =
|
|
|
|
connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
Local<Object> message_handler = message_handler_v->ToObject();
|
|
|
|
|
|
|
|
Local<Value> on_msg_complete_v = message_handler->Get(ON_MESSAGE_COMPLETE_SYMBOL);
|
|
|
|
if (on_msg_complete_v->IsFunction()) {
|
|
|
|
Handle<Function> on_msg_complete = Handle<Function>::Cast(on_msg_complete_v);
|
|
|
|
on_msg_complete->Call(message_handler, 0, NULL);
|
|
|
|
}
|
|
|
|
connection->handle_->DeleteHiddenValue(MESSAGE_HANDLER_SYMBOL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-04 12:08:13 +02:00
|
|
|
HTTPConnection::HTTPConnection (Handle<Object> handle, Handle<Function> protocol_class, enum http_parser_type type)
|
|
|
|
: Connection(handle, protocol_class)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
2009-05-03 21:06:20 +02:00
|
|
|
http_parser_init (&parser_, type);
|
|
|
|
parser_.on_message_begin = on_message_begin;
|
|
|
|
parser_.on_path = on_path;
|
2009-05-04 12:08:13 +02:00
|
|
|
parser_.on_query_string = on_query_string;
|
|
|
|
parser_.on_uri = on_uri;
|
|
|
|
parser_.on_fragment = on_fragment;
|
2009-05-03 21:06:20 +02:00
|
|
|
parser_.on_header_field = on_header_field;
|
|
|
|
parser_.on_header_value = on_header_value;
|
|
|
|
parser_.on_headers_complete = on_headers_complete;
|
2009-05-03 21:37:10 +02:00
|
|
|
parser_.on_body = on_body;
|
|
|
|
parser_.on_message_complete = on_message_complete;
|
2009-05-03 21:06:20 +02:00
|
|
|
parser_.data = this;
|
|
|
|
}
|
|
|
|
|
2009-05-04 17:19:04 +02:00
|
|
|
Persistent<FunctionTemplate> HTTPServer::constructor_template;
|
|
|
|
|
|
|
|
void
|
|
|
|
HTTPServer::Initialize (Handle<Object> target)
|
2009-05-04 15:39:36 +02:00
|
|
|
{
|
2009-05-04 17:19:04 +02:00
|
|
|
HandleScope scope;
|
|
|
|
|
2009-05-04 17:38:17 +02:00
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(v8New);
|
2009-05-04 17:19:04 +02:00
|
|
|
constructor_template = Persistent<FunctionTemplate>::New(t);
|
|
|
|
constructor_template->Inherit(Acceptor::constructor_template);
|
|
|
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
target->Set(String::NewSymbol("HTTPServer"), constructor_template->GetFunction());
|
2009-05-04 15:39:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Value>
|
|
|
|
HTTPServer::v8New (const Arguments& args)
|
|
|
|
{
|
2009-05-04 17:19:04 +02:00
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
if (args.Length() < 1 || args[0]->IsFunction() == false)
|
|
|
|
return ThrowException(String::New("Must at give connection handler as the first argument"));
|
|
|
|
|
|
|
|
Local<Function> protocol_class = Local<Function>::Cast(args[0]);
|
|
|
|
Local<Object> options;
|
|
|
|
|
|
|
|
if (args.Length() > 1 && args[1]->IsObject()) {
|
|
|
|
options = args[1]->ToObject();
|
|
|
|
} else {
|
|
|
|
options = Object::New();
|
|
|
|
}
|
|
|
|
|
2009-05-04 17:38:17 +02:00
|
|
|
new HTTPServer(args.This(), protocol_class, options);
|
2009-05-04 17:19:04 +02:00
|
|
|
|
|
|
|
return args.This();
|
2009-05-04 15:39:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection*
|
|
|
|
HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len)
|
|
|
|
{
|
2009-05-04 17:19:04 +02:00
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
Local<Function> protocol_class = GetProtocolClass();
|
|
|
|
if (protocol_class.IsEmpty()) {
|
|
|
|
Close();
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-05-04 15:39:36 +02:00
|
|
|
|
2009-05-04 17:19:04 +02:00
|
|
|
Handle<Value> argv[] = { protocol_class };
|
|
|
|
Local<Object> connection_handle =
|
|
|
|
HTTPConnection::server_constructor_template->GetFunction()->NewInstance(1, argv);
|
|
|
|
|
|
|
|
HTTPConnection *connection = NODE_UNWRAP(HTTPConnection, connection_handle);
|
|
|
|
connection->SetAcceptor(handle_);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
2009-05-04 15:39:36 +02:00
|
|
|
|