@@ -329,8 +329,8 @@ local function apply_http(conf)
329
329
local host , port , target
330
330
if node .server ~= nil then
331
331
target = {
332
- value = node .server ,
333
- is_httpd_role = true ,
332
+ value = ' httpd_ ' .. node .server ,
333
+ httpd_name = node . server ,
334
334
}
335
335
elseif node .listen ~= nil then
336
336
local err
@@ -339,23 +339,38 @@ local function apply_http(conf)
339
339
error (" failed to parse URI: " .. err , 2 )
340
340
end
341
341
target = {
342
- value = node .listen ,
343
- is_httpd_role = false ,
342
+ value = ' listen_' .. host .. ' :' .. tostring (port ),
344
343
}
345
344
else
346
345
target = {
347
- value = httpd_role .DEFAULT_SERVER_NAME ,
348
- is_httpd_role = true ,
346
+ value = ' httpd_ ' .. httpd_role .DEFAULT_SERVER_NAME ,
347
+ httpd_name = httpd_role . DEFAULT_SERVER_NAME ,
349
348
}
350
349
end
351
350
352
351
http_servers = http_servers or {}
352
+
353
353
-- Since the 'listen' and 'server' names of other servers in the config may be
354
- -- the same, we create a unique string concatenating the key name and information
355
- -- about whether it is an httpd key or not.
356
- enabled [tostring (target .value ) .. tostring (target .is_httpd_role )] = true
354
+ -- the same, we create a unique string concatenating the key (which is listen or server)
355
+ -- and its value.
356
+ enabled [target .value ] = true
357
+
358
+ if http_servers [target .value ] == nil then
359
+ if target .httpd_name == nil then
360
+ if host == ' 0.0.0.0' then
361
+ for k in pairs (http_servers ) do
362
+ if string.find (k , ' :' .. port ) ~= nil then
363
+ http_servers [k ].httpd :stop ()
364
+ enabled [k ] = false
365
+ end
366
+ end
367
+ elseif http_servers [' listen_0.0.0.0:' .. port ] ~= nil then
368
+ local key = ' listen_0.0.0.0:' .. port
369
+ http_servers [key ].httpd :stop ()
370
+ enabled [key ] = false
371
+ end
372
+ end
357
373
358
- if http_servers [tostring (target .value ) .. tostring (target .is_httpd_role )] == nil then
359
374
local httpd
360
375
if node .listen ~= nil then
361
376
httpd = http_server .new (host , port , {
@@ -368,21 +383,21 @@ local function apply_http(conf)
368
383
})
369
384
httpd :start ()
370
385
else
371
- httpd = httpd_role .get_server (target .value )
386
+ httpd = httpd_role .get_server (target .httpd_name )
372
387
if httpd == nil then
373
388
error ((' failed to get server by name %q, check that roles.httpd was' ..
374
- ' already applied' ):format (target .value ))
389
+ ' already applied' ):format (target .httpd_name ))
375
390
end
376
391
end
377
392
378
- http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ] = {
393
+ http_servers [target .value ] = {
379
394
httpd = httpd ,
380
395
routes = {},
381
- is_httpd_role = target .is_httpd_role ,
396
+ httpd_name = target .httpd_name ,
382
397
}
383
398
end
384
399
385
- local server = http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ]
400
+ local server = http_servers [target .value ]
386
401
local httpd = server .httpd
387
402
local old_routes = server .routes
388
403
@@ -422,14 +437,16 @@ local function apply_http(conf)
422
437
end
423
438
end
424
439
425
- for target , server in pairs (http_servers ) do
440
+ for target , server in pairs (http_servers or {} ) do
426
441
if not enabled [target ] then
427
- if server .is_httpd_role then
442
+ if server .httpd_name ~= nil then
428
443
for path , _ in pairs (server .routes ) do
429
444
server .httpd :delete (path )
430
445
end
431
446
else
432
- server .httpd :stop ()
447
+ if server .httpd .is_run == true then
448
+ server .httpd :stop ()
449
+ end
433
450
end
434
451
http_servers [target ] = nil
435
452
end
@@ -438,12 +455,14 @@ end
438
455
439
456
local function stop_http ()
440
457
for _ , server in pairs (http_servers or {}) do
441
- if server .is_httpd_role then
458
+ if server .httpd_name ~= nil then
442
459
for path , _ in pairs (server .routes ) do
443
460
server .httpd :delete (path )
444
461
end
445
462
else
446
- server .httpd :stop ()
463
+ if server .httpd .is_run == true then
464
+ server .httpd :stop ()
465
+ end
447
466
end
448
467
end
449
468
http_servers = nil
0 commit comments