Changeset - 38f907aa6e08
[Not reviewed]
0 1 0
Jan Kaluza - 13 years ago 2012-10-22 15:05:17
hanzz.k@gmail.com
Log mongoose error
1 file changed with 8 insertions and 1 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -104,98 +104,100 @@ img{ display: block; max-width: 100%; }\
 
\
 
code {\
 
	border: 1px solid #FB7A31;\
 
	background: #FFC;\
 
}\
 
\
 
pre {\
 
 white-space: pre-wrap;\
 
 white-space: -moz-pre-wrap;\
 
 white-space: -o-pre-wrap;\
 
border: 1px solid #FB7A31;\
 
background: #FFC;\
 
padding:5px;\
 
padding-left: 15px;\
 
}\
 
\
 
  </style>\
 
  </head><body><h1>Spectrum 2 web interface</h1>";
 
}
 

	
 

	
 
static void get_qsvar(const struct mg_request_info *request_info,
 
                      const char *name, char *dst, size_t dst_len) {
 
  const char *qs = request_info->query_string;
 
  mg_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len);
 
}
 

	
 
static void my_strlcpy(char *dst, const char *src, size_t len) {
 
  strncpy(dst, src, len);
 
  dst[len - 1] = '\0';
 
}
 

	
 
// Generate session ID. buf must be 33 bytes in size.
 
// Note that it is easy to steal session cookies by sniffing traffic.
 
// This is why all communication must be SSL-ed.
 
static void generate_session_id(char *buf, const char *random,
 
                                const char *user) {
 
  mg_md5(buf, random, user, NULL);
 
}
 

	
 
Server::Server(ManagerConfig *config) {
 
	srand((unsigned) time(0));
 
	m_config = config;
 
	m_user = CONFIG_STRING(m_config, "service.admin_username");
 
	m_password = CONFIG_STRING(m_config, "service.admin_password");
 
}
 

	
 
Server::~Server() {
 
	if (ctx) {
 
		mg_stop(ctx);
 
	}
 
}
 

	
 

	
 
static void *_event_handler(enum mg_event event, struct mg_connection *conn) {
 
	const struct mg_request_info *request_info = mg_get_request_info(conn);
 
	return static_cast<Server *>(request_info->user_data)->event_handler(event, conn);
 
}
 

	
 
bool Server::start() {
 
	const char *options[] = {
 
		"listening_ports", boost::lexical_cast<std::string>(CONFIG_INT(m_config, "service.port")).c_str(),
 
		"num_threads", "1",
 
		NULL
 
	};
 

	
 
	// Setup and start Mongoose
 
	if ((ctx = mg_start(&_event_handler, this, options)) == NULL) {
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
bool Server::check_password(const char *user, const char *password) {
 
	return (m_user == user && m_password == password);
 
}
 

	
 
// Allocate new session object
 
Server::session *Server::new_session(const char *user) {
 
	Server::session *session = new Server::session;
 

	
 
	my_strlcpy(session->user, user, sizeof(session->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;
 

	
 
	sessions[session->session_id] = session;
 
	return session;
 
}
 

	
 
// Get session object for the connection. Caller must hold the lock.
 
Server::session *Server::get_session(const struct mg_connection *conn) {
 
	time_t now = time(NULL);
 
	char session_id[33];
 
	mg_get_cookie(conn, "session", session_id, sizeof(session_id));
 

	
 
	if (sessions.find(session_id) == sessions.end()) {
 
		return NULL;
 
	}
 
@@ -401,58 +403,63 @@ void Server::serve_root(struct mg_connection *conn, const struct mg_request_info
 
		}
 
		html += "<td>" + get_response() + "</td>";
 
		if (get_response().find("Running") == 0) {
 
			html += "<td><a href=\"/stop?jid=" + instance + "\">Stop</a></td>";
 
			html += "<td><form action=\"/cmd\">";
 
			html += "<input type=\"hidden\" name=\"jid\" value=\"" + instance + "\"></input>";
 
			html += "<input type=\"text\" name=\"cmd\"></input>";
 
			html += "<input type=\"submit\" value=\"Run\"></input>";
 
			html += "</form></td>";
 
		}
 
		else {
 
			html += "<td><a href=\"/start?jid=" + instance + "\">Start</a></td>";
 
			html += "<td></td>";
 
		}
 

	
 
		html += "</tr>";
 
	}
 

	
 
	html += "</table></body></html>";
 
	print_html(conn, request_info, html);
 
}
 

	
 
void *Server::event_handler(enum mg_event event, struct mg_connection *conn) {
 
	const struct mg_request_info *request_info = mg_get_request_info(conn);
 
	void *processed = (void *) 0x1;
 

	
 
	if (event == MG_NEW_REQUEST) {
 
		if (!is_authorized(conn, request_info)) {
 
			redirect_to(conn, request_info, "/login");
 
		} else if (strcmp(request_info->uri, "/authorize") == 0) {
 
			authorize(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/login") == 0) {
 
			serve_login(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/") == 0) {
 
			serve_root(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/onlineusers") == 0) {
 
			serve_onlineusers(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/cmd") == 0) {
 
			serve_cmd(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/start") == 0) {
 
			serve_start(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/stop") == 0) {
 
			serve_stop(conn, request_info);
 
		} else {
 
			// No suitable handler found, mark as not processed. Mongoose will
 
			// try to serve the request.
 
			processed = NULL;
 
		}
 
	} else {
 
	}
 
	else if (event == MG_EVENT_LOG) {
 
		// Called by Mongoose's cry()
 
		std::cerr << "Mongoose error: " << request_info->log_message << "\n";
 
	}
 
	else {
 
		processed = NULL;
 
	}
 

	
 
	return processed;
 
}
 

	
 

	
 

	
 

	
0 comments (0 inline, 0 general)