2 Copyright (C) 2010-2011 wpitchoune@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
29 #include <sys/types.h>
30 #include <sys/select.h>
31 #include <sys/socket.h>
36 #include <microhttpd.h>
43 #include "server_lua.h"
46 #include "psensor_json.h"
48 #include "plib/plib_io.h"
51 static const char *program_name;
53 #define DEFAULT_PORT 3131
55 #define PAGE_NOT_FOUND \
56 "<html><body><p>Page not found - Go to <a href='/index.lua'>Main page</a>\
59 static struct option long_options[] = {
60 {"version", no_argument, 0, 'v'},
61 {"help", no_argument, 0, 'h'},
62 {"port", required_argument, 0, 'p'},
63 {"wdir", required_argument, 0, 'w'},
67 static struct server_data server_data;
69 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
73 printf("psensor-server %s\n", VERSION);
74 printf(_("Copyright (C) %s wpitchoune@gmail.com\n\
75 License GPLv2: GNU GPL version 2 or later \
76 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
77 This is free software: you are free to change and redistribute it.\n\
78 There is NO WARRANTY, to the extent permitted by law.\n"),
84 printf(_("Usage: %s [OPTION]...\n"), program_name);
86 puts(_("psensor-server is an HTTP server "
87 "for monitoring hardware sensors remotely."));
92 -h, --help display this help and exit\n\
93 -v, --version display version information and exit"));
98 -p,--port=PORT webserver port\n\
99 -w,--wdir=DIR directory containing webserver pages"));
103 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
105 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
109 Returns '1' if the path denotates a Lua file, otherwise returns 0.
111 int is_path_lua(const char *path)
113 char *dot = rindex(path, '.');
115 if (dot && !strcasecmp(dot, ".lua"))
122 Returns the file path corresponding to a given URL
124 char *get_path(const char *url, const char *www_dir)
129 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
134 res = malloc(strlen(www_dir)+strlen(p)+1);
136 strcpy(res, www_dir);
142 static int cbk_http_request(void *cls,
143 struct MHD_Connection *connection,
147 const char *upload_data,
148 size_t *upload_data_size, void **ptr)
151 struct MHD_Response *response;
154 unsigned int resp_code;
157 if (strcmp(method, "GET"))
158 return MHD_NO; /* unexpected method */
160 if (&dummy != *ptr) {
161 /* The first time only the headers are valid, do not
162 respond in the first round... */
167 if (*upload_data_size)
168 return MHD_NO; /* upload data in a GET!? */
170 *ptr = NULL; /* clear context pointer */
172 nurl = url_normalize(url);
174 pthread_mutex_lock(&mutex);
176 if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS)) {
177 page = sensors_to_json_string(server_data.sensors);
179 } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
180 strlen(URL_BASE_API_1_0_SENSORS))
181 && nurl[strlen(URL_BASE_API_1_0_SENSORS)] == '/') {
183 char *sid = nurl + strlen(URL_BASE_API_1_0_SENSORS) + 1;
185 = psensor_list_get_by_id(server_data.sensors, sid);
188 page = sensor_to_json_string(s);
191 char *fpath = get_path(nurl, server_data.www_dir);
192 int n = strlen(nurl);
194 if (is_path_lua(fpath)) {
196 page = lua_to_html_page(&server_data, fpath);
198 page = strdup(_("ERROR: Lua support not enabled\n"));
201 page = file_get_content(fpath);
208 resp_code = MHD_HTTP_OK;
210 page = strdup(PAGE_NOT_FOUND);
211 resp_code = MHD_HTTP_NOT_FOUND;
214 pthread_mutex_unlock(&mutex);
216 response = MHD_create_response_from_data(strlen(page),
217 (void *)page, MHD_YES, MHD_NO);
219 ret = MHD_queue_response(connection, resp_code, response);
220 MHD_destroy_response(response);
227 int main(int argc, char *argv[])
229 struct MHD_Daemon *d;
230 int port = DEFAULT_PORT;
234 program_name = argv[0];
236 setlocale(LC_ALL, "");
239 bindtextdomain(PACKAGE, LOCALEDIR);
243 server_data.www_dir = DEFAULT_WWW_DIR;
245 while ((optc = getopt_long(argc, argv,
246 "vhp:w:", long_options, NULL)) != -1) {
250 server_data.www_dir = strdup(optarg);
268 if (!cmdok || optind != argc) {
269 fprintf(stderr, _("Try `%s --help' for more information.\n"),
274 if (!lmsensor_init()) {
275 fprintf(stderr, _("ERROR: failed to init lm-sensors\n"));
279 server_data.sensors = get_all_sensors(1);
281 if (!*server_data.sensors)
282 fprintf(stderr, _("ERROR: no sensors detected\n"));
284 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
286 NULL, NULL, &cbk_http_request, server_data.sensors,
289 fprintf(stderr, _("ERROR: Fail to create web server\n"));
293 printf(_("Web server started on port: %d\n"), port);
294 printf(_("WWW directory: %s\n"), server_data.www_dir);
295 printf(_("URL: http://localhost:%d\n"), port);
298 pthread_mutex_lock(&mutex);
301 sysinfo_update(&server_data.cpu_rate);
303 psensor_list_update_measures(server_data.sensors);
305 pthread_mutex_unlock(&mutex);