| 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 caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ |
| 16 | |
| 17 | import ( |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 20 | weakrandscip-go gomod github.com/golang/go/src go1.25.1 `math/rand/v2`/ "math/rand/v2scip-go gomod github.com/golang/go/src go1.25.1 `math/rand/v2`/" |
| 21 | "pathscip-go gomod github.com/golang/go/src go1.25.1 path/" |
| 22 | "runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/" |
| 23 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 24 | |
| 25 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 26 | ) |
| 27 | |
| 28 | // Error is a convenient way for a Handler to populate the |
| 29 | // essential fields of a HandlerError. If err is itself a |
| 30 | // HandlerError, then any essential fields that are not |
| 31 | // set will be populated. |
| 32 | func Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Error().(statusCode int, err error) HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError# { |
| 33 | const idLen = 9 |
| 34 | var he HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError# |
| 35 | if errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Asscip-go gomod github.com/golang/go/src go1.25.1 errors/As().(err, &he) { |
| 36 | if he.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID. == "" { |
| 37 | he.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID. = randStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/randString().(idLen, true) |
| 38 | } |
| 39 | if he.Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace. == "" { |
| 40 | he.Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace. = tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/trace().() |
| 41 | } |
| 42 | if he.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode. == 0 { |
| 43 | he.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode. = statusCode |
| 44 | } |
| 45 | return he |
| 46 | } |
| 47 | return HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#{ |
| 48 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID.: randStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/randString().(idLen, true), |
| 49 | StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode.: statusCode, |
| 50 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Err.: err, |
| 51 | Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace.: tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/trace().(), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // HandlerError is a serializable representation of |
| 56 | // an error from within an HTTP handler. |
| 57 | type HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError# struct { |
| 58 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Err. error // the original error value and message |
| 59 | StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode. int // the HTTP status code to associate with this error |
| 60 | |
| 61 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID. string // generated; for identifying this error in logs |
| 62 | Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace. string // produced from call stack |
| 63 | } |
| 64 | |
| 65 | func (e HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#) Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Error().() string { |
| 66 | var s string |
| 67 | if e.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID. != "" { |
| 68 | s += fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("{id=%s}", e.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#ID.) |
| 69 | } |
| 70 | if e.Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace. != "" { |
| 71 | s += " " + e.Tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Trace. |
| 72 | } |
| 73 | if e.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode. != 0 { |
| 74 | s += fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().(": HTTP %d", e.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode.) |
| 75 | } |
| 76 | if e.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Err. != nil { |
| 77 | s += ": " + e.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Err..Error() |
| 78 | } |
| 79 | return stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.1 strings/TrimSpace().(s) |
| 80 | } |
| 81 | |
| 82 | // Unwrap returns the underlying error value. See the `errors` package for info. |
| 83 | func (e HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#) Unwrapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Unwrap().() error { return e.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#Err. } |
| 84 | |
| 85 | // randString returns a string of n random characters. |
| 86 | // It is not even remotely secure OR a proper distribution. |
| 87 | // But it's good enough for some things. It excludes certain |
| 88 | // confusing characters like I, l, 1, 0, O, etc. If sameCase |
| 89 | // is true, then uppercase letters are excluded. |
| 90 | func randStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/randString().(n int, sameCase bool) string { |
| 91 | if n <= 0 { |
| 92 | return "" |
| 93 | } |
| 94 | dict := []byte("abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY23456789") |
| 95 | if sameCase { |
| 96 | dict = []byte("abcdefghijkmnpqrstuvwxyz0123456789") |
| 97 | } |
| 98 | b := make([]byte, n) |
| 99 | for i := range b { |
| 100 | //nolint:gosec |
| 101 | b[i] = dict[weakrandscip-go gomod github.com/golang/go/src go1.25.1 `math/rand/v2`/.IntNscip-go gomod github.com/golang/go/src go1.25.1 `math/rand/v2`/IntN().(len(dict))] |
| 102 | } |
| 103 | return string(b) |
| 104 | } |
| 105 | |
| 106 | func tracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/trace().() string { |
| 107 | if pc, file, line, ok := runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/.Callerscip-go gomod github.com/golang/go/src go1.25.1 runtime/Caller().(2); ok { |
| 108 | filename := pathscip-go gomod github.com/golang/go/src go1.25.1 path/.Basescip-go gomod github.com/golang/go/src go1.25.1 path/Base().(file) |
| 109 | pkgAndFuncName := pathscip-go gomod github.com/golang/go/src go1.25.1 path/.Basescip-go gomod github.com/golang/go/src go1.25.1 path/Base().(runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/.FuncForPCscip-go gomod github.com/golang/go/src go1.25.1 runtime/FuncForPC().(pc).Namescip-go gomod github.com/golang/go/src go1.25.1 runtime/Func#Name().()) |
| 110 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s (%s:%d)", pkgAndFuncName, filename, line) |
| 111 | } |
| 112 | return "" |
| 113 | } |
| 114 | |
| 115 | // ErrorCtxKey is the context key to use when storing |
| 116 | // an error (for use with context.Context). |
| 117 | const ErrorCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ErrorCtxKey. = caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.CtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/CtxKey#("handler_chain_error") |