comparison src/http/modules/ngx_http_browser_module.c @ 700:c1104f701926

ngx_http_browser_module
author Igor Sysoev <igor@sysoev.ru>
date Mon, 18 Sep 2006 12:25:07 +0000
parents
children e1d119bf5033
comparison
equal deleted inserted replaced
699:1f3cc64393b6 700:c1104f701926
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
12 /*
13 * The module can check browser versions conforming to the following formats:
14 * X, X.X, X.X.X, and X.X.X.X. The maximum values of each format may be
15 * 4000, 4000.99, 4000.99.99, and 4000.99.99.99.
16 */
17
18
19 typedef struct {
20 u_char browser[12];
21 size_t skip;
22 size_t add;
23 u_char name[12];
24 } ngx_http_modern_browser_mask_t;
25
26
27 typedef struct {
28 ngx_uint_t version;
29 size_t skip;
30 size_t add;
31 u_char name[12];
32 } ngx_http_modern_browser_t;
33
34
35 typedef struct {
36 ngx_str_t name;
37 ngx_http_get_variable_pt handler;
38 } ngx_http_browser_t;
39
40
41 typedef struct {
42 ngx_array_t *modern_browsers;
43 ngx_http_variable_value_t *value;
44 } ngx_http_browser_conf_t;
45
46
47 static ngx_int_t ngx_http_msie_variable(ngx_http_request_t *r,
48 ngx_http_variable_value_t *v, uintptr_t data);
49 static ngx_int_t ngx_http_browser_add_variable(ngx_conf_t *cf);
50 static void *ngx_http_browser_create_conf(ngx_conf_t *cf);
51 static char *ngx_http_browser_merge_conf(ngx_conf_t *cf, void *parent,
52 void *child);
53 static int ngx_libc_cdecl ngx_http_modern_browser_sort(const void *one,
54 const void *two);
55 static char *ngx_http_modern_browser(ngx_conf_t *cf, ngx_command_t *cmd,
56 void *conf);
57 static char *ngx_http_modern_browser_value(ngx_conf_t *cf, ngx_command_t *cmd,
58 void *conf);
59
60
61 static ngx_command_t ngx_http_browser_commands[] = {
62
63 { ngx_string("modern_browser"),
64 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
65 ngx_http_modern_browser,
66 NGX_HTTP_LOC_CONF_OFFSET,
67 0,
68 NULL },
69
70 { ngx_string("modern_browser_value"),
71 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
72 ngx_http_modern_browser_value,
73 NGX_HTTP_LOC_CONF_OFFSET,
74 0,
75 NULL },
76
77 ngx_null_command
78 };
79
80
81 static ngx_http_module_t ngx_http_browser_module_ctx = {
82 ngx_http_browser_add_variable, /* preconfiguration */
83 NULL, /* postconfiguration */
84
85 NULL, /* create main configuration */
86 NULL, /* init main configuration */
87
88 NULL, /* create server configuration */
89 NULL, /* merge server configuration */
90
91 ngx_http_browser_create_conf, /* create location configuration */
92 ngx_http_browser_merge_conf /* merge location configuration */
93 };
94
95
96 ngx_module_t ngx_http_browser_module = {
97 NGX_MODULE_V1,
98 &ngx_http_browser_module_ctx, /* module context */
99 ngx_http_browser_commands, /* module directives */
100 NGX_HTTP_MODULE, /* module type */
101 NULL, /* init master */
102 NULL, /* init module */
103 NULL, /* init process */
104 NULL, /* init thread */
105 NULL, /* exit thread */
106 NULL, /* exit process */
107 NULL, /* exit master */
108 NGX_MODULE_V1_PADDING
109 };
110
111
112 static ngx_http_modern_browser_mask_t ngx_http_modern_browser_masks[] = {
113
114 /* Opera must be the first browser to check */
115
116 /*
117 * "Opera/7.50 (X11; FreeBSD i386; U) [en]"
118 * "Mozilla/5.0 (X11; FreeBSD i386; U) Opera 7.50 [en]"
119 * "Mozilla/4.0 (compatible; MSIE 6.0; X11; FreeBSD i386) Opera 7.50 [en]"
120 * "Opera/8.0 (Windows NT 5.1; U; ru)"
121 * "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; en) Opera 8.0"
122 * "Opera/9.01 (X11; FreeBSD 6 i386; U; en)"
123 */
124
125 { "opera",
126 0,
127 sizeof("Opera ") - 1,
128 "Opera"},
129
130 /* "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" */
131
132 { "msie",
133 sizeof("Mozilla/4.0 (compatible; ") - 1,
134 sizeof("MSIE ") - 1,
135 "MSIE "},
136
137 /*
138 * "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020610"
139 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.5) Gecko/20031006"
140 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.6) Gecko/20040206
141 * Firefox/0.8"
142 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.7.8)
143 * Gecko/20050511 Firefox/1.0.4"
144 * "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.0.5) Gecko/20060729
145 * Firefox/1.5.0.5"
146 */
147
148 { "gecko",
149 sizeof("Mozilla/5.0 (") - 1,
150 sizeof("rv:") - 1,
151 "rv:"},
152
153 /*
154 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ru-ru) AppleWebKit/125.2
155 * (KHTML, like Gecko) Safari/125.7"
156 * "Mozilla/5.0 (SymbianOS/9.1; U; en-us) AppleWebKit/413
157 * (KHTML, like Gecko) Safari/413"
158 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418
159 * (KHTML, like Gecko) Safari/417.9.3"
160 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ru-ru) AppleWebKit/418.8
161 * (KHTML, like Gecko) Safari/419.3"
162 */
163
164 { "safari",
165 sizeof("Mozilla/5.0 (") - 1,
166 sizeof("Safari/") - 1,
167 "Safari/"},
168
169 /*
170 * "Mozilla/5.0 (compatible; Konqueror/3.1; Linux)"
171 * "Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.2 (like Gecko)"
172 * "Mozilla/5.0 (compatible; Konqueror/3.5; FreeBSD) KHTML/3.5.1
173 * (like Gecko)"
174 */
175
176 { "konqueror",
177 sizeof("Mozilla/5.0 (compatible; ") - 1,
178 sizeof("Konqueror/") - 1,
179 "Konqueror/"},
180
181 { "", 0, 0, "" }
182
183 };
184
185
186 static ngx_http_browser_t ngx_http_browsers[] = {
187 { ngx_string("msie"), ngx_http_msie_variable },
188 { ngx_null_string, NULL }
189 };
190
191
192 static ngx_int_t
193 ngx_http_browser_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
194 uintptr_t data)
195 {
196 u_char *name, *ua, *last, c;
197 ngx_uint_t i, version, ver, scale;
198 ngx_http_browser_conf_t *cf;
199 ngx_http_modern_browser_t *browsers;
200
201 cf = ngx_http_get_module_loc_conf(r, ngx_http_browser_module);
202
203 if (cf->modern_browsers == NULL || cf->value == NULL) {
204 *v = ngx_http_variable_null_value;
205 return NGX_OK;
206 }
207
208 #if 0
209 if (!r->headers_in.msie && !r->headers_in.opera
210 && !r->headers_in.gecko && !r->headers_in.konqueror)
211 {
212 *v = ngx_http_variable_null_value;
213 return NGX_OK;
214 }
215 #endif
216
217 if (r->headers_in.user_agent == NULL) {
218 *v = ngx_http_variable_null_value;
219 return NGX_OK;
220 }
221
222
223 ua = r->headers_in.user_agent->value.data;
224 last = ua + r->headers_in.user_agent->value.len;
225
226 browsers = cf->modern_browsers->elts;
227
228 for (i = 0; i < cf->modern_browsers->nelts; i++) {
229 name = ua + browsers[i].skip;
230
231 if (name >= last) {
232 continue;
233 }
234
235 name = (u_char *) ngx_strstr(name, browsers[i].name);
236
237 if (name == NULL) {
238 continue;
239 }
240
241 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
242 "browser: \"%s\"", name);
243
244 name += browsers[i].add;
245
246 if (name >= last) {
247 *v = ngx_http_variable_null_value;
248 }
249
250 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
251 "version: \"%ui\" \"%s\"", browsers[i].version, name);
252
253 version = 0;
254 ver = 0;
255 scale = 1000000;
256
257 while (name < last) {
258
259 c = *name++;
260
261 if (c >= '0' && c <= '9') {
262 ver = ver * 10 + (c - '0');
263 continue;
264 }
265
266 if (c == '.') {
267 version += ver * scale;
268
269 if (version > browsers[i].version) {
270 *v = *cf->value;
271 return NGX_OK;
272 }
273
274 ver = 0;
275 scale /= 100;
276 continue;
277 }
278
279 break;
280 }
281
282 version += ver * scale;
283
284 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
285 "version: \"%ui\" \"%ui\"",
286 browsers[i].version, version);
287
288 if (version >= browsers[i].version) {
289 *v = *cf->value;
290 return NGX_OK;
291 }
292 }
293
294 *v = ngx_http_variable_null_value;
295
296 return NGX_OK;
297 }
298
299
300 static ngx_int_t
301 ngx_http_msie_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
302 uintptr_t data)
303 {
304 if (r->headers_in.msie) {
305 *v = ngx_http_variable_true_value;
306
307 return NGX_OK;
308 }
309
310 *v = ngx_http_variable_null_value;
311
312 return NGX_OK;
313 }
314
315
316 static ngx_int_t
317 ngx_http_browser_add_variable(ngx_conf_t *cf)
318 {
319 ngx_http_browser_t *browser;
320 ngx_http_variable_t *var;
321
322 for (browser = ngx_http_browsers; browser->name.len; browser++) {
323
324 var = ngx_http_add_variable(cf, &browser->name, NGX_HTTP_VAR_CHANGABLE);
325 if (var == NULL) {
326 return NGX_ERROR;
327 }
328
329 var->get_handler = browser->handler;
330 }
331
332 return NGX_OK;
333 }
334
335
336 static void *
337 ngx_http_browser_create_conf(ngx_conf_t *cf)
338 {
339 ngx_http_browser_conf_t *conf;
340
341 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_browser_conf_t));
342 if (conf == NULL) {
343 return NGX_CONF_ERROR;
344 }
345
346 /*
347 * set by ngx_pcalloc():
348 *
349 * conf->browsers = NULL;
350 * conf->value = NULL;
351 */
352
353 return conf;
354 }
355
356
357 static char *
358 ngx_http_browser_merge_conf(ngx_conf_t *cf, void *parent, void *child)
359 {
360 ngx_http_browser_conf_t *prev = parent;
361 ngx_http_browser_conf_t *conf = child;
362
363 ngx_uint_t i, n;
364 ngx_http_modern_browser_t *browsers, *opera;
365
366 /*
367 * At the merge the skip field is used to store the browser slot,
368 * it will be used in sorting and then will overwritten
369 * with a real skip value. The zero value means Opera.
370 */
371
372 if (conf->modern_browsers == NULL) {
373 conf->modern_browsers = prev->modern_browsers;
374
375 } else {
376 browsers = conf->modern_browsers->elts;
377
378 for (i = 0; i < conf->modern_browsers->nelts; i++) {
379 if (browsers[i].skip == 0) {
380 goto found;
381 }
382 }
383
384 /*
385 * Opera may contain MSIE string, so if Opera was not enumerated
386 * as modern browsers, then add it and set a unreachable version
387 */
388
389 opera = ngx_array_push(conf->modern_browsers);
390 if (opera == NULL) {
391 return NGX_CONF_ERROR;
392 }
393
394 opera->skip = 0;
395 opera->version = 4001000000U;
396
397 browsers = conf->modern_browsers->elts;
398
399 found:
400
401 ngx_qsort(browsers, (size_t) conf->modern_browsers->nelts,
402 sizeof(ngx_http_modern_browser_t),
403 ngx_http_modern_browser_sort);
404
405 for (i = 0; i < conf->modern_browsers->nelts; i++) {
406 n = browsers[i].skip;
407
408 browsers[i].skip = ngx_http_modern_browser_masks[n].skip;
409 browsers[i].add = ngx_http_modern_browser_masks[n].add;
410 (void) ngx_cpystrn(browsers[i].name,
411 ngx_http_modern_browser_masks[n].name, 12);
412 }
413 }
414
415 if (conf->value == NULL) {
416 conf->value = prev->value;
417 }
418
419 return NGX_CONF_OK;
420 }
421
422
423 static int ngx_libc_cdecl
424 ngx_http_modern_browser_sort(const void *one, const void *two)
425 {
426 ngx_http_modern_browser_t *first = (ngx_http_modern_browser_t *) one;
427 ngx_http_modern_browser_t *second = (ngx_http_modern_browser_t *) two;
428
429 return (first->skip - second->skip);
430 }
431
432
433 static char *
434 ngx_http_modern_browser(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
435 {
436 ngx_http_browser_conf_t *bcf = conf;
437
438 u_char c;
439 ngx_str_t *value;
440 ngx_uint_t i, n, version, ver, scale;
441 ngx_http_modern_browser_t *browser;
442 ngx_http_modern_browser_mask_t *mask;
443
444 if (bcf->modern_browsers == NULL) {
445 bcf->modern_browsers = ngx_array_create(cf->pool, 4,
446 sizeof(ngx_http_modern_browser_t));
447 if (bcf->modern_browsers == NULL) {
448 return NGX_CONF_ERROR;
449 }
450 }
451
452 browser = ngx_array_push(bcf->modern_browsers);
453 if (browser == NULL) {
454 return NGX_CONF_ERROR;
455 }
456
457 value = cf->args->elts;
458
459 mask = ngx_http_modern_browser_masks;
460
461 for (n = 0; mask[n].browser[0] != '\0'; n++) {
462 if (ngx_strcasecmp(mask[n].browser, value[1].data) == 0) {
463 goto found;
464 }
465 }
466
467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
468 "unknown browser name \"%V\"", &value[1]);
469
470 return NGX_CONF_ERROR;
471
472 found:
473
474 /*
475 * at this stage the skip field is used to store the browser slot,
476 * it will be used in sorting in merge stage and then will overwritten
477 * with a real value
478 */
479
480 browser->skip = n;
481
482 version = 0;
483 ver = 0;
484 scale = 1000000;
485
486 for (i = 0; i < value[2].len; i++) {
487
488 c = value[2].data[i];
489
490 if (c >= '0' && c <= '9') {
491 ver = ver * 10 + (c - '0');
492 continue;
493 }
494
495 if (c == '.') {
496 version += ver * scale;
497 ver = 0;
498 scale /= 100;
499 continue;
500 }
501
502 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
503 "invalid browser version \"%V\"", &value[2]);
504
505 return NGX_CONF_ERROR;
506 }
507
508 version += ver * scale;
509
510 ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0,
511 "browser version \"%V\", %ui", &value[2], version);
512
513 browser->version = version;
514
515 return NGX_CONF_OK;
516 }
517
518
519 static char *
520 ngx_http_modern_browser_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
521 {
522 ngx_http_browser_conf_t *bcf = conf;
523
524 ngx_str_t *value, name;
525 ngx_http_variable_t *var;
526
527 value = cf->args->elts;
528
529 name.len = sizeof("browser") - 1;
530 name.data = (u_char *) "browser";
531
532 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGABLE);
533 if (var == NULL) {
534 return NGX_CONF_ERROR;
535 }
536
537 var->get_handler = ngx_http_browser_variable;
538
539 bcf->value = ngx_palloc(cf->pool, sizeof(ngx_http_variable_value_t));
540 if (bcf->value == NULL) {
541 return NGX_CONF_ERROR;
542 }
543
544 bcf->value->len = value[1].len;
545 bcf->value->valid = 1;
546 bcf->value->no_cachable = 0;
547 bcf->value->not_found = 0;
548 bcf->value->data = value[1].data;
549
550 return NGX_CONF_OK;
551 }