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)
30 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
37 #include <microhttpd.h>
44 #include "server_lua.h"
47 #include "psensor_json.h"
49 #include "plib/plib_io.h"
52 static const char *program_name;
54 #define DEFAULT_PORT 3131
56 #define PAGE_NOT_FOUND (_("<html><body><p>\
57 Page not found - Go to <a href='/'>Main page</a>\
60 static struct option long_options[] = {
61 {"version", no_argument, 0, 'v'},
62 {"help", no_argument, 0, 'h'},
63 {"port", required_argument, 0, 'p'},
64 {"wdir", required_argument, 0, 'w'},
65 {"debug", no_argument, 0, 'd'},
69 static struct server_data server_data;
71 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
75 static int server_stop_requested;
79 printf("psensor-server %s\n", VERSION);
80 printf(_("Copyright (C) %s wpitchoune@gmail.com\n\
81 License GPLv2: GNU GPL version 2 or later \
82 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
83 This is free software: you are free to change and redistribute it.\n\
84 There is NO WARRANTY, to the extent permitted by law.\n"),
90 printf(_("Usage: %s [OPTION]...\n"), program_name);
92 puts(_("psensor-server is an HTTP server "
93 "for monitoring hardware sensors remotely."));
98 -h, --help display this help and exit\n\
99 -v, --version display version information and exit"));
104 -d,--debug run in debug mode\n\
105 -p,--port=PORT webserver port\n\
106 -w,--wdir=DIR directory containing webserver pages"));
110 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
112 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
116 Returns '1' if the path denotates a Lua file, otherwise returns 0.
118 int is_path_lua(const char *path)
120 char *dot = rindex(path, '.');
122 if (dot && !strcasecmp(dot, ".lua"))
129 Returns the file path corresponding to a given URL
131 char *get_path(const char *url, const char *www_dir)
136 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
141 res = malloc(strlen(www_dir)+strlen(p)+1);
143 strcpy(res, www_dir);
150 file_reader(void *cls, uint64_t pos, char *buf, int max)
154 fseek(file, pos, SEEK_SET);
155 return fread(buf, 1, max, file);
158 struct MHD_Response *
159 create_response_api(const char *nurl,
161 unsigned int *rp_code)
165 if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS)) {
167 page = sensors_to_json_string(server_data.sensors);
169 } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
170 strlen(URL_BASE_API_1_0_SENSORS))
171 && nurl[strlen(URL_BASE_API_1_0_SENSORS)] == '/') {
173 const char *sid = nurl + strlen(URL_BASE_API_1_0_SENSORS) + 1;
176 = psensor_list_get_by_id(server_data.sensors, sid);
179 page = sensor_to_json_string(s);
181 } else if (debug && !strcmp(nurl, URL_API_1_0_SERVER_STOP)) {
183 server_stop_requested = 1;
184 page = strdup(_("<html><body><p>"
185 "Server stop requested</p></body></html>"));
189 *rp_code = MHD_HTTP_OK;
191 return MHD_create_response_from_data
192 (strlen(page), page, MHD_YES, MHD_NO);
198 struct MHD_Response *
199 create_response_lua(const char *nurl,
201 unsigned int *rp_code,
205 char *page = lua_to_html_page(&server_data, fpath);
208 *rp_code = MHD_HTTP_OK;
210 return MHD_create_response_from_data
211 (strlen(page), page, MHD_YES, MHD_NO);
218 struct MHD_Response *
219 create_response_file(const char *nurl,
221 unsigned int *rp_code,
224 if (is_file(fpath)) {
225 FILE *file = fopen(fpath, "rb");
231 *rp_code = MHD_HTTP_OK;
235 return MHD_create_response_from_data
236 (0, NULL, MHD_NO, MHD_NO);
239 return MHD_create_response_from_callback
244 (MHD_ContentReaderFreeCallback)&fclose);
252 struct MHD_Response *
253 create_response(const char *nurl, const char *method, unsigned int *rp_code)
255 struct MHD_Response *resp = NULL;
257 if (!strncmp(nurl, URL_BASE_API_1_0, strlen(URL_BASE_API_1_0))) {
258 resp = create_response_api(nurl, method, rp_code);
260 char *fpath = get_path(nurl, server_data.www_dir);
262 if (is_path_lua(fpath))
263 resp = create_response_lua
264 (nurl, method, rp_code, fpath);
266 resp = create_response_file
267 (nurl, method, rp_code, fpath);
275 char *page = strdup(PAGE_NOT_FOUND);
276 *rp_code = MHD_HTTP_NOT_FOUND;
278 return MHD_create_response_from_data
279 (strlen(page), page, MHD_YES, MHD_NO);
284 cbk_http_request(void *cls,
285 struct MHD_Connection *connection,
289 const char *upload_data,
290 size_t *upload_data_size, void **ptr)
293 struct MHD_Response *response;
296 unsigned int resp_code;
299 if (strcmp(method, "GET"))
302 if (&dummy != *ptr) {
303 /* The first time only the headers are valid, do not
304 respond in the first round... */
309 if (*upload_data_size)
312 *ptr = NULL; /* clear context pointer */
315 printf(_("HTTP Request: %s\n"), url);
317 nurl = url_normalize(url);
319 pthread_mutex_lock(&mutex);
320 response = create_response(nurl, method, &resp_code);
321 pthread_mutex_unlock(&mutex);
323 ret = MHD_queue_response(connection, resp_code, response);
324 MHD_destroy_response(response);
331 int main(int argc, char *argv[])
333 struct MHD_Daemon *d;
334 int port = DEFAULT_PORT;
338 program_name = argv[0];
340 setlocale(LC_ALL, "");
343 bindtextdomain(PACKAGE, LOCALEDIR);
347 server_data.www_dir = DEFAULT_WWW_DIR;
349 while ((optc = getopt_long(argc, argv,
350 "vhp:w:d", long_options, NULL)) != -1) {
354 server_data.www_dir = strdup(optarg);
375 if (!cmdok || optind != argc) {
376 fprintf(stderr, _("Try `%s --help' for more information.\n"),
381 if (!lmsensor_init()) {
382 fprintf(stderr, _("ERROR: failed to init lm-sensors\n"));
386 server_data.sensors = get_all_sensors(1);
388 if (!*server_data.sensors)
389 fprintf(stderr, _("ERROR: no sensors detected\n"));
391 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
393 NULL, NULL, &cbk_http_request, server_data.sensors,
396 fprintf(stderr, _("ERROR: Fail to create web server\n"));
400 printf(_("Web server started on port: %d\n"), port);
401 printf(_("WWW directory: %s\n"), server_data.www_dir);
402 printf(_("URL: http://localhost:%d\n"), port);
404 while (!server_stop_requested) {
405 pthread_mutex_lock(&mutex);
408 sysinfo_update(&server_data.psysinfo);
410 psensor_list_update_measures(server_data.sensors);
412 pthread_mutex_unlock(&mutex);
418 /* sanity cleanup for valgrind */
419 psensor_list_free(server_data.sensors);
420 free(server_data.www_dir);