Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
package caddyfile
|
2015-05-05 01:04:17 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dispenser is a type that dispenses tokens, similarly to a lexer,
|
|
|
|
// except that it can do so with some notion of structure and has
|
|
|
|
// some really convenient methods.
|
|
|
|
type Dispenser struct {
|
|
|
|
filename string
|
2016-06-05 12:50:23 +08:00
|
|
|
tokens []Token
|
2015-05-05 01:04:17 +08:00
|
|
|
cursor int
|
|
|
|
nesting int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDispenser returns a Dispenser, ready to use for parsing the given input.
|
|
|
|
func NewDispenser(filename string, input io.Reader) Dispenser {
|
2016-08-18 07:17:26 +08:00
|
|
|
tokens, _ := allTokens(input) // ignoring error because nothing to do with it
|
2015-05-05 01:04:17 +08:00
|
|
|
return Dispenser{
|
|
|
|
filename: filename,
|
2016-08-18 07:17:26 +08:00
|
|
|
tokens: tokens,
|
2015-05-05 01:04:17 +08:00
|
|
|
cursor: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDispenserTokens returns a Dispenser filled with the given tokens.
|
2016-06-05 12:50:23 +08:00
|
|
|
func NewDispenserTokens(filename string, tokens []Token) Dispenser {
|
2015-05-05 01:04:17 +08:00
|
|
|
return Dispenser{
|
|
|
|
filename: filename,
|
|
|
|
tokens: tokens,
|
|
|
|
cursor: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next loads the next token. Returns true if a token
|
|
|
|
// was loaded; false otherwise. If false, all tokens
|
2015-07-08 12:38:48 +08:00
|
|
|
// have been consumed.
|
2015-05-05 01:04:17 +08:00
|
|
|
func (d *Dispenser) Next() bool {
|
|
|
|
if d.cursor < len(d.tokens)-1 {
|
|
|
|
d.cursor++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextArg loads the next token if it is on the same
|
|
|
|
// line. Returns true if a token was loaded; false
|
|
|
|
// otherwise. If false, all tokens on the line have
|
2015-07-08 12:38:48 +08:00
|
|
|
// been consumed. It handles imported tokens correctly.
|
2015-05-05 01:04:17 +08:00
|
|
|
func (d *Dispenser) NextArg() bool {
|
|
|
|
if d.cursor < 0 {
|
|
|
|
d.cursor++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d.cursor >= len(d.tokens) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.cursor < len(d.tokens)-1 &&
|
2016-06-05 12:50:23 +08:00
|
|
|
d.tokens[d.cursor].File == d.tokens[d.cursor+1].File &&
|
|
|
|
d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) == d.tokens[d.cursor+1].Line {
|
2015-05-05 01:04:17 +08:00
|
|
|
d.cursor++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextLine loads the next token only if it is not on the same
|
|
|
|
// line as the current token, and returns true if a token was
|
|
|
|
// loaded; false otherwise. If false, there is not another token
|
2015-07-08 12:38:48 +08:00
|
|
|
// or it is on the same line. It handles imported tokens correctly.
|
2015-05-05 01:04:17 +08:00
|
|
|
func (d *Dispenser) NextLine() bool {
|
|
|
|
if d.cursor < 0 {
|
|
|
|
d.cursor++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d.cursor >= len(d.tokens) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.cursor < len(d.tokens)-1 &&
|
2016-06-05 12:50:23 +08:00
|
|
|
(d.tokens[d.cursor].File != d.tokens[d.cursor+1].File ||
|
|
|
|
d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) < d.tokens[d.cursor+1].Line) {
|
2015-05-05 01:04:17 +08:00
|
|
|
d.cursor++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextBlock can be used as the condition of a for loop
|
|
|
|
// to load the next token as long as it opens a block or
|
|
|
|
// is already in a block. It returns true if a token was
|
|
|
|
// loaded, or false when the block's closing curly brace
|
|
|
|
// was loaded and thus the block ended. Nested blocks are
|
|
|
|
// not supported.
|
|
|
|
func (d *Dispenser) NextBlock() bool {
|
|
|
|
if d.nesting > 0 {
|
|
|
|
d.Next()
|
|
|
|
if d.Val() == "}" {
|
|
|
|
d.nesting--
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !d.NextArg() { // block must open on same line
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.Val() != "{" {
|
|
|
|
d.cursor-- // roll back if not opening brace
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
d.Next()
|
2015-05-08 03:10:00 +08:00
|
|
|
if d.Val() == "}" {
|
|
|
|
// Open and then closed right away
|
|
|
|
return false
|
|
|
|
}
|
2015-05-05 01:04:17 +08:00
|
|
|
d.nesting++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Val gets the text of the current token. If there is no token
|
|
|
|
// loaded, it returns empty string.
|
|
|
|
func (d *Dispenser) Val() string {
|
|
|
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
|
|
|
return ""
|
|
|
|
}
|
2016-06-05 12:50:23 +08:00
|
|
|
return d.tokens[d.cursor].Text
|
2015-05-05 01:04:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line gets the line number of the current token. If there is no token
|
|
|
|
// loaded, it returns 0.
|
|
|
|
func (d *Dispenser) Line() int {
|
|
|
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
|
|
|
return 0
|
|
|
|
}
|
2016-06-05 12:50:23 +08:00
|
|
|
return d.tokens[d.cursor].Line
|
2015-05-05 01:04:17 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 12:38:48 +08:00
|
|
|
// File gets the filename of the current token. If there is no token loaded,
|
|
|
|
// it returns the filename originally given when parsing started.
|
|
|
|
func (d *Dispenser) File() string {
|
|
|
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
|
|
|
return d.filename
|
|
|
|
}
|
2016-06-05 12:50:23 +08:00
|
|
|
if tokenFilename := d.tokens[d.cursor].File; tokenFilename != "" {
|
2015-07-08 12:38:48 +08:00
|
|
|
return tokenFilename
|
|
|
|
}
|
|
|
|
return d.filename
|
|
|
|
}
|
|
|
|
|
2015-05-05 01:04:17 +08:00
|
|
|
// Args is a convenience function that loads the next arguments
|
|
|
|
// (tokens on the same line) into an arbitrary number of strings
|
|
|
|
// pointed to in targets. If there are fewer tokens available
|
|
|
|
// than string pointers, the remaining strings will not be changed
|
|
|
|
// and false will be returned. If there were enough tokens available
|
|
|
|
// to fill the arguments, then true will be returned.
|
|
|
|
func (d *Dispenser) Args(targets ...*string) bool {
|
|
|
|
enough := true
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
if !d.NextArg() {
|
|
|
|
enough = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
*targets[i] = d.Val()
|
|
|
|
}
|
|
|
|
return enough
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemainingArgs loads any more arguments (tokens on the same line)
|
|
|
|
// into a slice and returns them. Open curly brace tokens also indicate
|
|
|
|
// the end of arguments, and the curly brace is not included in
|
|
|
|
// the return value nor is it loaded.
|
|
|
|
func (d *Dispenser) RemainingArgs() []string {
|
|
|
|
var args []string
|
|
|
|
|
|
|
|
for d.NextArg() {
|
|
|
|
if d.Val() == "{" {
|
|
|
|
d.cursor--
|
|
|
|
break
|
|
|
|
}
|
|
|
|
args = append(args, d.Val())
|
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArgErr returns an argument error, meaning that another
|
|
|
|
// argument was expected but not found. In other words,
|
|
|
|
// a line break or open curly brace was encountered instead of
|
|
|
|
// an argument.
|
|
|
|
func (d *Dispenser) ArgErr() error {
|
|
|
|
if d.Val() == "{" {
|
|
|
|
return d.Err("Unexpected token '{', expecting argument")
|
|
|
|
}
|
|
|
|
return d.Errf("Wrong argument count or unexpected line ending after '%s'", d.Val())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyntaxErr creates a generic syntax error which explains what was
|
|
|
|
// found and what was expected.
|
|
|
|
func (d *Dispenser) SyntaxErr(expected string) error {
|
2015-07-08 12:38:48 +08:00
|
|
|
msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s'", d.File(), d.Line(), d.Val(), expected)
|
2015-05-05 01:04:17 +08:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2015-10-10 06:35:34 +08:00
|
|
|
// EOFErr returns an error indicating that the dispenser reached
|
|
|
|
// the end of the input when searching for the next token.
|
|
|
|
func (d *Dispenser) EOFErr() error {
|
2015-05-05 01:04:17 +08:00
|
|
|
return d.Errf("Unexpected EOF")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err generates a custom parse error with a message of msg.
|
|
|
|
func (d *Dispenser) Err(msg string) error {
|
2015-07-08 12:38:48 +08:00
|
|
|
msg = fmt.Sprintf("%s:%d - Parse error: %s", d.File(), d.Line(), msg)
|
2015-05-05 01:04:17 +08:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errf is like Err, but for formatted error messages
|
|
|
|
func (d *Dispenser) Errf(format string, args ...interface{}) error {
|
2015-05-05 06:23:16 +08:00
|
|
|
return d.Err(fmt.Sprintf(format, args...))
|
2015-05-05 01:04:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// numLineBreaks counts how many line breaks are in the token
|
|
|
|
// value given by the token index tknIdx. It returns 0 if the
|
|
|
|
// token does not exist or there are no line breaks.
|
|
|
|
func (d *Dispenser) numLineBreaks(tknIdx int) int {
|
|
|
|
if tknIdx < 0 || tknIdx >= len(d.tokens) {
|
|
|
|
return 0
|
|
|
|
}
|
2016-06-05 12:50:23 +08:00
|
|
|
return strings.Count(d.tokens[tknIdx].Text, "\n")
|
2015-05-05 01:04:17 +08:00
|
|
|
}
|
2015-07-08 12:38:48 +08:00
|
|
|
|
|
|
|
// isNewLine determines whether the current token is on a different
|
|
|
|
// line (higher line number) than the previous token. It handles imported
|
|
|
|
// tokens correctly. If there isn't a previous token, it returns true.
|
|
|
|
func (d *Dispenser) isNewLine() bool {
|
|
|
|
if d.cursor < 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d.cursor > len(d.tokens)-1 {
|
|
|
|
return false
|
|
|
|
}
|
2016-06-05 12:50:23 +08:00
|
|
|
return d.tokens[d.cursor-1].File != d.tokens[d.cursor].File ||
|
|
|
|
d.tokens[d.cursor-1].Line+d.numLineBreaks(d.cursor-1) < d.tokens[d.cursor].Line
|
2015-07-08 12:38:48 +08:00
|
|
|
}
|