Caddy: Error while parsing file_server browse template file

Created on 6 Aug 2020  路  13Comments  路  Source: caddyserver/caddy

Something seems to be broken with browse [<template_file>] as using the default template as given template doesn't work.
Or I'm missing smth. :)

It might be related to #3637

Binary:
caddy_linux_amd64 // v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

Command:
caddy start (or caddy run)

My Caddyfile(s):

# Caddyfile
{
    http_port 8081
    https_port 8433
}

# I commented out all the other imports
import tld.someName/Caddyfile tld.someName
# tld.someName/Caddyfile
localhost:7210 {
    root * ./{args.0}/

    encode zstd gzip

    file_server {
        hide *.zip Caddyfile /.*
        # Should the {args.0} be necessary here? I thought it uses the site's root but it uses the working directory.
        browse ./{args.0}/CaddyBrowseTemplate.html
        # Commenting the above line out and using the below line works fine.
        # browse
    }
}



md5-b6a078025459dcfad3f024e52bb65144



2020/08/06 00:16:21.499 ERROR   http.log.error  template: CaddyBrowseTemplate.html:245:114: executing "CaddyBrowseTemplate.html" at <.ItemOffset>: can't evaluate field ItemOffset in type fileserver.browseListing {"request": {"method": "GET", "uri": "/", "proto": "HTTP/2.0", "remote_addr": "[::1]:53010", "host": "localhost:7210", "headers": {"Cache-Control": ["max-age=0"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"], "Sec-Fetch-User": ["?1"], "Accept-Encoding": ["gzip, deflate, br"], "Dnt": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"], "Sec-Fetch-Site": ["cross-site"], "Sec-Fetch-Mode": ["navigate"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"]}, "tls": {"resumed": false, "version": 772, "ciphersuite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "localhost"}}, "duration": 0.000589595, "status": 500, "err_id": "1vt9v5ken", "err_trace": "fileserver.(*FileServer).serveBrowse (browse.go:80)"}



md5-eb48b6f5e1a418417837e8166364bd8d



<!DOCTYPE html>
<html>
    <head>
        <title>{{html .Name}}</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>
    <body>
        "Hello World"
    </body>
</html>

edit.: file_server docs - for fast access
edit.2: Clarifying version

Most helpful comment

Confirmed the template worked just fine after building from master with xcaddy. 馃憤

All 13 comments

Using the exact same browse template as the default one? CaddyBrowseTemplate.html:245:114: - there is no ItemOffset use on line 245, so something else is wrong about your setup. The browse template _does_ work. Double check your template file.

Oh, I'm very sorry! Seems like I had a blank line at the top of the file. It's CaddyBrowseTemplate.html:244:114.
I'm terribly sorry for this mistake!!!

I copied the content from the go file again to make sure it's the same (my only edits were in css before). Now the line number is correct:

2020/08/06 22:58:32.063 ERROR   http.log.error  template: CaddyBrowseTemplate.html:244:114: executing "CaddyBrowseTemplate.html" at <.ItemOffset>: can't evaluate field ItemOffset in type fileserver.browseListing {"request": {"method": "GET", "uri": "/", "proto": "HTTP/2.0", "remote_addr": "[::1]:37792", "host": "localhost:7210", "headers": {"User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-User": ["?1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"], "Accept-Encoding": ["gzip, deflate, br"], "Dnt": ["1"]}, "tls": {"resumed": false, "version": 772, "ciphersuite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "localhost"}}, "duration": 0.000670197, "status": 500, "err_id": "20rgmtkak", "err_trace": "fileserver.(*FileServer).serveBrowse (browse.go:80)"}

However, how is the columns counted (the :114)? Is there some kind of tab/space replacement? .ItemOffset is for me in column 126 and in 159 again (for line 244). Below, the content of (my) line 244 (without tabs at the beginning - I counted 7 tabs).

<a href="?sort=namedirfirst&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}{{if ne 0 .ItemOffset}}&offset={{.ItemOffset}}{{end}}" class="icon sort"><svg class="top" width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg><svg class="bottom" width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>

I'm sure caddy loads the correct file. After renaming CaddyBrowseTemplate.html to smth. else caddy stopped with parsing browse template file: open ./tld.someName/CaddyBrowseTemplate.html: no such file or directory. (I wanted to make sure caddy isn't loading it from another dir.)

I'm sorry again for my mistake with the lines and look further to your help.

edit.: To clarify: The error is logged every time the website (here localhost:7210) is called.
However, the error of the missing template file occurs at startup (+ caddy exits).

I can confirm. If I simply use the template from https://github.com/caddyserver/caddy/blob/309c1fec622c08bdc68a43de64fc0777f590d309/modules/caddyhttp/fileserver/browsetpl.go#L17-L427 stripped so it's just HTML, I get 500 errors as well. Caddy complains about template.html:287:114:

{"level":"error","ts":1600201947.9200754,"logger":"http.log.error","msg":"template: template.html:287:114: executing \"template.html\" at <.ItemOffset>: can't evaluate field ItemOffset in type fileserver.browseListing","request":{"method":"GET","uri":"/img/","proto":"HTTP/1.1" [...]

The offending line: https://github.com/caddyserver/caddy/blob/309c1fec622c08bdc68a43de64fc0777f590d309/modules/caddyhttp/fileserver/browsetpl.go#L303

Edit: My Caddy version is v2.2.0-rc.1.

I think this should be fixed by https://github.com/caddyserver/caddy/commit/d4f249741efa0fe300a197e27370a28d8748628f /cc @Mohammed90

Could you try building from master and giving it a shot?

That's what I'm pulling with no luck. 馃檭

Edit: Unless I'm misunderstanding something.

v2.2.0-rc.1 is not master. You could try with one of the CI artifacts from here: https://github.com/caddyserver/caddy/actions/runs/256193684

The template included with Caddy works. The issue is copying the template to work on it to customize colors and such.

For instance, if I copy the template from master and have this in a Caddy file:

file_server {
  root /whatever/site/root
  browse /path/to/template.html
}

It will not work, despite being identical to the template from master.

Well ItemOffset doesn't appear anywhere in the template from the master branch, nor anywhere else in the codebase. The error you posted makes no sense. Are you sure you're actually running the version of Caddy you think you are? Are you sure it's running the right config?

Sorry, I think I copied the wrong error before. (Likely from earlier testing.)

{"level":"error","ts":1600204005.3955119,"logger":"http.log.error","msg":"template: template.html:270:17: executing \"template.html\" at <.Limit>: can't evaluate field Limit in type fileserver.browseListing","request":{"method":"GET","uri":"/img/","proto":"HTTP/2.0" [...]

This is the offending line https://github.com/caddyserver/caddy/blob/309c1fec622c08bdc68a43de64fc0777f590d309/modules/caddyhttp/fileserver/browsetpl.go#L286

Ah, that might do it. Hmm...I guess I need to find a way to build Caddy from master for Docker... 馃檭

I need to find a way to build Caddy from master for Docker

Should be possible _very_ soon, once https://github.com/caddyserver/caddy-docker/pull/113 is merged and the new builder image is published

Confirmed the template worked just fine after building from master with xcaddy. 馃憤

Was this page helpful?
0 / 5 - 0 ratings