2020-03-01 01:12:16 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package caddyfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"unicode"
|
2024-01-25 22:46:08 +08:00
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
2020-03-01 01:12:16 +08:00
|
|
|
)
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
// Format formats the input Caddyfile to a standard, nice-looking
|
|
|
|
// appearance. It works by reading each rune of the input and taking
|
|
|
|
// control over all the bracing and whitespace that is written; otherwise,
|
|
|
|
// words, comments, placeholders, and escaped characters are all treated
|
|
|
|
// literally and written as they appear in the input.
|
|
|
|
func Format(input []byte) []byte {
|
|
|
|
input = bytes.TrimSpace(input)
|
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
rdr := bytes.NewReader(input)
|
2020-03-01 01:12:16 +08:00
|
|
|
|
2024-01-22 10:24:49 +08:00
|
|
|
type heredocState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
heredocClosed heredocState = 0
|
|
|
|
heredocOpening heredocState = 1
|
|
|
|
heredocOpened heredocState = 2
|
|
|
|
)
|
|
|
|
|
2020-03-01 01:12:16 +08:00
|
|
|
var (
|
2020-03-26 08:45:54 +08:00
|
|
|
last rune // the last character that was written to the result
|
|
|
|
|
|
|
|
space = true // whether current/previous character was whitespace (beginning of input counts as space)
|
|
|
|
beginningOfLine = true // whether we are at beginning of line
|
2020-03-01 01:12:16 +08:00
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
openBrace bool // whether current word/token is or started with open curly brace
|
|
|
|
openBraceWritten bool // if openBrace, whether that brace was written or not
|
2020-03-30 03:53:00 +08:00
|
|
|
openBraceSpace bool // whether there was a non-newline space before open brace
|
2020-03-01 01:12:16 +08:00
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
newLines int // count of newlines consumed
|
2020-03-01 04:23:08 +08:00
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
comment bool // whether we're in a comment
|
|
|
|
quoted bool // whether we're in a quoted segment
|
|
|
|
escaped bool // whether current char is escaped
|
2020-03-01 01:12:16 +08:00
|
|
|
|
2024-01-22 10:24:49 +08:00
|
|
|
heredoc heredocState // whether we're in a heredoc
|
|
|
|
heredocEscaped bool // whether heredoc is escaped
|
|
|
|
heredocMarker []rune
|
|
|
|
heredocClosingMarker []rune
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
nesting int // indentation level
|
2020-03-01 01:12:16 +08:00
|
|
|
)
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
write := func(ch rune) {
|
|
|
|
out.WriteRune(ch)
|
|
|
|
last = ch
|
2020-03-17 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
indent := func() {
|
|
|
|
for tabs := nesting; tabs > 0; tabs-- {
|
|
|
|
write('\t')
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
}
|
2020-03-01 01:12:16 +08:00
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
nextLine := func() {
|
|
|
|
write('\n')
|
|
|
|
beginningOfLine = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
ch, _, err := rdr.ReadRune()
|
2020-03-01 01:12:16 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2020-03-26 08:45:54 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-01-22 10:24:49 +08:00
|
|
|
// detect whether we have the start of a heredoc
|
|
|
|
if !quoted && !(heredoc != heredocClosed || heredocEscaped) &&
|
|
|
|
space && last == '<' && ch == '<' {
|
|
|
|
write(ch)
|
|
|
|
heredoc = heredocOpening
|
|
|
|
space = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if heredoc == heredocOpening {
|
|
|
|
if ch == '\n' {
|
|
|
|
if len(heredocMarker) > 0 && heredocMarkerRegexp.MatchString(string(heredocMarker)) {
|
|
|
|
heredoc = heredocOpened
|
|
|
|
} else {
|
|
|
|
heredocMarker = nil
|
|
|
|
heredoc = heredocClosed
|
|
|
|
nextLine()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
write(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if unicode.IsSpace(ch) {
|
|
|
|
// a space means it's just a regular token and not a heredoc
|
|
|
|
heredocMarker = nil
|
|
|
|
heredoc = heredocClosed
|
|
|
|
} else {
|
|
|
|
heredocMarker = append(heredocMarker, ch)
|
|
|
|
write(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we're in a heredoc, all characters are read&write as-is
|
|
|
|
if heredoc == heredocOpened {
|
|
|
|
heredocClosingMarker = append(heredocClosingMarker, ch)
|
2024-02-20 20:29:20 +08:00
|
|
|
if len(heredocClosingMarker) > len(heredocMarker)+1 { // We assert that the heredocClosingMarker is followed by a unicode.Space
|
2024-01-22 10:24:49 +08:00
|
|
|
heredocClosingMarker = heredocClosingMarker[1:]
|
|
|
|
}
|
|
|
|
// check if we're done
|
2024-02-20 20:29:20 +08:00
|
|
|
if unicode.IsSpace(ch) && slices.Equal(heredocClosingMarker[:len(heredocClosingMarker)-1], heredocMarker) {
|
2024-01-22 10:24:49 +08:00
|
|
|
heredocMarker = nil
|
|
|
|
heredocClosingMarker = nil
|
|
|
|
heredoc = heredocClosed
|
2024-02-20 20:29:20 +08:00
|
|
|
} else {
|
|
|
|
write(ch)
|
|
|
|
if ch == '\n' {
|
|
|
|
heredocClosingMarker = heredocClosingMarker[:0]
|
|
|
|
}
|
|
|
|
continue
|
2024-01-22 10:24:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if last == '<' && space {
|
|
|
|
space = false
|
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
if comment {
|
|
|
|
if ch == '\n' {
|
|
|
|
comment = false
|
2021-05-11 02:01:27 +08:00
|
|
|
space = true
|
2020-12-17 06:22:16 +08:00
|
|
|
nextLine()
|
|
|
|
continue
|
2020-03-01 01:12:16 +08:00
|
|
|
} else {
|
2020-03-26 08:45:54 +08:00
|
|
|
write(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !escaped && ch == '\\' {
|
|
|
|
if space {
|
|
|
|
write(' ')
|
|
|
|
space = false
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
write(ch)
|
|
|
|
escaped = true
|
|
|
|
continue
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
if escaped {
|
2024-01-22 10:24:49 +08:00
|
|
|
if ch == '<' {
|
|
|
|
heredocEscaped = true
|
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
write(ch)
|
|
|
|
escaped = false
|
2020-03-01 01:12:16 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if quoted {
|
2020-03-26 08:45:54 +08:00
|
|
|
if ch == '"' {
|
2020-03-01 01:12:16 +08:00
|
|
|
quoted = false
|
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
write(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if space && ch == '"' {
|
|
|
|
quoted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if unicode.IsSpace(ch) {
|
|
|
|
space = true
|
2024-01-22 10:24:49 +08:00
|
|
|
heredocEscaped = false
|
2020-03-26 08:45:54 +08:00
|
|
|
if ch == '\n' {
|
|
|
|
newLines++
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
spacePrior := space
|
|
|
|
space = false
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
// I find it helpful to think of the formatting loop in two
|
|
|
|
// main sections; by the time we reach this point, we
|
|
|
|
// know we are in a "regular" part of the file: we know
|
|
|
|
// the character is not a space, not in a literal segment
|
|
|
|
// like a comment or quoted, it's not escaped, etc.
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if ch == '#' {
|
|
|
|
comment = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if openBrace && spacePrior && !openBraceWritten {
|
|
|
|
if nesting == 0 && last == '}' {
|
|
|
|
nextLine()
|
|
|
|
nextLine()
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
|
|
|
|
openBrace = false
|
|
|
|
if beginningOfLine {
|
|
|
|
indent()
|
2020-03-30 03:53:00 +08:00
|
|
|
} else if !openBraceSpace {
|
2020-03-26 08:45:54 +08:00
|
|
|
write(' ')
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
write('{')
|
2020-03-30 03:53:00 +08:00
|
|
|
openBraceWritten = true
|
2020-03-26 08:45:54 +08:00
|
|
|
nextLine()
|
|
|
|
newLines = 0
|
2022-09-16 04:13:58 +08:00
|
|
|
// prevent infinite nesting from ridiculous inputs (issue #4169)
|
2022-09-16 04:12:53 +08:00
|
|
|
if nesting < 10 {
|
|
|
|
nesting++
|
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ch == '{':
|
|
|
|
openBrace = true
|
|
|
|
openBraceWritten = false
|
2020-03-30 03:53:00 +08:00
|
|
|
openBraceSpace = spacePrior && !beginningOfLine
|
|
|
|
if openBraceSpace {
|
|
|
|
write(' ')
|
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
case ch == '}' && (spacePrior || !openBrace):
|
|
|
|
if last != '\n' {
|
|
|
|
nextLine()
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
if nesting > 0 {
|
|
|
|
nesting--
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
2020-03-26 08:45:54 +08:00
|
|
|
indent()
|
|
|
|
write('}')
|
|
|
|
newLines = 0
|
|
|
|
continue
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
if newLines > 2 {
|
|
|
|
newLines = 2
|
|
|
|
}
|
|
|
|
for i := 0; i < newLines; i++ {
|
|
|
|
nextLine()
|
|
|
|
}
|
|
|
|
newLines = 0
|
|
|
|
if beginningOfLine {
|
|
|
|
indent()
|
|
|
|
}
|
2020-03-30 03:53:00 +08:00
|
|
|
if nesting == 0 && last == '}' && beginningOfLine {
|
2020-03-26 08:45:54 +08:00
|
|
|
nextLine()
|
|
|
|
nextLine()
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
if !beginningOfLine && spacePrior {
|
|
|
|
write(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
if openBrace && !openBraceWritten {
|
|
|
|
write('{')
|
|
|
|
openBraceWritten = true
|
|
|
|
}
|
2024-01-22 10:24:49 +08:00
|
|
|
|
|
|
|
if spacePrior && ch == '<' {
|
|
|
|
space = true
|
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
write(ch)
|
|
|
|
|
|
|
|
beginningOfLine = false
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:45:54 +08:00
|
|
|
// the Caddyfile does not need any leading or trailing spaces, but...
|
|
|
|
trimmedResult := bytes.TrimSpace(out.Bytes())
|
|
|
|
|
|
|
|
// ...Caddyfiles should, however, end with a newline because
|
|
|
|
// newlines are significant to the syntax of the file
|
|
|
|
return append(trimmedResult, '\n')
|
2020-03-01 01:12:16 +08:00
|
|
|
}
|