]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/commitdiff
Dynamically generate HTML on closed hours
authorBen Beltran <redacted>
Fri, 28 Dec 2018 19:50:08 +0000 (12:50 -0700)
committerBen Beltran <redacted>
Fri, 28 Dec 2018 19:50:08 +0000 (12:50 -0700)
ngx_http_office_hours_filter_module.c

index b9df683205ead8b2e83e5dd925705b62688edaff..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_str_t OUTPUT_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></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 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 */
 
@@ -57,9 +68,14 @@ 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_str_t OUTPUT_HTML;
 
 /*
  * Module Definitions
@@ -151,6 +167,10 @@ ngx_http_office_hours_header_filter(ngx_http_request_t * 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;
 
@@ -194,6 +214,10 @@ 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");
 
+    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.");
@@ -398,6 +422,7 @@ static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours)
 /*
  * Calculate the day of the week given a timestamp
  */
+
 static ngx_uint_t get_day_of_week(time_t time)
 {
 
@@ -408,6 +433,7 @@ static ngx_uint_t get_day_of_week(time_t time)
 /*
  * Calculate the number of seconds elapsed today
  */
+
 static ngx_uint_t get_seconds_of_day(time_t time)
 {
 
@@ -417,6 +443,7 @@ static ngx_uint_t get_seconds_of_day(time_t time)
 /*
  * 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)
 {
 
@@ -427,6 +454,115 @@ static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t en
     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