21 #include <boost/bind/bind.hpp>
22 #include <boost/tokenizer.hpp>
24 #include <sandesh/sandesh_types.h>
25 #include <sandesh/sandesh.h>
26 #include <sandesh/sandesh_http.h>
27 #include <sandesh/transport/TBufferTransports.h>
28 #include <sandesh/protocol/TXMLProtocol.h>
30 #include <sandesh/sandesh_trace.h>
31 #include <sandesh/sandesh_trace_types.h>
37 #include "css_bootstrap_min_css.cpp"
38 #include "css_DT_bootstrap_css.cpp"
39 #include "css_images_sort_asc_png.cpp"
40 #include "css_images_sort_asc_disabled_png.cpp"
41 #include "css_images_sort_both_png.cpp"
42 #include "css_images_sort_desc_png.cpp"
43 #include "css_images_sort_desc_disabled_png.cpp"
44 #include "css_style_css.cpp"
45 #include "js_bootstrap_min_js.cpp"
46 #include "js_DT_bootstrap_js.cpp"
47 #include "js_jquery_2_0_3_min_js.cpp"
48 #include "js_jquery_dataTables_min_js.cpp"
49 #include "js_util_js.cpp"
50 #include "universal_parse_xsl.cpp"
57 using namespace boost::placeholders;
67 #define SESSION_SEND(buf) \
68 HttpSession::SendSession(context,reinterpret_cast<const u_int8_t *>(buf), strlen(buf), NULL)
88 HttpSendXML(
const std::string& context,
const u_int8_t * buf, uint32_t len,
89 const char * name,
bool more) {
93 static const char xsl_response[] =
95 "Content-Type: text/xml\r\n"
97 static const char chunk_response[] =
99 "Content-Type: text/xml\r\n"
100 "Transfer-Encoding: chunked\r\n\r\n"
103 int loc = strcspn(
reinterpret_cast<const char *
>(buf),
" ");
104 strncpy(resp_name,
reinterpret_cast<const char *
>(buf + 1), loc - 1);
105 resp_name[loc - 1] = 0;
107 static std::mutex hmutex;
109 std::scoped_lock lock(hmutex);
110 std::string client_ctx;
117 if (!client_ctx.empty()) {
121 client_ctx = resp_name;
128 if ((
HXMLNew == state) && (!more)) {
133 size_t total_len = len;
135 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
138 "Content-Length: %zu\r\n\r\n", total_len + strlen(buffer_str));
144 }
else if ((
HXMLNew == state) && more) {
150 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
152 int hdr_len = strlen(buffer_str);
154 "<__%s_list type=\"slist\">\r\n", client_ctx.c_str());
155 hdr_len += (strlen(buffer_str) - 2);
157 sprintf(length_str,
"%x\r\n", hdr_len);
161 "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>"
166 "<__%s_list type=\"slist\">\r\n", client_ctx.c_str());
171 sprintf(length_str,
"%x\r\n", len);
176 sprintf(buffer_str,
"\r\n");
183 sprintf(length_str,
"%x\r\n", len);
188 sprintf(buffer_str,
"\r\n");
192 sprintf(buffer_str,
"</__%s_list>\r\n", client_ctx.c_str());
193 sprintf(length_str,
"%zx\r\n", strlen(buffer_str) - 2);
197 sprintf(length_str,
"0\r\n\r\n");
203 sprintf(length_str,
"%x\r\n", len);
208 sprintf(buffer_str,
"\r\n");
213 client_ctx = resp_name;
233 static const char html_response[] =
234 "HTTP/1.1 200 OK\r\n"
236 static const char json_response[] =
237 "HTTP/1.1 200 OK\r\n"
238 "Content-Type: application/json\r\n"
240 static const char xsl_response[] =
241 "HTTP/1.1 200 OK\r\n"
242 "Content-Type: text/xsl\r\n"
244 static const char xml_response[] =
245 "HTTP/1.1 200 OK\r\n"
246 "Content-Type: text/xml\r\n"
248 static const char css_response[] =
249 "HTTP/1.1 200 OK\r\n"
250 "Content-Type: text/css\r\n"
252 const char * response =
253 ((request->
UrlPath().find(
".js")!=string::npos) ?
254 json_response : ((request->
UrlPath().find(
".xsl")!=string::npos) ?
255 xsl_response : ((request->
UrlPath().find(
".css")!=string::npos) ?
256 css_response : ((request->
UrlPath().find(
".xml")!=string::npos) ?
257 xml_response : html_response))));
260 "Content-Length: %d\r\n\r\n", hti->
html_len());
261 session->
Send(
reinterpret_cast<const u_int8_t *
>(response),
262 strlen(response), NULL);
263 session->
Send(
reinterpret_cast<const u_int8_t *
>(length_str),
264 strlen(length_str), NULL);
284 string snh_name = request->
UrlPath().substr(5);
287 SANDESH_LOG(DEBUG, __func__ <<
" Unknown sandesh:" <<
288 snh_name << std::endl);
326 std::stringstream ss;
328 uint32_t xfer = 0, offset;
330 boost::shared_ptr<TMemoryBuffer> btrans =
331 boost::shared_ptr<TMemoryBuffer>(
333 boost::shared_ptr<TXMLProtocol> prot =
334 boost::shared_ptr<TXMLProtocol>(
337 xfer += snh->
Write(prot);
339 btrans->getBuffer(&buffer, &offset);
358 *hport = hServ_->GetPort();
361 ostringstream index_ss;
364 SANDESH_TRACE_TEXT_TRACE(httpbuf,
"<Initializing httpbuf");
365 SANDESH_TRACE_TEXT_TRACE(httpbuf,
"Size 100");
380 index_ss <<
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" <<
381 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" << endl;
382 index_ss <<
"<html xmlns=\"http://www.w3.org/1999/xhtml\">" << endl;
383 index_ss <<
"<head>" <<
384 "<link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\"/>" <<
385 "<title>" << module <<
"</title></head><body>" << endl;
386 index_ss <<
"<h1>Modules for " << module <<
"</h1>" << endl;
388 for (map_type::iterator it=map_->begin(); it!=map_->end(); it++) {
389 std::string regString =
"/" + it->first;
390 hServ_->RegisterHandler(regString.c_str(),
392 if (it->first.find(
".xml")!=string::npos) {
393 index_ss <<
"<a href=\"" << it->first <<
"\">" <<
394 it->first <<
"</a><br/>" << endl;
397 index_ss <<
"</body></html>" << endl;
399 index_str_ = index_ss.str();
400 const char * index_str = index_str_.c_str();
403 index_hti_ =
new HtmlInfo(
reinterpret_cast<const unsigned char *
>(
404 index_str),strlen(index_str));
405 hServ_->RegisterHandler(
"/index.html",
407 hServ_->RegisterHandler(
"/",
412 std::string regString =
"/Snh_" + (*it).first;
413 hServ_->RegisterHandler(regString.c_str(),
417 boost::system::error_code ec;
422 success = hServ_->Initialize(port, http_ip);
424 success = hServ_->Initialize(port);
426 hServ_->SetSocketOptions(config);
428 int lport(hServ_->GetPort());
429 SANDESH_LOG(DEBUG,
"Sandesh Http Server Port: " << lport);
433 SANDESH_LOG(ERROR,
"Failed to initialize Sandesh Http Server Port: "
438 hServ_->SetListenSocketDscp(dscp);
451 hServ_->ClearSessions();
452 hServ_->WaitForEmpty();
466 hServ_->UpdateDscp(dscp);
467 hServ_->UpdateSessionsDscp(dscp);
boost::asio::ip::address IpAddress
std::string UrlQuery() const
std::string UrlPath() const
static map_type::const_iterator End()
static map_type::const_iterator Begin()
static Sandesh * CreateInstance(std::string const &s)
uint8_t dscp_value() const
unsigned int html_len() const
const unsigned char * html_str() const
static HttpServer * hServ_
static std::string index_str_
static void UpdateDscp(uint8_t dscp)
static HtmlInfo * index_hti_
static void Response(Sandesh *snh, std::string context)
boost::function< int32_t(SandeshRequest *)> RequestCallbackFn
static bool Init(EventManager *evm, const std::string module, short port, RequestCallbackFn reqcb, int *hport, const SandeshConfig &config=SandeshConfig())
std::map< std::string, HtmlInfo > map_type
virtual bool RequestFromHttp(const std::string &ctx, const std::string &snh_query)=0
virtual const bool get_more() const =0
const bool get_more() const
const bool get_more() const
virtual int32_t Write(boost::shared_ptr< contrail::sandesh::protocol::TProtocol > oprot) const =0
static SandeshClient * client()
virtual std::string ModuleName() const =0
static void DeleteServer(TcpServer *server)
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
#define SANDESH_LOG(_Level, _Msg)
static int kEncodeBufferSize
static void HttpSandeshRequestCallback(HttpServer *hs, HttpSession *session, const HttpRequest *request)
SandeshHttp::RequestCallbackFn httpreqcb
static void HttpSandeshFileCallback(SandeshHttp::HtmlInfo *hti, HttpSession *session, const HttpRequest *request)
static void HttpSendXML(const std::string &context, const u_int8_t *buf, uint32_t len, const char *name, bool more)
#define SESSION_SEND(buf)
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
static bool set_client_context(std::string const &s, const std::string &ctx)
const std::string get_client_context()
static bool SendSession(std::string const &s, const uint8_t *data, size_t size, size_t *sent)
const std::string get_context()
std::string server_keyfile
bool introspect_ssl_insecure
std::string server_certfile
std::string http_server_ip
bool introspect_ssl_enable