allow time without spaces in ngx_parse_time()

This commit is contained in:
Igor Sysoev 2008-04-17 14:23:20 +00:00
parent 2e39e289e1
commit f57b24e70f
2 changed files with 46 additions and 58 deletions

View File

@ -93,12 +93,11 @@ ngx_parse_offset(ngx_str_t *line)
ngx_int_t ngx_int_t
ngx_parse_time(ngx_str_t *line, ngx_int_t sec) ngx_parse_time(ngx_str_t *line, ngx_uint_t sec)
{ {
size_t len; u_char *p, *last;
u_char *start, last;
ngx_int_t value, total, scale; ngx_int_t value, total, scale;
ngx_uint_t max, i; ngx_uint_t max, valid;
enum { enum {
st_start = 0, st_start = 0,
st_year, st_year,
@ -112,39 +111,30 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
st_last st_last
} step; } step;
valid = 0;
start = line->data; value = 0;
len = 0;
total = 0; total = 0;
step = sec ? st_start : st_month; step = sec ? st_start : st_month;
scale = sec ? 1 : 1000;
for (i = 0; /* void */ ; i++) { p = line->data;
last = p + line->len;
if (i < line->len) { while (p < last) {
if (line->data[i] != ' ') {
len++; if (*p >= '0' && *p <= '9') {
value = value * 10 + (*p++ - '0');
valid = 1;
continue; continue;
} }
if (line->data[i] == ' ' && len == 0) { switch (*p++) {
start = &line->data[i + 1];
continue;
}
}
if (len == 0) {
break;
}
last = line->data[i - 1];
switch (last) {
case 'y': case 'y':
if (step > st_start) { if (step > st_start) {
return NGX_ERROR; return NGX_ERROR;
} }
step = st_year; step = st_year;
len--;
max = 68; max = 68;
scale = 60 * 60 * 24 * 365; scale = 60 * 60 * 24 * 365;
break; break;
@ -154,7 +144,6 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
return NGX_ERROR; return NGX_ERROR;
} }
step = st_month; step = st_month;
len--;
max = 828; max = 828;
scale = 60 * 60 * 24 * 30; scale = 60 * 60 * 24 * 30;
break; break;
@ -164,7 +153,6 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
return NGX_ERROR; return NGX_ERROR;
} }
step = st_week; step = st_week;
len--;
max = 3550; max = 3550;
scale = 60 * 60 * 24 * 7; scale = 60 * 60 * 24 * 7;
break; break;
@ -174,7 +162,6 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
return NGX_ERROR; return NGX_ERROR;
} }
step = st_day; step = st_day;
len--;
max = 24855; max = 24855;
scale = 60 * 60 * 24; scale = 60 * 60 * 24;
break; break;
@ -184,52 +171,49 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
return NGX_ERROR; return NGX_ERROR;
} }
step = st_hour; step = st_hour;
len--;
max = 596523; max = 596523;
scale = 60 * 60; scale = 60 * 60;
break; break;
case 'm': case 'm':
if (*p == 's') {
if (sec || step > st_sec) {
return NGX_ERROR;
}
p++;
step = st_msec;
max = 2147483647;
scale = 1;
break;
}
if (step > st_hour) { if (step > st_hour) {
return NGX_ERROR; return NGX_ERROR;
} }
step = st_min; step = st_min;
len--;
max = 35791394; max = 35791394;
scale = 60; scale = 60;
break; break;
case 's': case 's':
len--;
if (line->data[i - 2] == 'm') {
if (sec || step > st_sec) {
return NGX_ERROR;
}
step = st_msec;
len--;
max = 2147483647;
scale = 1;
break;
}
if (step > st_min) { if (step > st_min) {
return NGX_ERROR; return NGX_ERROR;
} }
step = st_sec; step = st_sec;
max = 2147483647; max = 2147483647;
scale = 1; scale = 1;
break; break;
default: case ' ':
if (step > st_min) {
return NGX_ERROR;
}
step = st_last; step = st_last;
max = 2147483647; max = 2147483647;
scale = 1; scale = 1;
} break;
value = ngx_atoi(start, len); default:
if (value == NGX_ERROR) {
return NGX_ERROR; return NGX_ERROR;
} }
@ -238,23 +222,27 @@ ngx_parse_time(ngx_str_t *line, ngx_int_t sec)
max /= 1000; max /= 1000;
} }
if ((u_int) value > max) { if ((ngx_uint_t) value > max) {
return NGX_PARSE_LARGE_TIME; return NGX_ERROR;
} }
total += value * scale; total += value * scale;
if ((u_int) total > 2147483647) { if ((ngx_uint_t) total > 2147483647) {
return NGX_PARSE_LARGE_TIME; return NGX_ERROR;
} }
if (i >= line->len) { value = 0;
break; scale = sec ? 1 : 1000;
while (p < last && *p == ' ') {
p++;
}
} }
len = 0; if (valid) {
start = &line->data[i + 1]; return total + value * scale;
} }
return total; return NGX_ERROR;
} }

View File

@ -17,7 +17,7 @@
ssize_t ngx_parse_size(ngx_str_t *line); ssize_t ngx_parse_size(ngx_str_t *line);
off_t ngx_parse_offset(ngx_str_t *line); off_t ngx_parse_offset(ngx_str_t *line);
ngx_int_t ngx_parse_time(ngx_str_t *line, ngx_int_t sec); ngx_int_t ngx_parse_time(ngx_str_t *line, ngx_uint_t sec);
#endif /* _NGX_PARSE_H_INCLUDED_ */ #endif /* _NGX_PARSE_H_INCLUDED_ */