Uninstall nginx.
+## Adding more information
+
+This plugin also provides an `office_hours_additional_information` directive
+that lets you add informative HTML to the page that shows when outside
+office hours.
+
+```
+office_hours_additional_information "<h1>Additional Information></h1>
+<p>Please do not email us asking to open the website 24/7. Send all complaints to friendship.quest/@ruben</p>"
+```
+
## What you will need to get started
* [astyle][astyle]: Used to format the code
## Further Improvements
+* Add support for double shifts in the same day
+* Add support for serving custom HTML when server is closed
* Add support for timezones (currently only GMT)
* Add support for public holidays
* Add support to respect the time zone of the visitor and not just the
server
-* Add support for double shifts in the same day
[astyle]: http://astyle.sourceforge.net
[nginx]: https://nginx.org/en/download.html
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",
typedef struct {
ngx_array_t *office_hours;
+ ngx_str_t additional_information;
} ngx_http_office_hours_conf_t;
/* Lifecycle Functions For Module Context */
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_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);
- 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);