blib/lib/Apache/Queue.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::Queue;
2 $VERSION = 0.6;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Apache2 ();
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Apache::Connection;
6 use Apache::RequestIO;
7 use Apache::RequestRec;
8 use Apache::RequestUtil;
9 use Apache::SubRequest;
10 use Apache::Const qw( :common :methods :http );
11 use Apache::Log;
12 use Fcntl;
13 use DB_File;
14 use Template;
15  
16 use vars qw( @sends @queue $r $template );
17  
18 sub handler {
19 $r = shift;
20  
21 return DECLINED unless ($r->is_initial_req());
22  
23 my $status = OK;
24 my ($found, $x, $pos );
25 my $host = $r->connection->remote_ip;
26  
27 my $max_sends = $r->dir_config("MaxSends") || 10;
28 my $queue_size = $r->dir_config("QueueSize") || 300;
29 my $tmpdir = $r->dir_config("QueuePath") || "/tmp";
30  
31 my $s = tie @sends, 'DB_File', "$tmpdir/apache_queue-sends", O_RDWR|O_CREAT, 0666, $DB_RECNO;
32 my $q = tie @queue, 'DB_File', "$tmpdir/apache_queue-queue", O_RDWR|O_CREAT, 0666, $DB_RECNO;
33  
34 my $now = time;
35 @sends = grep { $now-$_ < 300 } @sends;
36 @queue = grep { $now-$_ < 300 } @queue;
37  
38 # Search the send queue to see if the visitor is already
39 # downloading, or is waiting to.
40 $found = $x = $pos = 0;
41 foreach(@sends) {
42 if(/^\d+\|\d+\|$host$/) {
43 $found = 1;
44 $pos = $x;
45 last;
46 }
47 $x++;
48 }
49  
50 if($found) {
51 # The user is in the send queue
52 my($time, $sending, $visitor) = split(/\|/, $sends[$pos]);
53 if($sending < 1) {
54 # The visitor was waiting, starting download
55 $sends[$pos] = "$now|1|$host";
56 $s->sync();
57
58 $status = send_file($r);
59  
60 splice(@sends, $pos, 1);
61 $s->sync();
62 } else {
63 # The visitor already is downloading
64 show_template("queue_sending.html");
65 $status = OK;
66 }
67 } else {
68 # The visitor was not in the send queue. Check if there are
69 # send slots open, if so send them the file, otherwise add
70 # them to the queue.
71 if($#sends < ($max_sends - 1) && $#queue < 0) {
72 # There is an open send slot, add the visitor, and let him go.
73 push @sends, "$now|0|$host";
74 show_template("queue_send.html", { 'uri' => $r->uri });
75 $status = OK;
76 } else {
77 # All send slots are full, attempt to queue the visitor.
78 $found = $x = $pos = 0;
79 foreach(@queue) {
80 if(m/^\d+\|$host$/) {
81 $found = 1;
82 $pos = $x;
83 last;
84 }
85 $x++;
86 }
87
88 if($found) {
89 my $open = $max_sends - ($#sends + 1);
90 if($pos < $open) {
91 push @sends, "$now|0|$host";
92 @queue = splice(@queue, $pos, 1);
93 show_template("queue_send.html", { 'uri' => $r->uri });
94 $status = OK;
95 } else {
96 $queue[$pos] = "$now|$host";
97 show_template("queue_position.html", { 'url' => $r->uri, 'position' => ($pos + 1), 'queue_size' => ($#queue + 1) });
98 $status = OK;
99 }
100 } else {
101 if(($#queue + 1) < $queue_size) {
102 $pos = push @queue, "$now|$host";
103 show_template("queue_position.html", { 'url' => $r->uri, 'position' => $pos, 'queue_size' => $#queue + 1 });
104 $status = OK;
105 } else {
106 show_template("queue_full.html", { 'queue_size' => $queue_size });
107 $status = OK;
108 }
109 }
110 }
111 }
112
113 untie @sends;
114 untie @queue;
115 return $status;
116 }
117  
118 sub send_file {
119 my $r = shift;
120 my $sub = $r->lookup_uri($r->uri);
121 return $sub->run();
122 }
123  
124 sub show_template {
125 my ($name, $vars) = @_;
126  
127 my $file = Template::Provider->new(ABSOLUTE => '1');
128 my $hash = Queue::Template::Default->new();
129 $template = Template->new( {
130 OUTPUT => $r,
131 LOAD_TEMPLATES => [ $file, $hash ],
132 PREFIX_MAP => {
133 file => '0',
134 hash => '1',
135 default => '1',
136 },
137 });
138  
139 $r->content_type("text/html");
140  
141 my $path = $r->dir_config("TemplatePath");
142 if($path ne '') {
143 $template->process("file:$path/$name", $vars) || warn $template->error();
144 } else {
145 $template->process("hash:$name", $vars) || warn $template->error();
146 }
147 }
148  
149 package Queue::Template::Default;
150 @Queue::Template::Default::ISA = qw(Template::Provider);
151  
152 my %Defaults = (
153 'queue_send.html' => <<EOQSH
154 <html>
155 <head>
156 <meta HTTP-EQUIV="refresh" content="1; URL=[% uri %]">
157 </head>
158 <body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
159 <table height=100% width=100%>
160 <tr><td valign=center align=center>
161 <font face="arial" size=2>
162 Click <a href="[% uri %]">here</a> if your download does not start
163 </font>
164 </td></tr></table>
165 </body>
166 </html>
167 EOQSH
168 ,
169 'queue_full.html' => <<EOQFH
170 <html>
171 <head>
172 <meta HTTP-EQUIV="refresh" content="300; URL=[% uri %]">
173 </head>
174 <body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
175 <table height=100% width=100%>
176 <tr><td valign=center align=center>
177 <font face="arial" size=2>
178 Sorry, the queue is full<BR>Keep this window open to keep trying
179 </font>
180 </td></tr></table>
181 </body>
182 </html>
183 EOQFH
184 ,
185 'queue_position.html' => <<EOQPH
186 <html>
187 <head>
188 <meta HTTP-EQUIV="refresh" content="60; URL=[% uri %]">
189 </head>
190 <body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
191 <table height=100% width=100%>
192 <tr><td valign=center align=center>
193 <font face="arial" size=2>
194 You are in position [% position %] of [% queue_size %]<BR>
195 Keep this window open to stay in line
196 </font>
197 </td></tr></table>
198 </body>
199 </html>
200 EOQPH
201 ,
202 'queue_sending.html' => <<EOQS
203 <html>
204 <body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>
205 <table height=100% width=100%>
206 <tr><td valign=center align=center>
207 <font face="arial" size=2>
208 You are already downloading a file
209 </font>
210 </td></tr></table>
211 </body>
212 </html>
213 EOQS
214 );
215  
216 sub new {
217 my $self = {};
218 bless $self;
219 return $self;
220 }
221  
222 sub fetch {
223 my ($self, $name) = @_;
224 my ($data, $error);
225 if($Defaults{$name} ne '') {
226 $data = { text => $Defaults{$name} };
227 ($data, $error) = $self->_compile($data);
228 $data = $data->{ data } unless $error;
229 return ($data, Template::Constants::STATUS_OK);
230 } else{
231 return (undef, Template::Constants::STATUS_ERROR);
232 }
233 }
234  
235 sub load {
236 my ($self, $name) = @_;
237 if($Defaults{$name} ne '') {
238 return ($Defaults{$name}, Template::Constants::STATUS_OK);
239 } else{
240 return (undef, Template::Constants::STATUS_ERROR);
241 }
242 }
243  
244 1;
245 __END__
↑ Summary