ngx_http_process_request_headers函數解析

ngx_http_process_request_headers函數被ngx_http_process_request_line函數調用,將請求頭逐個放到r->headers_in.headers結構體中。由於不確定請求中一共有多少個header,所以這個函數主要功能也是在for(;;){}循環中完成,一下所有的代碼都是在上述循環內部的。主要代碼和解析如下:

判斷是否超時,如果超時,報錯並結束請求。

    if (rev->timedout) {
       ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
       c->timedout = 1;
       ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
       return;
   }

ngx_http_parse_header_line函數解析請求行,如果返回NGX_OK表示成功的解析出來一個header,如果返回NGX_HTTP_PARSE_HEADER_DONE表示所有的header都解析完了,如果返回NGX_AGAIN表示尚有header沒有解析完,剩下的返回值說明出錯了。

        rc = ngx_http_parse_header_line(r, r->header_in,
                                       cscf->underscores_in_headers);
       if (rc == NGX_OK) {
           r->request_length += r->header_in->pos - r->header_name_start;
           if (r->invalid_header && cscf->ignore_invalid_headers) {
               /* there was error while a header line parsing */
               ngx_log_error(NGX_LOG_INFO, c->log, 0,
                             "client sent invalid header line: \"%*s\"",
                             r->header_end - r->header_name_start,
                             r->header_name_start);
               continue;
           }

r->headers_in.headers是一個ngx_list_t結構體,向其添加元素的順序是先通過ngx_list_push函數返回一個元素,實際上在ngx_list_push中完成了開闢內存的工作,之後再對返回的元素進行賦值。

            h = ngx_list_push(&r->headers_in.headers);
           if (h == NULL) {
               ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
               return;
           }
           h->hash = r->header_hash;

r->header_name_end和r->header_name_start兩個變量貌似就是專門爲了指向每個頭的key的開始和結束,其他地方沒看到被使用過

            h->key.len = r->header_name_end - r->header_name_start;
           h->key.data = r->header_name_start;
           h->key.data[h->key.len] = '\0';
           h->value.len = r->header_end - r->header_start;
           h->value.data = r->header_start;
           h->value.data[h->value.len] = '\0';
           h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
           if (h->lowcase_key == NULL) {
               ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
               return;
           }
           if (h->key.len == r->lowcase_index) {
               ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
           } else {
               ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
           }

針對某些頭,可能又一些處理函數,比如說User-Agent頭的處理函數會提取瀏覽器相關的一些信息

            hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
                              h->lowcase_key, h->key.len);
           if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
               return;
           }
           ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                          "http header: \"%V: %V\"",
                          &h->key, &h->value);


在一個頭全都操作完成之後需要繼續解析下一個(如果還有的話)。

            continue;
       }

如果所有的header都解析完了,在經過一些簡單的判斷(ngx_http_process_request_header函數中完成)之後,就進入了正式的請求處理邏輯

    if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
            "http header done");
        r->request_length += r->header_in->pos - r->header_name_start;
        r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
        rc = ngx_http_process_request_header(r);
        if (rc != NGX_OK) {
            return;
        }
        ngx_http_process_request(r);
        return;
    }











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章