diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index 39b7c1e2cadbf736ba4b1fbb8e4842bd603d8188..c279c2403f9bdcf583f6a6d399b5c72e27f15de9 100644 --- a/spectrum_manager/src/server.cpp +++ b/spectrum_manager/src/server.cpp @@ -8,123 +8,18 @@ #include #include #include +#include +#include +#include #define SESSION_TTL 120 -static std::string get_header() { -return "\ - \ - \ - \ - Spectrum 2 web interface\ - \ - \ -

Spectrum 2 web interface

"; -} +static struct mg_serve_http_opts s_http_server_opts; static void get_qsvar(const struct http_message *hm, const char *name, char *dst, size_t dst_len) { - mg_get_http_var(&hm->query_string, name, dst, dst_len); + mg_get_http_var(&hm->body, name, dst, dst_len); } static void my_strlcpy(char *dst, const char *src, size_t len) { @@ -153,6 +48,26 @@ Server::Server(ManagerConfig *config) { mg_mgr_init(&m_mgr, this); m_nc = mg_bind(&m_mgr, std::string(":" + boost::lexical_cast(CONFIG_INT(m_config, "service.port"))).c_str(), &_event_handler); mg_set_protocol_http_websocket(m_nc); + + s_http_server_opts.document_root = CONFIG_STRING(m_config, "service.data_dir").c_str(); + + std::ifstream header(std::string(CONFIG_STRING(m_config, "service.data_dir") + "/header.html").c_str(), std::ios::in); + if (header) { + header.seekg(0, std::ios::end); + m_header.resize(header.tellg()); + header.seekg(0, std::ios::beg); + header.read(&m_header[0], m_header.size()); + header.close(); + } + + std::ifstream footer(std::string(CONFIG_STRING(m_config, "service.data_dir") + "/footer.html").c_str(), std::ios::in); + if (footer) { + footer.seekg(0, std::ios::end); + m_footer.resize(footer.tellg()); + footer.seekg(0, std::ios::beg); + footer.read(&m_footer[0], m_footer.size()); + footer.close(); + } } Server::~Server() { @@ -179,6 +94,7 @@ Server::session *Server::new_session(const char *user) { snprintf(session->random, sizeof(session->random), "%d", rand()); generate_session_id(session->session_id, session->random, session->user); session->expire = time(0) + SESSION_TTL; + session->admin = std::string(user) == m_user; sessions[session->session_id] = session; return session; @@ -243,11 +159,14 @@ bool Server::is_authorized(const struct mg_connection *conn, struct http_message // Always authorize accesses to login page and to authorize URI if (!mg_vcmp(&hm->uri, "/login") || + !mg_vcmp(&hm->uri, "/login/") || + !mg_vcmp(&hm->uri, "/form.css") || + !mg_vcmp(&hm->uri, "/style.css") || + !mg_vcmp(&hm->uri, "/logo.png") || !mg_vcmp(&hm->uri, "/authorize")) { return true; } -// pthread_rwlock_rdlock(&rwlock); if ((session = get_session(hm)) != NULL) { generate_session_id(valid_id, session->random, session->user); if (strcmp(valid_id, session->session_id) == 0) { @@ -255,7 +174,6 @@ bool Server::is_authorized(const struct mg_connection *conn, struct http_message authorized = true; } } -// pthread_rwlock_unlock(&rwlock); return authorized; } @@ -272,37 +190,12 @@ void Server::print_html(struct mg_connection *conn, struct http_message *hm, con "Content-Type: text/html\r\n" "Content-Length: %d\r\n" // Always set Content-Length "\r\n" - "%s", - (int) html.size(), html.c_str()); -} - -void Server::serve_login(struct mg_connection *conn, struct http_message *hm) { - std::string html= "\ - \ - \ - \ - Spectrum 2 web interface\ - \ - \ - \ -
\ -

Spectrum 2 web interface login

\ -
\ -
\ - Username:
\ - Password:
\ - \ -
\ -
\ - \ -"; - - print_html(conn, hm, html); + "%s%s%s", + (int) html.size() + m_header.size() + m_footer.size(), m_header.c_str(), html.c_str(), m_footer.c_str()); } void Server::serve_onlineusers(struct mg_connection *conn, struct http_message *hm) { - std::string html = get_header(); + std::string html; char jid[255]; get_qsvar(hm, "jid", jid, sizeof(jid)); @@ -331,7 +224,7 @@ void Server::serve_onlineusers(struct mg_connection *conn, struct http_message * } void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) { - std::string html = get_header(); + std::string html; char jid[255]; get_qsvar(hm, "jid", jid, sizeof(jid)); char cmd[4096]; @@ -358,7 +251,7 @@ void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) { void Server::serve_start(struct mg_connection *conn, struct http_message *hm) { - std::string html= get_header() ; + std::string html; char jid[255]; get_qsvar(hm, "jid", jid, sizeof(jid)); @@ -369,7 +262,7 @@ void Server::serve_start(struct mg_connection *conn, struct http_message *hm) { } void Server::serve_stop(struct mg_connection *conn, struct http_message *hm) { - std::string html= get_header(); + std::string html; char jid[255]; get_qsvar(hm, "jid", jid, sizeof(jid)); @@ -378,40 +271,45 @@ void Server::serve_stop(struct mg_connection *conn, struct http_message *hm) { html += ""; print_html(conn, hm, html); } - void Server::serve_root(struct mg_connection *conn, struct http_message *hm) { std::vector list = show_list(m_config, false); - std::string html= get_header() + "

List of instances

"; - - BOOST_FOREACH(std::string &instance, list) { - html += ""; - html += ""; - Swift::SimpleEventLoop eventLoop; - Swift::BoostNetworkFactories networkFactories(&eventLoop); + std::string html = "

List of instances

"; - ask_local_server(m_config, networkFactories, instance, "status"); - eventLoop.runUntilEvents(); - while(get_response().empty()) { + if (list.empty()) { + html += "

There are no Spectrum 2 instances yet. You can create new instance by adding configuration files into

/etc/spectrum2/transports
directory. You can then maintain the Spectrum 2 instance here.

"; + } + else { + html += "
JIDStatusCommandRun command
" + instance + "
"; + BOOST_FOREACH(std::string &instance, list) { + html += ""; + html += ""; + Swift::SimpleEventLoop eventLoop; + Swift::BoostNetworkFactories networkFactories(&eventLoop); + + ask_local_server(m_config, networkFactories, instance, "status"); eventLoop.runUntilEvents(); - } - html += ""; - if (get_response().find("Running") == 0) { - html += ""; - html += ""; - } - else { - html += ""; - html += ""; + while(get_response().empty()) { + eventLoop.runUntilEvents(); + } + html += ""; + if (get_response().find("Running") == 0) { + html += ""; + html += ""; + } + else { + html += ""; + html += ""; + } + + html += ""; } - html += ""; + html += "
JIDStatusCommandRun command
" + instance + "" + get_response() + "Stop
"; - html += ""; - html += ""; - html += ""; - html += "
Start" + get_response() + "Stop
"; + html += ""; + html += ""; + html += ""; + html += "
Start
"; } - - html += ""; print_html(conn, hm, html); } @@ -421,12 +319,11 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) { if (ev != MG_EV_HTTP_REQUEST) { return; } + if (!is_authorized(conn, hm)) { redirect_to(conn, hm, "/login"); } else if (mg_vcmp(&hm->uri, "/authorize") == 0) { authorize(conn, hm); - } else if (mg_vcmp(&hm->uri, "/login") == 0) { - serve_login(conn, hm); } else if (mg_vcmp(&hm->uri, "/") == 0) { serve_root(conn, hm); } else if (mg_vcmp(&hm->uri, "/onlineusers") == 0) { @@ -437,6 +334,8 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) { serve_start(conn, hm); } else if (mg_vcmp(&hm->uri, "/stop") == 0) { serve_stop(conn, hm); + } else { + mg_serve_http(conn, hm, s_http_server_opts); } conn->flags |= MG_F_SEND_AND_CLOSE;