X-Git-Url: https://git.r.bdr.sh/rbdr/ngx_http_office_hours_filter_module/blobdiff_plain/9cef1f90776018015da6d03c4d17440128b47827..refs/heads/main:/ngx_http_office_hours_filter_module.c?ds=sidebyside diff --git a/ngx_http_office_hours_filter_module.c b/ngx_http_office_hours_filter_module.c index 6f169b9..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,9 +24,10 @@ 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("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 MIDDLE_HTML = ngx_string("

Current Server Time is: "); -const ngx_str_t FOOT_HTML = ngx_string("

"); +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", @@ -42,6 +42,7 @@ const char * DAY_NAMES[7] = { typedef struct { ngx_array_t *office_hours; + ngx_str_t additional_information; } ngx_http_office_hours_conf_t; /* Lifecycle Functions For Module Context */ @@ -92,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 = { @@ -113,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 = { @@ -131,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 @@ -149,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"); @@ -196,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"); @@ -282,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 @@ -468,21 +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; + 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; + } - 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); + int written = 0; + + written += snprintf(output_buffer + written, buffer_size - written, "%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])); + 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); @@ -552,13 +576,13 @@ static char * left_pad(unsigned int number) char * padding; padding = ""; - output_string = malloc(3 * sizeof(char)); + output_string = malloc(4 * sizeof(char)); if (number < 10) { padding = "0"; } - sprintf(output_string, "%s%u", padding, number); + snprintf(output_string, 4, "%s%u", padding, number); return output_string; }