MCPcopy
hub / github.com/caddyserver/caddy / cmdRespond

Function cmdRespond

modules/caddyhttp/staticresp.go:316–460  ·  view source on GitHub ↗
(fl caddycmd.Flags)

Source from the content-addressed store, hash-verified

314}
315
316func cmdRespond(fl caddycmd.Flags) (int, error) {
317 caddy.TrapSignals()
318
319 // get flag values
320 listen := fl.String("listen")
321 statusCodeFl := fl.Int("status")
322 bodyFl := fl.String("body")
323 accessLog := fl.Bool("access-log")
324 debug := fl.Bool("debug")
325 arg := fl.Arg(0)
326
327 if fl.NArg() > 1 {
328 return caddy.ExitCodeFailedStartup, fmt.Errorf("too many unflagged arguments")
329 }
330
331 // prefer status and body from explicit flags
332 statusCode, body := statusCodeFl, bodyFl
333
334 // figure out if status code was explicitly specified; this lets
335 // us set a non-zero value as the default but is a little hacky
336 statusCodeFlagSpecified := slices.Contains(os.Args, "--status")
337
338 // try to determine what kind of parameter the unnamed argument is
339 if arg != "" {
340 // specifying body and status flags makes the argument redundant/unused
341 if bodyFl != "" && statusCodeFlagSpecified {
342 return caddy.ExitCodeFailedStartup, fmt.Errorf("unflagged argument \"%s\" is overridden by flags", arg)
343 }
344
345 // if a valid 3-digit number, treat as status code; otherwise body
346 if argInt, err := strconv.Atoi(arg); err == nil && !statusCodeFlagSpecified {
347 if argInt >= 100 && argInt <= 999 {
348 statusCode = argInt
349 }
350 } else if body == "" {
351 body = arg
352 }
353 }
354
355 // if we still need a body, see if stdin is being piped
356 if body == "" {
357 stdinInfo, err := os.Stdin.Stat()
358 if err != nil {
359 return caddy.ExitCodeFailedStartup, err
360 }
361 if stdinInfo.Mode()&os.ModeNamedPipe != 0 {
362 bodyBytes, err := io.ReadAll(os.Stdin)
363 if err != nil {
364 return caddy.ExitCodeFailedStartup, err
365 }
366 body = string(bodyBytes)
367 }
368 }
369
370 // build headers map
371 headers, err := fl.GetStringArray("header")
372 if err != nil {
373 return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid header flag: %v", err)

Callers

nothing calls this directly

Calls 13

JSONFunction · 0.92
buildHTTPServerFunction · 0.85
StatMethod · 0.80
IsUnixNetworkMethod · 0.80
IsFdNetworkMethod · 0.80
PortRangeSizeMethod · 0.80
JoinHostPortMethod · 0.80
AppMethod · 0.80
AddrMethod · 0.80
StringMethod · 0.45
IntMethod · 0.45
BoolMethod · 0.45

Tested by

no test coverage detected