nginx 1.1.11
[nginx.git] / src / http / modules / ngx_http_browser_module.c
blobd400fec7fd6a40fb3e5b5263d79798cf3b95548b
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
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.
19 #define NGX_HTTP_MODERN_BROWSER 0
20 #define NGX_HTTP_ANCIENT_BROWSER 1
23 typedef struct {
24 u_char browser[12];
25 size_t skip;
26 size_t add;
27 u_char name[12];
28 } ngx_http_modern_browser_mask_t;
31 typedef struct {
32 ngx_uint_t version;
33 size_t skip;
34 size_t add;
35 u_char name[12];
36 } ngx_http_modern_browser_t;
39 typedef struct {
40 ngx_str_t name;
41 ngx_http_get_variable_pt handler;
42 uintptr_t data;
43 } ngx_http_browser_variable_t;
46 typedef struct {
47 ngx_array_t *modern_browsers;
48 ngx_array_t *ancient_browsers;
49 ngx_http_variable_value_t *modern_browser_value;
50 ngx_http_variable_value_t *ancient_browser_value;
52 unsigned modern_unlisted_browsers:1;
53 unsigned netscape4:1;
54 } ngx_http_browser_conf_t;
57 static ngx_int_t ngx_http_msie_variable(ngx_http_request_t *r,
58 ngx_http_variable_value_t *v, uintptr_t data);
59 static ngx_int_t ngx_http_browser_variable(ngx_http_request_t *r,
60 ngx_http_variable_value_t *v, uintptr_t data);
62 static ngx_uint_t ngx_http_browser(ngx_http_request_t *r,
63 ngx_http_browser_conf_t *cf);
65 static ngx_int_t ngx_http_browser_add_variable(ngx_conf_t *cf);
66 static void *ngx_http_browser_create_conf(ngx_conf_t *cf);
67 static char *ngx_http_browser_merge_conf(ngx_conf_t *cf, void *parent,
68 void *child);
69 static int ngx_libc_cdecl ngx_http_modern_browser_sort(const void *one,
70 const void *two);
71 static char *ngx_http_modern_browser(ngx_conf_t *cf, ngx_command_t *cmd,
72 void *conf);
73 static char *ngx_http_ancient_browser(ngx_conf_t *cf, ngx_command_t *cmd,
74 void *conf);
75 static char *ngx_http_modern_browser_value(ngx_conf_t *cf, ngx_command_t *cmd,
76 void *conf);
77 static char *ngx_http_ancient_browser_value(ngx_conf_t *cf, ngx_command_t *cmd,
78 void *conf);
81 static ngx_command_t ngx_http_browser_commands[] = {
83 { ngx_string("modern_browser"),
84 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
85 ngx_http_modern_browser,
86 NGX_HTTP_LOC_CONF_OFFSET,
88 NULL },
90 { ngx_string("ancient_browser"),
91 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
92 ngx_http_ancient_browser,
93 NGX_HTTP_LOC_CONF_OFFSET,
95 NULL },
97 { ngx_string("modern_browser_value"),
98 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
99 ngx_http_modern_browser_value,
100 NGX_HTTP_LOC_CONF_OFFSET,
102 NULL },
104 { ngx_string("ancient_browser_value"),
105 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
106 ngx_http_ancient_browser_value,
107 NGX_HTTP_LOC_CONF_OFFSET,
109 NULL },
111 ngx_null_command
115 static ngx_http_module_t ngx_http_browser_module_ctx = {
116 ngx_http_browser_add_variable, /* preconfiguration */
117 NULL, /* postconfiguration */
119 NULL, /* create main configuration */
120 NULL, /* init main configuration */
122 NULL, /* create server configuration */
123 NULL, /* merge server configuration */
125 ngx_http_browser_create_conf, /* create location configuration */
126 ngx_http_browser_merge_conf /* merge location configuration */
130 ngx_module_t ngx_http_browser_module = {
131 NGX_MODULE_V1,
132 &ngx_http_browser_module_ctx, /* module context */
133 ngx_http_browser_commands, /* module directives */
134 NGX_HTTP_MODULE, /* module type */
135 NULL, /* init master */
136 NULL, /* init module */
137 NULL, /* init process */
138 NULL, /* init thread */
139 NULL, /* exit thread */
140 NULL, /* exit process */
141 NULL, /* exit master */
142 NGX_MODULE_V1_PADDING
146 static ngx_http_modern_browser_mask_t ngx_http_modern_browser_masks[] = {
148 /* Opera must be the first browser to check */
151 * "Opera/7.50 (X11; FreeBSD i386; U) [en]"
152 * "Mozilla/5.0 (X11; FreeBSD i386; U) Opera 7.50 [en]"
153 * "Mozilla/4.0 (compatible; MSIE 6.0; X11; FreeBSD i386) Opera 7.50 [en]"
154 * "Opera/8.0 (Windows NT 5.1; U; ru)"
155 * "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; en) Opera 8.0"
156 * "Opera/9.01 (X11; FreeBSD 6 i386; U; en)"
159 { "opera",
161 sizeof("Opera ") - 1,
162 "Opera"},
164 /* "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" */
166 { "msie",
167 sizeof("Mozilla/4.0 (compatible; ") - 1,
168 sizeof("MSIE ") - 1,
169 "MSIE "},
172 * "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020610"
173 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.5) Gecko/20031006"
174 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.6) Gecko/20040206
175 * Firefox/0.8"
176 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.7.8)
177 * Gecko/20050511 Firefox/1.0.4"
178 * "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.0.5) Gecko/20060729
179 * Firefox/1.5.0.5"
182 { "gecko",
183 sizeof("Mozilla/5.0 (") - 1,
184 sizeof("rv:") - 1,
185 "rv:"},
188 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ru-ru) AppleWebKit/125.2
189 * (KHTML, like Gecko) Safari/125.7"
190 * "Mozilla/5.0 (SymbianOS/9.1; U; en-us) AppleWebKit/413
191 * (KHTML, like Gecko) Safari/413"
192 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418
193 * (KHTML, like Gecko) Safari/417.9.3"
194 * "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ru-ru) AppleWebKit/418.8
195 * (KHTML, like Gecko) Safari/419.3"
198 { "safari",
199 sizeof("Mozilla/5.0 (") - 1,
200 sizeof("Safari/") - 1,
201 "Safari/"},
204 * "Mozilla/5.0 (compatible; Konqueror/3.1; Linux)"
205 * "Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.2 (like Gecko)"
206 * "Mozilla/5.0 (compatible; Konqueror/3.5; FreeBSD) KHTML/3.5.1
207 * (like Gecko)"
210 { "konqueror",
211 sizeof("Mozilla/5.0 (compatible; ") - 1,
212 sizeof("Konqueror/") - 1,
213 "Konqueror/"},
215 { "", 0, 0, "" }
220 static ngx_http_browser_variable_t ngx_http_browsers[] = {
221 { ngx_string("msie"), ngx_http_msie_variable, 0 },
222 { ngx_string("modern_browser"), ngx_http_browser_variable,
223 NGX_HTTP_MODERN_BROWSER },
224 { ngx_string("ancient_browser"), ngx_http_browser_variable,
225 NGX_HTTP_ANCIENT_BROWSER },
226 { ngx_null_string, NULL, 0 }
230 static ngx_int_t
231 ngx_http_browser_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
232 uintptr_t data)
234 ngx_uint_t rc;
235 ngx_http_browser_conf_t *cf;
237 cf = ngx_http_get_module_loc_conf(r, ngx_http_browser_module);
239 rc = ngx_http_browser(r, cf);
241 if (data == NGX_HTTP_MODERN_BROWSER && rc == NGX_HTTP_MODERN_BROWSER) {
242 *v = *cf->modern_browser_value;
243 return NGX_OK;
246 if (data == NGX_HTTP_ANCIENT_BROWSER && rc == NGX_HTTP_ANCIENT_BROWSER) {
247 *v = *cf->ancient_browser_value;
248 return NGX_OK;
251 *v = ngx_http_variable_null_value;
252 return NGX_OK;
256 static ngx_uint_t
257 ngx_http_browser(ngx_http_request_t *r, ngx_http_browser_conf_t *cf)
259 size_t len;
260 u_char *name, *ua, *last, c;
261 ngx_str_t *ancient;
262 ngx_uint_t i, version, ver, scale;
263 ngx_http_modern_browser_t *modern;
265 if (r->headers_in.user_agent == NULL) {
266 if (cf->modern_unlisted_browsers) {
267 return NGX_HTTP_MODERN_BROWSER;
270 return NGX_HTTP_ANCIENT_BROWSER;
273 ua = r->headers_in.user_agent->value.data;
274 len = r->headers_in.user_agent->value.len;
275 last = ua + len;
277 if (cf->modern_browsers) {
278 modern = cf->modern_browsers->elts;
280 for (i = 0; i < cf->modern_browsers->nelts; i++) {
281 name = ua + modern[i].skip;
283 if (name >= last) {
284 continue;
287 name = (u_char *) ngx_strstr(name, modern[i].name);
289 if (name == NULL) {
290 continue;
293 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
294 "browser: \"%s\"", name);
296 name += modern[i].add;
298 if (name >= last) {
299 continue;
302 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
303 "version: \"%ui\" \"%s\"", modern[i].version, name);
305 version = 0;
306 ver = 0;
307 scale = 1000000;
309 while (name < last) {
311 c = *name++;
313 if (c >= '0' && c <= '9') {
314 ver = ver * 10 + (c - '0');
315 continue;
318 if (c == '.') {
319 version += ver * scale;
321 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
322 "version: \"%ui\" \"%ui\"",
323 modern[i].version, version);
325 if (version > modern[i].version) {
326 return NGX_HTTP_MODERN_BROWSER;
329 ver = 0;
330 scale /= 100;
331 continue;
334 break;
337 version += ver * scale;
339 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
340 "version: \"%ui\" \"%ui\"",
341 modern[i].version, version);
343 if (version >= modern[i].version) {
344 return NGX_HTTP_MODERN_BROWSER;
347 return NGX_HTTP_ANCIENT_BROWSER;
350 if (!cf->modern_unlisted_browsers) {
351 return NGX_HTTP_ANCIENT_BROWSER;
355 if (cf->netscape4) {
356 if (len > sizeof("Mozilla/4.72 ") - 1
357 && ngx_strncmp(ua, "Mozilla/", sizeof("Mozilla/") - 1) == 0
358 && ua[8] > '0' && ua[8] < '5')
360 return NGX_HTTP_ANCIENT_BROWSER;
364 if (cf->ancient_browsers) {
365 ancient = cf->ancient_browsers->elts;
367 for (i = 0; i < cf->ancient_browsers->nelts; i++) {
368 if (len >= ancient[i].len
369 && ngx_strstr(ua, ancient[i].data) != NULL)
371 return NGX_HTTP_ANCIENT_BROWSER;
376 if (cf->modern_unlisted_browsers) {
377 return NGX_HTTP_MODERN_BROWSER;
380 return NGX_HTTP_ANCIENT_BROWSER;
384 static ngx_int_t
385 ngx_http_msie_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
386 uintptr_t data)
388 if (r->headers_in.msie) {
389 *v = ngx_http_variable_true_value;
390 return NGX_OK;
393 *v = ngx_http_variable_null_value;
394 return NGX_OK;
398 static ngx_int_t
399 ngx_http_browser_add_variable(ngx_conf_t *cf)
401 ngx_http_browser_variable_t *var;
402 ngx_http_variable_t *v;
404 for (var = ngx_http_browsers; var->name.len; var++) {
406 v = ngx_http_add_variable(cf, &var->name, NGX_HTTP_VAR_CHANGEABLE);
407 if (v == NULL) {
408 return NGX_ERROR;
411 v->get_handler = var->handler;
412 v->data = var->data;
415 return NGX_OK;
419 static void *
420 ngx_http_browser_create_conf(ngx_conf_t *cf)
422 ngx_http_browser_conf_t *conf;
424 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_browser_conf_t));
425 if (conf == NULL) {
426 return NULL;
430 * set by ngx_pcalloc():
432 * conf->modern_browsers = NULL;
433 * conf->ancient_browsers = NULL;
434 * conf->modern_browser_value = NULL;
435 * conf->ancient_browser_value = NULL;
437 * conf->modern_unlisted_browsers = 0;
438 * conf->netscape4 = 0;
441 return conf;
445 static char *
446 ngx_http_browser_merge_conf(ngx_conf_t *cf, void *parent, void *child)
448 ngx_http_browser_conf_t *prev = parent;
449 ngx_http_browser_conf_t *conf = child;
451 ngx_uint_t i, n;
452 ngx_http_modern_browser_t *browsers, *opera;
455 * At the merge the skip field is used to store the browser slot,
456 * it will be used in sorting and then will overwritten
457 * with a real skip value. The zero value means Opera.
460 if (conf->modern_browsers == NULL) {
461 conf->modern_browsers = prev->modern_browsers;
463 } else {
464 browsers = conf->modern_browsers->elts;
466 for (i = 0; i < conf->modern_browsers->nelts; i++) {
467 if (browsers[i].skip == 0) {
468 goto found;
473 * Opera may contain MSIE string, so if Opera was not enumerated
474 * as modern browsers, then add it and set a unreachable version
477 opera = ngx_array_push(conf->modern_browsers);
478 if (opera == NULL) {
479 return NGX_CONF_ERROR;
482 opera->skip = 0;
483 opera->version = 4001000000U;
485 browsers = conf->modern_browsers->elts;
487 found:
489 ngx_qsort(browsers, (size_t) conf->modern_browsers->nelts,
490 sizeof(ngx_http_modern_browser_t),
491 ngx_http_modern_browser_sort);
493 for (i = 0; i < conf->modern_browsers->nelts; i++) {
494 n = browsers[i].skip;
496 browsers[i].skip = ngx_http_modern_browser_masks[n].skip;
497 browsers[i].add = ngx_http_modern_browser_masks[n].add;
498 (void) ngx_cpystrn(browsers[i].name,
499 ngx_http_modern_browser_masks[n].name, 12);
503 if (conf->ancient_browsers == NULL) {
504 conf->ancient_browsers = prev->ancient_browsers;
507 if (conf->modern_browser_value == NULL) {
508 conf->modern_browser_value = prev->modern_browser_value;
511 if (conf->modern_browser_value == NULL) {
512 conf->modern_browser_value = &ngx_http_variable_true_value;
515 if (conf->ancient_browser_value == NULL) {
516 conf->ancient_browser_value = prev->ancient_browser_value;
519 if (conf->ancient_browser_value == NULL) {
520 conf->ancient_browser_value = &ngx_http_variable_true_value;
523 return NGX_CONF_OK;
527 static int ngx_libc_cdecl
528 ngx_http_modern_browser_sort(const void *one, const void *two)
530 ngx_http_modern_browser_t *first = (ngx_http_modern_browser_t *) one;
531 ngx_http_modern_browser_t *second = (ngx_http_modern_browser_t *) two;
533 return (first->skip - second->skip);
537 static char *
538 ngx_http_modern_browser(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
540 ngx_http_browser_conf_t *bcf = conf;
542 u_char c;
543 ngx_str_t *value;
544 ngx_uint_t i, n, version, ver, scale;
545 ngx_http_modern_browser_t *browser;
546 ngx_http_modern_browser_mask_t *mask;
548 value = cf->args->elts;
550 if (cf->args->nelts == 2) {
551 if (ngx_strcmp(value[1].data, "unlisted") == 0) {
552 bcf->modern_unlisted_browsers = 1;
553 return NGX_CONF_OK;
556 return NGX_CONF_ERROR;
559 if (bcf->modern_browsers == NULL) {
560 bcf->modern_browsers = ngx_array_create(cf->pool, 5,
561 sizeof(ngx_http_modern_browser_t));
562 if (bcf->modern_browsers == NULL) {
563 return NGX_CONF_ERROR;
567 browser = ngx_array_push(bcf->modern_browsers);
568 if (browser == NULL) {
569 return NGX_CONF_ERROR;
572 mask = ngx_http_modern_browser_masks;
574 for (n = 0; mask[n].browser[0] != '\0'; n++) {
575 if (ngx_strcasecmp(mask[n].browser, value[1].data) == 0) {
576 goto found;
580 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
581 "unknown browser name \"%V\"", &value[1]);
583 return NGX_CONF_ERROR;
585 found:
588 * at this stage the skip field is used to store the browser slot,
589 * it will be used in sorting in merge stage and then will overwritten
590 * with a real value
593 browser->skip = n;
595 version = 0;
596 ver = 0;
597 scale = 1000000;
599 for (i = 0; i < value[2].len; i++) {
601 c = value[2].data[i];
603 if (c >= '0' && c <= '9') {
604 ver = ver * 10 + (c - '0');
605 continue;
608 if (c == '.') {
609 version += ver * scale;
610 ver = 0;
611 scale /= 100;
612 continue;
615 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
616 "invalid browser version \"%V\"", &value[2]);
618 return NGX_CONF_ERROR;
621 version += ver * scale;
623 browser->version = version;
625 return NGX_CONF_OK;
629 static char *
630 ngx_http_ancient_browser(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
632 ngx_http_browser_conf_t *bcf = conf;
634 ngx_str_t *value, *browser;
635 ngx_uint_t i;
637 value = cf->args->elts;
639 for (i = 1; i < cf->args->nelts; i++) {
640 if (ngx_strcmp(value[i].data, "netscape4") == 0) {
641 bcf->netscape4 = 1;
642 continue;
645 if (bcf->ancient_browsers == NULL) {
646 bcf->ancient_browsers = ngx_array_create(cf->pool, 4,
647 sizeof(ngx_str_t));
648 if (bcf->ancient_browsers == NULL) {
649 return NGX_CONF_ERROR;
653 browser = ngx_array_push(bcf->ancient_browsers);
654 if (browser == NULL) {
655 return NGX_CONF_ERROR;
658 *browser = value[i];
661 return NGX_CONF_OK;
665 static char *
666 ngx_http_modern_browser_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
668 ngx_http_browser_conf_t *bcf = conf;
670 ngx_str_t *value;
672 bcf->modern_browser_value = ngx_palloc(cf->pool,
673 sizeof(ngx_http_variable_value_t));
674 if (bcf->modern_browser_value == NULL) {
675 return NGX_CONF_ERROR;
678 value = cf->args->elts;
680 bcf->modern_browser_value->len = value[1].len;
681 bcf->modern_browser_value->valid = 1;
682 bcf->modern_browser_value->no_cacheable = 0;
683 bcf->modern_browser_value->not_found = 0;
684 bcf->modern_browser_value->data = value[1].data;
686 return NGX_CONF_OK;
690 static char *
691 ngx_http_ancient_browser_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
693 ngx_http_browser_conf_t *bcf = conf;
695 ngx_str_t *value;
697 bcf->ancient_browser_value = ngx_palloc(cf->pool,
698 sizeof(ngx_http_variable_value_t));
699 if (bcf->ancient_browser_value == NULL) {
700 return NGX_CONF_ERROR;
703 value = cf->args->elts;
705 bcf->ancient_browser_value->len = value[1].len;
706 bcf->ancient_browser_value->valid = 1;
707 bcf->ancient_browser_value->no_cacheable = 0;
708 bcf->ancient_browser_value->not_found = 0;
709 bcf->ancient_browser_value->data = value[1].data;
711 return NGX_CONF_OK;