Git Product home page Git Product logo

Comments (8)

xeioex avatar xeioex commented on August 20, 2024

Hi @Masterwow3,

I cannot reproduce the problem so far, I am using the following config and js code

error_log /dev/stdout info;
daemon off;
master_process off;

events { }

http {
    js_import main from fetch.js;

    resolver 1.1.1.1;

    server {
        listen       8000;

        location / {
            js_content main.fetch;
        }
    }

    server {
        listen       8001;

        location / {
            js_content main.reply;
        }
    }
}

async function fetch(r) {
    let reply = await ngx.fetch("http://127.0.0.1:8001/",
                                { headers: { 'x-ms': "a", 'foo': 'xxx' } })

    let body = await reply.text();

    r.return(200, body);
}

function reply(r) {
    let body = [njs.version, '\n'];
    r.rawHeadersIn.forEach((header) => {
        body.push(`${header[0]}: ${header[1]}\n`);
    });

    r.return(200, body.join(''));
}

export default { fetch, reply };

To run the nginx:
nginx -c fetch.conf

curl http://127.1:8000/                            
0.7.10                                                                                                                                   
Host: 127.0.0.1                                                     
Connection: close                                                   
x-ms: a                                                                                                                                  
foo: xxx

I tried njs versions from 0.7.10 to 0.7.12 and the latest one 0.8.2. Which njs version are you using?

from njs.

Masterwow3 avatar Masterwow3 commented on August 20, 2024

Hi @xeioex,
thanks for your reply, you are right, the mirror I was using swallowed the headers.

But there is still a problem. The header Content-Length cannot be set. This does not reach the server.

Log output: [info] 29#29: *3 js: [["Host","10.3.2.52"],["Connection","close"],["x-ms","123"]]
nginx/1.24.0

async function send(r) {
  await ngx.fetch("http://10.3.2.52:8082/", {
    headers: {
      "Content-Length": "0",
      "x-ms": "123"
    }
  });
  r.return(200);
}

async function log(r) {
  r.log(JSON.stringify(r.rawHeadersIn));
  r.return(200);
}


export default { send, log }
server {
  listen 8081;
  js_import njs/test.js;

  location / {
    js_content test.send;
  }
}

server {
  listen 8082;
  js_import njs/test.js;

  location / {
    js_content test.log;
  }
}

from njs.

xeioex avatar xeioex commented on August 20, 2024

Hi @Masterwow3,

"Content-Length" and a handful of other headers are treated in a special way by nginx. For example, with "Content-Length", the value is put to r->headers_in.content_length as a number and not as a header.
Whereas both r.rawHeadersIn and r.headersIn look for ordinary headers.
In njs you can get "Content-length" by r.requestBuffer.length for the text input.

from njs.

Masterwow3 avatar Masterwow3 commented on August 20, 2024

Hi @xeioex,

curl -v -H "Content-Length: 0" "http://10.3.2.52:8082/"
Arrives at nginx: [info] 29#29: *2 js: [["Host","10.3.2.52:8082"],["User-Agent","curl/7.87.0"],["Accept","*/*"],["Content-Length","0"]]
It does not arrive per NJS.

await ngx.fetch("http://10.3.2.52:8082/", {
    headers: {
      "Content-Length": "0",
      "x-ms": "123"
    }
  });

Does that mean I can't send this header via njs?

from njs.

xeioex avatar xeioex commented on August 20, 2024

Hi @Masterwow3,

There are no special treatment of Content-Length in njs Fetch for headers object.
With this code https://gist.github.com/xeioex/f3ceec948833c254dfb51bee4b7e48eb
I get the following output:

curl http://127.1:8000/
0.8.3
Host: 127.0.0.1
x-ms: a
foo: xxx
Content-Length: 0
Connection: close

Can you confirm the njs version? Just output njs.version in JS code.

from njs.

Masterwow3 avatar Masterwow3 commented on August 20, 2024

Hi @xeioex,

[info] 28#28: *1 js: NJS version: 0.7.12

Okay too bad, is it planned to support the header Content-Length in NJS?
I use the Microsoft Blob API which requires this header.

from njs.

xeioex avatar xeioex commented on August 20, 2024

Hi @Masterwow3,

There WAS a special treatment for Content-Length and other headers in accordance with Fetch API (between 0.7.10 and 0.7.12). But this was reverted back since 0.8.0. The latest released version is 0.8.2.

Feel free to upgrade to nginx 1.25.3 and njs 0.8.2.

from njs.

Masterwow3 avatar Masterwow3 commented on August 20, 2024

nginx/1.25.3
NJS version: 0.8.2
28#28: *3 js: [["Host","10.3.2.52"],["Content-Length","0"],["x-ms","123"],["Connection","close"]]

Thank you, it works.

from njs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.