0

我正在构建一个 nginx 模块并尝试从主请求创建一个 post 子请求。post 子请求的主体来自 nginx 临时文件而不是缓冲区。

    ngx_http_request_t *sr;
    ngx_http_post_subrequest_t *ps;

    ps = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
    if (ps == NULL) {
        ngx_log_stderr(0, "ngx_palloc failed");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    //sub-request callback
    ps->handler = subrequest_done;
    ps->data = last_buf;



    ngx_http_test_conf_t *conf = ngx_http_get_module_loc_conf(r, ngx_http_test_filter_module);
    if (ngx_http_subrequest(r, &conf->test_location, NULL, &sr, ps, NGX_HTTP_SUBREQUEST_IN_MEMORY) != NGX_OK)
    {
        ngx_log_stderr(0, "failed to create subrequest");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    ngx_str_t post_str = ngx_string("POST");
    sr->method = NGX_HTTP_POST;
    sr->method_name = post_str;
    sr->request_body_in_file_only = 1;
    ngx_http_set_ctx(sr, ctx, ngx_http_test_filter_module);

    // Create request body and assign temp file
    sr->request_body = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
    if (sr->request_body == NULL) {
        ngx_log_stderr(0, "request body ngx_pcalloc failed");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    sr->request_body->temp_file = ctx->temp_file;
    //sr->request_body->temp_file->offset = 0; (?)


    sr->header_only = 1;
    //sr->filter_need_in_memory = 1;

    sr->headers_in.content_length_n = ctx->file_size;

    ngx_log_stderr(0, "Created sub-request.");
    return NGX_OK;


但是,这不会发送请求正文,完成握手后请求会卡住。如果我使用请求缓冲区,它工作正常。

    sr->request_body->bufs->buf = payload_buf;
    sr->request_body->bufs->next = NULL;
    sr->request_body->buf = payload_buf;

使用临时文件时我缺少什么吗?我已经验证了临时文件在那里并且有效。有谁知道在发布子请求中是否支持使用临时文件?或者如何实现这一目标。

4

0 回答 0