1.关于设置头的一些函数指针初始化
- typedef struct {
- ngx_str_t name;
- ngx_uint_t offset;
- // 本文中搜索 h[i].handler(r, &h[i], &value,就是回调函数执行的地方
- ngx_http_set_header_pt handler;
- } ngx_http_set_header_t;
-
-
- static ngx_http_set_header_t ngx_http_set_headers[] = {
-
- { ngx_string("Cache-Control"),
- offsetof(ngx_http_headers_out_t, cache_control),
- ngx_http_add_multi_header_lines },
-
- { ngx_string("Link"),
- offsetof(ngx_http_headers_out_t, link),
- ngx_http_add_multi_header_lines },
-
- { ngx_string("Last-Modified"),
- offsetof(ngx_http_headers_out_t, last_modified),
- ngx_http_set_last_modified },
-
- { ngx_string("ETag"),
- offsetof(ngx_http_headers_out_t, etag),
- ngx_http_set_response_header },
-
- { ngx_null_string, 0, NULL }
- };
2.设置header的初始化函数
- static char *
- ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
- {
-
- ngx_http_headers_conf_t *hcf = conf;
-
- ngx_str_t *value;
- ngx_uint_t i;
- ngx_array_t **headers; // 指向headers数组的指针
- ngx_http_header_val_t *hv; // headers数组中的元素
- ngx_http_set_header_t *set;
- ngx_http_compile_complex_value_t ccv; // 这个需要关注一下
- /*
- `ngx_http_compile_complex_value_t` 是一个结构体,
- 它在Nginx的源代码中用于编译复杂的HTTP变量值。
- 在Nginx配置文件中,可以使用变量来动态地生成或引用值。
- 有时,变量的值需要通过一系列的操作、表达式或条件来计算。
- 这时候就需要使用复杂的HTTP变量值。
- `ngx_http_compile_complex_value_t` 结构体定义了一个
- 编译复杂HTTP变量值的上下文。通过该结构体,Nginx可以将配
- 置文件中的变量值编译成可执行的代码,以便在实际处理请求时
- 动态地计算变量的值。
- 这个结构体的使用是在Nginx的模块开发中,
- 当开发者需要在自定义模块中处理复杂的HTTP变量值时,
- 可以使用该结构体进行编译和计算。
-
- 总结起来,`ngx_http_compile_complex_value_t` 结构体
- 用于编译复杂的HTTP变量值,以实现在Nginx的模块开发中对
- 变量值的动态计算和处理。
- */
-
-
- value = cf->args->elts; // 获取配置文件中的参数值
-
- headers = (ngx_array_t **) ((char *) hcf + cmd->offset); // 根据offset找到headers数组的指针
-
- if (*headers == NULL) {
- *headers = ngx_array_create(cf->pool, 1,
- sizeof(ngx_http_header_val_t)); // 如果headers数组为空,创建一个数组
- if (*headers == NULL) {
- return NGX_CONF_ERROR;
- }
- }
-
- hv = ngx_array_push(*headers); // 在headers数组中添加一个元素
- if (hv == NULL) {
- return NGX_CONF_ERROR;
- }
-
- hv->key = value[1]; // 设置headers元素的key值
- hv->handler = NULL;
- hv->offset = 0;
- hv->always = 0;
-
- if (headers == &hcf->headers) {
- hv->handler = ngx_http_add_header; // 如果是headers数组,设置handler为ngx_http_add_header函数
-
- set = ngx_http_set_headers;
- for (i = 0; set[i].name.len; i++) {
- if (ngx_strcasecmp(value[1].data, set[i].name.data) != 0) {
- continue;
- }
-
- hv->offset = set[i].offset; // 根据name值设置offset和handler
- hv->handler = set[i].handler;
-
- break;
- }
- }
-
- if (value[2].len == 0) {
- ngx_memzero(&hv->value, sizeof(ngx_http_complex_value_t));
-
- } else {
- ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
-
- ccv.cf = cf;
- ccv.value = &value[2];
- ccv.complex_value = &hv->value;
-
- if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
- return NGX_CONF_ERROR;
- }
- }
-
- if (cf->args->nelts == 3) {
- return NGX_CONF_OK;
- }
-
- if (ngx_strcmp(value[3].data, "always") != 0) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid parameter \"%V\"", &value[3]);
- return NGX_CONF_ERROR;
- }
-
- hv->always = 1; // 如果有第四个参数,将always设置为1
-
- return NGX_CONF_OK;
- }
- ```
-
- 该函数的作用是向Nginx配置中添加HTTP头部信息。
- 它会解析配置文件中的参数值,然后将这些值设置到相应的结构体中,
- 最终将结构体添加到headers数组中。
- 函数还会根据参数值设置相应的处理函数和偏移量,
- 以便在处理请求时使用。
- 如果配置中有第四个参数并且值为"always",则将always标志设置为1。
- 最后,根据处理结果返回相应的状态码。
3.ngx_http_set_response_header阅读理解
- typedef struct {
- ngx_str_t value;
- ngx_uint_t *flushes;
- void *lengths;
- void *values;
-
- union {
- size_t size;
- } u;
- } ngx_http_complex_value_t;
-
- typedef struct ngx_http_header_val_s ngx_http_header_val_t;
- struct ngx_http_header_val_s {
- ngx_http_complex_value_t value;
- ngx_str_t key;
- ngx_http_set_header_pt handler;
- ngx_uint_t offset;
- ngx_uint_t always; /* unsigned always:1 */
- };
-
-
- static ngx_int_t
- ngx_http_set_response_header(ngx_http_request_t *r, ngx_http_header_val_t *hv,
- ngx_str_t *value)
- {
- ngx_table_elt_t *h, **old;
-
- // 获取要设置的响应头指针的地址
- old = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
-
- // 如果值的长度为0,则表示删除该响应头
- if (value->len == 0)
- {
- if (*old)
- {
- // 如果原来存在该响应头,则将其哈希值设为0,并将其指针置为空
- (*old)->hash = 0;
- *old = NULL;
- }
-
- return NGX_OK;
- }
-
- // 如果原来已经存在该响应头,则直接使用,否则创建一个新的响应头
- if (*old)
- {
- h = *old;
-
- } else {
- h = ngx_list_push(&r->headers_out.headers);
- if (h == NULL) {
- return NGX_ERROR;
- }
-
- *old = h;
- }
-
- h->hash = 1;
- h->key = hv->key;
- h->value = *value;
-
- return NGX_OK;
- }
-
-
- 这段代码是一个函数,用于设置HTTP响应头的值。
- 它接受一个HTTP请求结构体指针 `r`,
- 一个响应头结构体指针 `hv`,
- 和一个字符串指针 `value`。
- 函数首先通过偏移量找到要设置的响应头的指针地址。
- 然后,根据传入的 `value` 的长度,判断是否需要删除该响应头。
- 如果长度为0,则表示删除该响应头,将其哈希值设为0,并将其指针置为空。
- 如果长度不为0,则判断是否已经存在该响应头,
- 如果存在,则直接使用,否则创建一个新的响应头并将其加入到 `headers_out` 中。
- 最后,设置响应头的哈希值、键和值。函数返回 `NGX_OK` 表示成功,返回 `NGX_ERROR` 表示失败。
4.最终的执行是在ngx_http_headers_filter函数中
- static ngx_int_t
- ngx_http_headers_filter(ngx_http_request_t *r)
- {
- // ...
- if (h[i].handler(r, &h[i], &value) != NGX_OK) {
- return NGX_ERROR;
- }
- // ...
- }
5.ngx_array_t的定义
- typedef struct {
- void *elts;
- ngx_uint_t nelts;
- size_t size;
- ngx_uint_t nalloc;
- ngx_pool_t *pool;
- } ngx_array_t;
6.看一看special_header的处理-ngx_http_send_error_page
- static ngx_int_t
- ngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)
- {
- ngx_int_t overwrite;
- ngx_str_t uri, args;
- ngx_table_elt_t *location;
- ngx_http_core_loc_conf_t *clcf;
-
- overwrite = err_page->overwrite;
-
- if (overwrite && overwrite != NGX_HTTP_OK) {
- r->expect_tested = 1;
- }
-
- if (overwrite >= 0) {
- r->err_status = overwrite;
- }
-
- if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {
- return NGX_ERROR;
- }
-
- if (uri.len && uri.data[0] == '/') {
-
- if (err_page->value.lengths) {
- ngx_http_split_args(r, &uri, &args);
-
- } else {
- args = err_page->args;
- }
-
- if (r->method != NGX_HTTP_HEAD) {
- r->method = NGX_HTTP_GET;
- r->method_name = ngx_http_core_get_method;
- }
-
- return ngx_http_internal_redirect(r, &uri, &args);
- }
-
- if (uri.len && uri.data[0] == '@') {
- return ngx_http_named_location(r, &uri);
- }
-
- r->expect_tested = 1;
-
- if (ngx_http_discard_request_body(r) != NGX_OK) {
- r->keepalive = 0;
- }
-
- location = ngx_list_push(&r->headers_out.headers);
-
- if (location == NULL) {
- return NGX_ERROR;
- }
-
- if (overwrite != NGX_HTTP_MOVED_PERMANENTLY
- && overwrite != NGX_HTTP_MOVED_TEMPORARILY
- && overwrite != NGX_HTTP_SEE_OTHER
- && overwrite != NGX_HTTP_TEMPORARY_REDIRECT
- && overwrite != NGX_HTTP_PERMANENT_REDIRECT)
- {
- r->err_status = NGX_HTTP_MOVED_TEMPORARILY;
- }
-
- location->hash = 1;
- ngx_str_set(&location->key, "Location");
- location->value = uri;
-
- ngx_http_clear_location(r);
-
- r->headers_out.location = location;
-
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
- if (clcf->msie_refresh && r->headers_in.msie) {
- return ngx_http_send_refresh(r);
- }
-
- return ngx_http_send_special_response(r, clcf, r->err_status
- - NGX_HTTP_MOVED_PERMANENTLY
- + NGX_HTTP_OFF_3XX);
- }