Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
webserver.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_WEBSERVER_H
17 #define IMPALA_UTIL_WEBSERVER_H
18 
19 #include <squeasel/squeasel.h>
20 #include <string>
21 #include <map>
22 #include <boost/function.hpp>
23 #include <boost/thread/shared_mutex.hpp>
24 #include <rapidjson/document.h>
25 
26 #include "common/status.h"
27 #include "util/network-util.h"
28 
29 namespace impala {
30 
34 class Webserver {
35  public:
36  typedef std::map<std::string, std::string> ArgumentMap;
37  typedef boost::function<void (const ArgumentMap& args, rapidjson::Document* json)>
39 
43  static const char* ENABLE_RAW_JSON_KEY;
44 
46  Webserver(const int port);
47 
49  Webserver();
50 
51  ~Webserver();
52 
55  Status Start();
56 
58  void Stop();
59 
64  //
66  //
69  void RegisterUrlCallback(const std::string& path, const std::string& template_filename,
70  const UrlCallback& callback, bool is_on_nav_bar = true);
71 
72  const TNetworkAddress& http_address() { return http_address_; }
73 
75  bool IsSecure() const;
76 
77  private:
82  class UrlHandler {
83  public:
84  UrlHandler(const UrlCallback& cb, const std::string& template_filename,
85  bool is_on_nav_bar)
86  : is_on_nav_bar_(is_on_nav_bar), template_callback_(cb),
87  template_filename_(template_filename) { }
88 
89  bool is_on_nav_bar() const { return is_on_nav_bar_; }
90  const UrlCallback& callback() const { return template_callback_; }
91  const std::string& template_filename() const { return template_filename_; }
92 
93  private:
96 
99 
102  std::string template_filename_;
103  };
104 
106  static int LogMessageCallbackStatic(const struct sq_connection* connection,
107  const char* message);
108 
111  static int BeginRequestCallbackStatic(struct sq_connection* connection);
112 
114  int BeginRequestCallback(struct sq_connection* connection,
115  struct sq_request_info* request_info);
116 
118  void RootHandler(const ArgumentMap& args, rapidjson::Document* document);
119 
121  void ErrorHandler(const ArgumentMap& args, rapidjson::Document* document);
122 
126  void BuildArgumentMap(const std::string& args, ArgumentMap* output);
127 
130  void GetCommonJson(rapidjson::Document* document);
131 
133  boost::shared_mutex url_handlers_lock_;
134 
138  typedef std::map<std::string, UrlHandler> UrlHandlerMap;
140 
142  TNetworkAddress http_address_;
143 
145  struct sq_context* context_;
146 
149 };
150 
151 }
152 
153 #endif // IMPALA_UTIL_WEBSERVER_H
struct sq_context * context_
Handle to Squeasel context; owned and freed by Squeasel internally.
Definition: webserver.h:145
std::map< std::string, UrlHandler > UrlHandlerMap
Definition: webserver.h:138
static const char * ENABLE_RAW_JSON_KEY
Definition: webserver.h:43
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
boost::function< void(const ArgumentMap &args, rapidjson::Document *json)> UrlCallback
Definition: webserver.h:38
UrlHandler error_handler_
Catch-all handler for error messages.
Definition: webserver.h:148
Webserver()
Uses FLAGS_webserver_{port, interface}.
Definition: webserver.cc:136
void Stop()
Stops the webserver synchronously.
Definition: webserver.cc:288
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.
Definition: webserver.cc:173
UrlCallback template_callback_
Callback to produce a Json document to render via a template.
Definition: webserver.h:98
bool is_on_nav_bar() const
Definition: webserver.h:89
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.
Definition: webserver.cc:412
std::map< std::string, std::string > ArgumentMap
Definition: webserver.h:36
bool IsSecure() const
True if serving all traffic over SSL, false otherwise.
Definition: webserver.cc:199
TNetworkAddress http_address_
The address of the interface on which to run this webserver.
Definition: webserver.h:142
static int LogMessageCallbackStatic(const struct sq_connection *connection, const char *message)
Squeasel callback for log events. Returns squeasel success code.
Definition: webserver.cc:315
const UrlCallback & callback() const
Definition: webserver.h:90
void RootHandler(const ArgumentMap &args, rapidjson::Document *document)
Registered to handle "/", populates document with various system-wide information.
Definition: webserver.cc:156
void GetCommonJson(rapidjson::Document *document)
Definition: webserver.cc:295
UrlHandlerMap url_handlers_
Definition: webserver.h:139
const TNetworkAddress & http_address()
Definition: webserver.h:72
void BuildArgumentMap(const std::string &args, ArgumentMap *output)
Definition: webserver.cc:181
const std::string & template_filename() const
Definition: webserver.h:91
boost::shared_mutex url_handlers_lock_
Lock guarding the path_handlers_ map.
Definition: webserver.h:133
bool is_on_nav_bar_
If true, the page appears in the navigation bar.
Definition: webserver.h:95
static int BeginRequestCallbackStatic(struct sq_connection *connection)
Definition: webserver.cc:323
int BeginRequestCallback(struct sq_connection *connection, struct sq_request_info *request_info)
Dispatch point for all incoming requests. Returns squeasel success code.
Definition: webserver.cc:329
UrlHandler(const UrlCallback &cb, const std::string &template_filename, bool is_on_nav_bar)
Definition: webserver.h:84