diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2021-11-29 11:29:40 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-29 12:29:40 -0500 |
commit | ec14ccdd4075e46dd0235cc99ecfd92ed131c10a (patch) | |
tree | da8c59bb892499c07064b0878eb116c4f5a96b13 /modules/caddyhttp/templates/tplcontext.go | |
parent | f55b123d63132e290789bcd07077375c76b6e1dd (diff) |
templates: fix inconsistent nested includes (#4452)
Diffstat (limited to 'modules/caddyhttp/templates/tplcontext.go')
-rw-r--r-- | modules/caddyhttp/templates/tplcontext.go | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index b1646c1..4f3cbf5 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -91,7 +91,12 @@ func (c TemplateContext) OriginalReq() http.Request { // trusted files. If it is not trusted, be sure to use escaping functions // in your template. func (c TemplateContext) funcInclude(filename string, args ...interface{}) (string, error) { - bodyBuf, err := c.readFileToBuffer(filename) + + bodyBuf := bufPool.Get().(*bytes.Buffer) + bodyBuf.Reset() + defer bufPool.Put(bodyBuf) + + err := c.readFileToBuffer(filename, bodyBuf) if err != nil { return "", err @@ -107,28 +112,24 @@ func (c TemplateContext) funcInclude(filename string, args ...interface{}) (stri return bodyBuf.String(), nil } -// readFileToBuffer returns the contents of filename relative to root as a buffer -func (c TemplateContext) readFileToBuffer(filename string) (*bytes.Buffer, error) { +// readFileToBuffer reads a file into a buffer +func (c TemplateContext) readFileToBuffer(filename string, bodyBuf *bytes.Buffer) error { if c.Root == nil { - return nil, fmt.Errorf("root file system not specified") + return fmt.Errorf("root file system not specified") } file, err := c.Root.Open(filename) if err != nil { - return nil, err + return err } defer file.Close() - bodyBuf := bufPool.Get().(*bytes.Buffer) - bodyBuf.Reset() - defer bufPool.Put(bodyBuf) - _, err = io.Copy(bodyBuf, file) if err != nil { - return nil, err + return err } - return bodyBuf, nil + return nil } // funcHTTPInclude returns the body of a virtual (lightweight) request @@ -185,7 +186,12 @@ func (c TemplateContext) funcHTTPInclude(uri string) (string, error) { // {{ template }} from the standard template library. If the imported file has // no {{ define }} blocks, the name of the import will be the path func (c *TemplateContext) funcImport(filename string) (string, error) { - bodyBuf, err := c.readFileToBuffer(filename) + + bodyBuf := bufPool.Get().(*bytes.Buffer) + bodyBuf.Reset() + defer bufPool.Put(bodyBuf) + + err := c.readFileToBuffer(filename, bodyBuf) if err != nil { return "", err } |