blob: f44797066b821bddcd3e6bc70581352d08d63571 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include <include/v8.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include <include/libplatform/libplatform.h>
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include <map>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include <string>
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35using namespace std;
36using namespace v8;
37
38// These interfaces represent an existing request processing interface.
39// The idea is to imagine a real application that uses these interfaces
40// and then add scripting capabilities that allow you to interact with
41// the objects through JavaScript.
42
43/**
44 * A simplified http request.
45 */
46class HttpRequest {
47 public:
48 virtual ~HttpRequest() { }
49 virtual const string& Path() = 0;
50 virtual const string& Referrer() = 0;
51 virtual const string& Host() = 0;
52 virtual const string& UserAgent() = 0;
53};
54
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055
Steve Blocka7e24c12009-10-30 11:49:00 +000056/**
57 * The abstract superclass of http request processors.
58 */
59class HttpRequestProcessor {
60 public:
61 virtual ~HttpRequestProcessor() { }
62
63 // Initialize this processor. The map contains options that control
64 // how requests should be processed.
65 virtual bool Initialize(map<string, string>* options,
66 map<string, string>* output) = 0;
67
68 // Process a single request.
69 virtual bool Process(HttpRequest* req) = 0;
70
71 static void Log(const char* event);
72};
73
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074
Steve Blocka7e24c12009-10-30 11:49:00 +000075/**
76 * An http request processor that is scriptable using JavaScript.
77 */
78class JsHttpRequestProcessor : public HttpRequestProcessor {
79 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000080 // Creates a new processor that processes requests by invoking the
81 // Process function of the JavaScript script given as an argument.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 JsHttpRequestProcessor(Isolate* isolate, Handle<String> script)
83 : isolate_(isolate), script_(script) { }
Steve Blocka7e24c12009-10-30 11:49:00 +000084 virtual ~JsHttpRequestProcessor();
85
86 virtual bool Initialize(map<string, string>* opts,
87 map<string, string>* output);
88 virtual bool Process(HttpRequest* req);
89
90 private:
Steve Blocka7e24c12009-10-30 11:49:00 +000091 // Execute the script associated with this processor and extract the
92 // Process function. Returns true if this succeeded, otherwise false.
93 bool ExecuteScript(Handle<String> script);
94
95 // Wrap the options and output map in a JavaScript objects and
96 // install it in the global namespace as 'options' and 'output'.
97 bool InstallMaps(map<string, string>* opts, map<string, string>* output);
98
99 // Constructs the template that describes the JavaScript wrapper
100 // type for requests.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
102 static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
104 // Callbacks that access the individual fields of request objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 static void GetPath(Local<String> name,
106 const PropertyCallbackInfo<Value>& info);
107 static void GetReferrer(Local<String> name,
108 const PropertyCallbackInfo<Value>& info);
109 static void GetHost(Local<String> name,
110 const PropertyCallbackInfo<Value>& info);
111 static void GetUserAgent(Local<String> name,
112 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
114 // Callbacks that access maps
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400115 static void MapGet(Local<Name> name, const PropertyCallbackInfo<Value>& info);
116 static void MapSet(Local<Name> name, Local<Value> value,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
119 // Utility methods for wrapping C++ objects as JavaScript objects,
120 // and going back again.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 Handle<Object> WrapMap(map<string, string>* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 static map<string, string>* UnwrapMap(Handle<Object> obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 Handle<Object> WrapRequest(HttpRequest* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 static HttpRequest* UnwrapRequest(Handle<Object> obj);
125
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 Isolate* GetIsolate() { return isolate_; }
127
128 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 Handle<String> script_;
130 Persistent<Context> context_;
131 Persistent<Function> process_;
132 static Persistent<ObjectTemplate> request_template_;
133 static Persistent<ObjectTemplate> map_template_;
134};
135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136
Steve Blocka7e24c12009-10-30 11:49:00 +0000137// -------------------------
138// --- P r o c e s s o r ---
139// -------------------------
140
141
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
143 if (args.Length() < 1) return;
144 HandleScope scope(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 Handle<Value> arg = args[0];
146 String::Utf8Value value(arg);
147 HttpRequestProcessor::Log(*value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000148}
149
150
151// Execute the script and fetch the Process method.
152bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
153 map<string, string>* output) {
154 // Create a handle scope to hold the temporary references.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 HandleScope handle_scope(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
157 // Create a template for the global object where we set the
158 // built-in global functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 Handle<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
160 global->Set(String::NewFromUtf8(GetIsolate(), "log"),
161 FunctionTemplate::New(GetIsolate(), LogCallback));
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800163 // Each processor gets its own context so different processors don't
164 // affect each other. Context::New returns a persistent handle which
165 // is what we need for the reference to remain after we return from
166 // this method. That persistent handle has to be disposed in the
167 // destructor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 v8::Handle<v8::Context> context = Context::New(GetIsolate(), NULL, global);
169 context_.Reset(GetIsolate(), context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000170
171 // Enter the new context so all the following operations take place
172 // within it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000174
175 // Make the options mapping available within the context
176 if (!InstallMaps(opts, output))
177 return false;
178
179 // Compile and run the script
180 if (!ExecuteScript(script_))
181 return false;
182
183 // The script compiled and ran correctly. Now we fetch out the
184 // Process function from the global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 Handle<String> process_name = String::NewFromUtf8(GetIsolate(), "Process");
186 Handle<Value> process_val = context->Global()->Get(process_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187
188 // If there is no Process function, or if it is not a function,
189 // bail out
190 if (!process_val->IsFunction()) return false;
191
192 // It is a function; cast it to a Function
193 Handle<Function> process_fun = Handle<Function>::Cast(process_val);
194
195 // Store the function in a Persistent handle, since we also want
196 // that to remain after this call returns
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 process_.Reset(GetIsolate(), process_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199 // All done; all went well
200 return true;
201}
202
203
204bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 HandleScope handle_scope(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000206
207 // We're just about to compile the script; set up an error handler to
208 // catch any exceptions the script might throw.
209 TryCatch try_catch;
210
211 // Compile the script and check for errors.
212 Handle<Script> compiled_script = Script::Compile(script);
213 if (compiled_script.IsEmpty()) {
214 String::Utf8Value error(try_catch.Exception());
215 Log(*error);
216 // The script failed to compile; bail out.
217 return false;
218 }
219
220 // Run the script!
221 Handle<Value> result = compiled_script->Run();
222 if (result.IsEmpty()) {
223 // The TryCatch above is still in effect and will have caught the error.
224 String::Utf8Value error(try_catch.Exception());
225 Log(*error);
226 // Running the script failed; bail out.
227 return false;
228 }
229 return true;
230}
231
232
233bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
234 map<string, string>* output) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 HandleScope handle_scope(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000236
237 // Wrap the map object in a JavaScript wrapper
238 Handle<Object> opts_obj = WrapMap(opts);
239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 v8::Local<v8::Context> context =
241 v8::Local<v8::Context>::New(GetIsolate(), context_);
242
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 // Set the options object as a property on the global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 context->Global()->Set(String::NewFromUtf8(GetIsolate(), "options"),
245 opts_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246
247 Handle<Object> output_obj = WrapMap(output);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 context->Global()->Set(String::NewFromUtf8(GetIsolate(), "output"),
249 output_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000250
251 return true;
252}
253
254
255bool JsHttpRequestProcessor::Process(HttpRequest* request) {
256 // Create a handle scope to keep the temporary object references.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 HandleScope handle_scope(GetIsolate());
258
259 v8::Local<v8::Context> context =
260 v8::Local<v8::Context>::New(GetIsolate(), context_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261
262 // Enter this processor's context so all the remaining operations
263 // take place there
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265
266 // Wrap the C++ request object in a JavaScript wrapper
267 Handle<Object> request_obj = WrapRequest(request);
268
269 // Set up an exception handler before calling the Process function
270 TryCatch try_catch;
271
272 // Invoke the process function, giving the global object as 'this'
273 // and one argument, the request.
274 const int argc = 1;
275 Handle<Value> argv[argc] = { request_obj };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 v8::Local<v8::Function> process =
277 v8::Local<v8::Function>::New(GetIsolate(), process_);
278 Handle<Value> result = process->Call(context->Global(), argc, argv);
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 if (result.IsEmpty()) {
280 String::Utf8Value error(try_catch.Exception());
281 Log(*error);
282 return false;
283 } else {
284 return true;
285 }
286}
287
288
289JsHttpRequestProcessor::~JsHttpRequestProcessor() {
290 // Dispose the persistent handles. When noone else has any
291 // references to the objects stored in the handles they will be
292 // automatically reclaimed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 context_.Reset();
294 process_.Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +0000295}
296
297
298Persistent<ObjectTemplate> JsHttpRequestProcessor::request_template_;
299Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
300
301
302// -----------------------------------
303// --- A c c e s s i n g M a p s ---
304// -----------------------------------
305
306// Utility function that wraps a C++ http request object in a
307// JavaScript object.
308Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
309 // Handle scope for temporary handles.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 EscapableHandleScope handle_scope(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000311
312 // Fetch the template for creating JavaScript map wrappers.
313 // It only has to be created once, which we do on demand.
Kristian Monsen25f61362010-05-21 11:50:48 +0100314 if (map_template_.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
316 map_template_.Reset(GetIsolate(), raw_template);
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 Handle<ObjectTemplate> templ =
319 Local<ObjectTemplate>::New(GetIsolate(), map_template_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320
321 // Create an empty map wrapper.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 Local<Object> result = templ->NewInstance();
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 // Wrap the raw C++ pointer in an External so it can be referenced
325 // from within JavaScript.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 Handle<External> map_ptr = External::New(GetIsolate(), obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000327
328 // Store the map pointer in the JavaScript wrapper.
329 result->SetInternalField(0, map_ptr);
330
331 // Return the result through the current handle scope. Since each
332 // of these handles will go away when the handle scope is deleted
333 // we need to call Close to let one, the result, escape into the
334 // outer handle scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 return handle_scope.Escape(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000336}
337
338
339// Utility function that extracts the C++ map pointer from a wrapper
340// object.
341map<string, string>* JsHttpRequestProcessor::UnwrapMap(Handle<Object> obj) {
342 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
343 void* ptr = field->Value();
344 return static_cast<map<string, string>*>(ptr);
345}
346
347
348// Convert a JavaScript string to a std::string. To not bother too
349// much with string encodings we just use ascii.
350string ObjectToString(Local<Value> value) {
351 String::Utf8Value utf8_value(value);
352 return string(*utf8_value);
353}
354
355
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400356void JsHttpRequestProcessor::MapGet(Local<Name> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 const PropertyCallbackInfo<Value>& info) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400358 if (name->IsSymbol()) return;
359
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 // Fetch the map wrapped by this object.
361 map<string, string>* obj = UnwrapMap(info.Holder());
362
363 // Convert the JavaScript string to a std::string.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400364 string key = ObjectToString(Local<String>::Cast(name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
366 // Look up the value if it exists using the standard STL ideom.
367 map<string, string>::iterator iter = obj->find(key);
368
369 // If the key is not present return an empty handle as signal
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 if (iter == obj->end()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000371
372 // Otherwise fetch the value and wrap it in a JavaScript string
373 const string& value = (*iter).second;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 info.GetReturnValue().Set(String::NewFromUtf8(
375 info.GetIsolate(), value.c_str(), String::kNormalString,
376 static_cast<int>(value.length())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000377}
378
379
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400380void JsHttpRequestProcessor::MapSet(Local<Name> name, Local<Value> value_obj,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 const PropertyCallbackInfo<Value>& info) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400382 if (name->IsSymbol()) return;
383
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 // Fetch the map wrapped by this object.
385 map<string, string>* obj = UnwrapMap(info.Holder());
386
387 // Convert the key and value to std::strings.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400388 string key = ObjectToString(Local<String>::Cast(name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 string value = ObjectToString(value_obj);
390
391 // Update the map.
392 (*obj)[key] = value;
393
394 // Return the value; any non-empty handle will work.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 info.GetReturnValue().Set(value_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000396}
397
398
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
400 Isolate* isolate) {
401 EscapableHandleScope handle_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000402
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 Local<ObjectTemplate> result = ObjectTemplate::New(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 result->SetInternalFieldCount(1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400405 result->SetHandler(NamedPropertyHandlerConfiguration(MapGet, MapSet));
Steve Blocka7e24c12009-10-30 11:49:00 +0000406
407 // Again, return the result through the current handle scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 return handle_scope.Escape(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
412// -------------------------------------------
413// --- A c c e s s i n g R e q u e s t s ---
414// -------------------------------------------
415
416/**
417 * Utility function that wraps a C++ http request object in a
418 * JavaScript object.
419 */
420Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
421 // Handle scope for temporary handles.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 EscapableHandleScope handle_scope(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000423
424 // Fetch the template for creating JavaScript http request wrappers.
425 // It only has to be created once, which we do on demand.
426 if (request_template_.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
428 request_template_.Reset(GetIsolate(), raw_template);
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 Handle<ObjectTemplate> templ =
431 Local<ObjectTemplate>::New(GetIsolate(), request_template_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000432
433 // Create an empty http request wrapper.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 Local<Object> result = templ->NewInstance();
Steve Blocka7e24c12009-10-30 11:49:00 +0000435
436 // Wrap the raw C++ pointer in an External so it can be referenced
437 // from within JavaScript.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 Handle<External> request_ptr = External::New(GetIsolate(), request);
Steve Blocka7e24c12009-10-30 11:49:00 +0000439
440 // Store the request pointer in the JavaScript wrapper.
441 result->SetInternalField(0, request_ptr);
442
443 // Return the result through the current handle scope. Since each
444 // of these handles will go away when the handle scope is deleted
445 // we need to call Close to let one, the result, escape into the
446 // outer handle scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447 return handle_scope.Escape(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448}
449
450
451/**
452 * Utility function that extracts the C++ http request object from a
453 * wrapper object.
454 */
455HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) {
456 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
457 void* ptr = field->Value();
458 return static_cast<HttpRequest*>(ptr);
459}
460
461
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462void JsHttpRequestProcessor::GetPath(Local<String> name,
463 const PropertyCallbackInfo<Value>& info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 // Extract the C++ request object from the JavaScript wrapper.
465 HttpRequest* request = UnwrapRequest(info.Holder());
466
467 // Fetch the path.
468 const string& path = request->Path();
469
470 // Wrap the result in a JavaScript string and return it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 info.GetReturnValue().Set(String::NewFromUtf8(
472 info.GetIsolate(), path.c_str(), String::kNormalString,
473 static_cast<int>(path.length())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000474}
475
476
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477void JsHttpRequestProcessor::GetReferrer(
478 Local<String> name,
479 const PropertyCallbackInfo<Value>& info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 HttpRequest* request = UnwrapRequest(info.Holder());
481 const string& path = request->Referrer();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 info.GetReturnValue().Set(String::NewFromUtf8(
483 info.GetIsolate(), path.c_str(), String::kNormalString,
484 static_cast<int>(path.length())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000485}
486
487
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488void JsHttpRequestProcessor::GetHost(Local<String> name,
489 const PropertyCallbackInfo<Value>& info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 HttpRequest* request = UnwrapRequest(info.Holder());
491 const string& path = request->Host();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 info.GetReturnValue().Set(String::NewFromUtf8(
493 info.GetIsolate(), path.c_str(), String::kNormalString,
494 static_cast<int>(path.length())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000495}
496
497
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498void JsHttpRequestProcessor::GetUserAgent(
499 Local<String> name,
500 const PropertyCallbackInfo<Value>& info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 HttpRequest* request = UnwrapRequest(info.Holder());
502 const string& path = request->UserAgent();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 info.GetReturnValue().Set(String::NewFromUtf8(
504 info.GetIsolate(), path.c_str(), String::kNormalString,
505 static_cast<int>(path.length())));
Steve Blocka7e24c12009-10-30 11:49:00 +0000506}
507
508
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
510 Isolate* isolate) {
511 EscapableHandleScope handle_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000512
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513 Local<ObjectTemplate> result = ObjectTemplate::New(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 result->SetInternalFieldCount(1);
515
516 // Add accessors for each of the fields of the request.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 result->SetAccessor(
518 String::NewFromUtf8(isolate, "path", String::kInternalizedString),
519 GetPath);
520 result->SetAccessor(
521 String::NewFromUtf8(isolate, "referrer", String::kInternalizedString),
522 GetReferrer);
523 result->SetAccessor(
524 String::NewFromUtf8(isolate, "host", String::kInternalizedString),
525 GetHost);
526 result->SetAccessor(
527 String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString),
528 GetUserAgent);
Steve Blocka7e24c12009-10-30 11:49:00 +0000529
530 // Again, return the result through the current handle scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 return handle_scope.Escape(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000532}
533
534
535// --- Test ---
536
537
538void HttpRequestProcessor::Log(const char* event) {
539 printf("Logged: %s\n", event);
540}
541
542
543/**
544 * A simplified http request.
545 */
546class StringHttpRequest : public HttpRequest {
547 public:
548 StringHttpRequest(const string& path,
549 const string& referrer,
550 const string& host,
551 const string& user_agent);
552 virtual const string& Path() { return path_; }
553 virtual const string& Referrer() { return referrer_; }
554 virtual const string& Host() { return host_; }
555 virtual const string& UserAgent() { return user_agent_; }
556 private:
557 string path_;
558 string referrer_;
559 string host_;
560 string user_agent_;
561};
562
563
564StringHttpRequest::StringHttpRequest(const string& path,
565 const string& referrer,
566 const string& host,
567 const string& user_agent)
568 : path_(path),
569 referrer_(referrer),
570 host_(host),
571 user_agent_(user_agent) { }
572
573
574void ParseOptions(int argc,
575 char* argv[],
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 map<string, string>* options,
Steve Blocka7e24c12009-10-30 11:49:00 +0000577 string* file) {
578 for (int i = 1; i < argc; i++) {
579 string arg = argv[i];
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000580 size_t index = arg.find('=', 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 if (index == string::npos) {
582 *file = arg;
583 } else {
584 string key = arg.substr(0, index);
585 string value = arg.substr(index+1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 (*options)[key] = value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 }
588 }
589}
590
591
592// Reads a file into a v8 string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593Handle<String> ReadFile(Isolate* isolate, const string& name) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 FILE* file = fopen(name.c_str(), "rb");
595 if (file == NULL) return Handle<String>();
596
597 fseek(file, 0, SEEK_END);
598 int size = ftell(file);
599 rewind(file);
600
601 char* chars = new char[size + 1];
602 chars[size] = '\0';
603 for (int i = 0; i < size;) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 i += read;
606 }
607 fclose(file);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608 Handle<String> result =
609 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 delete[] chars;
611 return result;
612}
613
614
615const int kSampleSize = 6;
616StringHttpRequest kSampleRequests[kSampleSize] = {
617 StringHttpRequest("/process.cc", "localhost", "google.com", "firefox"),
618 StringHttpRequest("/", "localhost", "google.net", "firefox"),
619 StringHttpRequest("/", "localhost", "google.org", "safari"),
620 StringHttpRequest("/", "localhost", "yahoo.com", "ie"),
621 StringHttpRequest("/", "localhost", "yahoo.com", "safari"),
622 StringHttpRequest("/", "localhost", "yahoo.com", "firefox")
623};
624
625
626bool ProcessEntries(HttpRequestProcessor* processor, int count,
627 StringHttpRequest* reqs) {
628 for (int i = 0; i < count; i++) {
629 if (!processor->Process(&reqs[i]))
630 return false;
631 }
632 return true;
633}
634
635
636void PrintMap(map<string, string>* m) {
637 for (map<string, string>::iterator i = m->begin(); i != m->end(); i++) {
638 pair<string, string> entry = *i;
639 printf("%s: %s\n", entry.first.c_str(), entry.second.c_str());
640 }
641}
642
643
644int main(int argc, char* argv[]) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 v8::V8::InitializeICU();
646 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
647 v8::V8::InitializePlatform(platform);
648 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000649 map<string, string> options;
650 string file;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651 ParseOptions(argc, argv, &options, &file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 if (file.empty()) {
653 fprintf(stderr, "No script was specified.\n");
654 return 1;
655 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656 Isolate* isolate = Isolate::New();
657 Isolate::Scope isolate_scope(isolate);
658 HandleScope scope(isolate);
659 Handle<String> source = ReadFile(isolate, file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 if (source.IsEmpty()) {
661 fprintf(stderr, "Error reading '%s'.\n", file.c_str());
662 return 1;
663 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 JsHttpRequestProcessor processor(isolate, source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 map<string, string> output;
666 if (!processor.Initialize(&options, &output)) {
667 fprintf(stderr, "Error initializing processor.\n");
668 return 1;
669 }
670 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests))
671 return 1;
672 PrintMap(&output);
673}