| 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 | "bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/" |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/" |
| 20 | "ioscip-go gomod github.com/golang/go/src go1.25.1 io/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.25.1 net/" |
| 22 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 23 | "pathscip-go gomod github.com/golang/go/src go1.25.1 path/" |
| 24 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/" |
| 25 | "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/" |
| 26 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 27 | |
| 28 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 29 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/" |
| 30 | ) |
| 31 | |
| 32 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/init().() { |
| 33 | caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#{}) |
| 34 | } |
| 35 | |
| 36 | // RequestMatcher is a type that can match to a request. |
| 37 | // A route matcher MUST NOT modify the request, with the |
| 38 | // only exception being its context. |
| 39 | // |
| 40 | // Deprecated: Matchers should now implement RequestMatcherWithError. |
| 41 | // You may remove any interface guards for RequestMatcher |
| 42 | // but keep your Match() methods for backwards compatibility. |
| 43 | type RequestMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher# interface { |
| 44 | Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#Match.(*httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) bool |
| 45 | } |
| 46 | |
| 47 | // RequestMatcherWithError is like RequestMatcher but can return an error. |
| 48 | // An error during matching will abort the request middleware chain and |
| 49 | // invoke the error middleware chain. |
| 50 | // |
| 51 | // This will eventually replace RequestMatcher. Matcher modules |
| 52 | // should implement both interfaces, and once all modules have |
| 53 | // been updated to use RequestMatcherWithError, the RequestMatcher |
| 54 | // interface may eventually be dropped. |
| 55 | type RequestMatcherWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError# interface { |
| 56 | MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#MatchWithError.(*httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) (bool, error) |
| 57 | } |
| 58 | |
| 59 | // Handler is like http.Handler except ServeHTTP may return an error. |
| 60 | // |
| 61 | // If any handler encounters an error, it should be returned for proper |
| 62 | // handling. Return values should be propagated down the middleware chain |
| 63 | // by returning it unchanged. Returned errors should not be re-wrapped |
| 64 | // if they are already HandlerError values. |
| 65 | type Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# interface { |
| 66 | ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error |
| 67 | } |
| 68 | |
| 69 | // HandlerFunc is a convenience type like http.HandlerFunc. |
| 70 | type HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc# func(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error |
| 71 | |
| 72 | // ServeHTTP implements the Handler interface. |
| 73 | func (f HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 74 | return f(w, r) |
| 75 | } |
| 76 | |
| 77 | // Middleware chains one Handler to the next by being passed |
| 78 | // the next Handler in the chain. |
| 79 | type Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware# func(Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# |
| 80 | |
| 81 | // MiddlewareHandler is like Handler except it takes as a third |
| 82 | // argument the next handler in the chain. The next handler will |
| 83 | // never be nil, but may be a no-op handler if this is the last |
| 84 | // handler in the chain. Handlers which act as middleware should |
| 85 | // call the next handler's ServeHTTP method so as to propagate |
| 86 | // the request down the chain properly. Handlers which act as |
| 87 | // responders (content origins) need not invoke the next handler, |
| 88 | // since the last handler in the chain should be the first to |
| 89 | // write the response. |
| 90 | type MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler# interface { |
| 91 | ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#ServeHTTP.(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) error |
| 92 | } |
| 93 | |
| 94 | // emptyHandler is used as a no-op handler. |
| 95 | var emptyHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/emptyHandler. Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# = HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(_ httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/emptyHandler:req. *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 96 | SetVarscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/SetVar().(reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/emptyHandler:req..Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), "unhandled", true) |
| 97 | return nil |
| 98 | }) |
| 99 | |
| 100 | // An implicit suffix middleware that, if reached, sets the StatusCode to the |
| 101 | // error stored in the ErrorCtxKey. This is to prevent situations where the |
| 102 | // Error chain does not actually handle the error (for instance, it matches only |
| 103 | // on some errors). See #3053 |
| 104 | var errorEmptyHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler. Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# = HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(wscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler:w. httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, rscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler:r. *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 105 | httpError := rscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler:r..Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(ErrorCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ErrorCtxKey.) |
| 106 | if handlerError, ok := httpError.(HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#); ok { |
| 107 | wscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler:w..WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(handlerError.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode.) |
| 108 | } else { |
| 109 | wscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler:w..WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError.) |
| 110 | } |
| 111 | return nil |
| 112 | }) |
| 113 | |
| 114 | // ResponseHandler pairs a response matcher with custom handling |
| 115 | // logic. Either the status code can be changed to something else |
| 116 | // while using the original response body, or, if a status code |
| 117 | // is not set, it can execute a custom route list; this is useful |
| 118 | // for executing handler routes based on the properties of an HTTP |
| 119 | // response that has not been written out to the client yet. |
| 120 | // |
| 121 | // To use this type, provision it at module load time, then when |
| 122 | // ready to use, match the response against its matcher; if it |
| 123 | // matches (or doesn't have a matcher), change the status code on |
| 124 | // the response if configured; otherwise invoke the routes by |
| 125 | // calling `rh.Routes.Compile(next).ServeHTTP(rw, req)` (or similar). |
| 126 | type ResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler# struct { |
| 127 | // The response matcher for this handler. If empty/nil, |
| 128 | // it always matches. |
| 129 | Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#Match. *ResponseMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseMatcher# `json:"match,omitempty"` |
| 130 | |
| 131 | // To write the original response body but with a different |
| 132 | // status code, set this field to the desired status code. |
| 133 | // If set, this takes priority over routes. |
| 134 | StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#StatusCode. WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString# `json:"status_code,omitempty"` |
| 135 | |
| 136 | // The list of HTTP routes to execute if no status code is |
| 137 | // specified. If evaluated, the original response body |
| 138 | // will not be written. |
| 139 | Routesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#Routes. RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList# `json:"routes,omitempty"` |
| 140 | } |
| 141 | |
| 142 | // Provision sets up the routes in rh. |
| 143 | func (rh *ResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#Provision().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 144 | if rh.Routesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#Routes. != nil { |
| 145 | err := rh.Routesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseHandler#Routes..Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#Provision().(ctx) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | } |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | // WeakString is a type that unmarshals any JSON value |
| 154 | // as a string literal, with the following exceptions: |
| 155 | // |
| 156 | // 1. actual string values are decoded as strings; and |
| 157 | // 2. null is decoded as empty string; |
| 158 | // |
| 159 | // and provides methods for getting the value as various |
| 160 | // primitive types. However, using this type removes any |
| 161 | // type safety as far as deserializing JSON is concerned. |
| 162 | type WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString# string |
| 163 | |
| 164 | // UnmarshalJSON satisfies json.Unmarshaler according to |
| 165 | // this type's documentation. |
| 166 | func (ws *WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) UnmarshalJSONscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#UnmarshalJSON().(b []byte) error { |
| 167 | if len(b) == 0 { |
| 168 | return ioscip-go gomod github.com/golang/go/src go1.25.1 io/.EOFscip-go gomod github.com/golang/go/src go1.25.1 io/EOF. |
| 169 | } |
| 170 | if b[0] == byte('"') && b[len(b)-1] == byte('"') { |
| 171 | var s string |
| 172 | err := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Unmarshal().(b, &s) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | *ws = WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#(s) |
| 177 | return nil |
| 178 | } |
| 179 | if bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Equalscip-go gomod github.com/golang/go/src go1.25.1 bytes/Equal().(b, []byte("null")) { |
| 180 | return nil |
| 181 | } |
| 182 | *ws = WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#(b) |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // MarshalJSON marshals was a boolean if true or false, |
| 187 | // a number if an integer, or a string otherwise. |
| 188 | func (ws WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) MarshalJSONscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#MarshalJSON().() ([]byte, error) { |
| 189 | if ws == "true" { |
| 190 | return []byte("true"), nil |
| 191 | } |
| 192 | if ws == "false" { |
| 193 | return []byte("false"), nil |
| 194 | } |
| 195 | if num, err := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.1 strconv/Atoi().(string(ws)); err == nil { |
| 196 | return jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Marshal().(num) |
| 197 | } |
| 198 | return jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Marshal().(string(ws)) |
| 199 | } |
| 200 | |
| 201 | // Int returns ws as an integer. If ws is not an |
| 202 | // integer, 0 is returned. |
| 203 | func (ws WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) Intscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#Int().() int { |
| 204 | num, _ := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.1 strconv/Atoi().(string(ws)) |
| 205 | return num |
| 206 | } |
| 207 | |
| 208 | // Float64 returns ws as a float64. If ws is not a |
| 209 | // float value, the zero value is returned. |
| 210 | func (ws WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) Float64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#Float64().() float64 { |
| 211 | num, _ := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.25.1 strconv/ParseFloat().(string(ws), 64) |
| 212 | return num |
| 213 | } |
| 214 | |
| 215 | // Bool returns ws as a boolean. If ws is not a |
| 216 | // boolean, false is returned. |
| 217 | func (ws WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) Boolscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#Bool().() bool { |
| 218 | return string(ws) == "true" |
| 219 | } |
| 220 | |
| 221 | // String returns ws as a string. |
| 222 | func (ws WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#) Stringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#String().() string { |
| 223 | return string(ws) |
| 224 | } |
| 225 | |
| 226 | // StatusCodeMatches returns true if a real HTTP status code matches |
| 227 | // the configured status code, which may be either a real HTTP status |
| 228 | // code or an integer representing a class of codes (e.g. 4 for all |
| 229 | // 4xx statuses). |
| 230 | func StatusCodeMatchesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StatusCodeMatches().(actual, configured int) bool { |
| 231 | if actual == configured { |
| 232 | return true |
| 233 | } |
| 234 | if configured < 100 && |
| 235 | actual >= configured*100 && |
| 236 | actual < (configured+1)*100 { |
| 237 | return true |
| 238 | } |
| 239 | return false |
| 240 | } |
| 241 | |
| 242 | // SanitizedPathJoin performs filepath.Join(root, reqPath) that |
| 243 | // is safe against directory traversal attacks. It uses logic |
| 244 | // similar to that in the Go standard library, specifically |
| 245 | // in the implementation of http.Dir. The root is assumed to |
| 246 | // be a trusted path, but reqPath is not; and the output will |
| 247 | // never be outside of root. The resulting path can be used |
| 248 | // with the local file system. If root is empty, the current |
| 249 | // directory is assumed. If the cleaned request path is deemed |
| 250 | // not local according to lexical processing (i.e. ignoring links), |
| 251 | // it will be rejected as unsafe and only the root will be returned. |
| 252 | func SanitizedPathJoinscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/SanitizedPathJoin().(root, reqPath string) string { |
| 253 | if root == "" { |
| 254 | root = "." |
| 255 | } |
| 256 | |
| 257 | relPath := pathscip-go gomod github.com/golang/go/src go1.25.1 path/.Cleanscip-go gomod github.com/golang/go/src go1.25.1 path/Clean().("/" + reqPath)[1:] // clean path and trim the leading / |
| 258 | if relPath != "" && !filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.IsLocalscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/IsLocal().(relPath) { |
| 259 | // path is unsafe (see https://github.com/golang/go/issues/56336#issuecomment-1416214885) |
| 260 | return root |
| 261 | } |
| 262 | |
| 263 | path := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(root, filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/FromSlash().(relPath)) |
| 264 | |
| 265 | // filepath.Join also cleans the path, and cleaning strips |
| 266 | // the trailing slash, so we need to re-add it afterwards. |
| 267 | // if the length is 1, then it's a path to the root, |
| 268 | // and that should return ".", so we don't append the separator. |
| 269 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix().(reqPath, "/") && len(reqPath) > 1 { |
| 270 | path += separatorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/separator. |
| 271 | } |
| 272 | |
| 273 | return path |
| 274 | } |
| 275 | |
| 276 | // CleanPath cleans path p according to path.Clean(), but only |
| 277 | // merges repeated slashes if collapseSlashes is true, and always |
| 278 | // preserves trailing slashes. |
| 279 | func CleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/CleanPath().(p string, collapseSlashes bool) string { |
| 280 | if collapseSlashes { |
| 281 | return cleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/cleanPath().(p) |
| 282 | } |
| 283 | |
| 284 | // insert an invalid/impossible URI character into each two consecutive |
| 285 | // slashes to expand empty path segments; then clean the path as usual, |
| 286 | // and then remove the remaining temporary characters. |
| 287 | const tmpCh = 0xff |
| 288 | var sb stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder# |
| 289 | for i, ch := range p { |
| 290 | if ch == '/' && i > 0 && p[i-1] == '/' { |
| 291 | sb.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().(tmpCh) |
| 292 | } |
| 293 | sb.WriteRunescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteRune().(ch) |
| 294 | } |
| 295 | halfCleaned := cleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/cleanPath().(sb.Stringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#String().()) |
| 296 | halfCleaned = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(halfCleaned, string([]byte{tmpCh}), "") |
| 297 | |
| 298 | return halfCleaned |
| 299 | } |
| 300 | |
| 301 | // cleanPath does path.Clean(p) but preserves any trailing slash. |
| 302 | func cleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/cleanPath().(p string) string { |
| 303 | cleaned := pathscip-go gomod github.com/golang/go/src go1.25.1 path/.Cleanscip-go gomod github.com/golang/go/src go1.25.1 path/Clean().(p) |
| 304 | if cleaned != "/" && stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix().(p, "/") { |
| 305 | cleaned = cleaned + "/" |
| 306 | } |
| 307 | return cleaned |
| 308 | } |
| 309 | |
| 310 | // tlsPlaceholderWrapper is a no-op listener wrapper that marks |
| 311 | // where the TLS listener should be in a chain of listener wrappers. |
| 312 | // It should only be used if another listener wrapper must be placed |
| 313 | // in front of the TLS handshake. |
| 314 | type tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper# struct{} |
| 315 | |
| 316 | func (tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# { |
| 317 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{ |
| 318 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "caddy.listeners.tls", |
| 319 | Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#) }, |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func (tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#) WrapListenerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#WrapListener().(ln netscip-go gomod github.com/golang/go/src go1.25.1 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.1 net/Listener#) netscip-go gomod github.com/golang/go/src go1.25.1 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.1 net/Listener# { return ln } |
| 324 | |
| 325 | func (tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#) UnmarshalCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#UnmarshalCaddyfile().(d *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#) error { return nil } |
| 326 | |
| 327 | const ( |
| 328 | // DefaultHTTPPort is the default port for HTTP. |
| 329 | DefaultHTTPPortscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/DefaultHTTPPort. = 80 |
| 330 | |
| 331 | // DefaultHTTPSPort is the default port for HTTPS. |
| 332 | DefaultHTTPSPortscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/DefaultHTTPSPort. = 443 |
| 333 | ) |
| 334 | |
| 335 | const separatorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/separator. = string(filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Separator.) |
| 336 | |
| 337 | // Interface guard |
| 338 | var ( |
| 339 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ListenerWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ListenerWrapper# = (*tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#)(nil) |
| 340 | _ caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Unmarshalerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Unmarshaler# = (*tlsPlaceholderWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/tlsPlaceholderWrapper#)(nil) |
| 341 | ) |