Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttp |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net/http" |
| 20 | "strconv" |
| 21 | |
| 22 | "github.com/caddyserver/caddy/v2" |
| 23 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | caddy.RegisterModule(StaticError{}) |
| 28 | } |
| 29 | |
| 30 | // StaticError implements a simple handler that returns an error. |
| 31 | // This handler returns an error value, but does not write a response. |
| 32 | // This is useful when you want the server to act as if an error |
| 33 | // occurred; for example, to invoke your custom error handling logic. |
| 34 | // |
| 35 | // Since this handler does not write a response, the error information |
| 36 | // is for use by the server to know how to handle the error. |
| 37 | type StaticError struct { |
| 38 | // The error message. Optional. Default is no error message. |
| 39 | Error string `json:"error,omitempty"` |
| 40 | |
| 41 | // The recommended HTTP status code. Can be either an integer or a |
| 42 | // string if placeholders are needed. Optional. Default is 500. |
| 43 | StatusCode WeakString `json:"status_code,omitempty"` |
| 44 | } |
| 45 | |
| 46 | // CaddyModule returns the Caddy module information. |
| 47 | func (StaticError) CaddyModule() caddy.ModuleInfo { |
| 48 | return caddy.ModuleInfo{ |
| 49 | ID: "http.handlers.error", |
| 50 | New: func() caddy.Module { return new(StaticError) }, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax: |
| 55 | // |
| 56 | // error [<matcher>] <status>|<message> [<status>] { |
| 57 | // message <text> |
| 58 | // } |
| 59 | // |
| 60 | // If there is just one argument (other than the matcher), it is considered |
| 61 | // to be a status code if it's a valid positive integer of 3 digits. |
| 62 | func (e *StaticError) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 63 | d.Next() // consume directive name |
| 64 | args := d.RemainingArgs() |
| 65 | switch len(args) { |
| 66 | case 1: |
| 67 | if len(args[0]) == 3 { |
| 68 | if num, err := strconv.Atoi(args[0]); err == nil && num > 0 { |
| 69 | e.StatusCode = WeakString(args[0]) |
| 70 | break |
| 71 | } |
| 72 | } |
| 73 | e.Error = args[0] |
| 74 | case 2: |
| 75 | e.Error = args[0] |
| 76 | e.StatusCode = WeakString(args[1]) |
| 77 | default: |
| 78 | return d.ArgErr() |
| 79 | } |
| 80 | |
| 81 | for d.NextBlock(0) { |
| 82 | switch d.Val() { |
| 83 | case "message": |
| 84 | if e.Error != "" { |
| 85 | return d.Err("message already specified") |
| 86 | } |
| 87 | if !d.AllArgs(&e.Error) { |
| 88 | return d.ArgErr() |
| 89 | } |
| 90 | default: |
| 91 | return d.Errf("unrecognized subdirective '%s'", d.Val()) |
| 92 | } |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (e StaticError) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error { |
| 98 | repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) |
| 99 | |
| 100 | statusCode := http.StatusInternalServerError |
| 101 | if codeStr := e.StatusCode.String(); codeStr != "" { |
| 102 | intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, "")) |
| 103 | if err != nil { |
| 104 | return Error(http.StatusInternalServerError, err) |
| 105 | } |
| 106 | statusCode = intVal |
| 107 | } |
| 108 | return Error(statusCode, fmt.Errorf("%s", repl.ReplaceKnown(e.Error, ""))) |
| 109 | } |
| 110 | |
| 111 | // Interface guard |
| 112 | var ( |
| 113 | _ MiddlewareHandler = (*StaticError)(nil) |
| 114 | _ caddyfile.Unmarshaler = (*StaticError)(nil) |
| 115 | ) |