From 4860f1c92cb3ab111cc05d24ea7e5338e070779a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 3 May 2009 21:37:10 +0200 Subject: [PATCH] add onMessageComplete and onBody handlers. --- src/http.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/http.h | 3 ++- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/http.cc b/src/http.cc index 943adec..5ce7fd5 100644 --- a/src/http.cc +++ b/src/http.cc @@ -8,6 +8,8 @@ #define ON_MESSAGE_SYMBOL String::NewSymbol("onMessage") #define MESSAGE_HANDLER_SYMBOL String::NewSymbol("messageHandler") #define ON_HEADERS_COMPLETE_SYMBOL String::NewSymbol("onHeadersComplete") +#define ON_BODY_SYMBOL String::NewSymbol("onBody") +#define ON_MESSAGE_COMPLETE_SYMBOL String::NewSymbol("onMessageComplete") #define PATH_SYMBOL String::NewSymbol("path") #define STATUS_CODE_SYMBOL String::NewSymbol("status_code") @@ -174,6 +176,7 @@ int HTTPConnection::on_headers_complete (http_parser *parser) { HTTPConnection *connection = static_cast (parser->data); + HandleScope scope; Local message_handler_v = connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL); @@ -189,8 +192,6 @@ HTTPConnection::on_headers_complete (http_parser *parser) message_handler->Set(HTTP_VERSION_SYMBOL, String::New(version)); - - Local on_headers_complete_v = message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL); if (on_headers_complete_v->IsFunction() == false) return 0; @@ -202,6 +203,67 @@ HTTPConnection::on_headers_complete (http_parser *parser) return 0; } +int +HTTPConnection::on_body (http_parser *parser, const char *buf, size_t len) +{ + if(len == 0) return 0; + + HTTPConnection *connection = static_cast (parser->data); + HandleScope scope; + + Local message_handler_v = + connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL); + Local message_handler = message_handler_v->ToObject(); + + Local on_body_v = message_handler->Get(ON_BODY_SYMBOL); + if (on_body_v->IsFunction() == false) return 0; + Handle on_body = Handle::Cast(on_body_v); + + Handle 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 chunk = String::New((const char*)buf, len); + argv[0] = chunk; + + } else { + // raw encoding + Local array = Array::New(len); + for (size_t i = 0; i < len; i++) { + char val = static_cast(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 (parser->data); + HandleScope scope; + + Local message_handler_v = + connection->handle_->GetHiddenValue(MESSAGE_HANDLER_SYMBOL); + Local message_handler = message_handler_v->ToObject(); + + Local on_msg_complete_v = message_handler->Get(ON_MESSAGE_COMPLETE_SYMBOL); + if (on_msg_complete_v->IsFunction()) { + Handle on_msg_complete = Handle::Cast(on_msg_complete_v); + on_msg_complete->Call(message_handler, 0, NULL); + } + + connection->handle_->DeleteHiddenValue(MESSAGE_HANDLER_SYMBOL); + + return 0; +} + HTTPConnection::HTTPConnection (Handle handle, Handle protocol, enum http_parser_type type) : Connection(handle, protocol) { @@ -214,8 +276,8 @@ HTTPConnection::HTTPConnection (Handle handle, Handle protocol, parser_.on_header_field = on_header_field; parser_.on_header_value = on_header_value; parser_.on_headers_complete = on_headers_complete; - parser_.on_body = NULL; - parser_.on_message_complete = NULL; + parser_.on_body = on_body; + parser_.on_message_complete = on_message_complete; parser_.data = this; } diff --git a/src/http.h b/src/http.h index 63f8564..9ae9353 100644 --- a/src/http.h +++ b/src/http.h @@ -24,7 +24,8 @@ protected: static int on_header_field (http_parser *parser, const char *buf, size_t len); static int on_header_value (http_parser *parser, const char *buf, size_t len); static int on_headers_complete (http_parser *parser); - + static int on_body (http_parser *parser, const char *buf, size_t len); + static int on_message_complete (http_parser *parser); http_parser parser_; }; -- 2.7.4