]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blobdiff - ngx_http_office_hours_filter_module.c
Formatting
[rbdr/ngx_http_office_hours_filter_module] / ngx_http_office_hours_filter_module.c
index 49fe50f55d66fbf8f9c4ff26fb43360d0857fcde..5270a8d948d5112fa0d7dedd2b453f1a3210aac4 100644 (file)
 const ngx_uint_t WEEK_LENGTH = 7;
 const char * CLOSED_TOKEN = "closed";
 const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?");
 const ngx_uint_t WEEK_LENGTH = 7;
 const char * CLOSED_TOKEN = "closed";
 const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?");
-const ngx_str_t HEAD_HTML = ngx_string("<!doctype html><html><head><title>This website is currently closed!</title></head><body><h1>This website is currently closed!</h1><p>This website has closed for the day, please check our office hours below</p><ul>");
-const ngx_str_t MIDDLE_HTML = ngx_string("</ul><p><em>Current Server Time is: ");
-const ngx_str_t FOOT_HTML = ngx_string("</em></p></body></html>");
+const ngx_str_t HEAD_HTML = ngx_string("<!doctype html><html><head><title>This website is currently closed!</title></head><body><h1>This website is currently closed!</h1><p>This website has closed for the day, please check our office hours below:</p><ul>");
+const ngx_str_t OPEN_SERVER_TIME_HTML = ngx_string("</ul><p><em>Current Server Time is: ");
+const ngx_str_t CLOSE_SERVER_TIME_HTML = ngx_string("</em></p>");
+const ngx_str_t FOOT_HTML = ngx_string("</body></html>");
 const char * DAY_NAMES[7] = {
     "Monday",
     "Tuesday",
 const char * DAY_NAMES[7] = {
     "Monday",
     "Tuesday",
@@ -41,13 +42,14 @@ const char * DAY_NAMES[7] = {
 
 typedef struct {
     ngx_array_t *office_hours;
 
 typedef struct {
     ngx_array_t *office_hours;
+    ngx_str_t   additional_information;
 } ngx_http_office_hours_conf_t;
 
 /* Lifecycle Functions For Module Context */
 
 static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
 static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
 } ngx_http_office_hours_conf_t;
 
 /* Lifecycle Functions For Module Context */
 
 static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
 static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
-    void *parent, void *child);
+        void *parent, void *child);
 static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
 
 /* Configuration Handler */
 static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
 
 /* Configuration Handler */
@@ -91,6 +93,14 @@ static ngx_command_t ngx_http_office_hours_commands[] = {
         offsetof(ngx_http_office_hours_conf_t, office_hours),
         NULL
     },
         offsetof(ngx_http_office_hours_conf_t, office_hours),
         NULL
     },
+    {
+        ngx_string("office_hours_additional_information"),
+        NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 | NGX_CONF_TAKE2 | NGX_CONF_TAKE3 | NGX_CONF_TAKE4 | NGX_CONF_TAKE5 | NGX_CONF_TAKE6 | NGX_CONF_TAKE7,
+        ngx_conf_set_str_slot,
+        NGX_HTTP_LOC_CONF_OFFSET,
+        offsetof(ngx_http_office_hours_conf_t, additional_information),
+        NULL
+    },
 
     ngx_null_command
 };
 
     ngx_null_command
 };
@@ -303,7 +313,7 @@ static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf)
  */
 
 static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
  */
 
 static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
-    void *parent, void *child)
+        void *parent, void *child)
 {
 
     ngx_http_office_hours_conf_t *prev = parent;
 {
 
     ngx_http_office_hours_conf_t *prev = parent;
@@ -461,20 +471,42 @@ static ngx_str_t create_output_html(ngx_http_office_hours_conf_t * conf)
     ngx_uint_t i, seconds_of_day;
     ngx_uint_t ** parsed_office_hours;
 
     ngx_uint_t i, seconds_of_day;
     ngx_uint_t ** parsed_office_hours;
 
-    output_buffer = malloc(1024 * sizeof(char));
+    size_t base_size = 1024;
+    size_t additional_info_size = conf->additional_information.len;
+    size_t buffer_size = base_size + additional_info_size + 1;
+    output_buffer = malloc(buffer_size * sizeof(char));
+
+    /* UH-OH we couldn't allocate the bufer */
+    if (!output_buffer) {
+        output_html.data = NULL;
+        output_html.len = 0;
+        return output_html;
+    }
+
     parsed_office_hours = parse_office_hours(conf->office_hours);
     now = ngx_time();
     seconds_of_day = get_seconds_of_day(now);
 
     parsed_office_hours = parse_office_hours(conf->office_hours);
     now = ngx_time();
     seconds_of_day = get_seconds_of_day(now);
 
-    sprintf(output_buffer, "%s", (char *) HEAD_HTML.data);
+    int written = 0;
+
+    written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) HEAD_HTML.data);
 
     for (i = 0; i < 7; ++i) {
 
     for (i = 0; i < 7; ++i) {
-        sprintf(output_buffer + strlen(output_buffer), "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i]));
+        written +=  snprintf(output_buffer + written, buffer_size - written, "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i]));
+    }
+
+    written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) OPEN_SERVER_TIME_HTML.data);
+    written += snprintf(output_buffer + written, buffer_size - written, "%s", format_seconds(seconds_of_day));
+    written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) CLOSE_SERVER_TIME_HTML.data);
+
+    if (conf->additional_information.len > 0) {
+        char additional_info[conf->additional_information.len + 1];
+        ngx_memcpy(additional_info, conf->additional_information.data, conf->additional_information.len);
+        additional_info[conf->additional_information.len] = '\0';
+        written += snprintf(output_buffer + written, buffer_size - written, "%s", additional_info);
     }
 
     }
 
-    sprintf(output_buffer + strlen(output_buffer), "%s", (char *) MIDDLE_HTML.data);
-    sprintf(output_buffer + strlen(output_buffer), "%s", format_seconds(seconds_of_day));
-    sprintf(output_buffer + strlen(output_buffer), "%s", (char *) FOOT_HTML.data);
+    snprintf(output_buffer + written, buffer_size - written, "%s", (char *) FOOT_HTML.data);
 
     output_html.data = (unsigned char *) output_buffer;
     output_html.len = strlen(output_buffer);
 
     output_html.data = (unsigned char *) output_buffer;
     output_html.len = strlen(output_buffer);