mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-19 22:39:38 +00:00
Merge 3e53d7457e into 098cc69fe4
This commit is contained in:
commit
aafb2bb1d1
3 changed files with 93 additions and 1 deletions
|
|
@ -399,6 +399,25 @@ block {
|
|||
Here's more than one space Here's more than one space
|
||||
HEREDOC
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
description: "keep heredoc whose body contains the marker mid-line",
|
||||
input: `block {
|
||||
heredoc <<HEREDOC
|
||||
line one
|
||||
prefix HEREDOC suffix
|
||||
line three
|
||||
HEREDOC
|
||||
}
|
||||
`,
|
||||
expect: `block {
|
||||
heredoc <<HEREDOC
|
||||
line one
|
||||
prefix HEREDOC suffix
|
||||
line three
|
||||
HEREDOC
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -176,7 +176,8 @@ func (l *lexer) next() (bool, error) {
|
|||
var heredocMarker string
|
||||
var heredocPrefix []int
|
||||
var heredocMatched int
|
||||
var tokenStartSet bool // whether l.tokenStart has been set for the current token
|
||||
var heredocLineStart int // index into val where the current heredoc line begins
|
||||
var tokenStartSet bool // whether l.tokenStart has been set for the current token
|
||||
|
||||
// format-mode separator tracking: record how this token was separated from
|
||||
// the previous one. Only maintained when opts.Comments || opts.Raw.
|
||||
|
|
@ -275,6 +276,7 @@ func (l *lexer) next() (bool, error) {
|
|||
inHeredoc = true
|
||||
l.skippedLines++
|
||||
val = nil
|
||||
heredocLineStart = 0
|
||||
continue
|
||||
}
|
||||
val = append(val, ch)
|
||||
|
|
@ -287,6 +289,7 @@ func (l *lexer) next() (bool, error) {
|
|||
|
||||
if ch == '\n' {
|
||||
l.skippedLines++
|
||||
heredocLineStart = len(val)
|
||||
}
|
||||
|
||||
// Markers contain only ASCII, so compare each rune directly with
|
||||
|
|
@ -299,6 +302,34 @@ func (l *lexer) next() (bool, error) {
|
|||
heredocMatched++
|
||||
}
|
||||
if heredocMatched == len(heredocMarker) {
|
||||
// A complete marker only closes the heredoc when everything
|
||||
// before it on the line is indentation and it is followed by
|
||||
// whitespace, a newline, or EOF (see #8314). Otherwise the
|
||||
// match is part of the body; fall back within the marker scan
|
||||
// and keep reading the heredoc.
|
||||
matchStart := len(val) - len(heredocMarker)
|
||||
precededByIndent := true
|
||||
for _, r := range val[heredocLineStart:matchStart] {
|
||||
if r != ' ' && r != '\t' {
|
||||
precededByIndent = false
|
||||
break
|
||||
}
|
||||
}
|
||||
nextCh, nextErr := l.readRune()
|
||||
if nextErr != nil && nextErr != io.EOF {
|
||||
return false, nextErr
|
||||
}
|
||||
followedByBoundary := nextErr == io.EOF || unicode.IsSpace(nextCh)
|
||||
if nextErr == nil {
|
||||
if err := l.unreadRune(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
if !precededByIndent || !followedByBoundary {
|
||||
heredocMatched = heredocPrefix[heredocMatched-1]
|
||||
continue
|
||||
}
|
||||
|
||||
// set the final value
|
||||
val, err = l.finalizeHeredoc(val, heredocMarker)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -501,6 +501,48 @@ EOF`),
|
|||
expectErr: true,
|
||||
errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #3 [\t], expected whitespace [\t\t] to match the closing marker",
|
||||
},
|
||||
{
|
||||
input: []byte(`heredoc <<EOF
|
||||
line one
|
||||
prefix EOF suffix
|
||||
line three
|
||||
EOF
|
||||
`),
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `heredoc`},
|
||||
{Line: 1, Text: "line one\nprefix EOF suffix\nline three"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: []byte(`heredoc <<EOF
|
||||
EOFsuffix stays content
|
||||
EOF
|
||||
`),
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `heredoc`},
|
||||
{Line: 1, Text: "EOFsuffix stays content"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: []byte(`heredoc <<EOF
|
||||
overlap EOF EOF more
|
||||
EOF
|
||||
`),
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `heredoc`},
|
||||
{Line: 1, Text: "overlap EOF EOF more"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: []byte(`heredoc <<EOF
|
||||
foo EOF
|
||||
EOF
|
||||
`),
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `heredoc`},
|
||||
{Line: 1, Text: "foo EOF"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue