Browse Source

Add keep-alive handling.

v0.7.4-release
Ryan 16 years ago
parent
commit
7869ed6681
  1. 6
      src/http.cc
  2. 18
      src/http.js

6
src/http.cc

@ -23,6 +23,7 @@
#define STATUS_CODE_SYMBOL String::NewSymbol("status_code") #define STATUS_CODE_SYMBOL String::NewSymbol("status_code")
#define HTTP_VERSION_SYMBOL String::NewSymbol("http_version") #define HTTP_VERSION_SYMBOL String::NewSymbol("http_version")
#define SHOULD_KEEP_ALIVE_SYMBOL String::NewSymbol("should_keep_alive")
using namespace v8; using namespace v8;
using namespace node; using namespace node;
@ -163,6 +164,11 @@ HTTPConnection::on_headers_complete (http_parser *parser)
); );
message_handler->Set(HTTP_VERSION_SYMBOL, String::New(version)); message_handler->Set(HTTP_VERSION_SYMBOL, String::New(version));
// SHOULD KEEP ALIVE
message_handler->Set( SHOULD_KEEP_ALIVE_SYMBOL
, http_parser_should_keep_alive(&connection->parser_) ? True() : False()
);
Local<Value> on_headers_complete_v = message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL); Local<Value> on_headers_complete_v = message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL);
if (on_headers_complete_v->IsFunction() == false) return 0; if (on_headers_complete_v->IsFunction() == false) return 0;

18
src/http.js

@ -154,8 +154,15 @@ node.http.Server = function (RequestHandler, options) {
} }
} }
if (sent_connection_header == false) // keep-alive logic
header += "Connection: keep-alive\r\n"; if (sent_connection_header == false) {
if (this.should_keep_alive) {
header += "Connection: keep-alive\r\n";
} else {
connection_close = true;
header += "Connection: close\r\n";
}
}
if (sent_content_length_header == false && sent_transfer_encoding_header == false) { if (sent_content_length_header == false && sent_transfer_encoding_header == false) {
header += "Transfer-Encoding: chunked\r\n"; header += "Transfer-Encoding: chunked\r\n";
@ -193,12 +200,8 @@ node.http.Server = function (RequestHandler, options) {
responses.shift(); responses.shift();
} }
if (responses.length == 0) { if (responses.length == 0 && connection_close) {
connection.fullClose(); connection.fullClose();
// TODO keep-alive logic
// if http/1.0 without "Connection: keep-alive" header - yes
// if http/1.1 if the request was not GET or HEAD - yes
// if http/1.1 without "Connection: close" header - yes
} }
}; };
} }
@ -239,6 +242,7 @@ node.http.Server = function (RequestHandler, options) {
this.onHeadersComplete = function () { this.onHeadersComplete = function () {
req.http_version = this.http_version; req.http_version = this.http_version;
req.method = this.method; req.method = this.method;
res.should_keep_alive = this.should_keep_alive;
return req.onHeadersComplete(); return req.onHeadersComplete();
}; };

Loading…
Cancel
Save