X-Git-Url: https://git.r.bdr.sh/rbdr/ngx_http_office_hours_filter_module/blobdiff_plain/67206816625c33f24d0135c4df780c24afe6fece..refs/heads/main:/ngx_http_office_hours_filter_module.c diff --git a/ngx_http_office_hours_filter_module.c b/ngx_http_office_hours_filter_module.c index b9df683..5270a8d 100644 --- a/ngx_http_office_hours_filter_module.c +++ b/ngx_http_office_hours_filter_module.c @@ -11,7 +11,6 @@ * specific language governing permissions and limitations under the License. */ - #include #include #include @@ -25,12 +24,25 @@ 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("This website is currently closed!

This website is currently closed!

This website has closed for the day, please check our office hours below

"); +const ngx_str_t HEAD_HTML = ngx_string("This website is currently closed!

This website is currently closed!

This website has closed for the day, please check our office hours below:

    "); +const ngx_str_t OPEN_SERVER_TIME_HTML = ngx_string("

Current Server Time is: "); +const ngx_str_t CLOSE_SERVER_TIME_HTML = ngx_string("

"); +const ngx_str_t FOOT_HTML = ngx_string(""); +const char * DAY_NAMES[7] = { + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday" +}; /* Main Configuration Structure */ typedef struct { ngx_array_t *office_hours; + ngx_str_t additional_information; } ngx_http_office_hours_conf_t; /* Lifecycle Functions For Module Context */ @@ -57,9 +69,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 @@ -76,11 +93,18 @@ static ngx_command_t ngx_http_office_hours_commands[] = { 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 }; - /* Module Context */ static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { @@ -97,7 +121,6 @@ static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { ngx_http_office_hours_merge_conf /* Merge location configuration */ }; - /* Module Definition */ ngx_module_t ngx_http_office_hours_filter_module = { @@ -115,7 +138,6 @@ ngx_module_t ngx_http_office_hours_filter_module = { NGX_MODULE_V1_PADDING }; - /* * Main Header Filter * If the current time is within office hours, it goes to the next @@ -133,7 +155,6 @@ ngx_http_office_hours_header_filter(ngx_http_request_t * r) 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"); @@ -151,6 +172,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; @@ -176,7 +201,6 @@ ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t *in) 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"); @@ -194,6 +218,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."); @@ -258,7 +286,6 @@ static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, return NGX_CONF_OK; } - /* * Config Creator * Initializes the configuration structure @@ -398,6 +425,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 +436,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 +446,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 +457,136 @@ 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; + + 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); + + int written = 0; + + written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) HEAD_HTML.data); + + for (i = 0; i < 7; ++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); + } + + 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); + + 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, "
  • %s: CLOSED
  • ", + day + ); + return output_html; + } + + sprintf(output_html, "
  • %s: %s - %s
  • ", + 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(4 * sizeof(char)); + + if (number < 10) { + padding = "0"; + } + + snprintf(output_string, 4, "%s%u", padding, number); + + return output_string; +} + /* * Postconfig Initialization Handler * Sets the request filter at the top of the chain