mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-19 15:53:17 +00:00
templates: fix inconsistent nested includes (#4452)
This commit is contained in:
parent
f55b123d63
commit
ec14ccdd40
3 changed files with 109 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue