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) {
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];
: 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;
socket_.data = this;
}
+
Connection::~Connection ()
{
static int i = 0;
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."));
return 0;
}
+ puts("net.cc: resolve failed");
+
connection->OnDisconnect();
connection->Detach();
connection->OnTimeout();
}
+ void Init (void); // constructor helper.
+
static int Resolve (eio_req *req);
static int AfterResolve (eio_req *req);
char *host_;
--- /dev/null
+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);
+}