nginx采用遍历ngx_list_t获取源码列表中没有的http头部变量的方法

来源:互联网 发布:高效程序员的狂暴之路 编辑:程序博客网 时间:2024/06/07 07:22

nginx源码中,已解析好的字段存在如下结构体中

typedef struct {    ngx_list_t                        headers;    ngx_table_elt_t                  *host;    ngx_table_elt_t                  *connection;    ngx_table_elt_t                  *if_modified_since;    ngx_table_elt_t                  *if_unmodified_since;    ngx_table_elt_t                  *if_match;    ngx_table_elt_t                  *if_none_match;    ngx_table_elt_t                  *user_agent;    ngx_table_elt_t                  *referer;    ngx_table_elt_t                  *content_length;    ngx_table_elt_t                  *content_type;    ngx_table_elt_t                  *range;    ngx_table_elt_t                  *if_range;    ngx_table_elt_t                  *transfer_encoding;    ngx_table_elt_t                  *expect;    ngx_table_elt_t                  *upgrade;#if (NGX_HTTP_GZIP)    ngx_table_elt_t                  *accept_encoding;    ngx_table_elt_t                  *via;#endif    ngx_table_elt_t                  *authorization;    ngx_table_elt_t                  *keep_alive;#if (NGX_HTTP_X_FORWARDED_FOR)    ngx_array_t                       x_forwarded_for;#endif#if (NGX_HTTP_REALIP)    ngx_table_elt_t                  *x_real_ip;#endif#if (NGX_HTTP_HEADERS)    ngx_table_elt_t                  *accept;    ngx_table_elt_t                  *accept_language;#endif#if (NGX_HTTP_DAV)    ngx_table_elt_t                  *depth;    ngx_table_elt_t                  *destination;    ngx_table_elt_t                  *overwrite;    ngx_table_elt_t                  *date;#endif    ngx_str_t                         user;    ngx_str_t                         passwd;    ngx_array_t                       cookies;    ngx_str_t                         server;    off_t                             content_length_n;    time_t                            keep_alive_n;    unsigned                          connection_type:2;    unsigned                          chunked:1;    unsigned                          msie:1;    unsigned                          msie6:1;    unsigned                          opera:1;    unsigned                          gecko:1;    unsigned                          chrome:1;    unsigned                          safari:1;    unsigned                          konqueror:1;} ngx_http_headers_in_t;

对于http头中没有解析的字段,nginx将其存放在了ngx_list_t类型变量headers中,只要对该list进行遍历,即可找到相应字段

相应代码如下:

ngx_list_part_t *part = &r->headers_in.headers.part;ngx_table_elt_t *header = part->elts;//开始遍历链表for(i = 0;/*void*/;i++){//判断是否到达链表中当前数组结尾if(i>=part->nelts){//是否还有下一个链表数组元素if(part->next == NULL){break;}//part设置为next来访问下一个链表数组;header也指向下一个链表数组的首地址,设置i为0,表示从头开始遍历新的链表数组。part = part->next;header = part->elts;i=0;}//hash为0表示不是合法的头部if(header[i].hash == 0){continue;}//判断当前头部是否是“XXX-key”,如果想要忽略大小写,则应先用header[i].lowcase_key代替header[i].key.data,然后比较字符串。if(0 == ngx_strncasecmp(header[i].key.data,(u_char*) "XXX-key",header[i].key.len)){//判断头部的值是否是“XXX-value”if(0 == ngx_strncmp(header[i].key.data,"XXX-value",header[i].value.len)){//找到之后继续处理}}}


0 0
原创粉丝点击