ngx_http_request_body_filter 함수 해석
                                            
 5088 단어  nginx
                    
이 함 수 는 nginx body 해석 에서 중요 한 함수 입 니 다. nginx 가 데 이 터 를 읽 기만 하면 이 함 수 를 호출 합 니 다.
함수 정 의 는 다음 과 같다.
static ngx_int_t ngx_http_request_body_filter(ngx_http_request_t r, ngx_chain_t in) {
if (r->headers_in.chunked) {
    return ngx_http_request_body_chunked_filter(r, in);
} else {
    return ngx_http_request_body_length_filter(r, in);
}
  }
다음은 비 chunked 모드, 즉 ngx 만 분석 합 니 다.http_request_body_length_필터 함수
함수 정의 기능 에 영향 을 주지 않 는 줄 static ngx 삭제int_t ngx_http_request_body_length_filter(ngx_http_request_t r, ngx_chain_t in) {
size_t                     size;
ngx_int_t                  rc;
ngx_buf_t                 *b;
ngx_chain_t               *cl, *tl, *out, **ll;
ngx_http_request_body_t   *rb;
rb = r->request_body;
if (rb->rest == -1) {     // rest         content-length
    rb->rest = r->headers_in.content_length_n;
}
out = NULL;
ll = &out;
//      chain in
//        out     chain,chain    buf        ,
//    buf            in    buf        ,
// out     chain,     buf     last_buf   1
//              content length   ,   rest    0
//   ,out     chain   buf         ,buf           ,      
for (cl = in; cl; cl = cl->next) {
    if (rb->rest == 0) {
        break;
    }
    tl = ngx_chain_get_free_buf(r->pool, &rb->free);
    if (tl == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    b = tl->buf;
    ngx_memzero(b, sizeof(ngx_buf_t));
    b->temporary = 1;
    b->tag = (ngx_buf_tag_t) &ngx_http_read_client_request_body;
    b->start = cl->buf->pos;
    b->pos = cl->buf->pos;
    b->last = cl->buf->last;
    b->end = cl->buf->end;
    size = cl->buf->last - cl->buf->pos;
    if ((off_t) size < rb->rest) {
        cl->buf->pos = cl->buf->last;
        rb->rest -= size;
    } else {
        cl->buf->pos += (size_t) rb->rest;
        rb->rest = 0;
        b->last = cl->buf->pos;
        b->last_buf = 1;
    }
    *ll = tl;
    ll = &tl->next;
}
rc = ngx_http_request_body_save_filter(r, out);
ngx_chain_update_chains(r->pool, &rb->free, &rb->busy, &out,
                        (ngx_buf_tag_t) &ngx_http_read_client_request_body);
return rc;
  }
뒤의 함 수 를 보 세 요. 안에 함수 만 호출 되 었 습 니 다. ngxchain_add_copy
static ngx_int_t ngx_http_request_body_save_filter(ngx_http_request_t r, ngx_chain_t in) {
ngx_http_request_body_t   *rb;
rb = r->request_body;
//       buf
if (ngx_chain_add_copy(r->pool, &rb->bufs, in) != NGX_OK) {
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return NGX_OK;
  }
계속 하 다
/ / 처리 할 chain 을 request body 의 chain 목록 에 추가 합 니 다 / / 주의, chain 대상 자체 만 복사 합 니 다. buf 대상 은 재 활용 입 니 다. buf 가 가리 키 는 메모리 ngx 는 말 할 것 도 없습니다.int_t ngx_chain_add_copy(ngx_pool_t pool, ngx_chain_t chain, ngx_chain_t in) {
//     chain   request body    bufs    
//     in             body 
ngx_chain_t  *cl, **ll;
ll = chain;
//      ,ll        chain   next,next     ,   ll      
for (cl = *chain; cl; cl = cl->next) {
    ll = &cl->next;
}
//    in
//    chain       request body   bufs   ,          buf   ,   buf         
while (in) {
    cl = ngx_alloc_chain_link(pool);
    if (cl == NULL) {
        return NGX_ERROR;
    }
    cl->buf = in->buf;
    *ll = cl;
    ll = &cl->next;
    in = in->next;
}
*ll = NULL;
return NGX_OK;
  }
또 하나의 중요 한 함수 ngxchain_update_chains
호출 매개 변 수 는 r - > pool, & rb - > free, & rb - > busy, & out, (ngx buf tag t) & ngxhttp_read_client_request_body
void ngx_chain_update_chains(ngx_pool_t p, ngx_chain_t free, ngx_chain_t *busy,
ngx_chain_t **out, ngx_buf_tag_t tag)
  {
// free, busy, out       
ngx_chain_t  *cl;
if (*busy == NULL) {     // busy    out      
    *busy = *out;
} else {
    for (cl = *busy; cl->next; cl = cl->next) { /* void */ } // cl    busy chain        
    cl->next = *out;
}
//         out      busy  
*out = NULL;
while (*busy) {
    cl = *busy;
    // buf      0,       ;request body    bufs      ,    ,bufs      buf   busy     buf         
    if (ngx_buf_size(cl->buf) != 0) {
        break;
    }
    if (cl->buf->tag != tag) {     // tag           
        *busy = cl->next;
        ngx_free_chain(p, cl);
        continue;
    }
    cl->buf->pos = cl->buf->start;     //   
    cl->buf->last = cl->buf->start;
    *busy = cl->next;
    cl->next = *free;
    *free = cl;     //    chain    free       
}
  }
다시 말 하면 ngx http request body filter 함수 의 목적 은 읽 은 데 이 터 를 분석 하고 request body 의 bufs 목록 에 추가 하 는 것 입 니 다. busy 도 해석 할 chain 과 buf 를 가리 키 는 동시에 함수 가 request body 의 rest 값 을 업데이트 합 니 다. 이 값 은 현재 요청 이 얼마나 많은 바이트 가 읽 히 지 않 았 는 지 를 나타 냅 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단! Certbot을 사용하여 웹 사이트를 SSL(HTTPS)화하는 방법초보자가 인프라 주위를 정돈하는 것은 매우 어렵습니다. 이번은 사이트를 간단하게 SSL화(HTTP에서 HTTPS통신)로 변경하는 방법을 소개합니다! 이번에는 소프트웨어 시스템 Nginx CentOS7 의 환경에서 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.