@@ -329,8 +329,8 @@ local function apply_http(conf)
329329 local host , port , target
330330 if node .server ~= nil then
331331 target = {
332- value = node .server ,
333- is_httpd_role = true ,
332+ value = ' httpd_ ' .. node .server ,
333+ httpd_name = node . server ,
334334 }
335335 elseif node .listen ~= nil then
336336 local err
@@ -339,23 +339,33 @@ local function apply_http(conf)
339339 error (" failed to parse URI: " .. err , 2 )
340340 end
341341 target = {
342- value = node .listen ,
343- is_httpd_role = false ,
342+ value = ' listen_' .. host .. ' :' .. tostring (port ),
344343 }
345344 else
346345 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 ,
349348 }
350349 end
351350
352351 http_servers = http_servers or {}
352+
353353 -- 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 and port of a server.
355+ enabled [target .value ] = true
356+
357+ if http_servers [target .value ] == nil then
358+ if target .httpd_name == nil then
359+ for k in pairs (http_servers ) do
360+ -- We stop running server before creating a new one if port matches and hosts different
361+ -- only on 'listen' target.
362+ if k ~= target .value and string.find (k , ' :' .. tostring (port )) ~= nil then
363+ http_servers [k ].httpd :stop ()
364+ enabled [k ] = false
365+ end
366+ end
367+ end
357368
358- if http_servers [tostring (target .value ) .. tostring (target .is_httpd_role )] == nil then
359369 local httpd
360370 if node .listen ~= nil then
361371 httpd = http_server .new (host , port , {
@@ -368,17 +378,17 @@ local function apply_http(conf)
368378 })
369379 httpd :start ()
370380 else
371- httpd = httpd_role .get_server (target .value )
381+ httpd = httpd_role .get_server (target .httpd_name )
372382 end
373383
374- http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ] = {
384+ http_servers [target .value ] = {
375385 httpd = httpd ,
376386 routes = {},
377- is_httpd_role = target .is_httpd_role ,
387+ httpd_name = target .httpd_name ,
378388 }
379389 end
380390
381- local server = http_servers [tostring ( target .value ) .. tostring ( target . is_httpd_role ) ]
391+ local server = http_servers [target .value ]
382392 local httpd = server .httpd
383393 local old_routes = server .routes
384394
@@ -418,14 +428,16 @@ local function apply_http(conf)
418428 end
419429 end
420430
421- for target , server in pairs (http_servers ) do
431+ for target , server in pairs (http_servers or {} ) do
422432 if not enabled [target ] then
423- if server .is_httpd_role then
433+ if server .httpd_name ~= nil then
424434 for path , _ in pairs (server .routes ) do
425435 server .httpd :delete (path )
426436 end
427437 else
428- server .httpd :stop ()
438+ if server .httpd .is_run == true then
439+ server .httpd :stop ()
440+ end
429441 end
430442 http_servers [target ] = nil
431443 end
@@ -434,12 +446,14 @@ end
434446
435447local function stop_http ()
436448 for _ , server in pairs (http_servers or {}) do
437- if server .is_httpd_role then
449+ if server .httpd_name ~= nil then
438450 for path , _ in pairs (server .routes ) do
439451 server .httpd :delete (path )
440452 end
441453 else
442- server .httpd :stop ()
454+ if server .httpd .is_run == true then
455+ server .httpd :stop ()
456+ end
443457 end
444458 end
445459 http_servers = nil
0 commit comments