Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
file_server: Use HTTPS port when a qualifying domain is specified
Also little comment cleanups
Matthew Holt committed
Nov 16, 2019 at 10:44 UTC
9fe54e1c6025a45cdd531c093bb94d193a8b91a0
2 files changed
+13
-9
modules/caddyhttp/fileserver/command.go
+12
-5
@@ -18,12 +18,14 @@ import (
18
"encoding/json"
19
"flag"
20
"log"
21
+ "strconv"
22
23
"github.com/caddyserver/caddy/v2"
24
"github.com/caddyserver/caddy/v2/caddyconfig"
25
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
26
caddycmd "github.com/caddyserver/caddy/v2/cmd"
27
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
28
+ "github.com/mholt/certmagic"
29
)
30
31
func init() {
@@ -36,12 +38,13 @@ func init() {
38
A simple but production-ready file server. Useful for quick deployments,
39
demos, and development.
40
39
-If a qualifying hostname is specified with --domain, the server will use
40
-HTTPS if domain validation succeeds. Ensure A/AAAA records are properly
41
-configured before using this option.
42
-
41
The listener's socket address can be customized with the --listen flag.
42
43
+If a qualifying hostname is specified with --domain, the default listener
44
+address will be changed to the HTTPS port and the server will use HTTPS
45
+if domain validation succeeds. Ensure A/AAAA records are properly
46
+configured before using this option.
47
+
48
If --browse is enabled, requests for folders without an index file will
49
respond with a file listing.`,
50
Flags: func() *flag.FlagSet {
@@ -83,7 +86,11 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
86
Routes: caddyhttp.RouteList{route},
87
}
88
if listen == "" {
86
- listen = ":" + httpcaddyfile.DefaultPort
89
+ if certmagic.HostQualifies(domain) {
90
+ listen = ":" + strconv.Itoa(certmagic.HTTPSPort)
91
+ } else {
92
+ listen = ":" + httpcaddyfile.DefaultPort
93
+ }
94
}
95
server.Listen = []string{listen}
96
modules/caddyhttp/fileserver/staticfiles.go
+1
-4
@@ -150,9 +150,6 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
150
return fsrv.notFound(w, r, next)
151
}
152
153
- // TODO: maybe there should be a way to serve the next handler
154
- // instead of returning 404 if a file is not found?
155
-
153
// TODO: content negotiation (brotli sidecar files, etc...)
154
155
// one last check to ensure the file isn't hidden (we might
@@ -193,7 +190,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
190
if mtyp == "" {
191
// do not allow Go to sniff the content-type; see
192
// https://www.youtube.com/watch?v=8t8JYpt0egE
196
- // TODO: Consider writing a default mime type of application/octet-stream - this is secure but violates spec
193
+ // TODO: If we want a Content-Type, consider writing a default of application/octet-stream - this is secure but violates spec
194
w.Header()["Content-Type"] = nil
195
} else {
196
w.Header().Set("Content-Type", mtyp)