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-04 18:00:29 -05:00
|
|
|
package caddyfile
|
2015-05-04 13:04:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDispenser_Val_Next(t *testing.T) {
|
|
|
|
input := `host:port
|
|
|
|
dir1 arg1
|
|
|
|
dir2 arg2 arg3
|
|
|
|
dir3`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
if val := d.Val(); val != "" {
|
|
|
|
t.Fatalf("Val(): Should return empty string when no token loaded; got '%s'", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNext := func(shouldLoad bool, expectedCursor int, expectedVal string) {
|
|
|
|
if loaded := d.Next(); loaded != shouldLoad {
|
|
|
|
t.Errorf("Next(): Expected %v but got %v instead (val '%s')", shouldLoad, loaded, d.Val())
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("Expected cursor to be %d, but was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
if d.nesting != 0 {
|
|
|
|
t.Errorf("Nesting should be 0, was %d instead", d.nesting)
|
|
|
|
}
|
|
|
|
if val := d.Val(); val != expectedVal {
|
|
|
|
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNext(true, 0, "host:port")
|
|
|
|
assertNext(true, 1, "dir1")
|
|
|
|
assertNext(true, 2, "arg1")
|
|
|
|
assertNext(true, 3, "dir2")
|
|
|
|
assertNext(true, 4, "arg2")
|
|
|
|
assertNext(true, 5, "arg3")
|
|
|
|
assertNext(true, 6, "dir3")
|
|
|
|
// Note: This next test simply asserts existing behavior.
|
|
|
|
// If desired, we may wish to empty the token value after
|
|
|
|
// reading past the EOF. Open an issue if you want this change.
|
|
|
|
assertNext(false, 6, "dir3")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_NextArg(t *testing.T) {
|
|
|
|
input := `dir1 arg1
|
|
|
|
dir2 arg2 arg3
|
|
|
|
dir3`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
assertNext := func(shouldLoad bool, expectedVal string, expectedCursor int) {
|
|
|
|
if d.Next() != shouldLoad {
|
|
|
|
t.Errorf("Next(): Should load token but got false instead (val: '%s')", d.Val())
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("Next(): Expected cursor to be at %d, but it was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
if val := d.Val(); val != expectedVal {
|
|
|
|
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNextArg := func(expectedVal string, loadAnother bool, expectedCursor int) {
|
2016-10-25 11:45:30 -05:00
|
|
|
if !d.NextArg() {
|
2015-05-04 13:04:14 -05:00
|
|
|
t.Error("NextArg(): Should load next argument but got false instead")
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("NextArg(): Expected cursor to be at %d, but it was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
if val := d.Val(); val != expectedVal {
|
|
|
|
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
|
|
|
}
|
|
|
|
if !loadAnother {
|
2016-10-25 11:45:30 -05:00
|
|
|
if d.NextArg() {
|
2015-05-04 13:04:14 -05:00
|
|
|
t.Fatalf("NextArg(): Should NOT load another argument, but got true instead (val: '%s')", d.Val())
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("NextArg(): Expected cursor to remain at %d, but it was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNext(true, "dir1", 0)
|
|
|
|
assertNextArg("arg1", false, 1)
|
|
|
|
assertNext(true, "dir2", 2)
|
|
|
|
assertNextArg("arg2", true, 3)
|
|
|
|
assertNextArg("arg3", false, 4)
|
|
|
|
assertNext(true, "dir3", 5)
|
|
|
|
assertNext(false, "dir3", 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_NextLine(t *testing.T) {
|
|
|
|
input := `host:port
|
|
|
|
dir1 arg1
|
|
|
|
dir2 arg2 arg3`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
assertNextLine := func(shouldLoad bool, expectedVal string, expectedCursor int) {
|
|
|
|
if d.NextLine() != shouldLoad {
|
|
|
|
t.Errorf("NextLine(): Should load token but got false instead (val: '%s')", d.Val())
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("NextLine(): Expected cursor to be %d, instead was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
if val := d.Val(); val != expectedVal {
|
|
|
|
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNextLine(true, "host:port", 0)
|
|
|
|
assertNextLine(true, "dir1", 1)
|
|
|
|
assertNextLine(false, "dir1", 1)
|
|
|
|
d.Next() // arg1
|
|
|
|
assertNextLine(true, "dir2", 3)
|
|
|
|
assertNextLine(false, "dir2", 3)
|
|
|
|
d.Next() // arg2
|
|
|
|
assertNextLine(false, "arg2", 4)
|
|
|
|
d.Next() // arg3
|
|
|
|
assertNextLine(false, "arg3", 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_NextBlock(t *testing.T) {
|
|
|
|
input := `foobar1 {
|
|
|
|
sub1 arg1
|
|
|
|
sub2
|
|
|
|
}
|
|
|
|
foobar2 {
|
|
|
|
}`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
assertNextBlock := func(shouldLoad bool, expectedCursor, expectedNesting int) {
|
|
|
|
if loaded := d.NextBlock(); loaded != shouldLoad {
|
|
|
|
t.Errorf("NextBlock(): Should return %v but got %v", shouldLoad, loaded)
|
|
|
|
}
|
|
|
|
if d.cursor != expectedCursor {
|
|
|
|
t.Errorf("NextBlock(): Expected cursor to be %d, was %d", expectedCursor, d.cursor)
|
|
|
|
}
|
|
|
|
if d.nesting != expectedNesting {
|
|
|
|
t.Errorf("NextBlock(): Nesting should be %d, not %d", expectedNesting, d.nesting)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNextBlock(false, -1, 0)
|
|
|
|
d.Next() // foobar1
|
|
|
|
assertNextBlock(true, 2, 1)
|
|
|
|
assertNextBlock(true, 3, 1)
|
|
|
|
assertNextBlock(true, 4, 1)
|
|
|
|
assertNextBlock(false, 5, 0)
|
2015-05-07 14:10:00 -05:00
|
|
|
d.Next() // foobar2
|
|
|
|
assertNextBlock(false, 8, 0) // empty block is as if it didn't exist
|
2015-05-04 13:04:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_Args(t *testing.T) {
|
|
|
|
var s1, s2, s3 string
|
|
|
|
input := `dir1 arg1 arg2 arg3
|
|
|
|
dir2 arg4 arg5
|
|
|
|
dir3 arg6 arg7
|
|
|
|
dir4`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
d.Next() // dir1
|
|
|
|
|
|
|
|
// As many strings as arguments
|
|
|
|
if all := d.Args(&s1, &s2, &s3); !all {
|
|
|
|
t.Error("Args(): Expected true, got false")
|
|
|
|
}
|
|
|
|
if s1 != "arg1" {
|
|
|
|
t.Errorf("Args(): Expected s1 to be 'arg1', got '%s'", s1)
|
|
|
|
}
|
|
|
|
if s2 != "arg2" {
|
|
|
|
t.Errorf("Args(): Expected s2 to be 'arg2', got '%s'", s2)
|
|
|
|
}
|
|
|
|
if s3 != "arg3" {
|
|
|
|
t.Errorf("Args(): Expected s3 to be 'arg3', got '%s'", s3)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // dir2
|
|
|
|
|
|
|
|
// More strings than arguments
|
|
|
|
if all := d.Args(&s1, &s2, &s3); all {
|
|
|
|
t.Error("Args(): Expected false, got true")
|
|
|
|
}
|
|
|
|
if s1 != "arg4" {
|
|
|
|
t.Errorf("Args(): Expected s1 to be 'arg4', got '%s'", s1)
|
|
|
|
}
|
|
|
|
if s2 != "arg5" {
|
|
|
|
t.Errorf("Args(): Expected s2 to be 'arg5', got '%s'", s2)
|
|
|
|
}
|
|
|
|
if s3 != "arg3" {
|
|
|
|
t.Errorf("Args(): Expected s3 to be unchanged ('arg3'), instead got '%s'", s3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// (quick cursor check just for kicks and giggles)
|
|
|
|
if d.cursor != 6 {
|
|
|
|
t.Errorf("Cursor should be 6, but is %d", d.cursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // dir3
|
|
|
|
|
|
|
|
// More arguments than strings
|
|
|
|
if all := d.Args(&s1); !all {
|
|
|
|
t.Error("Args(): Expected true, got false")
|
|
|
|
}
|
|
|
|
if s1 != "arg6" {
|
|
|
|
t.Errorf("Args(): Expected s1 to be 'arg6', got '%s'", s1)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // dir4
|
|
|
|
|
|
|
|
// No arguments or strings
|
|
|
|
if all := d.Args(); !all {
|
|
|
|
t.Error("Args(): Expected true, got false")
|
|
|
|
}
|
|
|
|
|
|
|
|
// No arguments but at least one string
|
|
|
|
if all := d.Args(&s1); all {
|
|
|
|
t.Error("Args(): Expected false, got true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_RemainingArgs(t *testing.T) {
|
|
|
|
input := `dir1 arg1 arg2 arg3
|
|
|
|
dir2 arg4 arg5
|
|
|
|
dir3 arg6 { arg7
|
|
|
|
dir4`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
d.Next() // dir1
|
|
|
|
|
|
|
|
args := d.RemainingArgs()
|
|
|
|
if expected := []string{"arg1", "arg2", "arg3"}; !reflect.DeepEqual(args, expected) {
|
|
|
|
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // dir2
|
|
|
|
|
|
|
|
args = d.RemainingArgs()
|
|
|
|
if expected := []string{"arg4", "arg5"}; !reflect.DeepEqual(args, expected) {
|
|
|
|
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // dir3
|
|
|
|
|
|
|
|
args = d.RemainingArgs()
|
|
|
|
if expected := []string{"arg6"}; !reflect.DeepEqual(args, expected) {
|
|
|
|
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Next() // {
|
|
|
|
d.Next() // arg7
|
|
|
|
d.Next() // dir4
|
|
|
|
|
|
|
|
args = d.RemainingArgs()
|
|
|
|
if len(args) != 0 {
|
|
|
|
t.Errorf("RemainingArgs(): Expected %v, got %v", []string{}, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispenser_ArgErr_Err(t *testing.T) {
|
|
|
|
input := `dir1 {
|
|
|
|
}
|
|
|
|
dir2 arg1 arg2`
|
|
|
|
d := NewDispenser("Testfile", strings.NewReader(input))
|
|
|
|
|
|
|
|
d.cursor = 1 // {
|
|
|
|
|
|
|
|
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "{") {
|
|
|
|
t.Errorf("ArgErr(): Expected an error message with { in it, but got '%v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.cursor = 5 // arg2
|
|
|
|
|
|
|
|
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "arg2") {
|
|
|
|
t.Errorf("ArgErr(): Expected an error message with 'arg2' in it; got '%v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.Err("foobar")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Err(): Expected an error, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "Testfile:3") {
|
|
|
|
t.Errorf("Expected error message with filename:line in it; got '%v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "foobar") {
|
|
|
|
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
|
|
|
|
}
|
|
|
|
}
|