1 |
torben |
675 |
|
2 |
|
|
#include "embeddedhttp.h" |
3 |
|
|
|
4 |
|
|
#include <stdio.h> |
5 |
|
|
#include "mongoose.h" |
6 |
|
|
|
7 |
torben |
676 |
#include "Logger.h" |
8 |
|
|
#include "version.h" |
9 |
|
|
#include "Common.h" |
10 |
torben |
680 |
#include "ConfigFile.h" |
11 |
torben |
676 |
#include <sstream> |
12 |
torben |
683 |
#include <iomanip> |
13 |
torben |
680 |
#include <stdlib.h> |
14 |
torben |
676 |
|
15 |
torben |
685 |
void show_status(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
16 |
torben |
676 |
Common* cmn = Common::instance(); |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
int tot_sec = time(0) - cmn->daemonStart; |
20 |
|
|
|
21 |
|
|
int sec = tot_sec % 60; |
22 |
|
|
int min = (tot_sec / 60) % 60; |
23 |
|
|
int hour = (tot_sec / 3600) %24; |
24 |
|
|
int days = tot_sec / (3600*24); |
25 |
|
|
|
26 |
|
|
std::ostringstream os; |
27 |
|
|
os << VERSION << std::endl; |
28 |
|
|
os << SVNVERSION << std::endl; |
29 |
|
|
os << "Status:" << std::endl; |
30 |
|
|
os << "Recv: " << cmn->smsCounter.incomming << std::endl; |
31 |
|
|
os << "Send: " << cmn->smsCounter.outgoing << std::endl; |
32 |
torben |
683 |
os << "Uptime: " << days << "d, "; |
33 |
|
|
os << std::setw(2) << std::setfill('0') << hour << ":"; |
34 |
|
|
os << std::setw(2) << min << ":" ; |
35 |
|
|
os << std::setw(2) << sec; |
36 |
torben |
676 |
|
37 |
|
|
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
38 |
|
|
mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n"); |
39 |
|
|
|
40 |
torben |
679 |
mg_printf(conn, "%s", os.str().c_str() ); |
41 |
|
|
} |
42 |
torben |
676 |
|
43 |
torben |
679 |
void show_index(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
44 |
|
|
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
45 |
|
|
mg_printf(conn, "%s", "Content-Type: text/html\r\n\r\n"); |
46 |
torben |
676 |
|
47 |
torben |
679 |
mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n"); |
48 |
torben |
694 |
mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a><br>\n"); |
49 |
|
|
mg_printf(conn, "%s", "<a href='/log'>smsdaemon log</a><br>\n"); |
50 |
|
|
|
51 |
torben |
679 |
mg_printf(conn, "%s", "</body></html>"); |
52 |
torben |
676 |
} |
53 |
|
|
|
54 |
torben |
684 |
static void show_404(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) |
55 |
|
|
{ |
56 |
|
|
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
57 |
|
|
mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n"); |
58 |
|
|
mg_printf(conn, "%s", "Oops. File not found! "); |
59 |
|
|
} |
60 |
|
|
|
61 |
torben |
694 |
void show_log(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
62 |
|
|
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
63 |
|
|
mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n"); |
64 |
torben |
684 |
|
65 |
torben |
695 |
mg_printf(conn, "%s", Logger::getLog().c_str() ); |
66 |
torben |
694 |
} |
67 |
|
|
|
68 |
torben |
675 |
void EmbeddedHttp::startServer() { |
69 |
torben |
680 |
std::string portStr = Common::instance()->GetConfigfile()->GetValue("smsdaemon", "httpport"); |
70 |
|
|
int port = atoi( portStr.c_str() ); |
71 |
torben |
676 |
|
72 |
torben |
680 |
if (port > 0) { |
73 |
|
|
std::ostringstream oss; |
74 |
|
|
oss << "Starting mongoose embedded http engine (listenin on port " << port << ")"; |
75 |
|
|
Logger::logMessage( oss.str() ); |
76 |
torben |
676 |
|
77 |
torben |
680 |
oss.str(""); //empty string stream |
78 |
|
|
oss << port; |
79 |
|
|
|
80 |
|
|
m_context = mg_start(); |
81 |
|
|
mg_set_option(m_context, "ports", oss.str().c_str() ); |
82 |
torben |
686 |
mg_set_option(m_context, "root", "/nosuchpatch"); //set root to non-existant path to disable normal file serving - only callbacks are now serviced |
83 |
|
|
|
84 |
torben |
680 |
mg_set_uri_callback(m_context, "/", &show_index, 0); |
85 |
torben |
685 |
mg_set_uri_callback(m_context, "/status", &show_status, 0); |
86 |
torben |
694 |
mg_set_uri_callback(m_context, "/log", &show_log, 0); |
87 |
torben |
684 |
mg_set_error_callback(m_context, 404, show_404, NULL); |
88 |
|
|
|
89 |
|
|
|
90 |
torben |
680 |
} else { |
91 |
|
|
Logger::logMessage( "Mongoose http disabled" ); |
92 |
|
|
m_context = 0; |
93 |
|
|
} |
94 |
torben |
675 |
} |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
void EmbeddedHttp::stopServer() { |
98 |
torben |
680 |
if (m_context != 0) { |
99 |
|
|
mg_stop(m_context); |
100 |
|
|
} |
101 |
torben |
675 |
} |