|
|
@ -4,17 +4,31 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <strings.h> |
|
|
|
|
|
|
|
#define METHOD_SYMBOL String::NewSymbol("method") |
|
|
|
#define STATUS_CODE_SYMBOL String::NewSymbol("statusCode") |
|
|
|
#define HTTP_VERSION_SYMBOL String::NewSymbol("httpVersion") |
|
|
|
#define SHOULD_KEEP_ALIVE_SYMBOL String::NewSymbol("should_keep_alive") |
|
|
|
|
|
|
|
using namespace v8; |
|
|
|
using namespace node; |
|
|
|
|
|
|
|
Persistent<FunctionTemplate> HTTPConnection::client_constructor_template; |
|
|
|
Persistent<FunctionTemplate> HTTPConnection::server_constructor_template; |
|
|
|
|
|
|
|
static Persistent<String> method_symbol; |
|
|
|
static Persistent<String> status_code_symbol; |
|
|
|
static Persistent<String> http_version_symbol; |
|
|
|
static Persistent<String> version_major_symbol; |
|
|
|
static Persistent<String> version_minor_symbol; |
|
|
|
static Persistent<String> should_keep_alive_symbol; |
|
|
|
|
|
|
|
static Persistent<String> message_begin_symbol; |
|
|
|
static Persistent<String> message_complete_symbol; |
|
|
|
static Persistent<String> url_symbol; |
|
|
|
static Persistent<String> query_string_symbol; |
|
|
|
static Persistent<String> path_symbol; |
|
|
|
static Persistent<String> fragment_symbol; |
|
|
|
static Persistent<String> header_field_symbol; |
|
|
|
static Persistent<String> header_value_symbol; |
|
|
|
static Persistent<String> header_complete_symbol; |
|
|
|
static Persistent<String> body_symbol; |
|
|
|
static Persistent<String> eof_symbol; |
|
|
|
|
|
|
|
void |
|
|
|
HTTPConnection::Initialize (Handle<Object> target) |
|
|
|
{ |
|
|
@ -34,6 +48,11 @@ HTTPConnection::Initialize (Handle<Object> target) |
|
|
|
server_constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
|
|
|
NODE_SET_PROTOTYPE_METHOD(server_constructor_template, "resetParser", ResetParser); |
|
|
|
server_constructor_template->SetClassName(String::NewSymbol("ServerSideConnection")); |
|
|
|
|
|
|
|
method_symbol = NODE_PSYMBOL("method"); |
|
|
|
status_code_symbol = NODE_PSYMBOL("statusCode"); |
|
|
|
http_version_symbol = NODE_PSYMBOL("httpVersion"); |
|
|
|
should_keep_alive_symbol = NODE_PSYMBOL("should_keep_alive"); |
|
|
|
} |
|
|
|
|
|
|
|
Handle<Value> |
|
|
@ -96,15 +115,38 @@ HTTPConnection::OnEOF () |
|
|
|
} else { |
|
|
|
http_parse_responses(&parser_, NULL, 0); |
|
|
|
} |
|
|
|
Emit("eof", 0, NULL); |
|
|
|
Emit(eof_symbol, 0, NULL); |
|
|
|
} |
|
|
|
|
|
|
|
int |
|
|
|
HTTPConnection::on_message_begin (http_parser *parser) |
|
|
|
{ |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
if (message_begin_symbol.IsEmpty()) { |
|
|
|
method_symbol = NODE_PSYMBOL("method"); |
|
|
|
status_code_symbol = NODE_PSYMBOL("statusCode"); |
|
|
|
http_version_symbol = NODE_PSYMBOL("httpVersion"); |
|
|
|
version_major_symbol = NODE_PSYMBOL("versionMajor"); |
|
|
|
version_minor_symbol = NODE_PSYMBOL("versionMinor"); |
|
|
|
should_keep_alive_symbol = NODE_PSYMBOL("should_keep_alive"); |
|
|
|
|
|
|
|
message_begin_symbol = NODE_PSYMBOL("messageBegin"); |
|
|
|
message_complete_symbol = NODE_PSYMBOL("messageComplete"); |
|
|
|
url_symbol = NODE_PSYMBOL("url"); |
|
|
|
query_string_symbol = NODE_PSYMBOL("queryString"); |
|
|
|
path_symbol = NODE_PSYMBOL("path"); |
|
|
|
fragment_symbol = NODE_PSYMBOL("fragment"); |
|
|
|
header_field_symbol = NODE_PSYMBOL("headerField"); |
|
|
|
header_value_symbol = NODE_PSYMBOL("headerValue"); |
|
|
|
header_complete_symbol = NODE_PSYMBOL("headerComplete"); |
|
|
|
body_symbol = NODE_PSYMBOL("body"); |
|
|
|
eof_symbol = NODE_PSYMBOL("eof"); |
|
|
|
} |
|
|
|
|
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
connection->Emit("messageBegin", 0, NULL); |
|
|
|
connection->Emit(message_begin_symbol, 0, NULL); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -113,7 +155,7 @@ HTTPConnection::on_message_complete (http_parser *parser) |
|
|
|
{ |
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*> (parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
connection->Emit("messageComplete", 0, NULL); |
|
|
|
connection->Emit(message_complete_symbol, 0, NULL); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -124,7 +166,7 @@ HTTPConnection::on_url (http_parser *parser, const char *buf, size_t len) |
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("url", 1, argv); |
|
|
|
connection->Emit(url_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -135,7 +177,7 @@ HTTPConnection::on_query_string (http_parser *parser, const char *buf, size_t le |
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("queryString", 1, argv); |
|
|
|
connection->Emit(query_string_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -146,7 +188,7 @@ HTTPConnection::on_path (http_parser *parser, const char *buf, size_t len) |
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("path", 1, argv); |
|
|
|
connection->Emit(path_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -157,7 +199,7 @@ HTTPConnection::on_fragment (http_parser *parser, const char *buf, size_t len) |
|
|
|
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data); |
|
|
|
assert(connection->refs_); |
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("fragment", 1, argv); |
|
|
|
connection->Emit(fragment_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -184,7 +226,7 @@ HTTPConnection::on_header_field (http_parser *parser, const char *buf, size_t le |
|
|
|
for (i = 0; i < len; i++) { nonconstbuf[i] = normalizer[buf[i]]; } |
|
|
|
|
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("headerField", 1, argv); |
|
|
|
connection->Emit(header_field_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -196,7 +238,7 @@ HTTPConnection::on_header_value (http_parser *parser, const char *buf, size_t le |
|
|
|
assert(connection->refs_); |
|
|
|
|
|
|
|
Local<Value> argv[1] = { String::New(buf, len) }; |
|
|
|
connection->Emit("headerValue", 1, argv); |
|
|
|
connection->Emit(header_value_symbol, 1, argv); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@ -211,13 +253,13 @@ HTTPConnection::on_headers_complete (http_parser *parser) |
|
|
|
|
|
|
|
// METHOD
|
|
|
|
if (connection->type_ == HTTP_REQUEST) { |
|
|
|
message_info->Set(METHOD_SYMBOL, String::NewSymbol( |
|
|
|
message_info->Set(method_symbol, String::NewSymbol( |
|
|
|
http_method_str(connection->parser_.method))); |
|
|
|
} |
|
|
|
|
|
|
|
// STATUS
|
|
|
|
if (connection->type_ == HTTP_RESPONSE) { |
|
|
|
message_info->Set(STATUS_CODE_SYMBOL, |
|
|
|
message_info->Set(status_code_symbol, |
|
|
|
Integer::New(connection->parser_.status_code)); |
|
|
|
} |
|
|
|
|
|
|
@ -229,18 +271,18 @@ HTTPConnection::on_headers_complete (http_parser *parser) |
|
|
|
, connection->parser_.http_major |
|
|
|
, connection->parser_.http_minor |
|
|
|
); |
|
|
|
message_info->Set(HTTP_VERSION_SYMBOL, String::New(version)); |
|
|
|
message_info->Set(String::NewSymbol("versionMajor"), |
|
|
|
message_info->Set(http_version_symbol, String::New(version)); |
|
|
|
message_info->Set(version_major_symbol, |
|
|
|
Integer::New(connection->parser_.http_major)); |
|
|
|
message_info->Set(String::NewSymbol("versionMinor"), |
|
|
|
message_info->Set(version_minor_symbol, |
|
|
|
Integer::New(connection->parser_.http_minor)); |
|
|
|
|
|
|
|
message_info->Set(SHOULD_KEEP_ALIVE_SYMBOL, |
|
|
|
message_info->Set(should_keep_alive_symbol, |
|
|
|
http_should_keep_alive(&connection->parser_) ? True() : False()); |
|
|
|
|
|
|
|
Local<Value> argv[1] = { message_info }; |
|
|
|
|
|
|
|
connection->Emit("headerComplete", 1, argv); |
|
|
|
connection->Emit(header_complete_symbol, 1, argv); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
@ -257,7 +299,7 @@ HTTPConnection::on_body (http_parser *parser, const char *buf, size_t len) |
|
|
|
// TODO each message should have their encoding.
|
|
|
|
// don't look at the conneciton for encoding
|
|
|
|
Local<Value> data = Encode(buf, len, connection->encoding_); |
|
|
|
connection->Emit("body", 1, &data); |
|
|
|
connection->Emit(body_symbol, 1, &data); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|