comparison src/http/ngx_http_special_response.c @ 6432:cf3e75cfa951

Added variables support to server_tokens. It can now be set to "off" conditionally, e.g. using the map directive. An empty value will disable the emission of the Server: header and the signature in error messages generated by nginx. Any other value is treated as "on", meaning that full nginx version is emitted in the Server: header and error messages generated by nginx.
author Ruslan Ermilov <ru@nginx.com>
date Tue, 15 Mar 2016 13:36:19 +0300
parents 9d00576252aa
children e5076b96fd01
comparison
equal deleted inserted replaced
6431:3b9c6b91d988 6432:cf3e75cfa951
25 ; 25 ;
26 26
27 27
28 static u_char ngx_http_error_tail[] = 28 static u_char ngx_http_error_tail[] =
29 "<hr><center>nginx</center>" CRLF 29 "<hr><center>nginx</center>" CRLF
30 "</body>" CRLF
31 "</html>" CRLF
32 ;
33
34
35 static u_char ngx_http_error_no_tail[] =
30 "</body>" CRLF 36 "</body>" CRLF
31 "</html>" CRLF 37 "</html>" CRLF
32 ; 38 ;
33 39
34 40
607 613
608 static ngx_int_t 614 static ngx_int_t
609 ngx_http_send_special_response(ngx_http_request_t *r, 615 ngx_http_send_special_response(ngx_http_request_t *r,
610 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err) 616 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err)
611 { 617 {
612 u_char *tail;
613 size_t len;
614 ngx_int_t rc; 618 ngx_int_t rc;
619 ngx_str_t tail, tokens;
615 ngx_buf_t *b; 620 ngx_buf_t *b;
616 ngx_uint_t msie_padding; 621 ngx_uint_t msie_padding;
617 ngx_chain_t out[3]; 622 ngx_chain_t out[3];
618 623
619 if (clcf->server_tokens) {
620 len = sizeof(ngx_http_error_full_tail) - 1;
621 tail = ngx_http_error_full_tail;
622
623 } else {
624 len = sizeof(ngx_http_error_tail) - 1;
625 tail = ngx_http_error_tail;
626 }
627
628 msie_padding = 0; 624 msie_padding = 0;
629 625
630 if (ngx_http_error_pages[err].len) { 626 if (ngx_http_error_pages[err].len) {
631 r->headers_out.content_length_n = ngx_http_error_pages[err].len + len; 627
628 if (clcf->server_tokens == 0) {
629 ngx_str_set(&tail, ngx_http_error_tail);
630
631 } else if (clcf->server_tokens == 1) {
632 ngx_str_set(&tail, ngx_http_error_full_tail);
633
634 } else {
635 if (ngx_http_complex_value(r, &clcf->server_tokens_value, &tokens)
636 != NGX_OK)
637 {
638 return NGX_ERROR;
639 }
640
641 if (tokens.len == 3
642 && ngx_strncmp(tokens.data, "off", 3) == 0)
643 {
644 ngx_str_set(&tail, ngx_http_error_tail);
645
646 } else if (tokens.len) {
647 ngx_str_set(&tail, ngx_http_error_full_tail);
648
649 } else {
650 ngx_str_set(&tail, ngx_http_error_no_tail);
651 }
652 }
653
654 r->headers_out.content_length_n = ngx_http_error_pages[err].len
655 + tail.len;
656
632 if (clcf->msie_padding 657 if (clcf->msie_padding
633 && (r->headers_in.msie || r->headers_in.chrome) 658 && (r->headers_in.msie || r->headers_in.chrome)
634 && r->http_version >= NGX_HTTP_VERSION_10 659 && r->http_version >= NGX_HTTP_VERSION_10
635 && err >= NGX_HTTP_OFF_4XX) 660 && err >= NGX_HTTP_OFF_4XX)
636 { 661 {
643 ngx_str_set(&r->headers_out.content_type, "text/html"); 668 ngx_str_set(&r->headers_out.content_type, "text/html");
644 r->headers_out.content_type_lowcase = NULL; 669 r->headers_out.content_type_lowcase = NULL;
645 670
646 } else { 671 } else {
647 r->headers_out.content_length_n = 0; 672 r->headers_out.content_length_n = 0;
673
674 #if (NGX_SUPPRESS_WARN)
675 ngx_str_null(&tail);
676 #endif
648 } 677 }
649 678
650 if (r->headers_out.content_length) { 679 if (r->headers_out.content_length) {
651 r->headers_out.content_length->hash = 0; 680 r->headers_out.content_length->hash = 0;
652 r->headers_out.content_length = NULL; 681 r->headers_out.content_length = NULL;
682 if (b == NULL) { 711 if (b == NULL) {
683 return NGX_ERROR; 712 return NGX_ERROR;
684 } 713 }
685 714
686 b->memory = 1; 715 b->memory = 1;
687 716 b->pos = tail.data;
688 b->pos = tail; 717 b->last = tail.data + tail.len;
689 b->last = tail + len;
690 718
691 out[1].buf = b; 719 out[1].buf = b;
692 out[1].next = NULL; 720 out[1].next = NULL;
693 721
694 if (msie_padding) { 722 if (msie_padding) {