Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
package staticfiles
|
2015-03-25 11:55:51 +08:00
|
|
|
|
|
|
|
|
|
import (
|
2016-04-17 02:59:24 +08:00
|
|
|
|
"math/rand"
|
2015-03-25 11:55:51 +08:00
|
|
|
|
"net/http"
|
2017-02-28 20:54:12 +08:00
|
|
|
|
"net/url"
|
2015-03-29 06:49:42 +08:00
|
|
|
|
"os"
|
2016-03-11 22:39:13 +08:00
|
|
|
|
"path/filepath"
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
"runtime"
|
2016-04-17 02:59:24 +08:00
|
|
|
|
"strconv"
|
2015-03-25 11:55:51 +08:00
|
|
|
|
"strings"
|
2017-02-28 20:54:12 +08:00
|
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2015-03-25 11:55:51 +08:00
|
|
|
|
)
|
|
|
|
|
|
2015-09-20 10:34:23 +08:00
|
|
|
|
// FileServer implements a production-ready file server
|
|
|
|
|
// and is the 'default' handler for all requests to Caddy.
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
// It simply loads and serves the URI requested. FileServer
|
|
|
|
|
// is adapted from the one in net/http by the Go authors.
|
|
|
|
|
// Significant modifications have been made.
|
2015-04-13 07:44:02 +08:00
|
|
|
|
//
|
2015-09-20 10:34:23 +08:00
|
|
|
|
// Original license:
|
2015-03-25 11:55:51 +08:00
|
|
|
|
//
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
type FileServer struct {
|
|
|
|
|
// Jailed disk access
|
|
|
|
|
Root http.FileSystem
|
2015-03-25 11:55:51 +08: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-05 07:00:29 +08:00
|
|
|
|
// List of files to treat as "Not Found"
|
|
|
|
|
Hide []string
|
2015-03-25 11:55:51 +08: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-05 07:00:29 +08:00
|
|
|
|
// ServeHTTP serves static files for r according to fs's configuration.
|
|
|
|
|
func (fs FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
|
// r.URL.Path has already been cleaned by Caddy.
|
2016-04-17 00:57:16 +08:00
|
|
|
|
if r.URL.Path == "" {
|
|
|
|
|
r.URL.Path = "/"
|
2015-03-25 11:55:51 +08: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-05 07:00:29 +08:00
|
|
|
|
return fs.serveFile(w, r, r.URL.Path)
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 06:52:50 +08:00
|
|
|
|
// calculateEtag produces a strong etag by default. Prefix the result with "W/" to convert this into a weak one.
|
|
|
|
|
// see https://tools.ietf.org/html/rfc7232#section-2.3
|
|
|
|
|
func calculateEtag(d os.FileInfo) string {
|
|
|
|
|
t := strconv.FormatInt(d.ModTime().Unix(), 36)
|
|
|
|
|
s := strconv.FormatInt(d.Size(), 36)
|
|
|
|
|
return `"` + t + s + `"`
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-25 10:52:34 +08:00
|
|
|
|
// serveFile writes the specified file to the HTTP response.
|
2015-03-25 11:55:51 +08:00
|
|
|
|
// name is '/'-separated, not filepath.Separator.
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name string) (int, error) {
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
location := name
|
|
|
|
|
|
2016-03-12 06:11:21 +08:00
|
|
|
|
// Prevent absolute path access on Windows.
|
|
|
|
|
// TODO remove when stdlib http.Dir fixes this.
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
if runtime.GOOS == "windows" {
|
2016-03-12 06:11:21 +08:00
|
|
|
|
if filepath.IsAbs(name[1:]) {
|
|
|
|
|
return http.StatusNotFound, 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-05 07:00:29 +08:00
|
|
|
|
|
|
|
|
|
f, err := fs.Root.Open(name)
|
2015-03-25 11:55:51 +08:00
|
|
|
|
if err != nil {
|
2017-02-11 11:02:00 +08:00
|
|
|
|
// TODO: remove when http.Dir handles this
|
|
|
|
|
// Go issue #18984
|
|
|
|
|
err = mapFSRootOpenErr(err)
|
2015-05-01 00:14:58 +08:00
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return http.StatusNotFound, nil
|
2015-05-01 01:58:38 +08:00
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
|
return http.StatusForbidden, err
|
2015-03-29 06:49:42 +08:00
|
|
|
|
}
|
2015-05-01 00:14:58 +08:00
|
|
|
|
// Likely the server is under load and ran out of file descriptors
|
2016-04-17 02:59:24 +08:00
|
|
|
|
backoff := int(3 + rand.Int31()%3) // 3–5 seconds to prevent a stampede
|
|
|
|
|
w.Header().Set("Retry-After", strconv.Itoa(backoff))
|
2015-05-01 00:14:58 +08:00
|
|
|
|
return http.StatusServiceUnavailable, err
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2015-10-21 06:25:38 +08:00
|
|
|
|
d, err := f.Stat()
|
|
|
|
|
if err != nil {
|
2015-05-01 01:58:38 +08:00
|
|
|
|
if os.IsNotExist(err) {
|
2015-05-01 00:14:58 +08:00
|
|
|
|
return http.StatusNotFound, nil
|
2015-05-01 01:58:38 +08:00
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
|
return http.StatusForbidden, err
|
2015-03-29 06:49:42 +08:00
|
|
|
|
}
|
2015-05-01 00:14:58 +08:00
|
|
|
|
// Return a different status code than above so as to distinguish these cases
|
|
|
|
|
return http.StatusInternalServerError, err
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 03:24:54 +08:00
|
|
|
|
// redirect to canonical path
|
|
|
|
|
if d.IsDir() {
|
2017-02-28 20:54:12 +08:00
|
|
|
|
// Ensure / at end of directory url. If the original URL path is
|
|
|
|
|
// used then ensure / exists as well.
|
|
|
|
|
if !strings.HasSuffix(r.URL.Path, "/") {
|
2017-03-16 00:17:12 +08:00
|
|
|
|
RedirectToDir(w, r)
|
2015-04-19 03:24:54 +08:00
|
|
|
|
return http.StatusMovedPermanently, nil
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-02-28 20:54:12 +08:00
|
|
|
|
// Ensure no / at end of file url. If the original URL path is
|
|
|
|
|
// used then ensure no / exists as well.
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
2017-03-16 00:17:12 +08:00
|
|
|
|
RedirectToFile(w, r)
|
2015-04-19 03:24:54 +08:00
|
|
|
|
return http.StatusMovedPermanently, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 11:55:51 +08:00
|
|
|
|
// use contents of an index file, if present, for directory
|
|
|
|
|
if d.IsDir() {
|
2015-09-20 10:34:23 +08:00
|
|
|
|
for _, indexPage := range IndexPages {
|
2015-03-25 11:55:51 +08:00
|
|
|
|
index := strings.TrimSuffix(name, "/") + "/" + indexPage
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
ff, err := fs.Root.Open(index)
|
2016-12-20 00:51:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
// this defer does not leak fds because previous iterations
|
|
|
|
|
// of the loop must have had an err, so nothing to close
|
|
|
|
|
defer ff.Close()
|
|
|
|
|
|
|
|
|
|
dd, err := ff.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
ff.Close()
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close previous file - release fd immediately
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
|
|
d = dd
|
|
|
|
|
f = ff
|
|
|
|
|
location = index
|
|
|
|
|
break
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Still a directory? (we didn't find an index file)
|
2015-03-29 06:49:42 +08:00
|
|
|
|
// Return 404 to hide the fact that the folder exists
|
2015-03-25 11:55:51 +08:00
|
|
|
|
if d.IsDir() {
|
2015-03-29 06:49:42 +08:00
|
|
|
|
return http.StatusNotFound, nil
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 02:30:08 +08:00
|
|
|
|
if fs.IsHidden(d) {
|
2016-03-12 06:11:21 +08:00
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 00:51:09 +08:00
|
|
|
|
filename := d.Name()
|
2017-02-19 06:52:50 +08:00
|
|
|
|
etag := calculateEtag(d) // strong
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
for _, encoding := range staticEncodingPriority {
|
2017-02-19 06:52:50 +08:00
|
|
|
|
acceptEncoding := strings.Split(r.Header.Get("Accept-Encoding"), ",")
|
|
|
|
|
|
|
|
|
|
accepted := false
|
|
|
|
|
for _, acc := range acceptEncoding {
|
|
|
|
|
if accepted || strings.TrimSpace(acc) == encoding {
|
|
|
|
|
accepted = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !accepted {
|
2016-12-20 00:51:09 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encodedFile, err := fs.Root.Open(location + staticEncoding[encoding])
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encodedFileInfo, err := encodedFile.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
encodedFile.Close()
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close previous file - release fd
|
|
|
|
|
f.Close()
|
|
|
|
|
|
2017-02-19 06:52:50 +08:00
|
|
|
|
etag = calculateEtag(encodedFileInfo)
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
// Encoded file will be served
|
|
|
|
|
f = encodedFile
|
|
|
|
|
|
|
|
|
|
w.Header().Add("Vary", "Accept-Encoding")
|
|
|
|
|
w.Header().Set("Content-Encoding", encoding)
|
2017-03-13 06:41:49 +08:00
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(encodedFileInfo.Size(), 10))
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-19 06:52:50 +08:00
|
|
|
|
// Set the ETag returned to the user-agent. Note that a conditional If-None-Match
|
|
|
|
|
// request is handled in http.ServeContent below, which checks against this ETag value.
|
|
|
|
|
w.Header().Set("ETag", etag)
|
2016-04-02 05:24:04 +08:00
|
|
|
|
|
2016-03-12 06:11:21 +08:00
|
|
|
|
// Note: Errors generated by ServeContent are written immediately
|
|
|
|
|
// to the response. This usually only happens if seeking fails (rare).
|
2016-12-20 00:51:09 +08:00
|
|
|
|
http.ServeContent(w, r, filename, d.ModTime(), f)
|
2016-03-12 06:11:21 +08:00
|
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 02:30:08 +08:00
|
|
|
|
// IsHidden checks if file with FileInfo d is on hide list.
|
|
|
|
|
func (fs FileServer) IsHidden(d os.FileInfo) bool {
|
2015-04-13 07:44:02 +08:00
|
|
|
|
// If the file is supposed to be hidden, return a 404
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
for _, hiddenPath := range fs.Hide {
|
2016-03-13 00:47:53 +08:00
|
|
|
|
// Check if the served file is exactly the hidden file.
|
Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
|
|
|
|
if hFile, err := fs.Root.Open(hiddenPath); err == nil {
|
2016-03-13 00:47:53 +08:00
|
|
|
|
fs, _ := hFile.Stat()
|
|
|
|
|
hFile.Close()
|
|
|
|
|
if os.SameFile(d, fs) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2015-04-13 07:44:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-12 06:11:21 +08:00
|
|
|
|
return false
|
2015-03-25 11:55:51 +08:00
|
|
|
|
}
|
2015-04-19 03:24:54 +08:00
|
|
|
|
|
2017-03-16 00:17:12 +08:00
|
|
|
|
// RedirectToDir replies to the request with a redirect to the URL in r, which
|
|
|
|
|
// has been transformed to indicate that the resource being requested is a
|
|
|
|
|
// directory.
|
|
|
|
|
func RedirectToDir(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
toURL, _ := url.Parse(r.URL.String())
|
|
|
|
|
|
|
|
|
|
path, ok := r.Context().Value(URLPathCtxKey).(string)
|
|
|
|
|
if ok && !strings.HasSuffix(path, "/") {
|
|
|
|
|
toURL.Path = path
|
2015-04-19 03:24:54 +08:00
|
|
|
|
}
|
2017-03-16 00:17:12 +08:00
|
|
|
|
toURL.Path += "/"
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, toURL.String(), http.StatusMovedPermanently)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RedirectToFile replies to the request with a redirect to the URL in r, which
|
|
|
|
|
// has been transformed to indicate that the resource being requested is a
|
|
|
|
|
// file.
|
|
|
|
|
func RedirectToFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
toURL, _ := url.Parse(r.URL.String())
|
|
|
|
|
|
|
|
|
|
path, ok := r.Context().Value(URLPathCtxKey).(string)
|
|
|
|
|
if ok && strings.HasSuffix(path, "/") {
|
|
|
|
|
toURL.Path = path
|
|
|
|
|
}
|
|
|
|
|
toURL.Path = strings.TrimSuffix(toURL.Path, "/")
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, toURL.String(), http.StatusMovedPermanently)
|
2015-04-19 03:24:54 +08:00
|
|
|
|
}
|
2015-09-20 10:34:23 +08:00
|
|
|
|
|
|
|
|
|
// IndexPages is a list of pages that may be understood as
|
|
|
|
|
// the "index" files to directories.
|
|
|
|
|
var IndexPages = []string{
|
|
|
|
|
"index.html",
|
|
|
|
|
"index.htm",
|
|
|
|
|
"index.txt",
|
|
|
|
|
"default.html",
|
|
|
|
|
"default.htm",
|
|
|
|
|
"default.txt",
|
|
|
|
|
}
|
2016-12-20 00:51:09 +08:00
|
|
|
|
|
|
|
|
|
// staticEncoding is a map of content-encoding to a file extension.
|
|
|
|
|
// If client accepts given encoding (via Accept-Encoding header) and compressed file with given extensions exists
|
|
|
|
|
// it will be served to the client instead of original one.
|
|
|
|
|
var staticEncoding = map[string]string{
|
|
|
|
|
"gzip": ".gz",
|
|
|
|
|
"br": ".br",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// staticEncodingPriority is a list of preferred static encodings (most efficient compression to least one).
|
|
|
|
|
var staticEncodingPriority = []string{
|
|
|
|
|
"br",
|
|
|
|
|
"gzip",
|
|
|
|
|
}
|
2017-02-11 11:02:00 +08:00
|
|
|
|
|
|
|
|
|
// mapFSRootOpenErr maps the provided non-nil error
|
|
|
|
|
// to a possibly better non-nil error. In particular, it turns OS-specific errors
|
|
|
|
|
// about opening files in non-directories into os.ErrNotExist.
|
|
|
|
|
//
|
|
|
|
|
// TODO: remove when http.Dir handles this
|
|
|
|
|
// Go issue #18984
|
|
|
|
|
func mapFSRootOpenErr(originalErr error) error {
|
|
|
|
|
if os.IsNotExist(originalErr) || os.IsPermission(originalErr) {
|
|
|
|
|
return originalErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
perr, ok := originalErr.(*os.PathError)
|
|
|
|
|
if !ok {
|
|
|
|
|
return originalErr
|
|
|
|
|
}
|
|
|
|
|
name := perr.Path
|
|
|
|
|
parts := strings.Split(name, string(filepath.Separator))
|
|
|
|
|
for i := range parts {
|
|
|
|
|
if parts[i] == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return originalErr
|
|
|
|
|
}
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
|
return os.ErrNotExist
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return originalErr
|
|
|
|
|
}
|
2017-03-13 23:22:46 +08:00
|
|
|
|
|
|
|
|
|
// URLPathCtxKey is a context key. It can be used in HTTP handlers with
|
|
|
|
|
// context.WithValue to access the original request URI that accompanied the
|
|
|
|
|
// server request. The associated value will be of type string.
|
|
|
|
|
const URLPathCtxKey caddy.CtxKey = "url_path"
|