From ec14ccdd4075e46dd0235cc99ecfd92ed131c10a Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 29 Nov 2021 11:29:40 -0600 Subject: templates: fix inconsistent nested includes (#4452) --- modules/caddyhttp/templates/tplcontext.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'modules/caddyhttp/templates/tplcontext.go') 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 } -- cgit v1.2.3