Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

njs cannot read from body file stored (using fs.readFileSync) #453

Closed
exxocism opened this issue Dec 27, 2021 · 4 comments
Closed

njs cannot read from body file stored (using fs.readFileSync) #453

exxocism opened this issue Dec 27, 2021 · 4 comments

Comments

@exxocism
Copy link

Env

Version    : 0.7.0
Git Commit : d457c9545e7e71ebb5c0479eb16b9d33175855e2
OS         : Ubuntu 20.04
Configure  : nothing configured, just implemented using (--add-module=../njs/nginx) options

Poc

const test = (req) => {

  let body;
  try {
    body = JSON.parse(req.requestText);
  } catch (e) {
    body = require('fs').readFileSync(req.variables.request_body_file, 'utf8');
    body = JSON.parse(body);
  }

  req.headersOut['Content-Type'] = 'application/json; charset=utf-8';
  req.return(200, JSON.stringify(body));
};

export default { test };

Result

error.log

2021/12/28 00:00:00 [warn] 00000#00000: *6 a client request body is buffered to a temporary file /usr/local/nginx/client_body_temp/0000000006, client: 000.000.000.000, server: domain.dot.com, request: "POST /some/endpoint HTTP/2.0", host: "domain.dot.com"
2021/12/28 00:00:00 [error] 00000#00000: *6 js exception: Error: No such file or directory
    at fs.readFileSync (native)
    at anonymous (test.js:7)
, client: 000.000.000.000, server: domain.dot.com, request: "POST /some/endpoint HTTP/2.0", host: "domain.dot.com"

cannot read body file stored... idk why

@exxocism
Copy link
Author

I think it's definitely a bug.
i turned on this settings to on and got the desired result:

client_body_in_file_only on;

@xeioex
Copy link
Contributor

xeioex commented Dec 28, 2021

Hi @exxocism,

By default, nginx unlinks (removes) the open temp file from the directory, thus making it invisible.

https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request_body.c#L556

        // by default persistent is 0 and client_body_in_file_only sets it to 1
        tf->persistent = r->request_body_in_persistent_file;
        tf->clean = r->request_body_in_clean_file;

        if (r->request_body_file_group_access) {
            tf->access = 0660;
        }

        rb->temp_file = tf;

        if (rb->bufs == NULL) {
            /* empty body with r->request_body_in_file_only */

            if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
                                     tf->persistent, tf->clean, tf->access)
                != NGX_OK)
            {
                return NGX_ERROR;
            }

https://github.com/nginx/nginx/blob/master/src/os/unix/ngx_files.c#L276

ngx_fd_t
ngx_open_tempfile(u_char *name, ngx_uint_t persistent, ngx_uint_t access)
{
    ngx_fd_t  fd;

    fd = open((const char *) name, O_CREAT|O_EXCL|O_RDWR,
              access ? access : 0600);

    if (fd != -1 && !persistent) {
        (void) unlink((const char *) name);
    }

    return fd;
}

@xeioex
Copy link
Contributor

xeioex commented Dec 28, 2021

@exxocism

To ensure, the file is always in memory consider changing client_body_buffer_size directive.

@exxocism
Copy link
Author

exxocism commented Dec 28, 2021

@xeioex Thank you.
I increased the buffer, and changed client_body_in_file_only to clean and got it working.

currently I'm working on njs to look like node.js Express
and it's so cool!

thank you for this amazing plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants