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: Implement handler abort; new 'abort' directive (close #3871) (#3983)
* caddyhttp: Implement handler abort; new 'abort' directive (close #3871) * Move abort directive ordering; clean up redirects Seems logical for the end-all of handlers to go at the... end. The Connection header no longer needs to be set there, since Close is true, and the static_response handler now does that.
Matt Holt committed
Jan 28, 2021 at 12:54 UTC
e2c5c28597e6a8c5e7ef48fde88f0b7d740d9586
4 files changed
+27
-4
caddyconfig/httpcaddyfile/builtins.go
+10
@@ -40,6 +40,7 @@ func init() {
40
RegisterHandlerDirective("root", parseRoot)
41
RegisterHandlerDirective("redir", parseRedir)
42
RegisterHandlerDirective("respond", parseRespond)
43
+ RegisterHandlerDirective("abort", parseAbort)
44
RegisterHandlerDirective("route", parseRoute)
45
RegisterHandlerDirective("handle", parseHandle)
46
RegisterDirective("handle_errors", parseHandleErrors)
@@ -502,6 +503,15 @@ func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
503
return sr, nil
504
}
505
506
+// parseAbort parses the abort directive.
507
+func parseAbort(h Helper) (caddyhttp.MiddlewareHandler, error) {
508
+ h.Next() // consume directive
509
+ for h.Next() || h.NextBlock(0) {
510
+ return nil, h.ArgErr()
511
+ }
512
+ return &caddyhttp.StaticResponse{Abort: true}, nil
513
+}
514
+
515
// parseRoute parses the route directive.
516
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
517
sr := new(caddyhttp.Subroute)
caddyconfig/httpcaddyfile/directives.go
+1
@@ -69,6 +69,7 @@ var directiveOrder = []string{
69
"php_fastcgi",
70
"file_server",
71
"acme_server",
72
+ "abort",
73
}
74
75
// directiveIsOrdered returns true if dir is
modules/caddyhttp/autohttps.go
+1
-2
@@ -409,8 +409,7 @@ func (app *App) makeRedirRoute(redirToPort uint, matcherSet MatcherSet) Route {
409
StaticResponse{
410
StatusCode: WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
411
Headers: http.Header{
412
- "Location": []string{redirTo},
413
- "Connection": []string{"close"},
412
+ "Location": []string{redirTo},
413
},
414
Close: true,
415
},
modules/caddyhttp/staticresp.go
+15
-2
@@ -42,6 +42,11 @@ type StaticResponse struct {
42
// If true, the server will close the client's connection
43
// after writing the response.
44
Close bool `json:"close,omitempty"`
45
+
46
+ // Immediately and forcefully closes the connection without
47
+ // writing a response. Interrupts any other HTTP streams on
48
+ // the same connection.
49
+ Abort bool `json:"abort,omitempty"`
50
}
51
52
// CaddyModule returns the Caddy module information.
@@ -101,10 +106,18 @@ func (s *StaticResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
106
}
107
108
func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
104
- repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
109
+ // close the connection immediately
110
+ if s.Abort {
111
+ panic(http.ErrAbortHandler)
112
+ }
113
114
// close the connection after responding
107
- r.Close = s.Close
115
+ if s.Close {
116
+ r.Close = true
117
+ w.Header().Set("Connection", "close")
118
+ }
119
+
120
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
121
122
// set all headers
123
for field, vals := range s.Headers {