⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_header_filter_module.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>#include <nginx.h>static ngx_int_t ngx_http_header_filter_init(ngx_conf_t *cf);static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r);static ngx_http_module_t  ngx_http_header_filter_module_ctx = {    NULL,                                  /* preconfiguration */    ngx_http_header_filter_init,           /* postconfiguration */    NULL,                                  /* create main configuration */    NULL,                                  /* init main configuration */    NULL,                                  /* create server configuration */    NULL,                                  /* merge server configuration */    NULL,                                  /* create location configuration */    NULL,                                  /* merge location configuration */};ngx_module_t  ngx_http_header_filter_module = {    NGX_MODULE_V1,    &ngx_http_header_filter_module_ctx,    /* module context */    NULL,                                  /* module directives */    NGX_HTTP_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    NULL,                                  /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};static char ngx_http_server_string[] = "Server: nginx" CRLF;static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;static ngx_str_t ngx_http_status_lines[] = {    ngx_string("200 OK"),    ngx_string("201 Created"),    ngx_null_string,  /* "202 Accepted" */    ngx_null_string,  /* "203 Non-Authoritative Information" */    ngx_string("204 No Content"),    ngx_null_string,  /* "205 Reset Content" */    ngx_string("206 Partial Content"),    /* ngx_null_string, */  /* "207 Multi-Status" */#define NGX_HTTP_LEVEL_200  7    /* ngx_null_string, */  /* "300 Multiple Choices" */    ngx_string("301 Moved Permanently"),    ngx_string("302 Moved Temporarily"),    ngx_null_string,  /* "303 See Other" */    ngx_string("304 Not Modified"),    /* ngx_null_string, */  /* "305 Use Proxy" */    /* ngx_null_string, */  /* "306 unused" */    /* ngx_null_string, */  /* "307 Temporary Redirect" */#define NGX_HTTP_LEVEL_300  4    ngx_string("400 Bad Request"),    ngx_string("401 Unauthorized"),    ngx_string("402 Payment Required"),    ngx_string("403 Forbidden"),    ngx_string("404 Not Found"),    ngx_string("405 Not Allowed"),    ngx_string("406 Not Acceptable"),    ngx_null_string,  /* "407 Proxy Authentication Required" */    ngx_string("408 Request Time-out"),    ngx_string("409 Conflict"),    ngx_string("410 Gone"),    ngx_string("411 Length Required"),    ngx_string("412 Precondition Failed"),    ngx_string("413 Request Entity Too Large"),    ngx_null_string,  /* "414 Request-URI Too Large", but we never send it                       * because we treat such requests as the HTTP/0.9                       * requests and send only a body without a header                       */    ngx_string("415 Unsupported Media Type"),    ngx_string("416 Requested Range Not Satisfiable"),    /* ngx_null_string, */  /* "417 Expectation Failed" */    /* ngx_null_string, */  /* "418 unused" */    /* ngx_null_string, */  /* "419 unused" */    /* ngx_null_string, */  /* "420 unused" */    /* ngx_null_string, */  /* "421 unused" */    /* ngx_null_string, */  /* "422 Unprocessable Entity" */    /* ngx_null_string, */  /* "423 Locked" */    /* ngx_null_string, */  /* "424 Failed Dependency" */#define NGX_HTTP_LEVEL_400  17    ngx_string("500 Internal Server Error"),    ngx_string("501 Method Not Implemented"),    ngx_string("502 Bad Gateway"),    ngx_string("503 Service Temporarily Unavailable"),    ngx_string("504 Gateway Time-out"),    ngx_null_string,        /* "505 HTTP Version Not Supported" */    ngx_null_string,        /* "506 Variant Also Negotiates" */    ngx_string("507 Insufficient Storage"),    /* ngx_null_string, */  /* "508 unused" */    /* ngx_null_string, */  /* "509 unused" */    /* ngx_null_string, */  /* "510 Not Extended" */};ngx_http_header_out_t  ngx_http_headers_out[] = {    { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },    { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },#if 0    { ngx_string("Content-Type"),                 offsetof(ngx_http_headers_out_t, content_type) },#endif    { ngx_string("Content-Length"),                 offsetof(ngx_http_headers_out_t, content_length) },    { ngx_string("Content-Encoding"),                 offsetof(ngx_http_headers_out_t, content_encoding) },    { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },    { ngx_string("Last-Modified"),                 offsetof(ngx_http_headers_out_t, last_modified) },    { ngx_string("Accept-Ranges"),                 offsetof(ngx_http_headers_out_t, accept_ranges) },    { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },    { ngx_string("Cache-Control"),                 offsetof(ngx_http_headers_out_t, cache_control) },    { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },    { ngx_null_string, 0 }};static ngx_int_tngx_http_header_filter(ngx_http_request_t *r){    u_char                    *p;    size_t                     len;    ngx_str_t                  host;    ngx_buf_t                 *b;    ngx_uint_t                 status, i;    ngx_chain_t                out;    ngx_list_part_t           *part;    ngx_table_elt_t           *header;    ngx_http_core_loc_conf_t  *clcf;    ngx_http_core_srv_conf_t  *cscf;    /* AF_INET only */    u_char                     addr[INET_ADDRSTRLEN];    r->header_sent = 1;    if (r != r->main) {        return NGX_OK;    }    if (r->http_version < NGX_HTTP_VERSION_10) {        return NGX_OK;    }    if (r->method == NGX_HTTP_HEAD) {        r->header_only = 1;    }    if (r->headers_out.last_modified_time != -1) {        if (r->headers_out.status != NGX_HTTP_OK            && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT            && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)        {            r->headers_out.last_modified_time = -1;            r->headers_out.last_modified = NULL;        }    }    len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1          /* the end of the header */          + sizeof(CRLF) - 1;    /* status line */    if (r->headers_out.status_line.len) {        len += r->headers_out.status_line.len;#if (NGX_SUPPRESS_WARN)        status = NGX_INVALID_ARRAY_INDEX;#endif    } else {        if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) {            /* 2XX */            status = r->headers_out.status - NGX_HTTP_OK;            if (r->headers_out.status == NGX_HTTP_NO_CONTENT) {                r->header_only = 1;                r->headers_out.content_type.len = 0;                r->headers_out.content_type.data = NULL;                r->headers_out.last_modified_time = -1;                r->headers_out.last_modified = NULL;                r->headers_out.content_length = NULL;                r->headers_out.content_length_n = -1;            }        } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) {            /* 3XX */            status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY                                           + NGX_HTTP_LEVEL_200;            if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {                r->header_only = 1;            }        } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) {            /* 4XX */            status = r->headers_out.status - NGX_HTTP_BAD_REQUEST                                           + NGX_HTTP_LEVEL_200                                           + NGX_HTTP_LEVEL_300;        } else {            /* 5XX */            status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR                                           + NGX_HTTP_LEVEL_200                                           + NGX_HTTP_LEVEL_300                                           + NGX_HTTP_LEVEL_400;        }        len += ngx_http_status_lines[status].len;    }    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    if (r->headers_out.server == NULL) {        len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:                                     sizeof(ngx_http_server_string) - 1;    }    if (r->headers_out.date == NULL) {        len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;    }    if (r->headers_out.content_type.len) {        len += sizeof("Content-Type: ") - 1               + r->headers_out.content_type.len + 2;        if (r->headers_out.content_type_len == r->headers_out.content_type.len            && r->headers_out.charset.len)        {            len += sizeof("; charset=") - 1 + r->headers_out.charset.len;        }    }    if (r->headers_out.content_length == NULL        && r->headers_out.content_length_n >= 0)    {        len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;    }    if (r->headers_out.last_modified == NULL        && r->headers_out.last_modified_time != -1)    {        len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;    }    if (r->headers_out.location        && r->headers_out.location->value.len        && r->headers_out.location->value.data[0] == '/')    {        r->headers_out.location->hash = 0;        if (clcf->server_name_in_redirect) {            cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -