Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::Webserver Class Reference

#include <webserver.h>

Collaboration diagram for impala::Webserver:

Classes

class  UrlHandler
 

Public Types

typedef std::map< std::string,
std::string > 
ArgumentMap
 
typedef boost::function< void(const
ArgumentMap &args,
rapidjson::Document *json)> 
UrlCallback
 

Public Member Functions

 Webserver (const int port)
 Using this constructor, the webserver will bind to all available interfaces. More...
 
 Webserver ()
 Uses FLAGS_webserver_{port, interface}. More...
 
 ~Webserver ()
 
Status Start ()
 
void Stop ()
 Stops the webserver synchronously. More...
 
void RegisterUrlCallback (const std::string &path, const std::string &template_filename, const UrlCallback &callback, bool is_on_nav_bar=true)
 Only one callback may be registered per URL. More...
 
const TNetworkAddress & http_address ()
 
bool IsSecure () const
 True if serving all traffic over SSL, false otherwise. More...
 

Static Public Attributes

static const char * ENABLE_RAW_JSON_KEY = "__raw__"
 

Private Types

typedef std::map< std::string,
UrlHandler
UrlHandlerMap
 

Private Member Functions

int BeginRequestCallback (struct sq_connection *connection, struct sq_request_info *request_info)
 Dispatch point for all incoming requests. Returns squeasel success code. More...
 
void RootHandler (const ArgumentMap &args, rapidjson::Document *document)
 Registered to handle "/", populates document with various system-wide information. More...
 
void ErrorHandler (const ArgumentMap &args, rapidjson::Document *document)
 Called when an error is encountered, e.g. when a handler for a URI cannot be found. More...
 
void BuildArgumentMap (const std::string &args, ArgumentMap *output)
 
void GetCommonJson (rapidjson::Document *document)
 

Static Private Member Functions

static int LogMessageCallbackStatic (const struct sq_connection *connection, const char *message)
 Squeasel callback for log events. Returns squeasel success code. More...
 
static int BeginRequestCallbackStatic (struct sq_connection *connection)
 

Private Attributes

boost::shared_mutex url_handlers_lock_
 Lock guarding the path_handlers_ map. More...
 
UrlHandlerMap url_handlers_
 
TNetworkAddress http_address_
 The address of the interface on which to run this webserver. More...
 
struct sq_context * context_
 Handle to Squeasel context; owned and freed by Squeasel internally. More...
 
UrlHandler error_handler_
 Catch-all handler for error messages. More...
 

Detailed Description

Wrapper class for the Squeasel web server library. Clients may register callback methods which produce Json documents which are rendered via a template file to either HTML or text.

Definition at line 34 of file webserver.h.

Member Typedef Documentation

typedef std::map<std::string, std::string> impala::Webserver::ArgumentMap

Definition at line 36 of file webserver.h.

typedef boost::function<void (const ArgumentMap& args, rapidjson::Document* json)> impala::Webserver::UrlCallback

Definition at line 38 of file webserver.h.

typedef std::map<std::string, UrlHandler> impala::Webserver::UrlHandlerMap
private

Map of path to a UrlHandler containing a list of handlers for that path. More than one handler may register itself with a path so that many components may contribute to a single page.

Definition at line 138 of file webserver.h.

Constructor & Destructor Documentation

impala::Webserver::Webserver ( const int  port)

Using this constructor, the webserver will bind to all available interfaces.

Definition at line 145 of file webserver.cc.

References http_address_, and impala::MakeNetworkAddress().

impala::Webserver::Webserver ( )

Uses FLAGS_webserver_{port, interface}.

Definition at line 136 of file webserver.cc.

References http_address_, and impala::MakeNetworkAddress().

impala::Webserver::~Webserver ( )

Definition at line 152 of file webserver.cc.

References Stop().

Member Function Documentation

int impala::Webserver::BeginRequestCallbackStatic ( struct sq_connection *  connection)
staticprivate

Squeasel callback for HTTP request events. Static so that it can act as a function pointer, and then call the next method. Returns squeasel success code.

Definition at line 323 of file webserver.cc.

References BeginRequestCallback().

Referenced by Start().

void impala::Webserver::BuildArgumentMap ( const std::string &  args,
ArgumentMap output 
)
private

Builds a map of argument name to argument value from a typical URL argument string (that is, "key1=value1&key2=value2.."). If no value is given for a key, it is entered into the map as (key, "").

Definition at line 181 of file webserver.cc.

References impala::UrlDecode().

Referenced by BeginRequestCallback().

void impala::Webserver::ErrorHandler ( const ArgumentMap args,
rapidjson::Document *  document 
)
private

Called when an error is encountered, e.g. when a handler for a URI cannot be found.

Definition at line 173 of file webserver.cc.

References ERROR_KEY.

void impala::Webserver::GetCommonJson ( rapidjson::Document *  document)
private

Adds a common object to document with common data that every webpage might want to read (e.g. the names of links to write to the navbar).

Definition at line 295 of file webserver.cc.

References COMMON_JSON_KEY, and url_handlers_.

Referenced by BeginRequestCallback().

const TNetworkAddress& impala::Webserver::http_address ( )
inline

Definition at line 72 of file webserver.h.

References http_address_.

bool impala::Webserver::IsSecure ( ) const

True if serving all traffic over SSL, false otherwise.

Definition at line 199 of file webserver.cc.

Referenced by Start().

int impala::Webserver::LogMessageCallbackStatic ( const struct sq_connection *  connection,
const char *  message 
)
staticprivate

Squeasel callback for log events. Returns squeasel success code.

Definition at line 315 of file webserver.cc.

References PROCESSING_COMPLETE.

Referenced by Start().

void impala::Webserver::RegisterUrlCallback ( const std::string &  path,
const std::string &  template_filename,
const UrlCallback callback,
bool  is_on_nav_bar = true 
)

Only one callback may be registered per URL.

Register a callback for a Url that produces a json document that will be rendered with the template at 'template_filename'. The URL 'path' should not include the http://hostname/ prefix. If is_on_nav_bar is true, the page will appear in the standard navigation bar rendered on all pages.The path of the template file is relative to the webserver's document root.

Definition at line 412 of file webserver.cc.

References url_handlers_, and url_handlers_lock_.

Referenced by impala::AddDefaultUrlCallbacks(), impala::AddPprofUrlCallbacks(), impala::MetricGroup::Init(), impala::InitRpcEventTracing(), impala::CatalogServer::RegisterWebpages(), impala::Statestore::RegisterWebpages(), impala::ImpalaServer::RegisterWebserverCallbacks(), Start(), impala::ThreadMgr::StartInstrumentation(), and TEST().

void impala::Webserver::RootHandler ( const ArgumentMap args,
rapidjson::Document *  document 
)
private

Registered to handle "/", populates document with various system-wide information.

Definition at line 156 of file webserver.cc.

References impala_udf::DebugString(), impala::OsInfo::DebugString(), impala::MemInfo::DebugString(), impala::DiskInfo::DebugString(), impala::CpuInfo::DebugString(), and impala::GetVersionString().

Referenced by Start().

Status impala::Webserver::Start ( )

Starts a webserver on the port passed to the constructor. The webserver runs in a separate thread, so this call is non-blocking.

Definition at line 203 of file webserver.cc.

References BeginRequestCallbackStatic(), context_, http_address_, IsSecure(), LogMessageCallbackStatic(), impala::Status::OK, RegisterUrlCallback(), and RootHandler().

Referenced by TEST().

void impala::Webserver::Stop ( )

Stops the webserver synchronously.

Definition at line 288 of file webserver.cc.

References context_.

Referenced by ~Webserver().

Member Data Documentation

struct sq_context* impala::Webserver::context_
private

Handle to Squeasel context; owned and freed by Squeasel internally.

Definition at line 145 of file webserver.h.

Referenced by Start(), and Stop().

const char * impala::Webserver::ENABLE_RAW_JSON_KEY = "__raw__"
static

Any callback may add a member to their Json output with key ENABLE_RAW_JSON_KEY; this causes the result of the template rendering process to be sent to the browser as text, not HTML.

Definition at line 43 of file webserver.h.

Referenced by BeginRequestCallback(), and JsonCallback().

UrlHandler impala::Webserver::error_handler_
private

Catch-all handler for error messages.

Definition at line 148 of file webserver.h.

Referenced by BeginRequestCallback().

TNetworkAddress impala::Webserver::http_address_
private

The address of the interface on which to run this webserver.

Definition at line 142 of file webserver.h.

Referenced by http_address(), Start(), and Webserver().

UrlHandlerMap impala::Webserver::url_handlers_
private

Definition at line 139 of file webserver.h.

Referenced by BeginRequestCallback(), GetCommonJson(), and RegisterUrlCallback().

boost::shared_mutex impala::Webserver::url_handlers_lock_
private

Lock guarding the path_handlers_ map.

Definition at line 133 of file webserver.h.

Referenced by BeginRequestCallback(), and RegisterUrlCallback().


The documentation for this class was generated from the following files: