Welcome! Log In Create A New Profile

Advanced

Nginx use temp file as request body in post subrequest

March 10, 2021 02:49PM
I'm building an nginx module and trying to create a post subrequest from the main request. The response from the main request buffers is saved to a nginx temp file and a post subrequest is created. The body of the post subrequest is from the nginx temp file instead of buffers.

```
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;


```

However this does not send out the request body in the subrequest, the request gets stuck after finishing the handshake. If I use request buffers, it works fine.

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

Is there something I'm missing when using the temp file? I've verified the temp file is there and valid. Does anyone know if using a temp file is supported in post subrequest? or how else to achieve this. I was wondering if the post subrequest can stream the request body as new data is added to the temp file. Something like proxy buffering on, response buffers are added to temp file until the last one and the post subrequest keeps streaming the data from the tempfile. Cant use request buffers in the request body since I will run out of buffers after 64K, and the response can be larger than that.
Subject Author Posted

Nginx use temp file as request body in post subrequest

salmaanp March 10, 2021 02:49PM

Re: Nginx use temp file as request body in post subrequest

salmaanp March 10, 2021 06:09PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 58
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready