Skip to content

Commit b568a10

Browse files
caddyhttp: support unix sockets in caddy respond command (#6010)
previously the `caddy respond` command would treat the argument passed to --listen as a TCP socket address, iterating over a possible port range. this patch factors the server creation out into a separate function, allowing this to be reused in case the listen address is a unix network address.
1 parent 8f9ffc5 commit b568a10

File tree

1 file changed

+66
-46
lines changed

1 file changed

+66
-46
lines changed

modules/caddyhttp/staticresp.go

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,53 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, next H
257257
return nil
258258
}
259259

260+
func buildHTTPServer(i int, port uint, addr string, statusCode int, hdr http.Header, body string, accessLog bool) (*Server, error) {
261+
var handlers []json.RawMessage
262+
263+
// response body supports a basic template; evaluate it
264+
tplCtx := struct {
265+
N int // server number
266+
Port uint // only the port
267+
Address string // listener address
268+
}{
269+
N: i,
270+
Port: port,
271+
Address: addr,
272+
}
273+
tpl, err := template.New("body").Parse(body)
274+
if err != nil {
275+
return nil, err
276+
}
277+
buf := new(bytes.Buffer)
278+
err = tpl.Execute(buf, tplCtx)
279+
if err != nil {
280+
return nil, err
281+
}
282+
283+
// create route with handler
284+
handler := StaticResponse{
285+
StatusCode: WeakString(fmt.Sprintf("%d", statusCode)),
286+
Headers: hdr,
287+
Body: buf.String(),
288+
}
289+
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "static_response", nil))
290+
route := Route{HandlersRaw: handlers}
291+
292+
server := &Server{
293+
Listen: []string{addr},
294+
ReadHeaderTimeout: caddy.Duration(10 * time.Second),
295+
IdleTimeout: caddy.Duration(30 * time.Second),
296+
MaxHeaderBytes: 1024 * 10,
297+
Routes: RouteList{route},
298+
AutoHTTPS: &AutoHTTPSConfig{DisableRedir: true},
299+
}
300+
if accessLog {
301+
server.Logs = new(ServerLogConfig)
302+
}
303+
304+
return server, nil
305+
}
306+
260307
func cmdRespond(fl caddycmd.Flags) (int, error) {
261308
caddy.TrapSignals()
262309

@@ -332,65 +379,38 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
332379
hdr.Set(key, val)
333380
}
334381

382+
// build each HTTP server
383+
httpApp := App{Servers: make(map[string]*Server)}
384+
335385
// expand listen address, if more than one port
336386
listenAddr, err := caddy.ParseNetworkAddress(listen)
337387
if err != nil {
338388
return caddy.ExitCodeFailedStartup, err
339389
}
340-
listenAddrs := make([]string, 0, listenAddr.PortRangeSize())
341-
for offset := uint(0); offset < listenAddr.PortRangeSize(); offset++ {
342-
listenAddrs = append(listenAddrs, listenAddr.JoinHostPort(offset))
343-
}
344-
345-
// build each HTTP server
346-
httpApp := App{Servers: make(map[string]*Server)}
347390

348-
for i, addr := range listenAddrs {
349-
var handlers []json.RawMessage
350-
351-
// response body supports a basic template; evaluate it
352-
tplCtx := struct {
353-
N int // server number
354-
Port uint // only the port
355-
Address string // listener address
356-
}{
357-
N: i,
358-
Port: listenAddr.StartPort + uint(i),
359-
Address: addr,
391+
if !listenAddr.IsUnixNetwork() {
392+
listenAddrs := make([]string, 0, listenAddr.PortRangeSize())
393+
for offset := uint(0); offset < listenAddr.PortRangeSize(); offset++ {
394+
listenAddrs = append(listenAddrs, listenAddr.JoinHostPort(offset))
360395
}
361-
tpl, err := template.New("body").Parse(body)
362-
if err != nil {
363-
return caddy.ExitCodeFailedStartup, err
396+
397+
for i, addr := range listenAddrs {
398+
server, err := buildHTTPServer(i, listenAddr.StartPort+uint(i), addr, statusCode, hdr, body, accessLog)
399+
if err != nil {
400+
return caddy.ExitCodeFailedStartup, err
401+
}
402+
403+
// save server
404+
httpApp.Servers[fmt.Sprintf("static%d", i)] = server
364405
}
365-
buf := new(bytes.Buffer)
366-
err = tpl.Execute(buf, tplCtx)
406+
} else {
407+
server, err := buildHTTPServer(0, 0, listen, statusCode, hdr, body, accessLog)
367408
if err != nil {
368409
return caddy.ExitCodeFailedStartup, err
369410
}
370411

371-
// create route with handler
372-
handler := StaticResponse{
373-
StatusCode: WeakString(fmt.Sprintf("%d", statusCode)),
374-
Headers: hdr,
375-
Body: buf.String(),
376-
}
377-
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "static_response", nil))
378-
route := Route{HandlersRaw: handlers}
379-
380-
server := &Server{
381-
Listen: []string{addr},
382-
ReadHeaderTimeout: caddy.Duration(10 * time.Second),
383-
IdleTimeout: caddy.Duration(30 * time.Second),
384-
MaxHeaderBytes: 1024 * 10,
385-
Routes: RouteList{route},
386-
AutoHTTPS: &AutoHTTPSConfig{DisableRedir: true},
387-
}
388-
if accessLog {
389-
server.Logs = new(ServerLogConfig)
390-
}
391-
392412
// save server
393-
httpApp.Servers[fmt.Sprintf("static%d", i)] = server
413+
httpApp.Servers[fmt.Sprintf("static%d", 0)] = server
394414
}
395415

396416
// finish building the config

0 commit comments

Comments
 (0)