comparison src/core/ngx_parse.c @ 1972:9d2bdbda4ee3

allow time without spaces in ngx_parse_time()
author Igor Sysoev <igor@sysoev.ru>
date Thu, 17 Apr 2008 14:23:20 +0000
parents d7abb9e669e2
children c352c483263c
comparison
equal deleted inserted replaced
1971:d7abb9e669e2 1972:9d2bdbda4ee3
91 return offset; 91 return offset;
92 } 92 }
93 93
94 94
95 ngx_int_t 95 ngx_int_t
96 ngx_parse_time(ngx_str_t *line, ngx_int_t sec) 96 ngx_parse_time(ngx_str_t *line, ngx_uint_t sec)
97 { 97 {
98 size_t len; 98 u_char *p, *last;
99 u_char *start, last;
100 ngx_int_t value, total, scale; 99 ngx_int_t value, total, scale;
101 ngx_uint_t max, i; 100 ngx_uint_t max, valid;
102 enum { 101 enum {
103 st_start = 0, 102 st_start = 0,
104 st_year, 103 st_year,
105 st_month, 104 st_month,
106 st_week, 105 st_week,
110 st_sec, 109 st_sec,
111 st_msec, 110 st_msec,
112 st_last 111 st_last
113 } step; 112 } step;
114 113
115 114 valid = 0;
116 start = line->data; 115 value = 0;
117 len = 0;
118 total = 0; 116 total = 0;
119 step = sec ? st_start : st_month; 117 step = sec ? st_start : st_month;
120 118 scale = sec ? 1 : 1000;
121 for (i = 0; /* void */ ; i++) { 119
122 120 p = line->data;
123 if (i < line->len) { 121 last = p + line->len;
124 if (line->data[i] != ' ') { 122
125 len++; 123 while (p < last) {
126 continue; 124
127 } 125 if (*p >= '0' && *p <= '9') {
128 126 value = value * 10 + (*p++ - '0');
129 if (line->data[i] == ' ' && len == 0) { 127 valid = 1;
130 start = &line->data[i + 1]; 128 continue;
131 continue; 129 }
132 } 130
133 } 131 switch (*p++) {
134 132
135 if (len == 0) {
136 break;
137 }
138
139 last = line->data[i - 1];
140
141 switch (last) {
142 case 'y': 133 case 'y':
143 if (step > st_start) { 134 if (step > st_start) {
144 return NGX_ERROR; 135 return NGX_ERROR;
145 } 136 }
146 step = st_year; 137 step = st_year;
147 len--;
148 max = 68; 138 max = 68;
149 scale = 60 * 60 * 24 * 365; 139 scale = 60 * 60 * 24 * 365;
150 break; 140 break;
151 141
152 case 'M': 142 case 'M':
153 if (step > st_year) { 143 if (step > st_year) {
154 return NGX_ERROR; 144 return NGX_ERROR;
155 } 145 }
156 step = st_month; 146 step = st_month;
157 len--;
158 max = 828; 147 max = 828;
159 scale = 60 * 60 * 24 * 30; 148 scale = 60 * 60 * 24 * 30;
160 break; 149 break;
161 150
162 case 'w': 151 case 'w':
163 if (step > st_month) { 152 if (step > st_month) {
164 return NGX_ERROR; 153 return NGX_ERROR;
165 } 154 }
166 step = st_week; 155 step = st_week;
167 len--;
168 max = 3550; 156 max = 3550;
169 scale = 60 * 60 * 24 * 7; 157 scale = 60 * 60 * 24 * 7;
170 break; 158 break;
171 159
172 case 'd': 160 case 'd':
173 if (step > st_week) { 161 if (step > st_week) {
174 return NGX_ERROR; 162 return NGX_ERROR;
175 } 163 }
176 step = st_day; 164 step = st_day;
177 len--;
178 max = 24855; 165 max = 24855;
179 scale = 60 * 60 * 24; 166 scale = 60 * 60 * 24;
180 break; 167 break;
181 168
182 case 'h': 169 case 'h':
183 if (step > st_day) { 170 if (step > st_day) {
184 return NGX_ERROR; 171 return NGX_ERROR;
185 } 172 }
186 step = st_hour; 173 step = st_hour;
187 len--;
188 max = 596523; 174 max = 596523;
189 scale = 60 * 60; 175 scale = 60 * 60;
190 break; 176 break;
191 177
192 case 'm': 178 case 'm':
193 if (step > st_hour) { 179 if (*p == 's') {
194 return NGX_ERROR;
195 }
196 step = st_min;
197 len--;
198 max = 35791394;
199 scale = 60;
200 break;
201
202 case 's':
203 len--;
204
205 if (line->data[i - 2] == 'm') {
206 if (sec || step > st_sec) { 180 if (sec || step > st_sec) {
207 return NGX_ERROR; 181 return NGX_ERROR;
208 } 182 }
183 p++;
209 step = st_msec; 184 step = st_msec;
210 len--;
211 max = 2147483647; 185 max = 2147483647;
212 scale = 1; 186 scale = 1;
213 break; 187 break;
214 } 188 }
215 189
190 if (step > st_hour) {
191 return NGX_ERROR;
192 }
193 step = st_min;
194 max = 35791394;
195 scale = 60;
196 break;
197
198 case 's':
216 if (step > st_min) { 199 if (step > st_min) {
217 return NGX_ERROR; 200 return NGX_ERROR;
218 } 201 }
219
220 step = st_sec; 202 step = st_sec;
221 max = 2147483647; 203 max = 2147483647;
222 scale = 1; 204 scale = 1;
223 break; 205 break;
224 206
225 default: 207 case ' ':
208 if (step > st_min) {
209 return NGX_ERROR;
210 }
226 step = st_last; 211 step = st_last;
227 max = 2147483647; 212 max = 2147483647;
228 scale = 1; 213 scale = 1;
229 } 214 break;
230 215
231 value = ngx_atoi(start, len); 216 default:
232 if (value == NGX_ERROR) {
233 return NGX_ERROR; 217 return NGX_ERROR;
234 } 218 }
235 219
236 if (step != st_msec && !sec) { 220 if (step != st_msec && !sec) {
237 scale *= 1000; 221 scale *= 1000;
238 max /= 1000; 222 max /= 1000;
239 } 223 }
240 224
241 if ((u_int) value > max) { 225 if ((ngx_uint_t) value > max) {
242 return NGX_PARSE_LARGE_TIME; 226 return NGX_ERROR;
243 } 227 }
244 228
245 total += value * scale; 229 total += value * scale;
246 230
247 if ((u_int) total > 2147483647) { 231 if ((ngx_uint_t) total > 2147483647) {
248 return NGX_PARSE_LARGE_TIME; 232 return NGX_ERROR;
249 } 233 }
250 234
251 if (i >= line->len) { 235 value = 0;
252 break; 236 scale = sec ? 1 : 1000;
253 } 237
254 238 while (p < last && *p == ' ') {
255 len = 0; 239 p++;
256 start = &line->data[i + 1]; 240 }
257 } 241 }
258 242
259 return total; 243 if (valid) {
244 return total + value * scale;
245 }
246
247 return NGX_ERROR;
260 } 248 }