@@ -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,34 @@ 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 host, port of a server
355
+ -- and its key value which are 'httpd' or 'listen'.
356
+ enabled [target .value ] = true
357
+
358
+ if http_servers [target .value ] == nil then
359
+ if target .httpd_name == nil then
360
+ for k in pairs (http_servers ) do
361
+ -- We stop running server before creating a new one if port matches and hosts different
362
+ -- only on 'listen' target.
363
+ if string.find (k , target .value ) == nil and string.find (k , ' :' .. tostring (port )) ~= nil then
364
+ http_servers [k ].httpd :stop ()
365
+ enabled [k ] = false
366
+ end
367
+ end
368
+ end
357
369
358
- if http_servers [tostring (target .value ) .. tostring (target .is_httpd_role )] == nil then
359
370
local httpd
360
371
if node .listen ~= nil then
361
372
httpd = http_server .new (host , port , {
@@ -368,17 +379,17 @@ local function apply_http(conf)
368
379
})
369
380
httpd :start ()
370
381
else
371
- httpd = httpd_role .get_server (target .value )
382
+ httpd = httpd_role .get_server (target .httpd_name )
372
383
end
373
384
374
- http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ] = {
385
+ http_servers [target .value ] = {
375
386
httpd = httpd ,
376
387
routes = {},
377
- is_httpd_role = target .is_httpd_role ,
388
+ httpd_name = target .httpd_name ,
378
389
}
379
390
end
380
391
381
- local server = http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ]
392
+ local server = http_servers [target .value ]
382
393
local httpd = server .httpd
383
394
local old_routes = server .routes
384
395
@@ -418,14 +429,16 @@ local function apply_http(conf)
418
429
end
419
430
end
420
431
421
- for target , server in pairs (http_servers ) do
432
+ for target , server in pairs (http_servers or {} ) do
422
433
if not enabled [target ] then
423
- if server .is_httpd_role then
434
+ if server .httpd_name ~= nil then
424
435
for path , _ in pairs (server .routes ) do
425
436
server .httpd :delete (path )
426
437
end
427
438
else
428
- server .httpd :stop ()
439
+ if server .httpd .is_run == true then
440
+ server .httpd :stop ()
441
+ end
429
442
end
430
443
http_servers [target ] = nil
431
444
end
@@ -434,12 +447,14 @@ end
434
447
435
448
local function stop_http ()
436
449
for _ , server in pairs (http_servers or {}) do
437
- if server .is_httpd_role then
450
+ if server .httpd_name ~= nil then
438
451
for path , _ in pairs (server .routes ) do
439
452
server .httpd :delete (path )
440
453
end
441
454
else
442
- server .httpd :stop ()
455
+ if server .httpd .is_run == true then
456
+ server .httpd :stop ()
457
+ end
443
458
end
444
459
end
445
460
http_servers = nil
0 commit comments