Save response into temp file

Hi,
I am working on an nginx module, that has flow:

receive user request → parse request → read local file → process file
→ response to client

This flow is working well. But now we have another requirement. Afer
process this file and build response into ngx_chain, I want to write
response content into ngx_temp_file to do another job, after that return
response to client

receive user request → parse request → read local file → process file
→ save the processed file to temp file → response to client

I use this code to write file:=20

ngx_temp_file_t *tf;
tf =ngx_pcalloc(r->pool, sizeof (ngx_temp_file_t));
tf->file.fd = NGX_INVALID_FILE;
tf->file.log = nlog;
tf->path = clcf->client_body_temp_path;
tf->pool = r->pool;
tf->persistent = 1;
rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
tf->=
persistent, tf->clean, tf->access);
ngx_write_chain_to_file(&tf->file, m->chain,
m->content_length,=
r->pool);

File can be written into temporary file with following name:

-rw------- 1 root root 455712 Sep 23 13:58 0000000001
-rw------- 1 root root 455712 Sep 23 13:58 0000000002
-rw------- 1 root root 2748936 Sep 23 13:58 0000000003
-rw------- 1 root root 2831656 Sep 23 13:58 0000000004
-rw------- 1 root root 2826016 Sep 23 13:58 0000000005
-rw------- 1 root root 1786000 Sep 23 13:58 0000000006

But I cannot read from it. It seems like content of these file is not
just =
(or not enough) content that user browser receive.
Let say If nginx receive user request:
http://server.com/document.txt?type=csv , our module will process the
original document.txt file, process file to make it csv type, save it to
ngx_temp_file, and return to client document.csv file. But the file that
was saved into ngx_temp_file is not csv file.
Please show me what I am doing wrong on this.

Thanks,

Hung

anyone? Please!

Posted at Nginx Forum:

As your snippet is very short I cannot be sure but some questions to
guide
on debugging

  • did you closed the file when finished to write? some bytes may be in
    buffer and will be flushed after the close.
  • the m->chain was used before to write its content to other place? If
    yes,
    may be necessary to reset some internal pointers.
  • what do you mean by “not csv file”? What is the content of 0000000001
    file?

Regards

Hello,

Sorry for my late replay. Now I can be able to write file into disk and
read
from it without problem (csv file) by using this:
ngx_temp_file_t *tf;
tf = ngx_pcalloc(r->pool, sizeof (ngx_temp_file_t));
tf->file.fd = NGX_INVALID_FILE;
tf->file.log = nlog;
tf->path = clcf->client_body_temp_path;
tf->pool = r->pool;
tf->persistent = 1;
rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
tf->persistent, tf->clean, tf->access);
//ngx_write_chain_to_file(&tf->file, bucket->first,
bucket->content_length, r->pool);
ngx_write_chain_to_temp_file(tf, bucket->first);

I set persistent to 1 and I can read from it.
The problem is after reading file and processing it, I have to delete
file
manually.
How can file can be delete automatically just after sending nginx’s
chain to
next filter.

Other question: I write chain into file in order to read from it again.
is
it possible to use nginx’s chain as a file buffer?

Posted at Nginx Forum: