V8: process.cc (original) (raw)
#include <stdlib.h>
#include <string.h>
#include
#include
using std::map;
using std::pair;
using std::string;
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;
};
class HttpRequestProcessor {
public:
virtual ~HttpRequestProcessor() { }
virtual bool Initialize(map<string, string>* options,
map<string, string>* output) = 0;
virtual bool Process(HttpRequest* req) = 0;
static void Log(const char* event);
};
class JsHttpRequestProcessor : public HttpRequestProcessor {
public:
JsHttpRequestProcessor(Isolate* isolate, Local script)
: isolate_(isolate), script_(script) {}
virtual ~JsHttpRequestProcessor();
virtual bool Initialize(map<string, string>* opts,
map<string, string>* output);
virtual bool Process(HttpRequest* req);
private:
bool ExecuteScript(Local script);
bool InstallMaps(map<string, string>* opts, map<string, string>* output);
static Local MakeRequestTemplate(Isolate* isolate);
static Local MakeMapTemplate(Isolate* isolate);
static void GetPath(Local name,
const PropertyCallbackInfo& info);
static void GetReferrer(Local name,
const PropertyCallbackInfo& info);
static void GetHost(Local name,
const PropertyCallbackInfo& info);
static void GetUserAgent(Local name,
const PropertyCallbackInfo& info);
const PropertyCallbackInfo& info);
static v8::Intercepted MapSet(Local name, Local value,
const PropertyCallbackInfo& info);
Local WrapMap(map<string, string>* obj);
static map<string, string>* UnwrapMap(Local obj);
Local WrapRequest(HttpRequest* obj);
static HttpRequest* UnwrapRequest(Local obj);
Isolate* GetIsolate() { return isolate_; }
Isolate* isolate_;
Local script_;
Global context_;
Global process_;
static Global request_template_;
static Global map_template_;
};
if (info.Length() < 1) return;
HandleScope scope(isolate);
Local arg = info[0];
String::Utf8Value value(isolate, arg);
HttpRequestProcessor::Log(*value);
}
bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
map<string, string>* output) {
HandleScope handle_scope(GetIsolate());
Local global = ObjectTemplate::New(GetIsolate());
global->Set(GetIsolate(), "log",
FunctionTemplate::New(GetIsolate(), LogCallback));
context_.Reset(GetIsolate(), context);
Context::Scope context_scope(context);
if (!InstallMaps(opts, output))
return false;
if (!ExecuteScript(script_))
return false;
Local process_name =
String::NewFromUtf8Literal(GetIsolate(), "Process");
Local process_val;
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
!process_val->IsFunction()) {
return false;
}
Local process_fun = process_val.As();
process_.Reset(GetIsolate(), process_fun);
return true;
}
bool JsHttpRequestProcessor::ExecuteScript(Local script) {
HandleScope handle_scope(GetIsolate());
TryCatch try_catch(GetIsolate());
Local context(GetIsolate()->GetCurrentContext());
Local