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
caddyhttp: Improve docs, and Caddyfile for respond directive
Matthew Holt committed
Jan 17, 2020 at 10:57 UTC
793a4058105bb4da2a03291c391a91a432d1893f
2 files changed
+23
-8
modules/caddyhttp/matchers.go
+3
-3
@@ -35,7 +35,7 @@ import (
35
type (
36
// MatchHost matches requests by the Host value (case-insensitive).
37
//
38
- // When used in an HTTP route,
38
+ // When used in a top-level HTTP route,
39
// [qualifying domain names](/docs/automatic-https#hostname-requirements)
40
// may trigger [automatic HTTPS](/docs/automatic-https), which automatically
41
// provisions and renews certificates for you. Before doing this, you
@@ -55,8 +55,8 @@ type (
55
// - In the middle, for a globular match (`/accounts/*/info`)
56
//
57
// This matcher is fast, so it does not support regular expressions or
58
- // capture groups. For slower but more capable matching, use the path_regexp
59
- // matcher.
58
+ // capture groups. For slower but more powerful matching, use the
59
+ // path_regexp matcher.
60
MatchPath []string
61
62
// MatchPathRE matches requests by a regular expression on the URI's path.
modules/caddyhttp/staticresp.go
+20
-5
@@ -54,24 +54,39 @@ func (StaticResponse) CaddyModule() caddy.ModuleInfo {
54
55
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
56
//
57
-// respond [<matcher>] <status> {
57
+// respond [<matcher>] [<status>|[<body> [<status>]] {
58
// body <text>
59
// close
60
// }
61
//
62
+// If there is just one argument (other than the matcher), it is considered
63
+// to be a status code if it's a valid positive integer of 3 digits.
64
func (s *StaticResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
65
for d.Next() {
64
- var statusCodeStr string
65
- if d.Args(&statusCodeStr) {
66
- s.StatusCode = WeakString(statusCodeStr)
66
+ args := d.RemainingArgs()
67
+ switch len(args) {
68
+ case 1:
69
+ if len(args[0]) == 3 {
70
+ if num, err := strconv.Atoi(args[0]); err == nil && num > 0 {
71
+ s.StatusCode = WeakString(args[0])
72
+ break
73
+ }
74
+ }
75
+ s.Body = args[0]
76
+ case 2:
77
+ s.Body = args[0]
78
+ s.StatusCode = WeakString(args[1])
79
+ default:
80
+ return d.ArgErr()
81
}
82
+
83
for d.NextBlock(0) {
84
switch d.Val() {
85
case "body":
86
if s.Body != "" {
87
return d.Err("body already specified")
88
}
74
- if !d.Args(&s.Body) {
89
+ if !d.AllArgs(&s.Body) {
90
return d.ArgErr()
91
}
92
case "close":