2012-06-19 13:45:30 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-30 08:49:36 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-07-03 07:37:27 +00:00
|
|
|
#include <include/v8.h>
|
|
|
|
|
|
|
|
#include <include/libplatform/libplatform.h>
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2015-04-28 12:28:31 +00:00
|
|
|
#include <stdlib.h>
|
2015-04-28 11:25:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
#include <map>
|
2014-06-20 08:40:11 +00:00
|
|
|
#include <string>
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
// These interfaces represent an existing request processing interface.
|
|
|
|
// The idea is to imagine a real application that uses these interfaces
|
|
|
|
// and then add scripting capabilities that allow you to interact with
|
|
|
|
// the objects through JavaScript.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simplified http request.
|
|
|
|
*/
|
|
|
|
class HttpRequest {
|
|
|
|
public:
|
|
|
|
virtual ~HttpRequest() { }
|
|
|
|
virtual const string& Path() = 0;
|
|
|
|
virtual const string& Referrer() = 0;
|
|
|
|
virtual const string& Host() = 0;
|
|
|
|
virtual const string& UserAgent() = 0;
|
|
|
|
};
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
/**
|
|
|
|
* The abstract superclass of http request processors.
|
|
|
|
*/
|
|
|
|
class HttpRequestProcessor {
|
|
|
|
public:
|
|
|
|
virtual ~HttpRequestProcessor() { }
|
|
|
|
|
|
|
|
// Initialize this processor. The map contains options that control
|
|
|
|
// how requests should be processed.
|
|
|
|
virtual bool Initialize(map<string, string>* options,
|
2008-08-27 10:11:39 +00:00
|
|
|
map<string, string>* output) = 0;
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Process a single request.
|
|
|
|
virtual bool Process(HttpRequest* req) = 0;
|
|
|
|
|
|
|
|
static void Log(const char* event);
|
|
|
|
};
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
/**
|
|
|
|
* An http request processor that is scriptable using JavaScript.
|
|
|
|
*/
|
|
|
|
class JsHttpRequestProcessor : public HttpRequestProcessor {
|
|
|
|
public:
|
|
|
|
// Creates a new processor that processes requests by invoking the
|
|
|
|
// Process function of the JavaScript script given as an argument.
|
2015-07-02 15:42:35 +00:00
|
|
|
JsHttpRequestProcessor(Isolate* isolate, Local<String> script)
|
|
|
|
: isolate_(isolate), script_(script) {}
|
2008-07-30 08:49:36 +00:00
|
|
|
virtual ~JsHttpRequestProcessor();
|
|
|
|
|
|
|
|
virtual bool Initialize(map<string, string>* opts,
|
2008-08-27 10:11:39 +00:00
|
|
|
map<string, string>* output);
|
2008-07-30 08:49:36 +00:00
|
|
|
virtual bool Process(HttpRequest* req);
|
|
|
|
|
|
|
|
private:
|
2008-08-27 10:11:39 +00:00
|
|
|
// Execute the script associated with this processor and extract the
|
|
|
|
// Process function. Returns true if this succeeded, otherwise false.
|
2015-07-02 15:42:35 +00:00
|
|
|
bool ExecuteScript(Local<String> script);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Wrap the options and output map in a JavaScript objects and
|
|
|
|
// install it in the global namespace as 'options' and 'output'.
|
|
|
|
bool InstallMaps(map<string, string>* opts, map<string, string>* output);
|
|
|
|
|
|
|
|
// Constructs the template that describes the JavaScript wrapper
|
|
|
|
// type for requests.
|
2015-07-02 15:42:35 +00:00
|
|
|
static Local<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
|
|
|
|
static Local<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Callbacks that access the individual fields of request objects.
|
2013-06-05 12:36:33 +00:00
|
|
|
static void GetPath(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info);
|
|
|
|
static void GetReferrer(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info);
|
|
|
|
static void GetHost(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info);
|
|
|
|
static void GetUserAgent(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Callbacks that access maps
|
2014-11-27 10:21:32 +00:00
|
|
|
static void MapGet(Local<Name> name, const PropertyCallbackInfo<Value>& info);
|
|
|
|
static void MapSet(Local<Name> name, Local<Value> value,
|
2013-06-05 12:36:33 +00:00
|
|
|
const PropertyCallbackInfo<Value>& info);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Utility methods for wrapping C++ objects as JavaScript objects,
|
|
|
|
// and going back again.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> WrapMap(map<string, string>* obj);
|
|
|
|
static map<string, string>* UnwrapMap(Local<Object> obj);
|
|
|
|
Local<Object> WrapRequest(HttpRequest* obj);
|
|
|
|
static HttpRequest* UnwrapRequest(Local<Object> obj);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
Isolate* GetIsolate() { return isolate_; }
|
2013-01-25 08:31:46 +00:00
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
Isolate* isolate_;
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<String> script_;
|
|
|
|
Global<Context> context_;
|
|
|
|
Global<Function> process_;
|
|
|
|
static Global<ObjectTemplate> request_template_;
|
|
|
|
static Global<ObjectTemplate> map_template_;
|
2008-07-30 08:49:36 +00:00
|
|
|
};
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// -------------------------
|
|
|
|
// --- P r o c e s s o r ---
|
|
|
|
// -------------------------
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() < 1) return;
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(args.GetIsolate());
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Value> arg = args[0];
|
2008-09-10 11:41:48 +00:00
|
|
|
String::Utf8Value value(arg);
|
2008-07-30 08:49:36 +00:00
|
|
|
HttpRequestProcessor::Log(*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Execute the script and fetch the Process method.
|
|
|
|
bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
|
2008-08-27 10:11:39 +00:00
|
|
|
map<string, string>* output) {
|
2008-07-30 08:49:36 +00:00
|
|
|
// Create a handle scope to hold the temporary references.
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Create a template for the global object where we set the
|
|
|
|
// built-in global functions.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
|
|
|
|
global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
2013-12-18 10:31:42 +00:00
|
|
|
FunctionTemplate::New(GetIsolate(), LogCallback));
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2010-11-24 09:21:29 +00:00
|
|
|
// Each processor gets its own context so different processors don't
|
|
|
|
// affect each other. Context::New returns a persistent handle which
|
|
|
|
// is what we need for the reference to remain after we return from
|
|
|
|
// this method. That persistent handle has to be disposed in the
|
|
|
|
// destructor.
|
2015-07-02 15:42:35 +00:00
|
|
|
v8::Local<v8::Context> context = Context::New(GetIsolate(), NULL, global);
|
2013-05-28 10:36:21 +00:00
|
|
|
context_.Reset(GetIsolate(), context);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Enter the new context so all the following operations take place
|
|
|
|
// within it.
|
2013-05-28 10:36:21 +00:00
|
|
|
Context::Scope context_scope(context);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Make the options mapping available within the context
|
|
|
|
if (!InstallMaps(opts, output))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Compile and run the script
|
|
|
|
if (!ExecuteScript(script_))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The script compiled and ran correctly. Now we fetch out the
|
|
|
|
// Process function from the global object.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<String> process_name =
|
|
|
|
String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<Value> process_val;
|
2008-07-30 08:49:36 +00:00
|
|
|
// If there is no Process function, or if it is not a function,
|
|
|
|
// bail out
|
2015-07-02 15:42:35 +00:00
|
|
|
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
|
|
|
|
!process_val->IsFunction()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// It is a function; cast it to a Function
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Function> process_fun = Local<Function>::Cast(process_val);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
// Store the function in a Global handle, since we also want
|
2008-07-30 08:49:36 +00:00
|
|
|
// that to remain after this call returns
|
2013-05-29 08:39:37 +00:00
|
|
|
process_.Reset(GetIsolate(), process_fun);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// All done; all went well
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
bool JsHttpRequestProcessor::ExecuteScript(Local<String> script) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// We're just about to compile the script; set up an error handler to
|
|
|
|
// catch any exceptions the script might throw.
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch try_catch(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Context> context(GetIsolate()->GetCurrentContext());
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Compile the script and check for errors.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Script> compiled_script;
|
|
|
|
if (!Script::Compile(context, script).ToLocal(&compiled_script)) {
|
2008-09-10 11:41:48 +00:00
|
|
|
String::Utf8Value error(try_catch.Exception());
|
2008-07-30 08:49:36 +00:00
|
|
|
Log(*error);
|
|
|
|
// The script failed to compile; bail out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the script!
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Value> result;
|
|
|
|
if (!compiled_script->Run(context).ToLocal(&result)) {
|
2008-07-30 08:49:36 +00:00
|
|
|
// The TryCatch above is still in effect and will have caught the error.
|
2008-09-10 11:41:48 +00:00
|
|
|
String::Utf8Value error(try_catch.Exception());
|
2008-07-30 08:49:36 +00:00
|
|
|
Log(*error);
|
|
|
|
// Running the script failed; bail out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
|
2008-08-27 10:11:39 +00:00
|
|
|
map<string, string>* output) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Wrap the map object in a JavaScript wrapper
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> opts_obj = WrapMap(opts);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(GetIsolate(), context_);
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Set the options object as a property on the global object.
|
2015-07-02 15:42:35 +00:00
|
|
|
context->Global()
|
|
|
|
->Set(context,
|
|
|
|
String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
opts_obj)
|
|
|
|
.FromJust();
|
|
|
|
|
|
|
|
Local<Object> output_obj = WrapMap(output);
|
|
|
|
context->Global()
|
|
|
|
->Set(context,
|
|
|
|
String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
output_obj)
|
|
|
|
.FromJust();
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JsHttpRequestProcessor::Process(HttpRequest* request) {
|
|
|
|
// Create a handle scope to keep the temporary object references.
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(GetIsolate(), context_);
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Enter this processor's context so all the remaining operations
|
|
|
|
// take place there
|
2013-05-28 10:36:21 +00:00
|
|
|
Context::Scope context_scope(context);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Wrap the C++ request object in a JavaScript wrapper
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> request_obj = WrapRequest(request);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Set up an exception handler before calling the Process function
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch try_catch(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Invoke the process function, giving the global object as 'this'
|
|
|
|
// and one argument, the request.
|
|
|
|
const int argc = 1;
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Value> argv[argc] = {request_obj};
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Function> process =
|
|
|
|
v8::Local<v8::Function>::New(GetIsolate(), process_);
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Value> result;
|
|
|
|
if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
|
2008-09-10 11:41:48 +00:00
|
|
|
String::Utf8Value error(try_catch.Exception());
|
2008-07-30 08:49:36 +00:00
|
|
|
Log(*error);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsHttpRequestProcessor::~JsHttpRequestProcessor() {
|
2017-08-02 08:23:36 +00:00
|
|
|
// Dispose the persistent handles. When no one else has any
|
2008-07-30 08:49:36 +00:00
|
|
|
// references to the objects stored in the handles they will be
|
|
|
|
// automatically reclaimed.
|
2013-11-22 12:26:00 +00:00
|
|
|
context_.Reset();
|
|
|
|
process_.Reset();
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
Global<ObjectTemplate> JsHttpRequestProcessor::request_template_;
|
|
|
|
Global<ObjectTemplate> JsHttpRequestProcessor::map_template_;
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------
|
|
|
|
// --- A c c e s s i n g M a p s ---
|
|
|
|
// -----------------------------------
|
|
|
|
|
|
|
|
// Utility function that wraps a C++ http request object in a
|
|
|
|
// JavaScript object.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
|
|
|
|
// Local scope for temporary handles.
|
2013-12-04 10:09:08 +00:00
|
|
|
EscapableHandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Fetch the template for creating JavaScript map wrappers.
|
|
|
|
// It only has to be created once, which we do on demand.
|
2010-05-08 14:45:32 +00:00
|
|
|
if (map_template_.IsEmpty()) {
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
|
2013-05-29 08:39:37 +00:00
|
|
|
map_template_.Reset(GetIsolate(), raw_template);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> templ =
|
2013-05-02 20:18:42 +00:00
|
|
|
Local<ObjectTemplate>::New(GetIsolate(), map_template_);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Create an empty map wrapper.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> result =
|
|
|
|
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Wrap the raw C++ pointer in an External so it can be referenced
|
|
|
|
// from within JavaScript.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<External> map_ptr = External::New(GetIsolate(), obj);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Store the map pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, map_ptr);
|
|
|
|
|
|
|
|
// Return the result through the current handle scope. Since each
|
|
|
|
// of these handles will go away when the handle scope is deleted
|
|
|
|
// we need to call Close to let one, the result, escape into the
|
|
|
|
// outer handle scope.
|
2013-12-04 10:09:08 +00:00
|
|
|
return handle_scope.Escape(result);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utility function that extracts the C++ map pointer from a wrapper
|
|
|
|
// object.
|
2015-07-02 15:42:35 +00:00
|
|
|
map<string, string>* JsHttpRequestProcessor::UnwrapMap(Local<Object> obj) {
|
|
|
|
Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
|
2008-07-30 08:49:36 +00:00
|
|
|
void* ptr = field->Value();
|
|
|
|
return static_cast<map<string, string>*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert a JavaScript string to a std::string. To not bother too
|
|
|
|
// much with string encodings we just use ascii.
|
|
|
|
string ObjectToString(Local<Value> value) {
|
2008-09-10 11:41:48 +00:00
|
|
|
String::Utf8Value utf8_value(value);
|
|
|
|
return string(*utf8_value);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-27 10:21:32 +00:00
|
|
|
void JsHttpRequestProcessor::MapGet(Local<Name> name,
|
2013-06-05 12:36:33 +00:00
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2014-11-27 10:21:32 +00:00
|
|
|
if (name->IsSymbol()) return;
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Fetch the map wrapped by this object.
|
|
|
|
map<string, string>* obj = UnwrapMap(info.Holder());
|
|
|
|
|
|
|
|
// Convert the JavaScript string to a std::string.
|
2014-11-27 10:21:32 +00:00
|
|
|
string key = ObjectToString(Local<String>::Cast(name));
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Look up the value if it exists using the standard STL ideom.
|
|
|
|
map<string, string>::iterator iter = obj->find(key);
|
|
|
|
|
|
|
|
// If the key is not present return an empty handle as signal
|
2013-06-05 12:36:33 +00:00
|
|
|
if (iter == obj->end()) return;
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Otherwise fetch the value and wrap it in a JavaScript string
|
|
|
|
const string& value = (*iter).second;
|
2015-07-02 15:42:35 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(info.GetIsolate(), value.c_str(),
|
|
|
|
NewStringType::kNormal,
|
|
|
|
static_cast<int>(value.length())).ToLocalChecked());
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-27 10:21:32 +00:00
|
|
|
void JsHttpRequestProcessor::MapSet(Local<Name> name, Local<Value> value_obj,
|
2013-06-05 12:36:33 +00:00
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2014-11-27 10:21:32 +00:00
|
|
|
if (name->IsSymbol()) return;
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Fetch the map wrapped by this object.
|
|
|
|
map<string, string>* obj = UnwrapMap(info.Holder());
|
|
|
|
|
|
|
|
// Convert the key and value to std::strings.
|
2014-11-27 10:21:32 +00:00
|
|
|
string key = ObjectToString(Local<String>::Cast(name));
|
2008-07-30 08:49:36 +00:00
|
|
|
string value = ObjectToString(value_obj);
|
|
|
|
|
|
|
|
// Update the map.
|
|
|
|
(*obj)[key] = value;
|
|
|
|
|
|
|
|
// Return the value; any non-empty handle will work.
|
2013-06-05 12:36:33 +00:00
|
|
|
info.GetReturnValue().Set(value_obj);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
|
2013-03-15 12:06:53 +00:00
|
|
|
Isolate* isolate) {
|
2013-12-04 10:09:08 +00:00
|
|
|
EscapableHandleScope handle_scope(isolate);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2014-01-08 06:53:31 +00:00
|
|
|
Local<ObjectTemplate> result = ObjectTemplate::New(isolate);
|
2008-07-30 08:49:36 +00:00
|
|
|
result->SetInternalFieldCount(1);
|
2014-11-27 10:21:32 +00:00
|
|
|
result->SetHandler(NamedPropertyHandlerConfiguration(MapGet, MapSet));
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Again, return the result through the current handle scope.
|
2013-12-04 10:09:08 +00:00
|
|
|
return handle_scope.Escape(result);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------
|
|
|
|
// --- A c c e s s i n g R e q u e s t s ---
|
|
|
|
// -------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function that wraps a C++ http request object in a
|
|
|
|
* JavaScript object.
|
|
|
|
*/
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
|
|
|
|
// Local scope for temporary handles.
|
2013-12-04 10:09:08 +00:00
|
|
|
EscapableHandleScope handle_scope(GetIsolate());
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Fetch the template for creating JavaScript http request wrappers.
|
|
|
|
// It only has to be created once, which we do on demand.
|
|
|
|
if (request_template_.IsEmpty()) {
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
|
2013-05-29 08:39:37 +00:00
|
|
|
request_template_.Reset(GetIsolate(), raw_template);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> templ =
|
2013-05-02 20:18:42 +00:00
|
|
|
Local<ObjectTemplate>::New(GetIsolate(), request_template_);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Create an empty http request wrapper.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<Object> result =
|
|
|
|
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Wrap the raw C++ pointer in an External so it can be referenced
|
|
|
|
// from within JavaScript.
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<External> request_ptr = External::New(GetIsolate(), request);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Store the request pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, request_ptr);
|
|
|
|
|
|
|
|
// Return the result through the current handle scope. Since each
|
|
|
|
// of these handles will go away when the handle scope is deleted
|
|
|
|
// we need to call Close to let one, the result, escape into the
|
|
|
|
// outer handle scope.
|
2013-12-04 10:09:08 +00:00
|
|
|
return handle_scope.Escape(result);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function that extracts the C++ http request object from a
|
|
|
|
* wrapper object.
|
|
|
|
*/
|
2015-07-02 15:42:35 +00:00
|
|
|
HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Local<Object> obj) {
|
|
|
|
Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
|
2008-07-30 08:49:36 +00:00
|
|
|
void* ptr = field->Value();
|
|
|
|
return static_cast<HttpRequest*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void JsHttpRequestProcessor::GetPath(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2008-07-30 08:49:36 +00:00
|
|
|
// 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.
|
2015-07-02 15:42:35 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
|
|
|
NewStringType::kNormal,
|
|
|
|
static_cast<int>(path.length())).ToLocalChecked());
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void JsHttpRequestProcessor::GetReferrer(
|
|
|
|
Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2008-07-30 08:49:36 +00:00
|
|
|
HttpRequest* request = UnwrapRequest(info.Holder());
|
|
|
|
const string& path = request->Referrer();
|
2015-07-02 15:42:35 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
|
|
|
NewStringType::kNormal,
|
|
|
|
static_cast<int>(path.length())).ToLocalChecked());
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void JsHttpRequestProcessor::GetHost(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2008-07-30 08:49:36 +00:00
|
|
|
HttpRequest* request = UnwrapRequest(info.Holder());
|
|
|
|
const string& path = request->Host();
|
2015-07-02 15:42:35 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
|
|
|
NewStringType::kNormal,
|
|
|
|
static_cast<int>(path.length())).ToLocalChecked());
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void JsHttpRequestProcessor::GetUserAgent(
|
|
|
|
Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2008-07-30 08:49:36 +00:00
|
|
|
HttpRequest* request = UnwrapRequest(info.Holder());
|
|
|
|
const string& path = request->UserAgent();
|
2015-07-02 15:42:35 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(info.GetIsolate(), path.c_str(),
|
|
|
|
NewStringType::kNormal,
|
|
|
|
static_cast<int>(path.length())).ToLocalChecked());
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
|
2013-03-15 12:06:53 +00:00
|
|
|
Isolate* isolate) {
|
2013-12-04 10:09:08 +00:00
|
|
|
EscapableHandleScope handle_scope(isolate);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2014-01-08 06:53:31 +00:00
|
|
|
Local<ObjectTemplate> result = ObjectTemplate::New(isolate);
|
2008-07-30 08:49:36 +00:00
|
|
|
result->SetInternalFieldCount(1);
|
|
|
|
|
|
|
|
// Add accessors for each of the fields of the request.
|
2013-11-22 12:26:00 +00:00
|
|
|
result->SetAccessor(
|
2015-07-02 15:42:35 +00:00
|
|
|
String::NewFromUtf8(isolate, "path", NewStringType::kInternalized)
|
|
|
|
.ToLocalChecked(),
|
2013-11-22 12:26:00 +00:00
|
|
|
GetPath);
|
|
|
|
result->SetAccessor(
|
2015-07-02 15:42:35 +00:00
|
|
|
String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized)
|
|
|
|
.ToLocalChecked(),
|
2013-11-22 12:26:00 +00:00
|
|
|
GetReferrer);
|
|
|
|
result->SetAccessor(
|
2015-07-02 15:42:35 +00:00
|
|
|
String::NewFromUtf8(isolate, "host", NewStringType::kInternalized)
|
|
|
|
.ToLocalChecked(),
|
2013-11-22 12:26:00 +00:00
|
|
|
GetHost);
|
|
|
|
result->SetAccessor(
|
2015-07-02 15:42:35 +00:00
|
|
|
String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized)
|
|
|
|
.ToLocalChecked(),
|
2013-11-22 12:26:00 +00:00
|
|
|
GetUserAgent);
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
// Again, return the result through the current handle scope.
|
2013-12-04 10:09:08 +00:00
|
|
|
return handle_scope.Escape(result);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- Test ---
|
|
|
|
|
|
|
|
|
|
|
|
void HttpRequestProcessor::Log(const char* event) {
|
|
|
|
printf("Logged: %s\n", event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simplified http request.
|
|
|
|
*/
|
|
|
|
class StringHttpRequest : public HttpRequest {
|
|
|
|
public:
|
2008-08-27 10:11:39 +00:00
|
|
|
StringHttpRequest(const string& path,
|
|
|
|
const string& referrer,
|
|
|
|
const string& host,
|
|
|
|
const string& user_agent);
|
2008-07-30 08:49:36 +00:00
|
|
|
virtual const string& Path() { return path_; }
|
|
|
|
virtual const string& Referrer() { return referrer_; }
|
|
|
|
virtual const string& Host() { return host_; }
|
|
|
|
virtual const string& UserAgent() { return user_agent_; }
|
|
|
|
private:
|
|
|
|
string path_;
|
|
|
|
string referrer_;
|
|
|
|
string host_;
|
|
|
|
string user_agent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
StringHttpRequest::StringHttpRequest(const string& path,
|
2008-08-27 10:11:39 +00:00
|
|
|
const string& referrer,
|
|
|
|
const string& host,
|
|
|
|
const string& user_agent)
|
2008-07-30 08:49:36 +00:00
|
|
|
: path_(path),
|
|
|
|
referrer_(referrer),
|
|
|
|
host_(host),
|
|
|
|
user_agent_(user_agent) { }
|
|
|
|
|
|
|
|
|
2008-08-27 10:11:39 +00:00
|
|
|
void ParseOptions(int argc,
|
|
|
|
char* argv[],
|
2014-06-10 09:24:00 +00:00
|
|
|
map<string, string>* options,
|
2008-08-27 10:11:39 +00:00
|
|
|
string* file) {
|
2008-07-30 08:49:36 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
string arg = argv[i];
|
2011-06-09 09:07:17 +00:00
|
|
|
size_t index = arg.find('=', 0);
|
2008-07-30 08:49:36 +00:00
|
|
|
if (index == string::npos) {
|
|
|
|
*file = arg;
|
|
|
|
} else {
|
|
|
|
string key = arg.substr(0, index);
|
|
|
|
string value = arg.substr(index+1);
|
2014-06-10 09:24:00 +00:00
|
|
|
(*options)[key] = value;
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reads a file into a v8 string.
|
2015-07-02 15:42:35 +00:00
|
|
|
MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
|
2008-07-30 08:49:36 +00:00
|
|
|
FILE* file = fopen(name.c_str(), "rb");
|
2015-07-02 15:42:35 +00:00
|
|
|
if (file == NULL) return MaybeLocal<String>();
|
2008-07-30 08:49:36 +00:00
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
2015-04-28 06:54:08 +00:00
|
|
|
size_t size = ftell(file);
|
2008-07-30 08:49:36 +00:00
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
2015-04-28 06:54:08 +00:00
|
|
|
for (size_t i = 0; i < size;) {
|
|
|
|
i += fread(&chars[i], 1, size - i, file);
|
|
|
|
if (ferror(file)) {
|
|
|
|
fclose(file);
|
2015-07-02 15:42:35 +00:00
|
|
|
return MaybeLocal<String>();
|
2015-04-28 06:54:08 +00:00
|
|
|
}
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
fclose(file);
|
2015-07-02 15:42:35 +00:00
|
|
|
MaybeLocal<String> result = String::NewFromUtf8(
|
|
|
|
isolate, chars, NewStringType::kNormal, static_cast<int>(size));
|
2008-07-30 08:49:36 +00:00
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const int kSampleSize = 6;
|
|
|
|
StringHttpRequest kSampleRequests[kSampleSize] = {
|
|
|
|
StringHttpRequest("/process.cc", "localhost", "google.com", "firefox"),
|
|
|
|
StringHttpRequest("/", "localhost", "google.net", "firefox"),
|
|
|
|
StringHttpRequest("/", "localhost", "google.org", "safari"),
|
|
|
|
StringHttpRequest("/", "localhost", "yahoo.com", "ie"),
|
|
|
|
StringHttpRequest("/", "localhost", "yahoo.com", "safari"),
|
|
|
|
StringHttpRequest("/", "localhost", "yahoo.com", "firefox")
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-15 12:26:06 +00:00
|
|
|
bool ProcessEntries(v8::Platform* platform, HttpRequestProcessor* processor,
|
|
|
|
int count, StringHttpRequest* reqs) {
|
2008-07-30 08:49:36 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2015-07-15 12:26:06 +00:00
|
|
|
bool result = processor->Process(&reqs[i]);
|
|
|
|
while (v8::platform::PumpMessageLoop(platform, Isolate::GetCurrent()))
|
|
|
|
continue;
|
|
|
|
if (!result) return false;
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-29 12:52:21 +00:00
|
|
|
void PrintMap(map<string, string>* m) {
|
|
|
|
for (map<string, string>::iterator i = m->begin(); i != m->end(); i++) {
|
2008-07-30 08:49:36 +00:00
|
|
|
pair<string, string> entry = *i;
|
|
|
|
printf("%s: %s\n", entry.first.c_str(), entry.second.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2016-06-08 12:09:25 +00:00
|
|
|
v8::V8::InitializeICUDefaultLocation(argv[0]);
|
2015-08-14 12:11:29 +00:00
|
|
|
v8::V8::InitializeExternalStartupData(argv[0]);
|
2014-07-03 07:57:29 +00:00
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
2014-07-03 07:37:27 +00:00
|
|
|
v8::V8::InitializePlatform(platform);
|
2014-09-19 08:01:35 +00:00
|
|
|
v8::V8::Initialize();
|
2008-07-30 08:49:36 +00:00
|
|
|
map<string, string> options;
|
|
|
|
string file;
|
2014-06-10 09:24:00 +00:00
|
|
|
ParseOptions(argc, argv, &options, &file);
|
2008-07-30 08:49:36 +00:00
|
|
|
if (file.empty()) {
|
|
|
|
fprintf(stderr, "No script was specified.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate::CreateParams create_params;
|
2016-06-29 07:39:45 +00:00
|
|
|
create_params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate* isolate = Isolate::New(create_params);
|
2014-05-08 06:52:35 +00:00
|
|
|
Isolate::Scope isolate_scope(isolate);
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(isolate);
|
2015-07-02 15:42:35 +00:00
|
|
|
Local<String> source;
|
|
|
|
if (!ReadFile(isolate, file).ToLocal(&source)) {
|
2008-07-30 08:49:36 +00:00
|
|
|
fprintf(stderr, "Error reading '%s'.\n", file.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-15 12:06:53 +00:00
|
|
|
JsHttpRequestProcessor processor(isolate, source);
|
2008-07-30 08:49:36 +00:00
|
|
|
map<string, string> output;
|
|
|
|
if (!processor.Initialize(&options, &output)) {
|
|
|
|
fprintf(stderr, "Error initializing processor.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-07-15 12:26:06 +00:00
|
|
|
if (!ProcessEntries(platform, &processor, kSampleSize, kSampleRequests))
|
2008-07-30 08:49:36 +00:00
|
|
|
return 1;
|
2008-08-29 12:52:21 +00:00
|
|
|
PrintMap(&output);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|