]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blobdiff - ngx_http_office_hours_filter_module.c
Add dynamic HTML to changelog
[rbdr/ngx_http_office_hours_filter_module] / ngx_http_office_hours_filter_module.c
index a3a5af826f628e0e5981d64d246bd7e74fb22cbe..6f169b98abf5e9f79610bb2d7deee8072d4069ee 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 char * DAY_NAMES[7] = {
+    "Monday",
+    "Tuesday",
+    "Wednesday",
+    "Thursday",
+    "Friday",
+    "Saturday",
+    "Sunday"
+};
 
 /* Main Configuration Structure */
 
 
 /* Main Configuration Structure */
 
@@ -44,8 +56,9 @@ static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
 static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
                                    void *conf);
 
 static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
                                    void *conf);
 
-/* Body Filter Storage */
+/* Filter Storage */
 
 
+static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
 
 /* Utility Functions */
 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
 
 /* Utility Functions */
@@ -54,9 +67,15 @@ static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours);
 static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours);
 static ngx_uint_t get_day_of_week(time_t time);
 static ngx_uint_t get_seconds_of_day(time_t time);
 static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours);
 static ngx_uint_t get_day_of_week(time_t time);
 static ngx_uint_t get_seconds_of_day(time_t time);
+static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end);
+static ngx_str_t create_output_html(ngx_http_office_hours_conf_t * conf);
+static char * format_hours(ngx_uint_t * seconds, char * day);
+static char * format_seconds(ngx_uint_t seconds);
+static char * left_pad(unsigned int number);
 
 
-/* Compiled Regex */
+/* Globals */
 ngx_regex_compile_t rc;
 ngx_regex_compile_t rc;
+ngx_str_t OUTPUT_HTML;
 
 /*
  * Module Definitions
 
 /*
  * Module Definitions
@@ -113,17 +132,64 @@ ngx_module_t ngx_http_office_hours_filter_module = {
 };
 
 
 };
 
 
+/*
+ * Main Header Filter
+ * If the current time is within office hours, it goes to the next
+ * handler. Otherwise it sets the headers to 403
+ */
+
+static ngx_int_t
+ngx_http_office_hours_header_filter(ngx_http_request_t * r)
+{
+
+    ngx_uint_t ** parsed_office_hours;
+    ngx_http_office_hours_conf_t *conf;
+
+    conf =
+        ngx_http_get_module_loc_conf(r,
+                                     ngx_http_office_hours_filter_module);
+
+
+    if (conf->office_hours == NULL) {
+        ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
+                      "Office hours disabled");
+        return ngx_http_next_header_filter(r);
+    }
+
+    parsed_office_hours = parse_office_hours(conf->office_hours);
+
+    if (within_office_hours(parsed_office_hours)) {
+        ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
+                      "Within office hours");
+        return ngx_http_next_header_filter(r);
+    }
+
+    ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
+                  "Outside office hours");
+
+    if (OUTPUT_HTML.data == NULL) {
+        OUTPUT_HTML = create_output_html(conf);
+    }
+
+    r->headers_out.status = NGX_HTTP_FORBIDDEN;
+    r->headers_out.content_length_n = OUTPUT_HTML.len;
+
+    return ngx_http_next_header_filter(r);
+}
+
 /*
  * Main Body Filter
  * If the current time is within office hours, it goes to the next
 /*
  * Main Body Filter
  * If the current time is within office hours, it goes to the next
- * handler. Otherwise it returns 403 and the office hour listing.
+ * handler. Otherwise it replaces the body with the office hours.
  */
 
 static ngx_int_t
  */
 
 static ngx_int_t
-ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
+ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t *in)
 {
 
 {
 
+    ngx_buf_t    *b;
     ngx_uint_t ** parsed_office_hours;
     ngx_uint_t ** parsed_office_hours;
+    ngx_chain_t   out;
     ngx_http_office_hours_conf_t *conf;
 
     conf =
     ngx_http_office_hours_conf_t *conf;
 
     conf =
@@ -148,8 +214,28 @@ ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
     ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
                   "Outside office hours");
 
     ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
                   "Outside office hours");
 
-    r->keepalive = 0;
-    return NGX_HTTP_FORBIDDEN;
+    if (OUTPUT_HTML.data == NULL) {
+        OUTPUT_HTML = create_output_html(conf);
+    }
+
+    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
+    if (b == NULL) {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    out.buf = b;
+    out.next = NULL;
+
+    b->start = OUTPUT_HTML.data;
+    b->pos = b->start;
+    b->end = OUTPUT_HTML.data + OUTPUT_HTML.len;
+    b->last = b->end;
+
+    b->memory = 1;
+    b->last_buf = 1;
+
+    return ngx_http_next_body_filter(r, &out);
 }
 
 /*
 }
 
 /*
@@ -235,26 +321,6 @@ static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
     return NGX_CONF_OK;
 }
 
     return NGX_CONF_OK;
 }
 
-/*
- * Postconfig Initialization Handler
- * Sets the request filter at the top of the chain
- */
-
-static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
-{
-
-    ngx_http_next_body_filter = ngx_http_top_body_filter;
-    ngx_http_top_body_filter = ngx_http_office_hours_body_filter;
-
-    rc.pattern = TIME_REGEX;
-    rc.pool = cf->pool;
-    if (ngx_regex_compile(&rc) != NGX_OK) {
-        return NGX_ERROR;
-    }
-
-    return NGX_OK;
-}
-
 /*
  * Parse the office hour strings in the configuration file
  * to fill out the hours array (in seconds)
 /*
  * Parse the office hour strings in the configuration file
  * to fill out the hours array (in seconds)
@@ -316,11 +382,11 @@ static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours)
     if (n >= 0) {
         /* Opening Hours */
 
     if (n >= 0) {
         /* Opening Hours */
 
-        parsed_hours[0] = 60 * 60 * ngx_atoi(&office_hours.data[captures[2]], captures[3] - captures[2]);
-        parsed_hours[0] = parsed_hours[0] + 60 * ngx_atoi(&office_hours.data[captures[4]], captures[5] - captures[4]);
+        parsed_hours[0] = 60 * 60 * parse_number(office_hours, captures[2], captures[3]);
+        parsed_hours[0] = parsed_hours[0] + 60 * parse_number(office_hours, captures[4], captures[5]);
 
 
-        parsed_hours[1] = 60 * 60 * ngx_atoi(&office_hours.data[captures[6]], captures[7] - captures[6]);
-        parsed_hours[1] = parsed_hours[1] + 60 * ngx_atoi(&office_hours.data[captures[8]], captures[9] - captures[8]);
+        parsed_hours[1] = 60 * 60 * parse_number(office_hours, captures[6], captures[7]);
+        parsed_hours[1] = parsed_hours[1] + 60 * parse_number(office_hours, captures[8], captures[9]);
 
         return parsed_hours;
     }
 
         return parsed_hours;
     }
@@ -356,6 +422,7 @@ static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours)
 /*
  * Calculate the day of the week given a timestamp
  */
 /*
  * Calculate the day of the week given a timestamp
  */
+
 static ngx_uint_t get_day_of_week(time_t time)
 {
 
 static ngx_uint_t get_day_of_week(time_t time)
 {
 
@@ -366,8 +433,155 @@ static ngx_uint_t get_day_of_week(time_t time)
 /*
  * Calculate the number of seconds elapsed today
  */
 /*
  * Calculate the number of seconds elapsed today
  */
+
 static ngx_uint_t get_seconds_of_day(time_t time)
 {
 
     return time - (time / 86400) * 86400;
 }
 static ngx_uint_t get_seconds_of_day(time_t time)
 {
 
     return time - (time / 86400) * 86400;
 }
+
+/*
+ * Parses a string, returns 0 if match was not found
+ */
+
+static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end)
+{
+
+    if (end - start == 0) {
+        return 0;
+    }
+
+    return ngx_atoi(&string.data[start], end - start);
+}
+
+/*
+ * Given the current office hours configuration it creates the custom
+ * HTML
+ */
+
+static ngx_str_t create_output_html(ngx_http_office_hours_conf_t * conf)
+{
+
+    char * output_buffer;
+    time_t now;
+    ngx_str_t output_html;
+    ngx_uint_t i, seconds_of_day;
+    ngx_uint_t ** parsed_office_hours;
+
+
+    output_buffer = malloc(1024 * sizeof(char));
+    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);
+
+    for (i = 0; i < 7; ++i) {
+        sprintf(output_buffer + strlen(output_buffer), "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i]));
+    }
+
+    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);
+
+    output_html.data = (unsigned char *) output_buffer;
+    output_html.len = strlen(output_buffer);
+
+    return output_html;
+}
+
+/*
+ * Given a tuple of seconds and a day name, outputs an HTML
+ * string containing the formatted data as a list item
+ */
+
+static char * format_hours(ngx_uint_t * hours, char * day)
+{
+
+    char * output_html;
+
+    output_html = malloc(64 * sizeof(char));
+    if (hours[0] == hours[1]) {
+        sprintf(output_html, "<li><strong>%s</strong>: CLOSED</li>",
+                day
+               );
+        return output_html;
+    }
+
+    sprintf(output_html, "<li><strong>%s</strong>: %s - %s</li>",
+            day,
+            (char *) format_seconds(hours[0]),
+            (char *) format_seconds(hours[1])
+           );
+
+    return output_html;
+}
+
+/*
+ * Given seconds of the day, it returns a string showing
+ * HH:MM in 24 hour format
+ */
+
+static char * format_seconds(ngx_uint_t seconds)
+{
+
+    char * output_html;
+    unsigned int hours, minutes;
+
+    output_html = malloc(6 * sizeof(char));
+
+    hours = seconds / 60 / 60;
+    minutes = (seconds / 60) % 60;
+
+    sprintf(output_html, "%s:%s",
+            left_pad(hours),
+            left_pad(minutes)
+           );
+
+    return output_html;
+}
+
+/*
+ * Returns a number as a string adding 0 if < 10
+ */
+
+static char * left_pad(unsigned int number)
+{
+
+    char * output_string;
+    char * padding;
+
+    padding = "";
+    output_string = malloc(3 * sizeof(char));
+
+    if (number < 10) {
+        padding = "0";
+    }
+
+    sprintf(output_string, "%s%u", padding, number);
+
+    return output_string;
+}
+
+/*
+ * Postconfig Initialization Handler
+ * Sets the request filter at the top of the chain
+ */
+
+static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
+{
+
+    ngx_http_next_header_filter = ngx_http_top_header_filter;
+    ngx_http_top_header_filter = ngx_http_office_hours_header_filter;
+
+    ngx_http_next_body_filter = ngx_http_top_body_filter;
+    ngx_http_top_body_filter = ngx_http_office_hours_body_filter;
+
+    rc.pattern = TIME_REGEX;
+    rc.pool = cf->pool;
+    if (ngx_regex_compile(&rc) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    return NGX_OK;
+}