lua-resty-template icon indicating copy to clipboard operation
lua-resty-template copied to clipboard

The content within block is being attempted to be included as a file.

Open snouker opened this issue 2 years ago • 5 comments

I have a file defining some blocks, such as

{-head-}<br>
<h1>....</h1>
<br>
<br>{-head-}

When I use template.render("some.html"), an error occurs:

20xx/xx/xx xx:xx:xx [error] 217324#217324: *10239 open() "/path/to/templates/<br>
<h1>....</h1>
<br>
<br>" failed (2: No such file or directory), other unimportant information.

I spent some time reading code, and identified the issue at https://github.com/bungle/lua-resty-template/blob/2822213d4e0fc0b8b9ac00559a360d099947f002/lib/resty/template.lua#L584

Deleting it will result in the blocks content not being processed!

I added a functionlocal function plain(v, c) return template.process_string(v, c or context) end at line 413 and replace line 584 withc[j+2] = '"]=plain[=[', and the issue disappeared.

I'm not sure this change is necessary anymore.

snouker avatar Dec 06 '23 16:12 snouker

@snouker, is it possible to give a full example on how to reproduce this?

bungle avatar Dec 20 '23 10:12 bungle

@bungle Yes,like this(Some private information has been replaced):

#nginx.conf

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type text/html;

    error_log logs/error.log debug;#the ngx.location.capture gives a error,but I like debug
    lua_code_cache off; #make error occured in every request

    server {
        listen 80;
        server_name localhost;
        root html;

        set $template_location /view;

        location /view {
            internal;
            alias html/view;
        }
        location / {
            content_by_lua_block {
                local template = require("resty.template")
                template.render("test.html",{version=template._VERSION})
            }
        }
    }
}

<!-- html/view/test.html -->
{-head-}<br>
<h1>....</h1>
<br>
<br>{-head-}

<p>head block:<br />
    <pre>{* blocks.head *}</pre><!-- remove this line can occur also -->
    <pre>{* version *}</pre>
</p>

this is the error:

#logs/error.log

2023/*/* *:*:* [alert] 4724#4724: lua_code_cache is off; this will hurt performance in /path/to/openresty/conf/nginx.conf:12
2023/*/* *:*:* [error] 4726#4726: *1 open() "/path/to/openresty/html/view/<br>
<h1>....</h1>
<br>
<br>" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", subrequest: "/view/<br>
<h1>....</h1>
<br>
<br>", host: "localhost"
2023/*/* *:*:* [info] 4726#4726: *1 client 127.0.0.1 closed keepalive connection

this is the result of output:

<!-- html/view/test.html -->

<p>head block:<br />
    <pre><br>
<h1>....</h1>
<br>
<br></pre><!-- remove this line can occur also -->
    <pre>2.0</pre>
</p>

snouker avatar Dec 20 '23 11:12 snouker

I tried this and got:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html
Date: Tue, 20 Aug 2024 08:05:52 GMT
Transfer-Encoding: chunked

<!-- html/view/test.html -->

<p>head block:<br />
    <pre><br>
<h1>....</h1>
<br>
<br></pre><!-- remove this line can occur also -->
    <pre>2.0</pre>
</p>

bungle avatar Aug 20 '24 08:08 bungle

But you are right, I also get:

2024/08/20 11:07:45 [error] 44089#0: *1 open() "./html/view/test.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", subrequest: "/view/test.html", host: "localhost:8123"
2024/08/20 11:07:45 [error] 44089#0: *1 open() "./html/view/<br>
<h1>....</h1>
<br>
<br>" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", subrequest: "/view/<br>
<h1>....</h1>
<br>
<br>", host: "localhost:8123"

in logs.

bungle avatar Aug 20 '24 08:08 bungle

Seems like a bug.

bungle avatar Aug 20 '24 08:08 bungle