2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-07 10:56:13 -05:00
|
|
|
package caddyhttp
|
2019-05-04 14:21:20 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-05-20 22:21:33 -05:00
|
|
|
"strconv"
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-08-09 13:05:47 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2019-05-04 14:21:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 11:46:35 -05:00
|
|
|
caddy.RegisterModule(StaticResponse{})
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
// StaticResponse implements a simple responder for static responses.
|
|
|
|
type StaticResponse struct {
|
2019-12-23 14:45:35 -05:00
|
|
|
// The HTTP status code to respond with. Can be an integer or,
|
|
|
|
// if needing to use a placeholder, a string.
|
|
|
|
StatusCode WeakString `json:"status_code,omitempty"`
|
|
|
|
|
|
|
|
// Header fields to set on the response.
|
|
|
|
Headers http.Header `json:"headers,omitempty"`
|
|
|
|
|
|
|
|
// The response body.
|
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
|
|
|
|
// If true, the server will close the client's connection
|
|
|
|
// after writing the response.
|
|
|
|
Close bool `json:"close,omitempty"`
|
2021-01-28 14:54:55 -05:00
|
|
|
|
|
|
|
// Immediately and forcefully closes the connection without
|
|
|
|
// writing a response. Interrupts any other HTTP streams on
|
|
|
|
// the same connection.
|
|
|
|
Abort bool `json:"abort,omitempty"`
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (StaticResponse) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.handlers.static_response",
|
|
|
|
New: func() caddy.Module { return new(StaticResponse) },
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:05:47 -05:00
|
|
|
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2020-01-22 11:32:38 -05:00
|
|
|
// respond [<matcher>] <status>|<body> [<status>] {
|
2019-08-09 13:05:47 -05:00
|
|
|
// body <text>
|
|
|
|
// close
|
|
|
|
// }
|
|
|
|
//
|
2020-01-17 12:57:57 -05:00
|
|
|
// If there is just one argument (other than the matcher), it is considered
|
|
|
|
// to be a status code if it's a valid positive integer of 3 digits.
|
2019-08-09 13:05:47 -05:00
|
|
|
func (s *StaticResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
2020-01-17 12:57:57 -05:00
|
|
|
args := d.RemainingArgs()
|
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
if len(args[0]) == 3 {
|
|
|
|
if num, err := strconv.Atoi(args[0]); err == nil && num > 0 {
|
|
|
|
s.StatusCode = WeakString(args[0])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Body = args[0]
|
|
|
|
case 2:
|
|
|
|
s.Body = args[0]
|
|
|
|
s.StatusCode = WeakString(args[1])
|
|
|
|
default:
|
|
|
|
return d.ArgErr()
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
2020-01-17 12:57:57 -05:00
|
|
|
|
2019-09-10 20:21:52 -05:00
|
|
|
for d.NextBlock(0) {
|
2019-08-09 13:05:47 -05:00
|
|
|
switch d.Val() {
|
|
|
|
case "body":
|
|
|
|
if s.Body != "" {
|
|
|
|
return d.Err("body already specified")
|
|
|
|
}
|
2020-01-17 12:57:57 -05:00
|
|
|
if !d.AllArgs(&s.Body) {
|
2019-08-09 13:05:47 -05:00
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
case "close":
|
|
|
|
if s.Close {
|
|
|
|
return d.Err("close already specified")
|
|
|
|
}
|
|
|
|
s.Close = true
|
2021-02-02 18:19:58 -05:00
|
|
|
default:
|
|
|
|
return d.Errf("unrecognized subdirective '%s'", d.Val())
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
|
2021-01-28 14:54:55 -05:00
|
|
|
// close the connection immediately
|
|
|
|
if s.Abort {
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
}
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-05-16 12:46:17 -05:00
|
|
|
// close the connection after responding
|
2021-01-28 14:54:55 -05:00
|
|
|
if s.Close {
|
|
|
|
r.Close = true
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
}
|
|
|
|
|
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-05-20 23:00:54 -05:00
|
|
|
// set all headers
|
2019-05-04 14:21:20 -05:00
|
|
|
for field, vals := range s.Headers {
|
2019-05-20 11:59:20 -05:00
|
|
|
field = repl.ReplaceAll(field, "")
|
2019-06-21 15:36:26 -05:00
|
|
|
newVals := make([]string, len(vals))
|
2019-05-04 14:21:20 -05:00
|
|
|
for i := range vals {
|
2019-06-21 15:36:26 -05:00
|
|
|
newVals[i] = repl.ReplaceAll(vals[i], "")
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
2019-06-21 15:36:26 -05:00
|
|
|
w.Header()[field] = newVals
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:59:17 -05:00
|
|
|
// do not allow Go to sniff the content-type
|
|
|
|
if w.Header().Get("Content-Type") == "" {
|
|
|
|
w.Header()["Content-Type"] = nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:32:01 -05:00
|
|
|
// get the status code; if this handler exists in an error route,
|
|
|
|
// use the recommended status code as the default; otherwise 200
|
2019-07-09 13:58:39 -05:00
|
|
|
statusCode := http.StatusOK
|
2020-06-04 11:32:01 -05:00
|
|
|
if reqErr, ok := r.Context().Value(ErrorCtxKey).(error); ok {
|
|
|
|
if handlerErr, ok := reqErr.(HandlerError); ok {
|
|
|
|
if handlerErr.StatusCode > 0 {
|
|
|
|
statusCode = handlerErr.StatusCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
if codeStr := s.StatusCode.String(); codeStr != "" {
|
|
|
|
intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, ""))
|
|
|
|
if err != nil {
|
|
|
|
return Error(http.StatusInternalServerError, err)
|
2019-05-20 22:21:33 -05:00
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
statusCode = intVal
|
2019-05-20 22:21:33 -05:00
|
|
|
}
|
2019-05-20 23:00:54 -05:00
|
|
|
|
|
|
|
// write headers
|
2019-05-04 14:21:20 -05:00
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
|
2019-05-20 23:00:54 -05:00
|
|
|
// write response body
|
2019-05-04 14:21:20 -05:00
|
|
|
if s.Body != "" {
|
2019-10-28 15:39:37 -05:00
|
|
|
fmt.Fprint(w, repl.ReplaceKnown(s.Body, ""))
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:04:18 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ MiddlewareHandler = (*StaticResponse)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*StaticResponse)(nil)
|
|
|
|
)
|