OwnServer.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 183 SCAR 61.4
CC
21
Cov
0%
run
240.0
get_cmd
20.0
make_cmd
12.0
CRAP 462.0
1 package QBit::WebInterface::OwnServer;
2 $QBit::WebInterface::OwnServer::VERSION = '0.007';
3 use qbit;
4  
5 use base qw(QBit::WebInterface);
6  
7 use IO::Socket;
8 use MIME::Types;
9 use URI::Escape qw(uri_escape_utf8);
10  
11 use QBit::WebInterface::OwnServer::Request;
12 use QBit::WebInterface::Response;
13  
14 my %RESPONSE_TEXT = (
15 200 => 'OK',
16 201 => 'CREATED',
17 202 => 'Accepted',
18 203 => 'Partial Information',
19 204 => 'No Response',
20 301 => 'Moved',
21 302 => 'Found',
22 303 => 'Method',
23 304 => 'Not Modified',
24 400 => 'Bad request',
25 401 => 'Unauthorized',
26 402 => 'PaymentRequired',
27 403 => 'Forbidden',
28 404 => 'Not found',
29 500 => 'Internal Error',
30 501 => 'Not implemented',
31 502 => 'Service temporarily overloaded',
32 503 => 'Gateway timeout',
33 );
34  
35 sub run {
36 my ($self, %opts) = @_;
37  
38 $opts{'port'} ||= 5000;
39  
40 my $server_socket = IO::Socket::INET->new(
41 Proto => 'tcp',
42 LocalPort => $opts{'port'},
43 Listen => SOMAXCONN,
44 Reuse => 1,
45 ) || throw gettext('Cannot create socket');
46  
47 l "http://127.0.0.1:$opts{'port'}";
48 l "Ctrl+C to terminate";
49  
50 my $mimetypes = MIME::Types->new();
51  
52 while (my $socket = $server_socket->accept()) {
53 try {
54 $self->request(QBit::WebInterface::OwnServer::Request->new(socket => $socket, port => $opts{'port'}));
55  
56 l $self->request->uri;
57  
58 if (length(my $static_path = $self->_is_static($self->request->uri))) {
59 if ($static_path =~ /\.\.\// || !-f $static_path) {
60 $self->response(QBit::WebInterface::Response->new(status => 404));
61 } else {
62 my $mimetype = $mimetypes->mimeTypeOf($static_path);
63  
64 $self->response(
65 QBit::WebInterface::Response->new(
66 status => 200,
67 data => \readfile($static_path, binary => $mimetype && $mimetype->isBinary()),
68 )
69 );
70 $self->response->content_type($mimetype->type() . ($mimetype->isAscii() ? '; charset=UTF-8' : ''))
71 if $mimetype;
72 }
73 } else {
74 $self->build_response();
75 }
76 }
77 catch {
78 l shift->as_string();
79 $self->response(QBit::WebInterface::Response->new()) unless $self->response();
80 $self->response->status(500);
81 $self->response->data('');
82 };
83  
84 binmode($socket);
85  
86 my $status = $self->response->status || 200;
87  
88 print $socket "HTTP/1.0 $status "
89 . (exists($RESPONSE_TEXT{$status}) ? " $RESPONSE_TEXT{$status}" : 'Unknown') . "\n";
90 print $socket 'Server: QBit::WebInterface::OwnServer (' . ref($self) . ")\n";
91 print $socket 'Set-Cookie: ' . $_->as_string() . "\n" foreach values(%{$self->response->cookies});
92  
93 while (my ($key, $value) = each(%{$self->response->headers})) {
94 print $socket "$key: $value\n";
95 }
96  
97 if ($status == 200) {
98 print $socket 'Content-Type: ' . $self->response->content_type . "\n\n";
99 print $socket 'Content-Disposition: '
100 . 'attachment; filename="'
101 . $self->_escape_filename($self->response->filename) . '"'
102 if $self->response->filename;
103  
104 if (defined($self->response->data)) {
105 my $data_ref = ref($self->response->data) ? $self->response->data : \$self->response->data;
106 utf8::encode($$data_ref) if utf8::is_utf8($$data_ref);
107 print $socket $$data_ref;
108 }
109  
110 } elsif ($status == 301 || $status == 302) {
111 print $socket 'Location: ' . $self->response->location . "\n\n";
112 } elsif ($status == 404) {
113 print $socket "Content-Type: text/html\n\n";
114 print $socket '<html><body><h1>Not found</h1><body></html>';
115 } elsif ($status == 500) {
116 print $socket "Content-Type: text/html\n\n";
117 print $socket '<html><body><h1>Internal server error</h1><body></html>';
118 } else {
119 print $socket "\n\n";
120 }
121  
122 close($socket);
123 }
124 }
125  
126 sub get_cmd {
127 my ($self) = @_;
128  
129 my ($path, $cmd);
130 if ($self->request->uri() =~ /^\/([^?\/]+)(?:\/([^\/?#]+))?/) {
131 ($path, $cmd) = ($1, $2);
132 } else {
133 ($path, $cmd) = $self->default_cmd();
134 }
135  
136 $path = '' unless defined($path);
137 $cmd = '' unless defined($cmd);
138  
139 return ($path, $cmd);
140 }
141  
142 sub make_cmd {
143 my ($self, $new_cmd, $new_path, @params) = @_;
144  
145 my %vars = defined($params[0])
146 && ref($params[0]) eq 'HASH' ? %{$params[0]} : @params;
147  
148 my ($path, $cmd) = $self->get_cmd();
149  
150 $path = uri_escape_utf8($self->_get_new_path($new_path, $path));
151 $cmd = uri_escape_utf8($self->_get_new_cmd($new_cmd, $cmd));
152  
153 return "/$path/$cmd"
154 . (
155 %vars
156 ? '?'
157 . join(
158 $self->get_option('link_param_separator', '&amp;'),
159 map {uri_escape_utf8($_) . '=' . uri_escape_utf8($vars{$_})} keys(%vars)
160 )
161 : ''
162 );
163 }
164  
165 sub _is_static {
166 my ($self, $uri) = @_;
167  
168 my $path = __normalize_path($uri);
169  
170 $self->{'static_locations'} ||= {'/qbit' => $self->{'__ORIG_OPTIONS__'}{'FrameworkPath'} . 'QBit/data'};
171  
172 foreach my $l (sort {length($b) <=> length($a)} keys(%{$self->{'static_locations'}})) {
173 my $location = $l;
174 $location =~ s/\/$//;
175 if ($path =~ /^$location\/(.+)$/) {
176 return "$self->{'static_locations'}{$l}/$1";
177 }
178 }
179  
180 return '';
181  
182 }
183  
184 sub __normalize_path {
185 my ($path) = @_;
186  
187 for ($path) {
188 s/\?.+$//;
189 s/[^\/a-zA-Z0-9_\.-]//g;
190 while (s/\/[a-zA-Z0-9_-]+?\/\.\.(\/|$)/$1/) { }
191 }
192  
193 return $path;
194 }
195  
196 TRUE;
197  
198 __END__