nginx源码分析四----重要的函数

本文主要分析了nginx中处理请求的关键函数,包括读取请求body的ngx_http_read_client_request_body,发送HTTP头部给客户端的ngx_http_send_header,以及发送响应包体的ngx_http_output_filter。同时,探讨了与配置相关的ngx_http_conf_get_module_main_conf和ngx_http_get_module_loc_conf函数,以及日志记录函数ngx_log_error的使用。
摘要由CSDN通过智能技术生成

一 、与请求相关

读取请求body

ngx_http_read_client_request_body
作用 : 用于读取请求体
位置: ngx_http_request_body.c

/**
 *  读取请求体, post_handler是回调函数,当body全部读取完成之后,会调用该函数   
 */
ngx_int_t
ngx_http_read_client_request_body(ngx_http_request_t *r,
    ngx_http_client_body_handler_pt post_handler)
{
    size_t                     preread;
    ssize_t                    size;
    ngx_int_t                  rc;
    ngx_buf_t                 *b;
    ngx_chain_t                out;
    ngx_http_request_body_t   *rb;
    ngx_http_core_loc_conf_t  *clcf;

    r->main->count++;
    // 请求没有请求体、主动放弃了请求体,没有分配body的buffer,则直接调用post_handler指向的回调方法
    if (r != r->main || r->request_body || r->discard_body) {
        r->request_body_no_buffering = 0;
        post_handler(r);
        return NGX_OK;
    }

    if (ngx_http_test_expect(r) != NGX_OK) {
        rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
        goto done;
    }
    //  申请内存用于存放request body
    rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
    if (rb == NULL) {
        rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
        goto done;
    }

    /*
     * set by ngx_pcalloc():
     *
     *     rb->bufs = NULL;
     *     rb->buf = NULL;
     *     rb->free = NULL;
     *     rb->busy = NULL;
     * 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值