2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
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 12:04:17 -05:00
|
|
|
|
|
|
|
import (
|
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
|
|
|
"io"
|
2016-09-22 12:07:37 -05:00
|
|
|
"log"
|
2015-05-04 12:04:17 -05:00
|
|
|
"os"
|
2015-07-07 23:38:48 -05:00
|
|
|
"path/filepath"
|
2015-05-04 12:04:17 -05:00
|
|
|
"strings"
|
2018-02-10 14:59:23 -05:00
|
|
|
|
2018-03-22 19:05:31 -05:00
|
|
|
"github.com/mholt/caddy/telemetry"
|
2015-05-04 12:04:17 -05:00
|
|
|
)
|
|
|
|
|
2016-06-20 19:25:31 -05:00
|
|
|
// Parse parses the input just enough to group tokens, in
|
|
|
|
// order, by server block. No further parsing is performed.
|
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
|
|
|
// Server blocks are returned in the order in which they appear.
|
|
|
|
// Directives that do not appear in validDirectives will cause
|
|
|
|
// an error. If you do not want to check for valid directives,
|
|
|
|
// pass in nil instead.
|
2016-06-20 19:25:31 -05:00
|
|
|
func Parse(filename string, input io.Reader, validDirectives []string) ([]ServerBlock, error) {
|
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
|
|
|
p := parser{Dispenser: NewDispenser(filename, input), validDirectives: validDirectives}
|
2016-10-25 11:45:30 -05:00
|
|
|
return p.parseAll()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// allTokens lexes the entire input, but does not parse it.
|
|
|
|
// It returns all the tokens from the input, unstructured
|
|
|
|
// and in order.
|
2016-08-17 18:17:26 -05:00
|
|
|
func allTokens(input io.Reader) ([]Token, error) {
|
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
|
|
|
l := new(lexer)
|
2016-08-17 18:17:26 -05:00
|
|
|
err := l.load(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tokens []Token
|
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
|
|
|
for l.next() {
|
|
|
|
tokens = append(tokens, l.token)
|
|
|
|
}
|
2016-08-17 18:17:26 -05:00
|
|
|
return tokens, nil
|
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
|
|
|
}
|
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
type parser struct {
|
|
|
|
Dispenser
|
2016-01-03 18:41:29 -05:00
|
|
|
block ServerBlock // current server block being parsed
|
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
|
|
|
validDirectives []string // a directive must be valid or it's an error
|
2015-10-27 01:07:22 -05:00
|
|
|
eof bool // if we encounter a valid EOF in a hard place
|
2017-10-31 22:56:24 -05:00
|
|
|
definedSnippets map[string][]Token
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 18:41:29 -05:00
|
|
|
func (p *parser) parseAll() ([]ServerBlock, error) {
|
|
|
|
var blocks []ServerBlock
|
2015-05-04 12:04:17 -05:00
|
|
|
|
|
|
|
for p.Next() {
|
|
|
|
err := p.parseOne()
|
|
|
|
if err != nil {
|
|
|
|
return blocks, err
|
|
|
|
}
|
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
|
|
|
if len(p.block.Keys) > 0 {
|
2015-08-03 18:31:10 -05:00
|
|
|
blocks = append(blocks, p.block)
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return blocks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseOne() error {
|
2016-06-04 23:50:23 -05:00
|
|
|
p.block = ServerBlock{Tokens: make(map[string][]Token)}
|
2015-05-04 12:04:17 -05:00
|
|
|
|
2016-10-25 11:45:30 -05:00
|
|
|
return p.begin()
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) begin() error {
|
2015-05-21 19:46:42 -05:00
|
|
|
if len(p.tokens) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
err := p.addresses()
|
2016-10-25 11:45:30 -05:00
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-06 09:58:15 -05:00
|
|
|
if p.eof {
|
|
|
|
// this happens if the Caddyfile consists of only
|
|
|
|
// a line of addresses and nothing else
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 22:56:24 -05:00
|
|
|
if ok, name := p.isSnippet(); ok {
|
|
|
|
if p.definedSnippets == nil {
|
|
|
|
p.definedSnippets = map[string][]Token{}
|
2017-10-13 10:04:44 -05:00
|
|
|
}
|
2017-10-31 22:56:24 -05:00
|
|
|
if _, found := p.definedSnippets[name]; found {
|
|
|
|
return p.Errf("redeclaration of previously declared snippet %s", name)
|
2017-10-13 10:04:44 -05:00
|
|
|
}
|
|
|
|
// consume all tokens til matched close brace
|
2017-10-31 22:56:24 -05:00
|
|
|
tokens, err := p.snippetTokens()
|
2017-10-13 10:04:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-31 22:56:24 -05:00
|
|
|
p.definedSnippets[name] = tokens
|
2017-10-13 10:04:44 -05:00
|
|
|
// empty block keys so we don't save this block as a real server.
|
|
|
|
p.block.Keys = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-25 11:45:30 -05:00
|
|
|
return p.blockContents()
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) addresses() error {
|
|
|
|
var expectingAnother bool
|
|
|
|
|
|
|
|
for {
|
2015-11-15 10:01:24 -05:00
|
|
|
tkn := replaceEnvVars(p.Val())
|
2015-11-05 02:26:10 -05:00
|
|
|
|
2015-07-07 23:38:48 -05:00
|
|
|
// special case: import directive replaces tokens during parse-time
|
|
|
|
if tkn == "import" && p.isNewLine() {
|
|
|
|
err := p.doImport()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
|
|
|
|
// Open brace definitely indicates end of addresses
|
|
|
|
if tkn == "{" {
|
|
|
|
if expectingAnother {
|
|
|
|
return p.Errf("Expected another address but had '%s' - check for extra comma", tkn)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if tkn != "" { // empty token possible if user typed ""
|
2015-08-03 18:31:10 -05:00
|
|
|
// Trailing comma indicates another address will follow, which
|
|
|
|
// may possibly be on the next line
|
|
|
|
if tkn[len(tkn)-1] == ',' {
|
|
|
|
tkn = tkn[:len(tkn)-1]
|
|
|
|
expectingAnother = true
|
|
|
|
} else {
|
|
|
|
expectingAnother = false // but we may still see another one on this line
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
|
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
|
|
|
p.block.Keys = append(p.block.Keys, tkn)
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advance token and possibly break out of loop or return error
|
|
|
|
hasNext := p.Next()
|
|
|
|
if expectingAnother && !hasNext {
|
2015-10-09 17:35:34 -05:00
|
|
|
return p.EOFErr()
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
if !hasNext {
|
|
|
|
p.eof = true
|
|
|
|
break // EOF
|
|
|
|
}
|
2015-07-07 23:38:48 -05:00
|
|
|
if !expectingAnother && p.isNewLine() {
|
|
|
|
break
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) blockContents() error {
|
|
|
|
errOpenCurlyBrace := p.openCurlyBrace()
|
|
|
|
if errOpenCurlyBrace != nil {
|
|
|
|
// single-server configs don't need curly braces
|
|
|
|
p.cursor--
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.directives()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only look for close curly brace if there was an opening
|
|
|
|
if errOpenCurlyBrace == nil {
|
|
|
|
err = p.closeCurlyBrace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// directives parses through all the lines for directives
|
|
|
|
// and it expects the next token to be the first
|
|
|
|
// directive. It goes until EOF or closing curly brace
|
|
|
|
// which ends the server block.
|
|
|
|
func (p *parser) directives() error {
|
|
|
|
for p.Next() {
|
|
|
|
// end of server block
|
|
|
|
if p.Val() == "}" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case: import directive replaces tokens during parse-time
|
|
|
|
if p.Val() == "import" {
|
|
|
|
err := p.doImport()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-07 23:38:48 -05:00
|
|
|
p.cursor-- // cursor is advanced when we continue, so roll back one more
|
2015-05-04 12:04:17 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal case: parse a directive on this line
|
|
|
|
if err := p.directive(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// doImport swaps out the import directive and its argument
|
2015-12-11 16:02:31 -05:00
|
|
|
// (a total of 2 tokens) with the tokens in the specified file
|
|
|
|
// or globbing pattern. When the function returns, the cursor
|
|
|
|
// is on the token before where the import directive was. In
|
|
|
|
// other words, call Next() to access the first token that was
|
|
|
|
// imported.
|
2015-05-04 12:04:17 -05:00
|
|
|
func (p *parser) doImport() error {
|
2016-08-17 18:03:21 -05:00
|
|
|
// syntax checks
|
2015-05-04 12:04:17 -05:00
|
|
|
if !p.NextArg() {
|
|
|
|
return p.ArgErr()
|
|
|
|
}
|
2016-08-17 18:03:21 -05:00
|
|
|
importPattern := replaceEnvVars(p.Val())
|
|
|
|
if importPattern == "" {
|
|
|
|
return p.Err("Import requires a non-empty filepath")
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
if p.NextArg() {
|
2016-01-09 12:52:21 -05:00
|
|
|
return p.Err("Import takes only one argument (glob pattern or file)")
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
2017-10-13 10:04:44 -05:00
|
|
|
// splice out the import directive and its argument (2 tokens total)
|
|
|
|
tokensBefore := p.tokens[:p.cursor-1]
|
|
|
|
tokensAfter := p.tokens[p.cursor+1:]
|
|
|
|
var importedTokens []Token
|
2015-05-04 12:04:17 -05:00
|
|
|
|
2017-10-31 22:56:24 -05:00
|
|
|
// first check snippets. That is a simple, non-recursive replacement
|
|
|
|
if p.definedSnippets != nil && p.definedSnippets[importPattern] != nil {
|
|
|
|
importedTokens = p.definedSnippets[importPattern]
|
2016-07-14 19:48:56 -05:00
|
|
|
} else {
|
2018-06-28 11:06:52 -05:00
|
|
|
// make path relative to the file of the _token_ being processed rather
|
|
|
|
// than current working directory (issue #867) and then use glob to get
|
|
|
|
// list of matching filenames
|
|
|
|
absFile, err := filepath.Abs(p.Dispenser.File())
|
2017-10-13 10:04:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.filename, err)
|
|
|
|
}
|
2016-07-13 11:58:42 -05:00
|
|
|
|
2017-10-13 10:04:44 -05:00
|
|
|
var matches []string
|
|
|
|
var globPattern string
|
|
|
|
if !filepath.IsAbs(importPattern) {
|
|
|
|
globPattern = filepath.Join(filepath.Dir(absFile), importPattern)
|
2016-09-22 12:07:37 -05:00
|
|
|
} else {
|
2017-10-13 10:04:44 -05:00
|
|
|
globPattern = importPattern
|
2016-09-22 12:07:37 -05:00
|
|
|
}
|
2018-04-03 12:54:32 -05:00
|
|
|
if strings.Count(globPattern, "*") > 1 || strings.Count(globPattern, "?") > 1 ||
|
|
|
|
(strings.Contains(globPattern, "[") && strings.Contains(globPattern, "]")) {
|
|
|
|
// See issue #2096 - a pattern with many glob expansions can hang for too long
|
|
|
|
return p.Errf("Glob pattern may only contain one wildcard (*), but has others: %s", globPattern)
|
|
|
|
}
|
2017-10-13 10:04:44 -05:00
|
|
|
matches, err = filepath.Glob(globPattern)
|
2015-12-04 17:21:28 -05:00
|
|
|
|
2016-01-09 12:52:21 -05:00
|
|
|
if err != nil {
|
2017-10-13 10:04:44 -05:00
|
|
|
return p.Errf("Failed to use import pattern %s: %v", importPattern, err)
|
2015-12-04 17:04:12 -05:00
|
|
|
}
|
2017-10-13 10:04:44 -05:00
|
|
|
if len(matches) == 0 {
|
2018-04-03 12:54:32 -05:00
|
|
|
if strings.ContainsAny(globPattern, "*?[]") {
|
|
|
|
log.Printf("[WARNING] No files matching import glob pattern: %s", importPattern)
|
2017-10-13 10:04:44 -05:00
|
|
|
} else {
|
|
|
|
return p.Errf("File to import not found: %s", importPattern)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all the imported tokens
|
2017-09-22 21:02:48 -05:00
|
|
|
|
2017-10-13 10:04:44 -05:00
|
|
|
for _, importFile := range matches {
|
|
|
|
newTokens, err := p.doSingleImport(importFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-07-14 19:48:56 -05:00
|
|
|
}
|
2017-10-13 10:04:44 -05:00
|
|
|
importedTokens = append(importedTokens, newTokens...)
|
|
|
|
}
|
2015-12-04 17:04:12 -05:00
|
|
|
}
|
|
|
|
|
2016-01-09 12:52:21 -05:00
|
|
|
// splice the imported tokens in the place of the import statement
|
|
|
|
// and rewind cursor so Next() will land on first imported token
|
|
|
|
p.tokens = append(tokensBefore, append(importedTokens, tokensAfter...)...)
|
|
|
|
p.cursor--
|
2015-12-06 17:49:21 -05:00
|
|
|
|
2015-12-04 17:04:12 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-09 12:52:21 -05:00
|
|
|
// doSingleImport lexes the individual file at importFile and returns
|
|
|
|
// its tokens or an error, if any.
|
2016-06-04 23:50:23 -05:00
|
|
|
func (p *parser) doSingleImport(importFile string) ([]Token, error) {
|
2015-05-04 12:04:17 -05:00
|
|
|
file, err := os.Open(importFile)
|
|
|
|
if err != nil {
|
2016-01-09 12:52:21 -05:00
|
|
|
return nil, p.Errf("Could not import %s: %v", importFile, err)
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
defer file.Close()
|
2016-08-17 18:03:21 -05:00
|
|
|
|
|
|
|
if info, err := file.Stat(); err != nil {
|
|
|
|
return nil, p.Errf("Could not import %s: %v", importFile, err)
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return nil, p.Errf("Could not import %s: is a directory", importFile)
|
|
|
|
}
|
|
|
|
|
2016-08-17 18:17:26 -05:00
|
|
|
importedTokens, err := allTokens(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
|
|
|
|
}
|
2015-05-04 12:04:17 -05:00
|
|
|
|
2017-09-22 21:02:48 -05:00
|
|
|
// Tack the file path onto these tokens so errors show the imported file's name
|
|
|
|
// (we use full, absolute path to avoid bugs: issue #1892)
|
|
|
|
filename, err := filepath.Abs(importFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.filename, err)
|
|
|
|
}
|
2015-07-07 23:38:48 -05:00
|
|
|
for i := 0; i < len(importedTokens); i++ {
|
2016-06-04 23:50:23 -05:00
|
|
|
importedTokens[i].File = filename
|
2015-07-07 23:38:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-09 12:52:21 -05:00
|
|
|
return importedTokens, nil
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// directive collects tokens until the directive's scope
|
|
|
|
// closes (either end of line or end of curly brace block).
|
|
|
|
// It expects the currently-loaded token to be a directive
|
|
|
|
// (or } that ends a server block). The collected tokens
|
|
|
|
// are loaded into the current server block for later use
|
|
|
|
// by directive setup functions.
|
|
|
|
func (p *parser) directive() error {
|
|
|
|
dir := p.Val()
|
|
|
|
nesting := 0
|
|
|
|
|
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
|
|
|
// TODO: More helpful error message ("did you mean..." or "maybe you need to install its server type")
|
|
|
|
if !p.validDirective(dir) {
|
|
|
|
return p.Errf("Unknown directive '%s'", dir)
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The directive itself is appended as a relevant token
|
2015-08-01 14:08:31 -05:00
|
|
|
p.block.Tokens[dir] = append(p.block.Tokens[dir], p.tokens[p.cursor])
|
2018-03-22 19:05:31 -05:00
|
|
|
telemetry.AppendUnique("directives", dir)
|
2015-05-04 12:04:17 -05:00
|
|
|
|
|
|
|
for p.Next() {
|
|
|
|
if p.Val() == "{" {
|
|
|
|
nesting++
|
2015-07-07 23:38:48 -05:00
|
|
|
} else if p.isNewLine() && nesting == 0 {
|
2015-05-04 12:04:17 -05:00
|
|
|
p.cursor-- // read too far
|
|
|
|
break
|
|
|
|
} else if p.Val() == "}" && nesting > 0 {
|
|
|
|
nesting--
|
|
|
|
} else if p.Val() == "}" && nesting == 0 {
|
|
|
|
return p.Err("Unexpected '}' because no matching opening brace")
|
|
|
|
}
|
2016-06-04 23:50:23 -05:00
|
|
|
p.tokens[p.cursor].Text = replaceEnvVars(p.tokens[p.cursor].Text)
|
2015-08-01 14:08:31 -05:00
|
|
|
p.block.Tokens[dir] = append(p.block.Tokens[dir], p.tokens[p.cursor])
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if nesting > 0 {
|
2015-10-09 17:35:34 -05:00
|
|
|
return p.EOFErr()
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// openCurlyBrace expects the current token to be an
|
|
|
|
// opening curly brace. This acts like an assertion
|
|
|
|
// because it returns an error if the token is not
|
2015-07-07 23:38:48 -05:00
|
|
|
// a opening curly brace. It does NOT advance the token.
|
2015-05-04 12:04:17 -05:00
|
|
|
func (p *parser) openCurlyBrace() error {
|
|
|
|
if p.Val() != "{" {
|
|
|
|
return p.SyntaxErr("{")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeCurlyBrace expects the current token to be
|
|
|
|
// a closing curly brace. This acts like an assertion
|
|
|
|
// because it returns an error if the token is not
|
2015-07-07 23:38:48 -05:00
|
|
|
// a closing curly brace. It does NOT advance the token.
|
2015-05-04 12:04:17 -05:00
|
|
|
func (p *parser) closeCurlyBrace() error {
|
|
|
|
if p.Val() != "}" {
|
|
|
|
return p.SyntaxErr("}")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// validDirective returns true if dir is in p.validDirectives.
|
|
|
|
func (p *parser) validDirective(dir string) bool {
|
|
|
|
if p.validDirectives == nil {
|
|
|
|
return true
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
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
|
|
|
for _, d := range p.validDirectives {
|
|
|
|
if d == dir {
|
|
|
|
return true
|
2016-03-16 12:27:53 -05:00
|
|
|
}
|
2015-05-20 21:15:39 -05:00
|
|
|
}
|
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
|
|
|
return false
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
|
2015-11-15 10:01:24 -05:00
|
|
|
// replaceEnvVars replaces environment variables that appear in the token
|
2016-01-03 18:41:29 -05:00
|
|
|
// and understands both the $UNIX and %WINDOWS% syntaxes.
|
2015-11-15 10:01:24 -05:00
|
|
|
func replaceEnvVars(s string) string {
|
|
|
|
s = replaceEnvReferences(s, "{%", "%}")
|
|
|
|
s = replaceEnvReferences(s, "{$", "}")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// replaceEnvReferences performs the actual replacement of env variables
|
|
|
|
// in s, given the placeholder start and placeholder end strings.
|
|
|
|
func replaceEnvReferences(s, refStart, refEnd string) string {
|
|
|
|
index := strings.Index(s, refStart)
|
|
|
|
for index != -1 {
|
|
|
|
endIndex := strings.Index(s, refEnd)
|
2018-04-03 12:54:32 -05:00
|
|
|
if endIndex > index+len(refStart) {
|
2015-11-15 10:01:24 -05:00
|
|
|
ref := s[index : endIndex+len(refEnd)]
|
|
|
|
s = strings.Replace(s, ref, os.Getenv(ref[len(refStart):len(ref)-len(refEnd)]), -1)
|
|
|
|
} else {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
index = strings.Index(s, refStart)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-06-04 23:50:23 -05:00
|
|
|
// ServerBlock associates any number of keys (usually addresses
|
|
|
|
// of some sort) with tokens (grouped by directive name).
|
|
|
|
type ServerBlock struct {
|
|
|
|
Keys []string
|
|
|
|
Tokens map[string][]Token
|
|
|
|
}
|
2017-10-13 10:04:44 -05:00
|
|
|
|
2017-10-31 22:56:24 -05:00
|
|
|
func (p *parser) isSnippet() (bool, string) {
|
2017-10-13 10:04:44 -05:00
|
|
|
keys := p.block.Keys
|
2017-10-31 22:56:24 -05:00
|
|
|
// A snippet block is a single key with parens. Nothing else qualifies.
|
2017-10-13 10:04:44 -05:00
|
|
|
if len(keys) == 1 && strings.HasPrefix(keys[0], "(") && strings.HasSuffix(keys[0], ")") {
|
|
|
|
return true, strings.TrimSuffix(keys[0][1:], ")")
|
|
|
|
}
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// read and store everything in a block for later replay.
|
2017-10-31 22:56:24 -05:00
|
|
|
func (p *parser) snippetTokens() ([]Token, error) {
|
|
|
|
// TODO: disallow imports in snippets for simplicity at import time
|
|
|
|
// snippet must have curlies.
|
2017-10-13 10:04:44 -05:00
|
|
|
err := p.openCurlyBrace()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
count := 1
|
|
|
|
tokens := []Token{}
|
|
|
|
for p.Next() {
|
|
|
|
if p.Val() == "}" {
|
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Val() == "{" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
tokens = append(tokens, p.tokens[p.cursor])
|
|
|
|
}
|
|
|
|
// make sure we're matched up
|
|
|
|
if count != 0 {
|
|
|
|
return nil, p.SyntaxErr("}")
|
|
|
|
}
|
|
|
|
return tokens, nil
|
|
|
|
}
|