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. 53
      js_http_request_processor.cc
  4. 6
      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) {
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();

10
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;
};

53
js_http_request_processor.cc

@ -385,47 +385,36 @@ Handle<Value> 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<Value> JsHttpRequestProcessor::GetReferrer
Handle<Value> JsHttpRequestProcessor::GetMethod
( Local<String> name
, const AccessorInfo& info
)
{
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Referrer();
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();
}
Handle<Value> JsHttpRequestProcessor::GetHost
( Local<String> name
, const AccessorInfo& info
)
{
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Host();
return String::New(path.c_str(), path.length());
}
Handle<Value> JsHttpRequestProcessor::GetUserAgent
( Local<String> name
, const AccessorInfo& info
)
{
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->UserAgent();
return String::New(path.c_str(), path.length());
}
@ -440,9 +429,7 @@ Handle<ObjectTemplate> 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);

6
js_http_request_processor.h

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

3
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<HttpRequest*> (req->data);
oi_socket_close(&request->connection.socket);
}
ebb_request * on_request

Loading…
Cancel
Save