2017-09-23 08:56:58 +03: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-05 02:00:29 +03:00
|
|
|
package caddy
|
2015-03-26 18:52:03 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-10-21 23:03:33 +03:00
|
|
|
"runtime"
|
|
|
|
"unicode"
|
2015-03-26 18:52:03 +03:00
|
|
|
|
|
|
|
"github.com/flynn/go-shlex"
|
|
|
|
)
|
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
var runtimeGoos = runtime.GOOS
|
|
|
|
|
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 02:00:29 +03:00
|
|
|
// SplitCommandAndArgs takes a command string and parses it shell-style into the
|
|
|
|
// command and its separate arguments.
|
2015-03-26 18:52:03 +03:00
|
|
|
func SplitCommandAndArgs(command string) (cmd string, args []string, err error) {
|
2015-10-21 23:03:33 +03:00
|
|
|
var parts []string
|
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
if runtimeGoos == "windows" {
|
2015-10-21 23:03:33 +03:00
|
|
|
parts = parseWindowsCommand(command) // parse it Windows-style
|
|
|
|
} else {
|
2015-10-24 15:33:04 +03:00
|
|
|
parts, err = parseUnixCommand(command) // parse it Unix-style
|
2015-10-21 23:03:33 +03:00
|
|
|
if err != nil {
|
|
|
|
err = errors.New("error parsing command: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
2015-06-08 03:49:17 +03:00
|
|
|
err = errors.New("no command contained in '" + command + "'")
|
2015-03-26 18:52:03 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
args = parts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 23:03:33 +03:00
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
// parseUnixCommand parses a unix style command line and returns the
|
|
|
|
// command and its arguments or an error
|
|
|
|
func parseUnixCommand(cmd string) ([]string, error) {
|
|
|
|
return shlex.Split(cmd)
|
2015-10-21 23:03:33 +03:00
|
|
|
}
|
2015-10-23 20:21:05 +03:00
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
// parseWindowsCommand parses windows command lines and
|
|
|
|
// returns the command and the arguments as an array. It
|
|
|
|
// should be able to parse commonly used command lines.
|
|
|
|
// Only basic syntax is supported:
|
|
|
|
// - spaces in double quotes are not token delimiters
|
|
|
|
// - double quotes are escaped by either backspace or another double quote
|
|
|
|
// - except for the above case backspaces are path separators (not special)
|
|
|
|
//
|
|
|
|
// Many sources point out that escaping quotes using backslash can be unsafe.
|
|
|
|
// Use two double quotes when possible. (Source: http://stackoverflow.com/a/31413730/2616179 )
|
|
|
|
//
|
|
|
|
// This function has to be used on Windows instead
|
|
|
|
// of the shlex package because this function treats backslash
|
|
|
|
// characters properly.
|
2015-10-23 20:21:05 +03:00
|
|
|
func parseWindowsCommand(cmd string) []string {
|
2015-10-24 15:33:04 +03:00
|
|
|
const backslash = '\\'
|
|
|
|
const quote = '"'
|
|
|
|
|
2015-10-23 20:21:05 +03:00
|
|
|
var parts []string
|
|
|
|
var part string
|
|
|
|
var inQuotes bool
|
2015-10-24 15:33:04 +03:00
|
|
|
var lastRune rune
|
2015-10-23 20:21:05 +03:00
|
|
|
|
|
|
|
for i, ch := range cmd {
|
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
if i != 0 {
|
|
|
|
lastRune = rune(cmd[i-1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch == backslash {
|
|
|
|
// put it in the part - for now we don't know if it's an
|
|
|
|
// escaping char or path separator
|
2015-10-23 20:21:05 +03:00
|
|
|
part += string(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-24 15:33:04 +03:00
|
|
|
if ch == quote {
|
|
|
|
if lastRune == backslash {
|
2015-10-23 20:21:05 +03:00
|
|
|
// remove the backslash from the part and add the escaped quote instead
|
|
|
|
part = part[:len(part)-1]
|
|
|
|
part += string(ch)
|
|
|
|
continue
|
2015-10-24 15:33:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if lastRune == quote {
|
|
|
|
// revert the last change of the inQuotes state
|
|
|
|
// it was an escaping quote
|
2015-10-23 20:21:05 +03:00
|
|
|
inQuotes = !inQuotes
|
2015-10-24 15:33:04 +03:00
|
|
|
part += string(ch)
|
2015-10-23 20:21:05 +03:00
|
|
|
continue
|
|
|
|
}
|
2015-10-24 15:33:04 +03:00
|
|
|
|
|
|
|
// normal escaping quotes
|
|
|
|
inQuotes = !inQuotes
|
|
|
|
continue
|
|
|
|
|
2015-10-23 20:21:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if unicode.IsSpace(ch) && !inQuotes && len(part) > 0 {
|
|
|
|
parts = append(parts, part)
|
|
|
|
part = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
part += string(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(part) > 0 {
|
|
|
|
parts = append(parts, part)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts
|
|
|
|
}
|