parseCaddyfileHandlePath parses the handle_path directive. Syntax: handle_path [<matcher>] { <directives...> } Only path matchers (with a `/` prefix) are supported as this is a shortcut for the handle directive with a strip_prefix rewrite.
(h httpcaddyfile.Helper)
| 232 | // Only path matchers (with a `/` prefix) are supported as this is a shortcut |
| 233 | // for the handle directive with a strip_prefix rewrite. |
| 234 | func parseCaddyfileHandlePath(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) { |
| 235 | h.Next() // consume directive name |
| 236 | |
| 237 | // there must be a path matcher |
| 238 | if !h.NextArg() { |
| 239 | return nil, h.ArgErr() |
| 240 | } |
| 241 | |
| 242 | // read the prefix to strip |
| 243 | path := h.Val() |
| 244 | if !strings.HasPrefix(path, "/") { |
| 245 | return nil, h.Errf("path matcher must begin with '/', got %s", path) |
| 246 | } |
| 247 | |
| 248 | // we only want to strip what comes before the '/' if |
| 249 | // the user specified it (e.g. /api/* should only strip /api) |
| 250 | var stripPath string |
| 251 | if strings.HasSuffix(path, "/*") { |
| 252 | stripPath = path[:len(path)-2] |
| 253 | } else if strings.HasSuffix(path, "*") { |
| 254 | stripPath = path[:len(path)-1] |
| 255 | } else { |
| 256 | stripPath = path |
| 257 | } |
| 258 | |
| 259 | // the ParseSegmentAsSubroute function expects the cursor |
| 260 | // to be at the token just before the block opening, |
| 261 | // so we need to rewind because we already read past it |
| 262 | h.Reset() |
| 263 | h.Next() |
| 264 | |
| 265 | // parse the block contents as a subroute handler |
| 266 | handler, err := httpcaddyfile.ParseSegmentAsSubroute(h) |
| 267 | if err != nil { |
| 268 | return nil, err |
| 269 | } |
| 270 | subroute, ok := handler.(*caddyhttp.Subroute) |
| 271 | if !ok { |
| 272 | return nil, h.Errf("segment was not parsed as a subroute") |
| 273 | } |
| 274 | |
| 275 | // make a matcher on the path and everything below it |
| 276 | pathMatcher := caddy.ModuleMap{ |
| 277 | "path": h.JSON(caddyhttp.MatchPath{path}), |
| 278 | } |
| 279 | |
| 280 | // build a route with a rewrite handler to strip the path prefix |
| 281 | route := caddyhttp.Route{ |
| 282 | HandlersRaw: []json.RawMessage{ |
| 283 | caddyconfig.JSONModuleObject(Rewrite{ |
| 284 | StripPathPrefix: stripPath, |
| 285 | }, "handler", "rewrite", nil), |
| 286 | }, |
| 287 | } |
| 288 | |
| 289 | // prepend the route to the subroute |
| 290 | subroute.Routes = append([]caddyhttp.Route{route}, subroute.Routes...) |
| 291 |
nothing calls this directly
no test coverage detected