#ifndef js_http_request_processor_h #define js_http_request_processor_h #include #include #include #include "http_request.h" 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. /** * 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* options, map* output) = 0; // Process a single request. virtual bool Process(HttpRequest* req) = 0; static void Log(const char* event); }; /** * 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. explicit JsHttpRequestProcessor(Handle script) : script_(script) { } virtual ~JsHttpRequestProcessor(); virtual bool Initialize(map* opts, map* output); virtual bool Process(HttpRequest* req); private: // Execute the script associated with this processor and extract the // Process function. Returns true if this succeeded, otherwise false. bool ExecuteScript(Handle script); // Wrap the options and output map in a JavaScript objects and // install it in the global namespace as 'options' and 'output'. bool InstallMaps(map* opts, map* output); // Constructs the template that describes the JavaScript wrapper // type for requests. static Handle MakeRequestTemplate(); static Handle MakeMapTemplate(); // Callbacks that access the individual fields of request objects. static Handle GetPath(Local name, const AccessorInfo& info); static Handle GetReferrer(Local name, const AccessorInfo& info); static Handle GetHost(Local name, const AccessorInfo& info); static Handle GetUserAgent(Local name, const AccessorInfo& info); // Callbacks that access maps static Handle MapGet(Local name, const AccessorInfo& info); static Handle MapSet(Local name, Local value, const AccessorInfo& info); // Utility methods for wrapping C++ objects as JavaScript objects, // and going back again. static Handle WrapMap(map* obj); static map* UnwrapMap(Handle obj); static Handle WrapRequest(HttpRequest* obj); static HttpRequest* UnwrapRequest(Handle obj); Handle script_; Persistent context_; Persistent process_; static Persistent request_template_; static Persistent map_template_; }; #endif // js_http_request_processor_h