2012 copyright
[psensor.git] / src / server / server.c
index e50984c..65f0ee8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2011 jeanfi@gmail.com
+ * Copyright (C) 2010-2012 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 
 #include "psensor_json.h"
 #include "url.h"
-#include "p_io.h"
 #include "server.h"
 
 static const char *program_name;
 
 #define DEFAULT_PORT 3131
 
-#define PAGE_NOT_FOUND (_("<html><body><p>\
-Page not found - Go to <a href='/'>Main page</a>\
-</p></body>"))
+#define PAGE_NOT_FOUND (_("<html><body><p>"\
+"Page not found - Go to <a href='/'>Main page</a></p></body>"))
 
 static struct option long_options[] = {
        {"version", no_argument, 0, 'v'},
@@ -74,12 +72,12 @@ static int server_stop_requested;
 void print_version()
 {
        printf("psensor-server %s\n", VERSION);
-       printf(_("Copyright (C) %s jeanfi@gmail.com\n\
-License GPLv2: GNU GPL version 2 or later \
-<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
-This is free software: you are free to change and redistribute it.\n\
-There is NO WARRANTY, to the extent permitted by law.\n"),
-              "2010-2011");
+       printf(_("Copyright (C) %s jeanfi@gmail.com\n"
+                "License GPLv2: GNU GPL version 2 or later "
+                "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
+                "This is free software: you are free to change and redistribute it.\n"
+                "There is NO WARRANTY, to the extent permitted by law.\n"),
+              "2010-2012");
 }
 
 void print_help()
@@ -91,16 +89,14 @@ void print_help()
 
        puts("");
        puts("Options:");
-       puts(_("\
-  -h, --help           display this help and exit\n\
-  -v, --version                display version information and exit"));
+       puts(_("  -h, --help            display this help and exit\n"
+              "  -v, --version         display version information and exit"));
 
        puts("");
 
-       puts(_("\
-  -d,--debug           run in debug mode\n\
-  -p,--port=PORT       webserver port\n\
-  -w,--wdir=DIR                directory containing webserver pages"));
+       puts(_("  -d,--debug            run in debug mode\n"
+              "  -p,--port=PORT        webserver port\n"
+              "  -w,--wdir=DIR         directory containing webserver pages"));
 
        puts("");
 
@@ -131,7 +127,7 @@ char *get_path(const char *url, const char *www_dir)
 }
 
 #if MHD_VERSION >= 0x00090200
-static int
+static ssize_t
 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
 #else
 static int
@@ -154,12 +150,9 @@ create_response_api(const char *nurl,
        char *page = NULL;
 
        if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS))  {
-
                page = sensors_to_json_string(server_data.sensors);
-
 #ifdef HAVE_GTOP
        } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
-
                page = sysinfo_to_json_string(&server_data.psysinfo);
        } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
                page = sensor_to_json_string(server_data.cpu_usage);
@@ -203,28 +196,33 @@ create_response_file(const char *nurl,
                     unsigned int *rp_code,
                     const char *fpath)
 {
-       if (is_file(fpath)) {
-               FILE *file = fopen(fpath, "rb");
+       struct stat st;
+       int ret;
+       FILE *file;
 
-               if (file) {
-                       struct stat buf;
+       ret = stat(fpath, &st);
+
+       if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
+               file = fopen(fpath, "rb");
 
-                       stat(fpath, &buf);
+               if (file) {
                        *rp_code = MHD_HTTP_OK;
 
-                       if (!buf.st_size) {
+                       if (!st.st_size) {
                                fclose(file);
                                return MHD_create_response_from_data
                                        (0, NULL, MHD_NO, MHD_NO);
                        }
 
                        return MHD_create_response_from_callback
-                               (buf.st_size,
+                               (st.st_size,
                                 32 * 1024,
                                 &file_reader,
                                 file,
                                 (MHD_ContentReaderFreeCallback)&fclose);
 
+               } else {
+                       log_printf(LOG_ERR, "Failed to open: %s\n", fpath);
                }
        }
 
@@ -320,7 +318,7 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
 #endif
 
-       server_data.www_dir = DEFAULT_WWW_DIR;
+       server_data.www_dir = NULL;
        server_data.psysinfo.interfaces = NULL;
 
        while ((optc = getopt_long(argc, argv,
@@ -349,6 +347,8 @@ int main(int argc, char *argv[])
                }
        }
 
+       server_data.www_dir = strdup(DEFAULT_WWW_DIR);
+
        if (!cmdok || optind != argc) {
                fprintf(stderr, _("Try `%s --help' for more information.\n"),
                        program_name);