From a8ac42384d769ac0aefb81015a01c0cab0ba0e59 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 9 Mar 2009 14:09:09 +0100 Subject: [PATCH] change HTTPServer constructor. Now: HTTPServer(host, port, onrequest); --- node_http.cc | 72 ++++++++++++-------------- spec/index.html | 7 ++- test/test_http_server_echo.rb | 6 +-- test/test_http_server_null_response.rb | 25 +++++++++ 4 files changed, 65 insertions(+), 45 deletions(-) create mode 100755 test/test_http_server_null_response.rb diff --git a/node_http.cc b/node_http.cc index 6844af4a86..df5a85827f 100644 --- a/node_http.cc +++ b/node_http.cc @@ -137,14 +137,17 @@ RespondCallback (const Arguments& args) { HandleScope scope; - printf("respondcallback!\n"); - // TODO check that args.Holder()->GetInternalField(0) // is not NULL if so raise INVALID_STATE_ERR - - Handle field = Handle::Cast(args.Holder()->GetInternalField(0)); + Handle v = args.Holder()->GetInternalField(0); + if(v->IsUndefined()) { + printf("null request external\n"); + return Undefined(); + } + Handle field = Handle::Cast(v); HttpRequest* request = static_cast(field->Value()); request->Respond(args[0]); + return Undefined(); } void @@ -163,29 +166,6 @@ HttpRequest::Respond (Handle data) connection.Write(); } -/* -static Handle -RespondHeadersCallback (const Arguments& args) -{ - HandleScope scope; - - int status = args[0]->IntegerValue(); - Local headers = Local::Cast(args[1]); - - for(int i = 0; i < headers->Length(); i++) { - Local v = headers->Get(i); - Local pair = Local::Cast(v); - if(pair->Length() != 2) { - assert(0); //error - } - - - - } - -} -*/ - static void on_path (ebb_request *req, const char *buf, size_t len) @@ -215,15 +195,30 @@ on_fragment (ebb_request *req, const char *buf, size_t len) request->fragment.append(buf, len); } +static const char upcase[] = + "\0______________________________" + "_________________0123456789_____" + "__ABCDEFGHIJKLMNOPQRSTUVWXYZ____" + "__ABCDEFGHIJKLMNOPQRSTUVWXYZ____" + "________________________________" + "________________________________" + "________________________________" + "________________________________"; + static void on_header_field (ebb_request *req, const char *buf, size_t len, int header_index) { HttpRequest *request = static_cast (req->data); + char upbuf[len]; + + for(int i = 0; i < len; i++) + upbuf[i] = upcase[buf[i]]; + if( request->header_fields.size() == header_index - 1) { - request->header_fields.back().append(buf, len); + request->header_fields.back().append(upbuf, len); } else { - request->header_fields.push_back( string(buf, len) ); + request->header_fields.push_back( string(upbuf, len) ); } } @@ -246,7 +241,7 @@ on_headers_complete (ebb_request *req) HandleScope scope; - Local js_request = request->CreateJSObject(); + Handle js_request = request->CreateJSObject(); // Set up an exception handler before calling the Process function TryCatch try_catch; @@ -308,15 +303,13 @@ static void on_close ( oi_socket *socket ) { - printf("on_close\n"); Connection *connection = static_cast (socket->data); delete connection; } HttpRequest::~HttpRequest () { - printf("request destruct %s\n", path.c_str()); - // HandleScope scope; needed? + HandleScope scope; // needed? // delete a reference c++ HttpRequest js_object->SetInternalField(0, Undefined()); // dispose of Persistent handle so that @@ -344,8 +337,6 @@ HttpRequest::HttpRequest (Connection &c) : connection(c) void HttpRequest::MakeBodyCallback (const char *base, size_t length) { - printf("makebodycallback\n"); - HandleScope handle_scope; Handle onbody_val = js_object->Get(on_body_str); @@ -453,7 +444,6 @@ HttpRequest::CreateJSObject () static oi_socket* on_connection (oi_server *_server, struct sockaddr *addr, socklen_t len) { - printf("on connection\n"); HandleScope scope; Server *server = static_cast (_server->data); @@ -535,8 +525,6 @@ Connection::Write ( ) } if(request->done) { - printf("pop request\n"); - if(!ebb_request_should_keep_alive(&request->parser_info)) { socket.on_drain = oi_socket_close; } @@ -600,7 +588,7 @@ Server::Stop() static Handle newHTTPServer (const Arguments& args) { - if (args.Length() < 2) + if (args.Length() < 3) return Undefined(); HandleScope scope; @@ -608,6 +596,9 @@ newHTTPServer (const Arguments& args) String::AsciiValue host(args[0]->ToString()); String::AsciiValue port(args[1]->ToString()); + Handle onrequest = Handle::Cast(args[2]); + args.This()->Set(on_request_str, onrequest); + // get addrinfo for localhost, PORT struct addrinfo *servinfo; struct addrinfo hints; @@ -615,7 +606,8 @@ newHTTPServer (const Arguments& args) hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; - int r = getaddrinfo(NULL, *port, &hints, &servinfo); + // FIXME BLOCKING + int r = getaddrinfo(*host, *port, &hints, &servinfo); if (r != 0) return Undefined(); // XXX raise error? diff --git a/spec/index.html b/spec/index.html index bb384a9f34..a75e25216c 100644 --- a/spec/index.html +++ b/spec/index.html @@ -88,7 +88,8 @@ spawn() to start a new context/event loop?

2 HTTP Server

-
[Constructor(in String host, in String port)]
+  
[Constructor(in String host, in String port, in Function
+  onrequest)]
 interface HTTPServer  {
   readonly attribute String host;
   readonly attribute String port;
@@ -98,6 +99,10 @@ interface HTTPServer  {
   void close(); // yet not implemented
 };
+
+
HTTPServer(host, port, onrequest)
+
+

error handling?

diff --git a/test/test_http_server_echo.rb b/test/test_http_server_echo.rb index 884ccb8904..b515c8f536 100755 --- a/test/test_http_server_echo.rb +++ b/test/test_http_server_echo.rb @@ -32,9 +32,7 @@ function encode(data) { } var port = 8000; -var server = new HTTPServer("localhost", port); - -server.onrequest = function (request) { +var server = new HTTPServer("localhost", port, function (request) { // onBody sends null on the last chunk. request.onbody = function (chunk) { @@ -50,6 +48,6 @@ server.onrequest = function (request) { request.respond("Content-Type: text/plain\r\n"); request.respond("Transfer-Encoding: chunked\r\n"); request.respond("\r\n"); -}; +}); log("Running at http://localhost:" + port + "/"); diff --git a/test/test_http_server_null_response.rb b/test/test_http_server_null_response.rb new file mode 100755 index 0000000000..794acae834 --- /dev/null +++ b/test/test_http_server_null_response.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +require 'net/http' +require File.dirname(__FILE__) + "/common" + +pid = fork do + exec($node, $tf.path) +end + +begin + wait_for_server("localhost", 8000) + connection = Net::HTTP.new("localhost", 8000) + + response, body = connection.get("/") +ensure + `kill -9 #{pid}` +end + +__END__ +var server = new HTTPServer("localhost", 8000, function (request) { + log("request"); + request.respond("HTTP/1.1 200 OK\r\n"); + request.respond("Content-Length: 0\r\n"); + request.respond("\r\n"); + request.respond(null); +});