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 httpserver
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-04-23 14:35:56 -05:00
|
|
|
import (
|
2015-06-11 22:42:28 -05:00
|
|
|
"bufio"
|
2017-02-17 11:25:22 -05:00
|
|
|
"errors"
|
2015-06-11 22:42:28 -05:00
|
|
|
"net"
|
2015-04-23 14:35:56 -05:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2016-03-20 22:56:13 -05:00
|
|
|
// ResponseRecorder is a type of http.ResponseWriter that captures
|
2015-01-13 14:43:45 -05:00
|
|
|
// the status code written to it and also the size of the body
|
|
|
|
// written in the response. A status code does not have
|
|
|
|
// to be written, however, in which case 200 must be assumed.
|
|
|
|
// It is best to have the constructor initialize this type
|
|
|
|
// with that default status code.
|
2016-03-20 22:56:13 -05:00
|
|
|
//
|
|
|
|
// Setting the Replacer field allows middlewares to type-assert
|
|
|
|
// the http.ResponseWriter to ResponseRecorder and set their own
|
|
|
|
// placeholder values for logging utilities to use.
|
|
|
|
//
|
|
|
|
// Beware when accessing the Replacer value; it may be nil!
|
2016-02-20 16:52:42 -05:00
|
|
|
type ResponseRecorder struct {
|
2015-01-13 14:43:45 -05:00
|
|
|
http.ResponseWriter
|
2016-03-20 22:56:13 -05:00
|
|
|
Replacer Replacer
|
|
|
|
status int
|
|
|
|
size int
|
|
|
|
start time.Time
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:06:53 -05:00
|
|
|
// NewResponseRecorder makes and returns a new responseRecorder,
|
2015-01-13 14:43:45 -05:00
|
|
|
// which captures the HTTP Status code from the ResponseWriter
|
|
|
|
// and also the length of the response body written through it.
|
|
|
|
// Because a status is not set unless WriteHeader is called
|
|
|
|
// explicitly, this constructor initializes with a status code
|
|
|
|
// of 200 to cover the default case.
|
2016-02-20 16:52:42 -05:00
|
|
|
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
|
|
|
|
return &ResponseRecorder{
|
2015-01-13 14:43:45 -05:00
|
|
|
ResponseWriter: w,
|
|
|
|
status: http.StatusOK,
|
2015-04-23 14:35:56 -05:00
|
|
|
start: time.Now(),
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader records the status code and calls the
|
|
|
|
// underlying ResponseWriter's WriteHeader method.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) WriteHeader(status int) {
|
2015-01-13 14:43:45 -05:00
|
|
|
r.status = status
|
|
|
|
r.ResponseWriter.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is a wrapper that records the size of the body
|
|
|
|
// that gets written.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) Write(buf []byte) (int, error) {
|
2015-01-13 14:43:45 -05:00
|
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
|
|
if err == nil {
|
|
|
|
r.size += n
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2015-06-11 22:42:28 -05:00
|
|
|
|
2016-02-20 16:52:42 -05:00
|
|
|
// Size is a Getter to size property
|
|
|
|
func (r *ResponseRecorder) Size() int {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status is a Getter to status property
|
|
|
|
func (r *ResponseRecorder) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:23:15 -05:00
|
|
|
// Hijack implements http.Hijacker. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Hijack method if there is one, or returns an error.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2015-06-11 22:42:28 -05:00
|
|
|
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2016-10-10 21:15:33 -05:00
|
|
|
return nil, nil, NonHijackerError{Underlying: r.ResponseWriter}
|
2015-06-11 22:42:28 -05:00
|
|
|
}
|
2016-04-09 01:17:15 -05:00
|
|
|
|
|
|
|
// Flush implements http.Flusher. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Flush method if there is one, or does nothing.
|
|
|
|
func (r *ResponseRecorder) Flush() {
|
|
|
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
2016-04-09 23:11:36 -05:00
|
|
|
} else {
|
2016-10-10 21:15:33 -05:00
|
|
|
panic(NonFlusherError{Underlying: r.ResponseWriter}) // should be recovered at the beginning of middleware stack
|
2016-04-09 01:17:15 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 23:16:41 -05:00
|
|
|
|
|
|
|
// CloseNotify implements http.CloseNotifier.
|
|
|
|
// It just inherits the underlying ResponseWriter's CloseNotify method.
|
|
|
|
func (r *ResponseRecorder) CloseNotify() <-chan bool {
|
|
|
|
if cn, ok := r.ResponseWriter.(http.CloseNotifier); ok {
|
|
|
|
return cn.CloseNotify()
|
|
|
|
}
|
2016-10-10 21:15:33 -05:00
|
|
|
panic(NonCloseNotifierError{Underlying: r.ResponseWriter})
|
2016-04-10 23:16:41 -05:00
|
|
|
}
|
2017-02-17 11:25:22 -05:00
|
|
|
|
|
|
|
// Push resource to client
|
|
|
|
func (r *ResponseRecorder) Push(target string, opts *http.PushOptions) error {
|
|
|
|
if pusher, hasPusher := r.ResponseWriter.(http.Pusher); hasPusher {
|
|
|
|
return pusher.Push(target, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("push is unavailable (probably chained http.ResponseWriter does not implement http.Pusher)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var _ http.Pusher = (*ResponseRecorder)(nil)
|
|
|
|
var _ http.Flusher = (*ResponseRecorder)(nil)
|
|
|
|
var _ http.CloseNotifier = (*ResponseRecorder)(nil)
|
|
|
|
var _ http.Hijacker = (*ResponseRecorder)(nil)
|