summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/staticresp.go
diff options
context:
space:
mode:
authorMatt Holt <mholt@users.noreply.github.com>2022-09-05 13:50:44 -0600
committerGitHub <noreply@github.com>2022-09-05 13:50:44 -0600
commitca4fae64d99a63291a91e59af5a1e8eef8c8e2d8 (patch)
tree45ceb6051ca12027b8b323ac59ca5833a0f0a6ce /modules/caddyhttp/staticresp.go
parentad69503aefeead7782022e8e8698c16b1e6c638d (diff)
caddyhttp: Support `respond` with HTTP 103 Early Hints (#5006)
* caddyhttp: Support sending HTTP 103 Early Hints This adds support for early hints in the static_response handler. * caddyhttp: Don't record 1xx responses
Diffstat (limited to 'modules/caddyhttp/staticresp.go')
-rw-r--r--modules/caddyhttp/staticresp.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go
index f429692..ccc70e2 100644
--- a/modules/caddyhttp/staticresp.go
+++ b/modules/caddyhttp/staticresp.go
@@ -86,6 +86,12 @@ Response headers may be added using the --header flag for each header field.
type StaticResponse struct {
// The HTTP status code to respond with. Can be an integer or,
// if needing to use a placeholder, a string.
+ //
+ // If the status code is 103 (Early Hints), the response headers
+ // will be written to the client immediately, the body will be
+ // ignored, and the next handler will be invoked. This behavior
+ // is EXPERIMENTAL while RFC 8297 is a draft, and may be changed
+ // or removed.
StatusCode WeakString `json:"status_code,omitempty"`
// Header fields to set on the response; overwrites any existing
@@ -170,7 +176,7 @@ func (s *StaticResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}
-func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
+func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
// close the connection immediately
if s.Abort {
panic(http.ErrAbortHandler)
@@ -237,10 +243,15 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Hand
w.WriteHeader(statusCode)
// write response body
- if body != "" {
+ if statusCode != http.StatusEarlyHints && body != "" {
fmt.Fprint(w, body)
}
+ // continue handling after Early Hints as they are not the final response
+ if statusCode == http.StatusEarlyHints {
+ return next.ServeHTTP(w, r)
+ }
+
return nil
}