Browse Source

add http method access

v0.7.4-release
Ryan 16 years ago
parent
commit
74f4eb9a2e
  1. 48
      count-hosts.js
  2. 10
      http_request.h
  3. 55
      js_http_request_processor.cc
  4. 8
      js_http_request_processor.h
  5. 3
      server.cc

48
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) { function Process(request) {
if (options.verbose) { log("Processing " + request.path + ". method: " + request.method);
log("Processing " + request.host + request.path +
" from " + request.referrer + "@" + request.userAgent); /*
} request.onBody = function (chunk) {
if (!output[request.host]) { log("body chunk: " + chunk);
output[request.host] = 1;
} else {
output[request.host]++
} }
request.respond("HTTP/1.1 200 OK\r\nContent-Type: text-plain\r\n\r\nhello");
request.response_complete
*/
} }
Initialize();

10
http_request.h

@ -21,20 +21,14 @@ public:
class HttpRequest { class HttpRequest {
public: public:
HttpRequest (Connection &c) : connection_(c) { ebb_request_init(&parser_info); } HttpRequest (Connection &c) : connection(c) { ebb_request_init(&parser_info); }
~HttpRequest() { } ~HttpRequest() { }
const string& Path () { return path; } const string& Path () { return path; }
const string& Referrer () { return referrer; }
const string& Host () { return host; }
const string& UserAgent () { return user_agent; }
string path; string path;
string referrer;
string host;
string user_agent;
Connection &connection_; Connection &connection;
ebb_request parser_info; ebb_request parser_info;
}; };

55
js_http_request_processor.cc

@ -385,47 +385,36 @@ Handle<Value> JsHttpRequestProcessor::GetPath
, const AccessorInfo& info , const AccessorInfo& info
) )
{ {
// Extract the C++ request object from the JavaScript wrapper.
HttpRequest* request = UnwrapRequest(info.Holder()); HttpRequest* request = UnwrapRequest(info.Holder());
// Fetch the path.
const string& path = request->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<Value> JsHttpRequestProcessor::GetReferrer
( Local<String> name
, const AccessorInfo& info
)
{
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Referrer();
return String::New(path.c_str(), path.length()); return String::New(path.c_str(), path.length());
} }
Handle<Value> JsHttpRequestProcessor::GetMethod
Handle<Value> JsHttpRequestProcessor::GetHost
( Local<String> name ( Local<String> name
, const AccessorInfo& info , const AccessorInfo& info
) )
{ {
HttpRequest* request = UnwrapRequest(info.Holder()); HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Host(); // TODO allocate these strings only once. reference global
return String::New(path.c_str(), path.length()); 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");
Handle<Value> JsHttpRequestProcessor::GetUserAgent case ebb_request::EBB_HEAD: return String::New("HEAD");
( Local<String> name case ebb_request::EBB_LOCK: return String::New("LOCK");
, const AccessorInfo& info 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");
HttpRequest* request = UnwrapRequest(info.Holder()); case ebb_request::EBB_POST: return String::New("POST");
const string& path = request->UserAgent(); case ebb_request::EBB_PROPFIND: return String::New("PROPFIND");
return String::New(path.c_str(), path.length()); 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<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate
// Add accessors for each of the fields of the request. // Add accessors for each of the fields of the request.
result->SetAccessor(String::NewSymbol("path"), GetPath); result->SetAccessor(String::NewSymbol("path"), GetPath);
result->SetAccessor(String::NewSymbol("referrer"), GetReferrer); result->SetAccessor(String::NewSymbol("method"), GetMethod);
result->SetAccessor(String::NewSymbol("host"), GetHost);
result->SetAccessor(String::NewSymbol("userAgent"), GetUserAgent);
// Again, return the result through the current handle scope. // Again, return the result through the current handle scope.
return handle_scope.Close(result); return handle_scope.Close(result);

8
js_http_request_processor.h

@ -65,12 +65,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
static Handle<ObjectTemplate> MakeMapTemplate(); static Handle<ObjectTemplate> MakeMapTemplate();
// Callbacks that access the individual fields of request objects. // Callbacks that access the individual fields of request objects.
static Handle<Value> GetPath(Local<String> name, const AccessorInfo& info); static Handle<Value> GetPath (Local<String> name, const AccessorInfo& info);
static Handle<Value> GetReferrer(Local<String> name, static Handle<Value> GetMethod (Local<String> name, const AccessorInfo& info);
const AccessorInfo& info);
static Handle<Value> GetHost(Local<String> name, const AccessorInfo& info);
static Handle<Value> GetUserAgent(Local<String> name,
const AccessorInfo& info);
// Callbacks that access maps // Callbacks that access maps
static Handle<Value> MapGet(Local<String> name, const AccessorInfo& info); static Handle<Value> MapGet(Local<String> name, const AccessorInfo& info);

3
server.cc

@ -45,12 +45,15 @@ void on_headers_complete
} else { } else {
printf("unsuccesful request\n"); printf("unsuccesful request\n");
} }
} }
void on_request_complete void on_request_complete
( ebb_request *req ( ebb_request *req
) )
{ {
HttpRequest *request = static_cast<HttpRequest*> (req->data);
oi_socket_close(&request->connection.socket);
} }
ebb_request * on_request ebb_request * on_request

Loading…
Cancel
Save