Skip to content

njs breaks fancyindex after commit a14be61 #783

Closed
@Harry-Chen

Description

@Harry-Chen

Describe the bug

Commit a14be61 changed the way of checking subrequest from subrequest. This change of behaviour breaks fancyindex when the configuration issues a subrequest to fetch a template file in fancyindex_header subrequest.

  • The bug is reproducible with the latest version of njs.
  • I minimized the code and NGINX configuration to the smallest
    possible to reproduce the issue.

This was originally reported (in Chinese) at tuna/mirror-web#452. I bisected the code and found the first breaking commit to be a14be61, which indeed is a modification to checking-related code.

To reproduce

Steps to reproduce the behavior:

  • JS script: /path/to/webroot/fancy_index.njs
"use strict";

const exports = {};
export default exports;
exports.fancyIndexBeforeRender = fancyIndexBeforeRender;

function fancyIndexRender(r, templateUrl) {
  r.subrequest(templateUrl, {
    args: "",
    body: "",
    method: "GET"
  }, function (rTmpl) {
    if (rTmpl.status != 200) {
      return r.return(rTmpl.status);
    }
    const result = rTmpl.responseText;
    r.status = 200;
    r.headersOut["Content-Type"] = "text/html";
    r.sendHeader();
    r.send(result);
    r.finish();
  });
}
function fancyIndexBeforeRender(r) {
  return fancyIndexRender(r, "/fancy-index/template.html");
}
  • Empty template file: touch /path/to/webroot/fancy-index/template.html
  • NGINX configuration:
server {
  listen 8000;
  server_name  localhost;
  root /path/to/webroot/;

  fancyindex_header /fancy-index/before;
  location / {
    fancyindex on;
  }
  js_path /path/to/webroot/;
  location /fancy-index {
    internal;
    js_import fancyIndexRender from fancy_index.njs;
    location = /fancy-index/before {
      js_content fancyIndexRender.fancyIndexBeforeRender;
    }
  }
}
  • NGINX logs:
2024/09/14 15:57:50 [error] 407026#0: *1 js exception: Error: subrequest can only be created for the primary request
    at Request.subrequest (native)
    at fancyIndexRender (fancy_index.njs:8)
    at fancyIndexBeforeRender (fancy_index.njs:25)
, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", subrequest: "/fancy-index/before", host: "127.0.0.1:8000"
  • Output of the nginx -V command:
nginx version: nginx/1.27.1
built by gcc 12.2.0 (Debian 12.2.0-14)
built with OpenSSL 3.0.14 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/path/to/nginx_install --with-http_addition_module --add-dynamic-module=/path/to/njs/nginx --add-dynamic-module=/path/to/ngx-fancyindex
  • Exact steps to reproduce the behavior

Run NGINX with the configuration above, and access http://127.0.0.1:8000. Server will reply with 500 Internal Server Error with similar logs as provided.

Expected behavior

fancyindex should be able to fetch and render the template as before.

Your environment

  • Version of njs or specific commit: a14be61
  • List of other enabled nginx modules if applicable: fancy-index
  • OS: Debian 12

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions