0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00

HTTP Client: add fix to allow TCP connection to reconnect.

This commit is contained in:
Ryan 2009-05-19 21:53:26 +02:00
parent b445514898
commit 82e773630b
4 changed files with 67 additions and 5 deletions

View File

@ -136,7 +136,7 @@ DEFINE_PARSER_CALLBACK(on_uri, ON_URI_SYMBOL)
DEFINE_PARSER_CALLBACK(on_header_field, ON_HEADER_FIELD_SYMBOL)
DEFINE_PARSER_CALLBACK(on_header_value, ON_HEADER_VALUE_SYMBOL)
static Local<String>
static inline Local<String>
GetMethod (int method)
{
switch (method) {
@ -169,11 +169,13 @@ HTTPConnection::on_headers_complete (http_parser *parser)
Local<Object> message_handler = message_handler_v->ToObject();
// METHOD
message_handler->Set(METHOD_SYMBOL, GetMethod(connection->parser_.method));
if (connection->parser_.type == HTTP_REQUEST)
message_handler->Set(METHOD_SYMBOL, GetMethod(connection->parser_.method));
// STATUS
message_handler->Set(STATUS_CODE_SYMBOL,
Integer::New(connection->parser_.status_code));
if (connection->parser_.type == HTTP_RESPONSE)
message_handler->Set(STATUS_CODE_SYMBOL,
Integer::New(connection->parser_.status_code));
// VERSION
char version[10];

View File

@ -137,11 +137,17 @@ Connection::Connection (Handle<Object> handle)
: ObjectWrap(handle)
{
encoding_ = RAW;
double timeout = 60.0; // default
host_ = NULL;
port_ = NULL;
Init();
}
void
Connection::Init (void)
{
double timeout = 60.0; // default
oi_socket_init(&socket_, timeout);
socket_.on_connect = Connection::on_connect;
socket_.on_read = Connection::on_read;
@ -151,6 +157,7 @@ Connection::Connection (Handle<Object> handle)
socket_.data = this;
}
Connection::~Connection ()
{
static int i = 0;
@ -190,6 +197,13 @@ Connection::Connect (const Arguments& args)
HandleScope scope;
if (connection->ReadyState() != CLOSED) {
return ThrowException(String::New("Socket is already connected."));
} else {
// XXX ugly.
connection->Init(); // in case we're reusing the socket... ?
}
if (args.Length() == 0 || args[0]->IsInt32() == false)
return ThrowException(String::New("Must specify a port."));
@ -258,6 +272,8 @@ Connection::AfterResolve (eio_req *req)
return 0;
}
puts("net.cc: resolve failed");
connection->OnDisconnect();
connection->Detach();

View File

@ -115,6 +115,8 @@ private:
connection->OnTimeout();
}
void Init (void); // constructor helper.
static int Resolve (eio_req *req);
static int AfterResolve (eio_req *req);
char *host_;

View File

@ -0,0 +1,42 @@
include("mjsunit");
var port = 8921;
function onLoad () {
new node.tcp.Server(function (socket) {
puts("new connection");
socket.onConnect = function () {
socket.send("hello\r\n");
};
socket.onEOF = function () {
socket.close();
};
socket.onDisconnect = function () {
socket.server.close();
};
}).listen(port);
var count = 0;
var client = new node.tcp.Connection();
client.encoding = "UTF8";
client.onConnect = function () {
puts("client connected");
};
client.onReceive = function (chunk) {
puts("got msg");
assertEquals("hello\r\n", chunk);
client.fullClose();
};
client.onDisconnect = function () {
puts("client disconnected");
if (count++ < 5)
client.connect(port);
};
client.connect(port);
}