Skip to content

Instantly share code, notes, and snippets.

@agentzh
Created April 24, 2013 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agentzh/5455071 to your computer and use it in GitHub Desktop.
Save agentzh/5455071 to your computer and use it in GitHub Desktop.
Matthieu's original patch for built-in-header-set
diff --git a/src/ngx_http_lua_headers_in.c b/src/ngx_http_lua_headers_in.c
index 9dd5a83..425005c 100644
--- a/src/ngx_http_lua_headers_in.c
+++ b/src/ngx_http_lua_headers_in.c
@@ -23,6 +23,12 @@ static ngx_int_t ngx_http_set_header_helper(ngx_http_request_t *r,
ngx_table_elt_t **output_header, unsigned no_create);
static ngx_int_t ngx_http_set_builtin_header(ngx_http_request_t *r,
ngx_http_lua_header_val_t *hv, ngx_str_t *value);
+static ngx_int_t ngx_http_set_connection(ngx_http_request_t *r,
+ ngx_http_lua_header_val_t *hv, ngx_str_t *value);
+static ngx_int_t ngx_http_set_user_agent(ngx_http_request_t *r,
+ ngx_http_lua_header_val_t *hv, ngx_str_t *value);
+static ngx_int_t ngx_http_set_cookie(ngx_http_request_t *r,
+ ngx_http_lua_header_val_t *hv, ngx_str_t *value);
static ngx_int_t ngx_http_set_content_length_header(ngx_http_request_t *r,
ngx_http_lua_header_val_t *hv, ngx_str_t *value);
static ngx_int_t ngx_http_clear_builtin_header(ngx_http_request_t *r,
@@ -49,7 +55,7 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
{ ngx_string("Connection"),
offsetof(ngx_http_headers_in_t, connection),
- ngx_http_set_builtin_header },
+ ngx_http_set_connection },
{ ngx_string("If-Modified-Since"),
offsetof(ngx_http_headers_in_t, if_modified_since),
@@ -57,7 +63,7 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
{ ngx_string("User-Agent"),
offsetof(ngx_http_headers_in_t, user_agent),
- ngx_http_set_builtin_header },
+ ngx_http_set_user_agent },
{ ngx_string("Referer"),
offsetof(ngx_http_headers_in_t, referer),
@@ -95,6 +101,16 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
offsetof(ngx_http_headers_in_t, content_length),
ngx_http_set_content_length_header },
+#if (NGX_HTTP_REALIP)
+ { ngx_string("X-Real-IP"),
+ offsetof(ngx_http_headers_in_t, x_real_ip),
+ ngx_http_set_builtin_header },
+#endif
+
+ { ngx_string("Cookie"),
+ offsetof(ngx_http_headers_in_t, cookies),
+ ngx_http_set_cookie },
+
{ ngx_null_string, 0, ngx_http_set_header }
};
@@ -255,6 +271,164 @@ ngx_http_set_builtin_header(ngx_http_request_t *r,
static ngx_int_t
+ngx_http_set_connection(ngx_http_request_t *r,
+ ngx_http_lua_header_val_t *hv, ngx_str_t *value)
+{
+ if (ngx_strcasestrn(value->data, "close", 5 - 1)) {
+ r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
+
+ } else if (ngx_strcasestrn(value->data, "keep-alive", 10 - 1)) {
+ r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
+ }
+
+ return ngx_http_set_builtin_header(r, hv, value);
+}
+
+
+/* inspired from ngx_http_request.c:ngx_http_process_user_agent() */
+static ngx_int_t
+ngx_http_set_user_agent(ngx_http_request_t *r,
+ ngx_http_lua_header_val_t *hv, ngx_str_t *value)
+{
+ u_char *user_agent, *msie;
+
+ /* clear previous settings */
+ r->headers_in.msie = 0;
+ r->headers_in.msie6 = 0;
+ r->headers_in.opera = 0;
+ r->headers_in.gecko = 0;
+ r->headers_in.chrome = 0;
+ r->headers_in.safari = 0;
+ r->headers_in.konqueror = 0;
+
+ if (value->len == 0) {
+ return ngx_http_set_builtin_header(r, hv, value);
+ }
+
+ /* check some widespread browsers while the header is in CPU cache */
+
+ user_agent = value->data;
+
+ msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
+
+ if (msie && msie + 7 < user_agent + value->len) {
+
+ r->headers_in.msie = 1;
+
+ if (msie[6] == '.') {
+
+ switch (msie[5]) {
+ case '4':
+ case '5':
+ r->headers_in.msie6 = 1;
+ break;
+ case '6':
+ if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
+ r->headers_in.msie6 = 1;
+ }
+ break;
+ }
+ }
+ }
+
+ if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
+ r->headers_in.opera = 1;
+ r->headers_in.msie = 0;
+ r->headers_in.msie6 = 0;
+ }
+
+ if (!r->headers_in.msie && !r->headers_in.opera) {
+
+ if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
+ r->headers_in.gecko = 1;
+
+ } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
+ r->headers_in.chrome = 1;
+
+ } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)
+ && ngx_strstrn(user_agent, "Mac OS X", 8 - 1))
+ {
+ r->headers_in.safari = 1;
+
+ } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
+ r->headers_in.konqueror = 1;
+ }
+ }
+
+ return ngx_http_set_builtin_header(r, hv, value);
+}
+
+
+static ngx_int_t
+ngx_http_set_cookie(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
+ ngx_str_t *value)
+{
+ ngx_str_t cookie_name;
+ ngx_table_elt_t *cookie, **cookies, **new_cookie;
+ u_char *p;
+ ngx_uint_t i;
+
+ if (hv->no_override) {
+ goto new_cookie;
+ }
+
+ /* extract name of the cookie */
+ p = ngx_strstrn(value->data, "=", 0);
+ if (p != 0) {
+
+ cookie_name.data = value->data;
+ cookie_name.len = p - value->data;
+
+ cookies = r->headers_in.cookies.elts;
+
+ for (i = 0; i < r->headers_in.cookies.nelts; i++) {
+
+ if (ngx_strncasecmp(cookies[i]->value.data,
+ cookie_name.data, cookie_name.len)
+ == 0) {
+
+ /* cookie found, replace value. */
+ cookies[i]->value = *value;
+
+ return NGX_OK;
+ }
+ }
+ }
+
+ /* not found, insert new cookie. */
+
+new_cookie:
+
+ cookie = ngx_palloc(r->pool, sizeof (ngx_table_elt_t));
+ if (cookie == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ cookie->key = hv->key;
+ cookie->hash = hv->hash;
+ cookie->value = *value;
+
+#if 1
+ cookie->lowcase_key = ngx_pnalloc(r->pool, cookie->key.len);
+ if (cookie->lowcase_key == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ ngx_strlow(cookie->lowcase_key, cookie->key.data, cookie->key.len);
+#endif
+
+ new_cookie = ngx_array_push(&r->headers_in.cookies);
+ if (new_cookie == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ *new_cookie = cookie;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
ngx_http_set_host_header(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
ngx_str_t *value)
{
diff --git a/t/111-req-header-ua.t b/t/111-req-header-ua.t
new file mode 100644
index 0000000..4b1d39c
--- /dev/null
+++ b/t/111-req-header-ua.t
@@ -0,0 +1,677 @@
+# vim:set ft= ts=4 sw=4 et fdm=marker:
+
+use lib 'lib';
+use Test::Nginx::Socket;
+
+#worker_connections(1014);
+#master_process_enabled(1);
+#log_level('warn');
+
+repeat_each(2);
+
+plan tests => repeat_each() * (4 * blocks());
+
+#no_diff();
+no_long_string();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: clear Opera user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Opera/9.80 (Macintosh; Intel Mac OS X 10.7.4; U; en) Presto/2.10.229 Version/11.62
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: opera: %d\n", $r->headers_in->opera)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: opera: %d\n", $r->headers_in->opera)
+}
+
+--- stap_out
+rewrite: opera: 1
+content: opera: 0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: clear MSIE 4 user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0)
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=1 msie6=1
+content: msie=0 msie6=0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: set custom MSIE 4 user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=0 msie6=0
+content: msie=1 msie6=1
+
+--- response_body
+User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0)
+--- no_error_log
+[error]
+
+
+
+=== TEST 4: clear MSIE 5 user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows 95; MSIECrawler)
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=1 msie6=1
+content: msie=0 msie6=0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 5: set custom MSIE 5 user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.01; Windows 95; MSIECrawler)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=0 msie6=0
+content: msie=1 msie6=1
+
+--- response_body
+User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows 95; MSIECrawler)
+--- no_error_log
+[error]
+
+
+
+=== TEST 6: clear MSIE 6 (without SV1) user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Google Wireless Transcoder;)
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=1 msie6=1
+content: msie=0 msie6=0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 7: set custom MSIE 6 (without SV1) user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Google Wireless Transcoder;)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=0 msie6=0
+content: msie=1 msie6=1
+
+--- response_body
+User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Google Wireless Transcoder;)
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: clear MSIE 6 (with SV1) user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=1 msie6=0
+content: msie=0 msie6=0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: set custom MSIE 6 (with SV1) user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=0 msie6=0
+content: msie=1 msie6=0
+
+--- response_body
+User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: set custom MSIE 7 user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; winfx; .NET CLR 1.1.4322; .NET CLR 2.0.50727; Zune 2.0)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+F(ngx_http_core_content_phase) {
+ printf("content: msie=%d msie6=%d\n",
+ $r->headers_in->msie,
+ $r->headers_in->msie6)
+}
+
+--- stap_out
+rewrite: msie=0 msie6=0
+content: msie=1 msie6=0
+
+--- response_body
+User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; winfx; .NET CLR 1.1.4322; .NET CLR 2.0.50727; Zune 2.0)
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: clear Gecko user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: gecko: %d\n", $r->headers_in->gecko)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: gecko: %d\n", $r->headers_in->gecko)
+}
+
+--- stap_out
+rewrite: gecko: 1
+content: gecko: 0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: set custom Gecko user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0")
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: gecko: %d\n", $r->headers_in->gecko)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: gecko: %d\n", $r->headers_in->gecko)
+}
+
+--- stap_out
+rewrite: gecko: 0
+content: gecko: 1
+
+--- response_body
+User-Agent: Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: clear Chrome user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: chrome: %d\n", $r->headers_in->chrome)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: chrome: %d\n", $r->headers_in->chrome)
+}
+
+--- stap_out
+rewrite: chrome: 1
+content: chrome: 0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: set custom Chrome user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19")
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: chrome: %d\n", $r->headers_in->chrome)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: chrome: %d\n", $r->headers_in->chrome)
+}
+
+--- stap_out
+rewrite: chrome: 0
+content: chrome: 1
+
+--- response_body
+User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19
+--- no_error_log
+[error]
+
+
+
+=== TEST 15: clear Safari (Mac OS X) user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: safari: %d\n", $r->headers_in->safari)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: safari: %d\n", $r->headers_in->safari)
+}
+
+--- stap_out
+rewrite: safari: 1
+content: safari: 0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 16: set custom Safari user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: safari: %d\n", $r->headers_in->safari)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: safari: %d\n", $r->headers_in->safari)
+}
+
+--- stap_out
+rewrite: safari: 0
+content: safari: 1
+
+--- response_body
+User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8
+--- no_error_log
+[error]
+
+
+
+=== TEST 17: clear Konqueror user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", nil)
+
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- more_headers
+User-Agent: Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.10 (like Gecko) (Kubuntu)
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: konqueror: %d\n", $r->headers_in->konqueror)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: konqueror: %d\n", $r->headers_in->konqueror)
+}
+
+--- stap_out
+rewrite: konqueror: 1
+content: konqueror: 0
+
+--- response_body
+User-Agent:
+--- no_error_log
+[error]
+
+
+
+=== TEST 18: set custom Konqueror user-agent
+--- config
+ location /t {
+ rewrite_by_lua '
+ ngx.req.set_header("User-Agent", "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.10 (like Gecko) (Kubuntu)")
+ ';
+ echo "User-Agent: $http_user_agent";
+ }
+
+--- request
+GET /t
+
+--- stap
+F(ngx_http_lua_rewrite_by_chunk) {
+ printf("rewrite: konqueror: %d\n", $r->headers_in->konqueror)
+}
+
+
+F(ngx_http_core_content_phase) {
+ printf("content: konqueror: %d\n", $r->headers_in->konqueror)
+}
+
+--- stap_out
+rewrite: konqueror: 0
+content: konqueror: 1
+
+--- response_body
+User-Agent: Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.10 (like Gecko) (Kubuntu)
+--- no_error_log
+[error]
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment