]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/commitdiff
Fix bug with negative minutes
authorBen Beltran <redacted>
Fri, 28 Dec 2018 00:40:22 +0000 (17:40 -0700)
committerBen Beltran <redacted>
Fri, 28 Dec 2018 00:40:22 +0000 (17:40 -0700)
ngx_http_office_hours_filter_module.c

index a3a5af826f628e0e5981d64d246bd7e74fb22cbe..ee914b1a76ed245e331ae5493d323f02354b3e1e 100644 (file)
@@ -54,6 +54,7 @@ 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_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end);
 
 /* Compiled Regex */
 ngx_regex_compile_t rc;
@@ -235,26 +236,6 @@ static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
     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)
@@ -316,11 +297,11 @@ static ngx_uint_t * parse_office_hours_string(ngx_str_t office_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;
     }
@@ -371,3 +352,36 @@ 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);
+}
+
+/*
+ * 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;
+}