From 74f4eb9a2e9e8756b195caaa9d678cbfea4aae06 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 21 Feb 2009 21:00:40 +0100 Subject: [PATCH] add http method access --- count-hosts.js | 48 ++++++------------------------- http_request.h | 10 ++----- js_http_request_processor.cc | 55 ++++++++++++++---------------------- js_http_request_processor.h | 8 ++---- server.cc | 3 ++ 5 files changed, 36 insertions(+), 88 deletions(-) diff --git a/count-hosts.js b/count-hosts.js index 2b333e78fa..0a16d90b94 100644 --- a/count-hosts.js +++ b/count-hosts.js @@ -1,43 +1,11 @@ -// Copyright 2008 the V8 project authors. All rights reserved. -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following -// disclaimer in the documentation and/or other materials provided -// with the distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived -// from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -function Initialize() { } - - function Process(request) { - if (options.verbose) { - log("Processing " + request.host + request.path + - " from " + request.referrer + "@" + request.userAgent); - } - if (!output[request.host]) { - output[request.host] = 1; - } else { - output[request.host]++ + log("Processing " + request.path + ". method: " + request.method); + + /* + request.onBody = function (chunk) { + log("body chunk: " + chunk); } + request.respond("HTTP/1.1 200 OK\r\nContent-Type: text-plain\r\n\r\nhello"); + request.response_complete + */ } - -Initialize(); diff --git a/http_request.h b/http_request.h index e611495067..3c679dfea5 100644 --- a/http_request.h +++ b/http_request.h @@ -21,20 +21,14 @@ public: class HttpRequest { public: - HttpRequest (Connection &c) : connection_(c) { ebb_request_init(&parser_info); } + HttpRequest (Connection &c) : connection(c) { ebb_request_init(&parser_info); } ~HttpRequest() { } const string& Path () { return path; } - const string& Referrer () { return referrer; } - const string& Host () { return host; } - const string& UserAgent () { return user_agent; } string path; - string referrer; - string host; - string user_agent; - Connection &connection_; + Connection &connection; ebb_request parser_info; }; diff --git a/js_http_request_processor.cc b/js_http_request_processor.cc index dd3d832385..46fed66379 100644 --- a/js_http_request_processor.cc +++ b/js_http_request_processor.cc @@ -385,47 +385,36 @@ Handle JsHttpRequestProcessor::GetPath , const AccessorInfo& info ) { - // Extract the C++ request object from the JavaScript wrapper. HttpRequest* request = UnwrapRequest(info.Holder()); - - // Fetch the path. const string& path = request->Path(); - - // Wrap the result in a JavaScript string and return it. - return String::New(path.c_str(), path.length()); -} - - -Handle JsHttpRequestProcessor::GetReferrer - ( Local name - , const AccessorInfo& info - ) -{ - HttpRequest* request = UnwrapRequest(info.Holder()); - const string& path = request->Referrer(); return String::New(path.c_str(), path.length()); } - -Handle JsHttpRequestProcessor::GetHost +Handle JsHttpRequestProcessor::GetMethod ( Local name , const AccessorInfo& info ) { HttpRequest* request = UnwrapRequest(info.Holder()); - const string& path = request->Host(); - return String::New(path.c_str(), path.length()); -} - - -Handle JsHttpRequestProcessor::GetUserAgent - ( Local name - , const AccessorInfo& info - ) -{ - HttpRequest* request = UnwrapRequest(info.Holder()); - const string& path = request->UserAgent(); - return String::New(path.c_str(), path.length()); + // TODO allocate these strings only once. reference global + switch(request->parser_info.method) { + case ebb_request::EBB_COPY: return String::New("COPY"); + case ebb_request::EBB_DELETE: return String::New("DELETE"); + case ebb_request::EBB_GET: return String::New("GET"); + case ebb_request::EBB_HEAD: return String::New("HEAD"); + case ebb_request::EBB_LOCK: return String::New("LOCK"); + case ebb_request::EBB_MKCOL: return String::New("MKCOL"); + case ebb_request::EBB_MOVE: return String::New("MOVE"); + case ebb_request::EBB_OPTIONS: return String::New("OPTIONS"); + case ebb_request::EBB_POST: return String::New("POST"); + case ebb_request::EBB_PROPFIND: return String::New("PROPFIND"); + case ebb_request::EBB_PROPPATCH: return String::New("PROPPATCH"); + case ebb_request::EBB_PUT: return String::New("PUT"); + case ebb_request::EBB_TRACE: return String::New("TRACE"); + case ebb_request::EBB_UNLOCK: return String::New("UNLOCK"); + default: + return Null(); + } } @@ -440,9 +429,7 @@ Handle JsHttpRequestProcessor::MakeRequestTemplate // Add accessors for each of the fields of the request. result->SetAccessor(String::NewSymbol("path"), GetPath); - result->SetAccessor(String::NewSymbol("referrer"), GetReferrer); - result->SetAccessor(String::NewSymbol("host"), GetHost); - result->SetAccessor(String::NewSymbol("userAgent"), GetUserAgent); + result->SetAccessor(String::NewSymbol("method"), GetMethod); // Again, return the result through the current handle scope. return handle_scope.Close(result); diff --git a/js_http_request_processor.h b/js_http_request_processor.h index f2abb7321c..b4de2df525 100644 --- a/js_http_request_processor.h +++ b/js_http_request_processor.h @@ -65,12 +65,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { static Handle MakeMapTemplate(); // Callbacks that access the individual fields of request objects. - static Handle GetPath(Local name, const AccessorInfo& info); - static Handle GetReferrer(Local name, - const AccessorInfo& info); - static Handle GetHost(Local name, const AccessorInfo& info); - static Handle GetUserAgent(Local name, - const AccessorInfo& info); + static Handle GetPath (Local name, const AccessorInfo& info); + static Handle GetMethod (Local name, const AccessorInfo& info); // Callbacks that access maps static Handle MapGet(Local name, const AccessorInfo& info); diff --git a/server.cc b/server.cc index 197fc2b6f2..681eebee11 100644 --- a/server.cc +++ b/server.cc @@ -45,12 +45,15 @@ void on_headers_complete } else { printf("unsuccesful request\n"); } + } void on_request_complete ( ebb_request *req ) { + HttpRequest *request = static_cast (req->data); + oi_socket_close(&request->connection.socket); } ebb_request * on_request