Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 19:30 UTC
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.
networkException committed
Jan 1, 2024 at 04:34 UTC
b568a10dd4feedd5539b181e26a5bbafebd4f984
1 file changed
+66
-46
modules/caddyhttp/staticresp.go
+66
-46
@@ -257,6 +257,53 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, next H
257
return nil
258
}
259
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
+
307
func cmdRespond(fl caddycmd.Flags) (int, error) {
308
caddy.TrapSignals()
309
@@ -332,65 +379,38 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
379
hdr.Set(key, val)
380
}
381
382
+ // build each HTTP server
383
+ httpApp := App{Servers: make(map[string]*Server)}
384
+
385
// expand listen address, if more than one port
386
listenAddr, err := caddy.ParseNetworkAddress(listen)
387
if err != nil {
388
return caddy.ExitCodeFailedStartup, err
389
}
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)}
390
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))
395
}
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
405
}
365
- buf := new(bytes.Buffer)
366
- err = tpl.Execute(buf, tplCtx)
406
+ } else {
407
+ server, err := buildHTTPServer(0, 0, listen, statusCode, hdr, body, accessLog)
408
if err != nil {
409
return caddy.ExitCodeFailedStartup, err
410
}
411
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
-
412
// save server
393
- httpApp.Servers[fmt.Sprintf("static%d", i)] = server
413
+ httpApp.Servers[fmt.Sprintf("static%d", 0)] = server
414
}
415
416
// finish building the config