1 #################################################################################################
2 #################################################################################################
3 # HTTP::Daemon Operations
4 #################################################################################################
5 #################################################################################################
7 # do not accumulate defunct processes
9 $SIG{USR1} = \&listen; # sent by the child to make the parent read the pipe
11 pipe LIST_CHILD, TELL_PARENT;
12 pipe LIST_PARENT, TELL_CHILD;
13 TELL_PARENT->autoflush(1);
14 TELL_CHILD->autoflush(1);
19 my $query = <LIST_CHILD>;
20 if ($query =~ /^add /) {
25 elsif ($query =~ /^reload /) {
26 $query =~ s/^reload //;
28 $res = reload($query);
30 elsif ($query =~ /^remove /) {
31 $query =~ s/^remove //;
33 $res = remove($query);
35 print TELL_CHILD "$res\n";
36 print TELL_CHILD "____\n"; # end of response
39 while (my $c = $d->accept) #connect
41 if (fork() == 0) #start new concurrent process
43 while (my $r = $c->get_request) #get http request
45 if ($r->method eq 'GET' &&
46 ($r->url->path eq $working_path or $r->url->path eq $working_path."/"))#start dir
48 my $response = new HTTP::Response;
49 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
50 $response->content(home($r->url->query));
51 $c->send_response($response);
53 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/help")#usage
55 my $response = new HTTP::Response;
56 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
57 $response->content(help($r->url->query));
58 $c->send_response($response);
60 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/add")#add
62 my $response = new HTTP::Response;
63 kill(USR1,getppid()); # ask the parent to read the pipe
64 my $qs = $r->url->query;
65 print TELL_PARENT "add $qs\n";
67 while (($in = <LIST_PARENT>) ne "____\n") {
71 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
72 $response->content($res);
73 $c->send_response($response);
75 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/remove")#remove
77 my $response = new HTTP::Response;
78 kill(USR1,getppid()); # ask the parent to read the pipe
79 my $qs = $r->url->query;
80 print TELL_PARENT "remove $qs\n";
83 while (($in = <LIST_PARENT>) ne "____\n") {
87 $response->content($res);
88 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
89 $c->send_response($response);
91 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/reload")#reload
93 my $response = new HTTP::Response;
94 kill(USR1,getppid()); # ask the parent to read the pipe
95 my $qs = $r->url->query;
96 print TELL_PARENT "reload $qs\n";
99 while (($in = <LIST_PARENT>) ne "____\n") {
103 $response->content($res);
104 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
105 $c->send_response($response);
107 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/list")#list
109 my $response = new HTTP::Response;
110 $response->header('Cache-Control' => 'no-cache','Pragma' => "no-cache",'Expires' => '0');
111 $response->content(list($r->url->query));
112 $c->send_response($response);
114 elsif ($r->method eq 'GET' && $r->url->path eq $working_path."/apply")#apply
117 my $response = new HTTP::Response;
118 $response->content(apply($r->url->query,\%headers));
119 $response->header(%headers);
120 $c->send_response($response);
122 else #wrong command or not working_path
124 $c->send_error(RC_FORBIDDEN)