#include <stdlib.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
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:
: isolate_(isolate), script_(script) {}
virtual ~JsHttpRequestProcessor();
virtual bool Initialize(map<string, string>* opts,
map<string, string>* output);
virtual bool Process(HttpRequest* req);
private:
bool InstallMaps(map<string, string>* opts, map<string, string>* output);
Isolate* GetIsolate() {
return isolate_; }
};
if (args.
Length() < 1)
return;
HttpRequestProcessor::Log(*value);
}
bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
map<string, string>* output) {
global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal)
.ToLocalChecked(),
FunctionTemplate::New(GetIsolate(), LogCallback));
context_.Reset(GetIsolate(), context);
if (!InstallMaps(opts, output))
return false;
if (!ExecuteScript(script_))
return false;
String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal)
.ToLocalChecked();
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
!process_val->IsFunction()) {
return false;
}
process_.Reset(GetIsolate(), process_fun);
return true;
}
bool JsHttpRequestProcessor::ExecuteScript(
Local<String> script) {
if (!Script::Compile(context, script).ToLocal(&compiled_script)) {
Log(*error);
return false;
}
if (!compiled_script->Run(context).ToLocal(&result)) {
Log(*error);
return false;
}
return true;
}
bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
map<string, string>* output) {
context->Global()
->Set(context,
String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal)
.ToLocalChecked(),
opts_obj)
.FromJust();
context->Global()
->Set(context,
String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal)
.ToLocalChecked(),
output_obj)
.FromJust();
return true;
}
bool JsHttpRequestProcessor::Process(HttpRequest* request) {
const int argc = 1;
if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
Log(*error);
return false;
}
return true;
}
JsHttpRequestProcessor::~JsHttpRequestProcessor() {
context_.Reset();
process_.Reset();
}
Local<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
if (map_template_.IsEmpty()) {
map_template_.Reset(GetIsolate(), raw_template);
}
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
result->SetInternalField(0, map_ptr);
return handle_scope.Escape(result);
}
map<string, string>* JsHttpRequestProcessor::UnwrapMap(
Local<Object> obj) {
void* ptr = field->Value();
return static_cast<map<string, string>*>(ptr);
}
return string(*utf8_value);
}
if (name->IsSymbol()) return;
map<string, string>* obj = UnwrapMap(info.
Holder());
map<string, string>::iterator iter = obj->find(key);
if (iter == obj->end()) return;
const string& value = (*iter).second;
String::NewFromUtf8(info.
GetIsolate(), value.c_str(),
NewStringType::kNormal,
static_cast<int>(value.length())).ToLocalChecked());
}
if (name->IsSymbol()) return;
map<string, string>* obj = UnwrapMap(info.
Holder());
string value = ObjectToString(info.
GetIsolate(), value_obj);
(*obj)[key] = value;
}
result->SetInternalFieldCount(1);
return handle_scope.Escape(result);
}
Local<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
if (request_template_.IsEmpty()) {
request_template_.Reset(GetIsolate(), raw_template);
}
templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
result->SetInternalField(0, request_ptr);
return handle_scope.Escape(result);
}
HttpRequest* JsHttpRequestProcessor::UnwrapRequest(
Local<Object> obj) {
void* ptr = field->Value();
return static_cast<HttpRequest*>(ptr);
}
HttpRequest* request = UnwrapRequest(info.
Holder());
const string& path = request->Path();
String::NewFromUtf8(info.
GetIsolate(), path.c_str(),
NewStringType::kNormal,
static_cast<int>(path.length())).ToLocalChecked());
}
void JsHttpRequestProcessor::GetReferrer(
HttpRequest* request = UnwrapRequest(info.
Holder());
const string& path = request->Referrer();
String::NewFromUtf8(info.
GetIsolate(), path.c_str(),
NewStringType::kNormal,
static_cast<int>(path.length())).ToLocalChecked());
}
HttpRequest* request = UnwrapRequest(info.
Holder());
const string& path = request->Host();
String::NewFromUtf8(info.
GetIsolate(), path.c_str(),
NewStringType::kNormal,
static_cast<int>(path.length())).ToLocalChecked());
}
void JsHttpRequestProcessor::GetUserAgent(
HttpRequest* request = UnwrapRequest(info.
Holder());
const string& path = request->UserAgent();
String::NewFromUtf8(info.
GetIsolate(), path.c_str(),
NewStringType::kNormal,
static_cast<int>(path.length())).ToLocalChecked());
}
result->SetInternalFieldCount(1);
result->SetAccessor(
String::NewFromUtf8(isolate, "path", NewStringType::kInternalized)
.ToLocalChecked(),
GetPath);
result->SetAccessor(
String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized)
.ToLocalChecked(),
GetReferrer);
result->SetAccessor(
String::NewFromUtf8(isolate, "host", NewStringType::kInternalized)
.ToLocalChecked(),
GetHost);
result->SetAccessor(
String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized)
.ToLocalChecked(),
GetUserAgent);
return handle_scope.Escape(result);
}
void HttpRequestProcessor::Log(const char* event) {
printf("Logged: %s\n", event);
}
class StringHttpRequest : public HttpRequest {
public:
StringHttpRequest(const string& path,
const string& referrer,
const string& host,
const string& user_agent);
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,
const string& referrer,
const string& host,
const string& user_agent)
: path_(path),
referrer_(referrer),
host_(host),
user_agent_(user_agent) { }
void ParseOptions(int argc,
char* argv[],
map<string, string>* options,
string* file) {
for (int i = 1; i < argc; i++) {
string arg = argv[i];
size_t index = arg.find('=', 0);
if (index == string::npos) {
*file = arg;
} else {
string key = arg.substr(0, index);
string value = arg.substr(index+1);
(*options)[key] = value;
}
}
}
FILE* file = fopen(name.c_str(), "rb");
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
for (size_t i = 0; i < size;) {
i += fread(&chars[i], 1, size - i, file);
if (ferror(file)) {
fclose(file);
}
}
fclose(file);
isolate, chars, NewStringType::kNormal, static_cast<int>(size));
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")
};
bool ProcessEntries(
v8::Platform* platform, HttpRequestProcessor* processor,
int count, StringHttpRequest* reqs) {
for (int i = 0; i < count; i++) {
bool result = processor->Process(&reqs[i]);
continue;
if (!result) return false;
}
return true;
}
void PrintMap(map<string, string>* m) {
for (map<string, string>::iterator i = m->begin(); i != m->end(); i++) {
pair<string, string> entry = *i;
printf("%s: %s\n", entry.first.c_str(), entry.second.c_str());
}
}
int main(int argc, char* argv[]) {
map<string, string> options;
string file;
ParseOptions(argc, argv, &options, &file);
if (file.empty()) {
fprintf(stderr, "No script was specified.\n");
return 1;
}
Isolate* isolate = Isolate::New(create_params);
if (!ReadFile(isolate, file).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'.\n", file.c_str());
return 1;
}
JsHttpRequestProcessor processor(isolate, source);
map<string, string> output;
if (!processor.Initialize(&options, &output)) {
fprintf(stderr, "Error initializing processor.\n");
return 1;
}
if (!ProcessEntries(platform, &processor, kSampleSize, kSampleRequests))
return 1;
PrintMap(&output);
}
static Allocator * NewDefaultAllocator()
Isolate * GetIsolate() const
Definition: v8.h:9509
int Length() const
Definition: v8.h:9527
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8.h:9136
Local< Object > Holder() const
Definition: v8.h:10139
ReturnValue< T > GetReturnValue() const
Definition: v8.h:10145
Isolate * GetIsolate() const
Definition: v8.h:10121
static void InitializePlatform(Platform *platform)
static bool InitializeICUDefaultLocation(const char *exec_path, const char *icu_data_file=nullptr)
static void InitializeExternalStartupData(const char *directory_path)
Definition: libplatform.h:12
ArrayBuffer::Allocator * array_buffer_allocator
Definition: v8.h:6831