19 #include <boost/bind.hpp>
20 #include <boost/tokenizer.hpp>
22 #include <sandesh/sandesh_types.h>
23 #include <sandesh/sandesh.h>
24 #include <sandesh/sandesh_http.h>
25 #include <sandesh/transport/TBufferTransports.h>
26 #include <sandesh/protocol/TXMLProtocol.h>
28 #include <sandesh/sandesh_trace.h>
29 #include <sandesh/sandesh_trace_types.h>
35 #include "css_bootstrap_min_css.cpp"
36 #include "css_DT_bootstrap_css.cpp"
37 #include "css_images_sort_asc_png.cpp"
38 #include "css_images_sort_asc_disabled_png.cpp"
39 #include "css_images_sort_both_png.cpp"
40 #include "css_images_sort_desc_png.cpp"
41 #include "css_images_sort_desc_disabled_png.cpp"
42 #include "css_style_css.cpp"
43 #include "js_bootstrap_min_js.cpp"
44 #include "js_DT_bootstrap_js.cpp"
45 #include "js_jquery_2_0_3_min_js.cpp"
46 #include "js_jquery_dataTables_min_js.cpp"
47 #include "js_util_js.cpp"
48 #include "universal_parse_xsl.cpp"
53 using namespace contrail::sandesh::protocol;
64 #define SESSION_SEND(buf) \
65 HttpSession::SendSession(context,reinterpret_cast<const u_int8_t *>(buf), strlen(buf), NULL)
85 HttpSendXML(
const std::string& context,
const u_int8_t * buf, uint32_t len,
86 const char * name,
bool more) {
90 static const char xsl_response[] =
92 "Content-Type: text/xml\r\n"
94 static const char chunk_response[] =
96 "Content-Type: text/xml\r\n"
97 "Transfer-Encoding: chunked\r\n\r\n"
100 int loc = strcspn(reinterpret_cast<const char *>(buf),
" ");
101 strncpy(resp_name, reinterpret_cast<const char *>(buf + 1), loc - 1);
102 resp_name[loc - 1] = 0;
104 static tbb::mutex hmutex;
106 tbb::mutex::scoped_lock lock(hmutex);
107 std::string client_ctx;
114 if (!client_ctx.empty()) {
118 client_ctx = resp_name;
125 if ((
HXMLNew == state) && (!more)) {
130 size_t total_len = len;
132 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
135 "Content-Length: %zu\r\n\r\n", total_len + strlen(buffer_str));
141 }
else if ((
HXMLNew == state) && more) {
147 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
149 int hdr_len = strlen(buffer_str);
151 "<__%s_list type=\"slist\">\r\n", client_ctx.c_str());
152 hdr_len += (strlen(buffer_str) - 2);
154 sprintf(length_str,
"%x\r\n", hdr_len);
158 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
163 "<__%s_list type=\"slist\">\r\n", client_ctx.c_str());
168 sprintf(length_str,
"%x\r\n", len);
173 sprintf(buffer_str,
"\r\n");
180 sprintf(length_str,
"%x\r\n", len);
185 sprintf(buffer_str,
"\r\n");
189 sprintf(buffer_str,
"</__%s_list>\r\n", client_ctx.c_str());
190 sprintf(length_str,
"%zx\r\n", strlen(buffer_str) - 2);
194 sprintf(length_str,
"0\r\n\r\n");
200 sprintf(length_str,
"%x\r\n", len);
205 sprintf(buffer_str,
"\r\n");
210 client_ctx = resp_name;
230 static const char html_response[] =
231 "HTTP/1.1 200 OK\r\n"
233 static const char json_response[] =
234 "HTTP/1.1 200 OK\r\n"
235 "Content-Type: application/json\r\n"
237 static const char xsl_response[] =
238 "HTTP/1.1 200 OK\r\n"
239 "Content-Type: text/xsl\r\n"
241 static const char xml_response[] =
242 "HTTP/1.1 200 OK\r\n"
243 "Content-Type: text/xml\r\n"
245 static const char css_response[] =
246 "HTTP/1.1 200 OK\r\n"
247 "Content-Type: text/css\r\n"
249 const char * response =
250 ((request->
UrlPath().find(
".js")!=string::npos) ?
251 json_response : ((request->
UrlPath().find(
".xsl")!=string::npos) ?
252 xsl_response : ((request->
UrlPath().find(
".css")!=string::npos) ?
253 css_response : ((request->
UrlPath().find(
".xml")!=string::npos) ?
254 xml_response : html_response))));
257 "Content-Length: %d\r\n\r\n", hti->
html_len());
258 session->
Send(reinterpret_cast<const u_int8_t *>(response),
259 strlen(response), NULL);
260 session->
Send(reinterpret_cast<const u_int8_t *>(length_str),
261 strlen(length_str), NULL);
281 string snh_name = request->
UrlPath().substr(5);
283 if (sandesh == NULL) {
284 SANDESH_LOG(DEBUG, __func__ <<
" Unknown sandesh:" <<
285 snh_name << std::endl);
323 std::stringstream ss;
325 uint32_t xfer = 0, offset;
327 boost::shared_ptr<TMemoryBuffer> btrans =
328 boost::shared_ptr<TMemoryBuffer>(
330 boost::shared_ptr<TXMLProtocol> prot =
331 boost::shared_ptr<TXMLProtocol>(
334 xfer += snh->
Write(prot);
336 btrans->getBuffer(&buffer, &offset);
355 *hport = hServ_->GetPort();
358 ostringstream index_ss;
361 SANDESH_TRACE_TEXT_TRACE(httpbuf,
"<Initializing httpbuf");
362 SANDESH_TRACE_TEXT_TRACE(httpbuf,
"Size 100");
375 hServ_ =
new HttpServer(evm, sslConfig, dscp);
377 index_ss <<
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" <<
378 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" << endl;
379 index_ss <<
"<html xmlns=\"http://www.w3.org/1999/xhtml\">" << endl;
380 index_ss <<
"<head>" <<
381 "<link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\"/>" <<
382 "<title>" << module <<
"</title></head><body>" << endl;
383 index_ss <<
"<h1>Modules for " << module <<
"</h1>" << endl;
385 for (map_type::iterator it=map_->begin(); it!=map_->end(); it++) {
386 std::string regString =
"/" + it->first;
387 hServ_->RegisterHandler(regString.c_str(),
389 if (it->first.find(
".xml")!=string::npos) {
390 index_ss <<
"<a href=\"" << it->first <<
"\">" <<
391 it->first <<
"</a><br/>" << endl;
394 index_ss <<
"</body></html>" << endl;
396 index_str_ = index_ss.str();
397 const char * index_str = index_str_.c_str();
400 index_hti_ =
new HtmlInfo(reinterpret_cast<const unsigned char *>(
401 index_str),strlen(index_str));
402 hServ_->RegisterHandler(
"/index.html",
404 hServ_->RegisterHandler(
"/",
409 std::string regString =
"/Snh_" + (*it).first;
410 hServ_->RegisterHandler(regString.c_str(),
414 boost::system::error_code ec;
419 success = hServ_->Initialize(port, http_ip);
421 success = hServ_->Initialize(port);
423 hServ_->SetSocketOptions(config);
425 int lport(hServ_->GetPort());
426 SANDESH_LOG(DEBUG,
"Sandesh Http Server Port: " << lport);
430 SANDESH_LOG(ERROR,
"Failed to initialize Sandesh Http Server Port: "
435 hServ_->SetListenSocketDscp(dscp);
448 hServ_->ClearSessions();
449 hServ_->WaitForEmpty();
463 hServ_->UpdateDscp(dscp);
464 hServ_->UpdateSessionsDscp(dscp);
unsigned int html_len() const
uint8_t dscp_value() const
bool introspect_ssl_enable
static void HttpSandeshRequestCallback(HttpServer *hs, HttpSession *session, const HttpRequest *request)
static map_type::const_iterator Begin()
const unsigned char * html_str() const
static Sandesh * CreateInstance(std::string const &s)
virtual std::string ModuleName() const =0
const bool get_more() const
boost::asio::ip::address IpAddress
static void HttpSendXML(const std::string &context, const u_int8_t *buf, uint32_t len, const char *name, bool more)
std::string server_certfile
std::map< std::string, HtmlInfo > map_type
static std::string index_str_
#define SANDESH_LOG(_Level, _Msg)
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
bool introspect_ssl_insecure
static HtmlInfo * index_hti_
SandeshHttp::RequestCallbackFn httpreqcb
const std::string get_client_context()
static void HttpSandeshFileCallback(SandeshHttp::HtmlInfo *hti, HttpSession *session, const HttpRequest *request)
static map_type::const_iterator End()
static SandeshClient * client()
std::string http_server_ip
static void DeleteServer(TcpServer *server)
static HttpServer * hServ_
const std::string get_context()
static bool set_client_context(std::string const &s, const std::string &ctx)
const bool get_more() const
virtual const bool get_more() const =0
std::string server_keyfile
#define SESSION_SEND(buf)
boost::function< int32_t(SandeshRequest *)> RequestCallbackFn
static bool SendSession(std::string const &s, const uint8_t *data, size_t size, size_t *sent)
std::string UrlQuery() const
virtual int32_t Write(boost::shared_ptr< contrail::sandesh::protocol::TProtocol > oprot) const =0
static bool Init(EventManager *evm, const std::string module, short port, RequestCallbackFn reqcb, int *hport, const SandeshConfig &config=SandeshConfig())
static void Response(Sandesh *snh, std::string context)
static int kEncodeBufferSize
std::string UrlPath() const
virtual bool RequestFromHttp(const std::string &ctx, const std::string &snh_query)=0
static void UpdateDscp(uint8_t dscp)
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)