-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalived
executable file
·196 lines (171 loc) · 3.75 KB
/
palived
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl -w
use v5.16.3;
use lib "./";
use strict;
use warnings;
use autodie;
#use Debug;
use Socket;
use IO::Socket;
use IO::File;
use Getopt::Long;
use threads ( 'yield', 'stack_size'=>64*4096, 'exit'=>'threads_only', 'stringify');
use threads::shared;
use my::debug;
my $port=0;
my $DOCUMENT_ROOT="/var/www/alived";
my $uid;
my $gid;
our %threads;
our @workers;
our @accepted; share(@accepted);
our @responsed; share(@responsed);
GetOptions ("port=i" => \$port, "docroot=s" => \$DOCUMENT_ROOT,
"user=s"=>\$uid,"group=s"=>\$gid) or die ("Error in command line arguments");
( defined($port) && $port != 0 ) or die "Usage: $0 --port=N [--docroot=/path] [--user=C] [--group=C]\n";
$threads{accepter} = threads->create(\&acceptance);
STDOUT->autoflush(1);
STDERR->autoflush(1);
my $c=5;
our %request = ();
our %data;
our %get;
our %post;
our $request_uri;
main_thread();
sub acceptance
{
STDOUT->autoflush(1);
STDERR->autoflush(1);
my $server;
my %fds;
logit("[t0] Acceptance started");
$server = new IO::Socket::INET(Proto => 'tcp'
,LocalPort => $port
,Listen => SOMAXCONN
,Reuse => 1
,Timeout=> 0.001
,Blocking=> 0
);
$server or die "Unable to create server socket: $!" ;
# my $client;
while ( 1 )
{
my $client = $server->accept();
if ( defined $client )
{
print "Connected? ".($client->connected)."\n";
logit("[t0] Client accepted on ".(fileno $client));
$client->autoflush(1);
my $fno=fileno $client;
$fds{$fno} = \$client;
push(@accepted, fileno $client);
undef $client;
}
# logit "Check responsed\n";
# while ( scalar @responsed > 0 )
foreach my $fd( @responsed )
{
# my $fd = shift @responsed;
if ( exists $fds{$fd} )
{
close $fds{$fd};
delete $fds{$fd};
}
#for(my $i=0;$i<scalar @fds;$i++)
#{
# # logit("fds[$i]=".$fds[$i]->connected);
# if ( fileno $fds[$i] == $fd ) { logit("deleted {$fds[$i]}"); close($fds[$i]); splice(@fds,$i,1);}
#}
}
}
}
sub main_thread
{
while( exists $threads{accepter} )
{
if ( $#accepted > -1 )
{
#print "fd=".join(", fd=", @accepted);
my $fd = shift(@accepted);
logit("[m] accepted client for $fd");
open (my $client, "+<&=$fd") or die "Can't open fd=$fd: $!";
parse_request($client, $client);
prepare_response($client);
push(@responsed, fileno $client);
close $client;
$c--;
}
if ( $c < 1 )
{
logit("[m] Detaching accepter");
$threads{accepter}->detach();
delete($threads{accepter});
}
sleep 0.01;
}
}
sub parse_request
{
my $client = shift;
my $out = shift || $client;
local $\="\n"; # print ending
local $/="\r\n"; # chomp cuts
while (<$client>)
{
chomp; # Main http request
print "$_";
if (/\s*(\w+)\s*([^\s]+)\s*HTTP\/(\d.\d)/) {
$request{METHOD} = uc $1;
$request{URL} = $2;
$request_uri = $2;
$request{HTTP_VERSION} = $3;
} # Standard headers
elsif (/:/) {
(my $type, my $val) = split /:/, $_, 2;
$type =~ s/^\s+//;
foreach ($type, $val) {
s/^\s+//;
s/\s+$//;
}
$request{lc $type} = $val;
} # POST data
elsif(/^$/) {
read($client, $request{CONTENT}, $request{'content-length'})
if defined $request{'content-length'};
last;
}
}
logit("End stdflow");
}
sub parse_request_get_data
{
# get request params are always present
if ($request{URL} =~ /(.*)\?(.*)/)
{
$request{URL} = $1;
#$request{CONTENT} = $2;
%get = parse_form($2);
}
else
{
%get = ();
}
}
sub parse_request_data
{
parse_request_get_data;
if ($request{METHOD} eq 'POST')
{
%post = parse_form($request{CONTENT});
}
}
sub prepare_response
{
my $client = shift;
local $\="\r\n";
print $client "HTTP/1.1 200 OK";
print $client "Connection: close";
print $client "";
print $client "<!DOCTYPE><html>allok</html>";
}