-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathhttp_proxy.pl
86 lines (66 loc) · 2.27 KB
/
http_proxy.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use File::Temp qw( tempfile );
my $http_proxy_pid;
$ENV{TEST_NGINX_HTTP_PROXY_PORT} = Test::APIcast->get_random_port();
$ENV{TEST_NGINX_HTTP_PROXY} = "http://$Test::Nginx::Util::ServerAddr:$ENV{TEST_NGINX_HTTP_PROXY_PORT}";
$ENV{TEST_NGINX_HTTPS_PROXY} = "http://$Test::Nginx::Util::ServerAddr:$ENV{TEST_NGINX_HTTP_PROXY_PORT}";
sub start_proxy {
if ($http_proxy_pid = fork) {
if ($Test::Nginx::Util::Verbose) {
warn "started proxy process $http_proxy_pid";
}
sleep( Test::Nginx::Util->sleep_time() );
} else {
my $err_log_file = $Test::Nginx::Util::ErrLogFile;
my ($proxy_config, $proxy_config_file) = tempfile();
print $proxy_config Test::Nginx::Util::expand_env_in_config <<'NGINX';
daemon off;
events {
worker_connections 1024;
}
stream {
lua_code_cache on;
lua_socket_log_errors off;
resolver local=on;
init_worker_by_lua_block { ngx.log(ngx.INFO, 'started proxy worker') }
init_by_lua_block { proxy = require('t.fixtures.proxy') }
# define a TCP server listening on the port 1234:
server {
listen $TEST_NGINX_HTTP_PROXY_PORT;
content_by_lua_block { proxy() }
}
}
NGINX
close $proxy_config;
warn($proxy_config_file);
if ($Test::Nginx::Util::Verbose) {
warn "starting proxy process on port $ENV{TEST_NGINX_HTTP_PROXY_PORT}";
}
exec("$Test::Nginx::Util::NginxBinary",
'-p', $Test::Nginx::Util::ServRoot,
'-c', $proxy_config_file,
'-g', "error_log $err_log_file $Test::Nginx::Util::LogLevel;"
);
}
}
Test::Nginx::Socket::set_http_config_filter(sub {
my $config = shift;
if (defined $http_proxy_pid) {
if ($Test::Nginx::Util::Verbose) {
warn "reloading proxy process $http_proxy_pid";
}
# reload proxy to reopen log file
kill 'HUP', $http_proxy_pid;
} else {
start_proxy();
}
return $config;
});
add_cleanup_handler(sub {
if (defined $http_proxy_pid && !$ENV{TEST_NGINX_NO_CLEAN}) {
if ($Test::Nginx::Util::Verbose) {
warn "stopping proxy process $http_proxy_pid";
}
Test::Nginx::Util::kill_process($http_proxy_pid, 1, 'proxy');
undef $http_proxy_pid;
}
});