OwnServer.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 161 CC 17 SCAR 51.7
CC
17
Cov
0%
run
272.0
_is_static
6.0
__normalize_path
2.0
CRAP 175.1
1 package QBit::WebInterface::OwnServer;
2 $QBit::WebInterface::OwnServer::VERSION = '0.008';
3 use qbit;
4  
5 use base qw(QBit::WebInterface);
6  
7 use IO::Socket;
8 use MIME::Types;
9  
10 use QBit::WebInterface::OwnServer::Request;
11 use QBit::WebInterface::Response;
12  
13 sub run {
14 my ($self, %opts) = @_;
15  
16 $opts{'port'} ||= 5000;
17  
18 my $server_socket = IO::Socket::INET->new(
19 Proto => 'tcp',
20 LocalPort => $opts{'port'},
21 Listen => SOMAXCONN,
22 Reuse => 1,
23 )
24 || throw Exception gettext('Cannot create socket');
25  
26 l "http://127.0.0.1:$opts{'port'}";
27 l "Ctrl+C to terminate";
28  
29 my $mimetypes = MIME::Types->new();
30  
31 while (my $socket = $server_socket->accept()) {
32 try {
33 $self->request(QBit::WebInterface::OwnServer::Request->new(socket => $socket, port => $opts{'port'}));
34  
35 l $self->request->uri;
36  
37 if (length(my $static_path = $self->_is_static($self->request->uri))) {
38 if ($static_path =~ /\.\.\// || !-f $static_path) {
39 $self->response(QBit::WebInterface::Response->new(status => 404));
40 } else {
41 my $mimetype = $mimetypes->mimeTypeOf($static_path);
42  
43 $self->response(
44 QBit::WebInterface::Response->new(
45 status => 200,
46 data => \readfile($static_path, binary => $mimetype && $mimetype->isBinary()),
47 )
48 );
49 $self->response->content_type($mimetype->type() . ($mimetype->isAscii() ? '; charset=UTF-8' : ''))
50 if $mimetype;
51 }
52 } else {
53 $self->build_response();
54 }
55 }
56 catch {
57 l shift->as_string();
58 $self->response(QBit::WebInterface::Response->new()) unless $self->response();
59 $self->response->status(500);
60 $self->response->data('');
61 };
62  
63 binmode($socket);
64  
65 my $status = $self->response->status || 200;
66  
67 print $socket "HTTP/1.0 $status "
68 . (
69 exists($QBit::WebInterface::HTTP_STATUSES{$status})
70 ? " $QBit::WebInterface::HTTP_STATUSES{$status}"
71 : 'Unknown'
72 )
73 . "\n";
74 print $socket 'Server: QBit::WebInterface::OwnServer (' . ref($self) . ")\n";
75 print $socket 'Set-Cookie: ' . $_->as_string() . "\n" foreach values(%{$self->response->cookies});
76  
77 while (my ($key, $value) = each(%{$self->response->headers})) {
78 print $socket "$key: $value\n";
79 }
80  
81 if ($status == 200) {
82 print $socket 'Content-Type: ' . $self->response->content_type . "\n\n";
83  
84 my $filename = $self->response->filename;
85 if (defined($filename)) {
86 utf8::encode($filename) if utf8::is_utf8($filename);
87  
88 print $socket 'Content-Disposition: '
89 . 'attachment; filename="'
90 . $self->_escape_filename($self->response->filename) . '"';
91 }
92  
93 if (defined($self->response->data)) {
94 my $data_ref = ref($self->response->data) ? $self->response->data : \$self->response->data;
95 utf8::encode($$data_ref) if utf8::is_utf8($$data_ref);
96 print $socket $$data_ref;
97 }
98  
99 } elsif ($status == 301 || $status == 302) {
100 print $socket 'Location: ' . $self->response->location . "\n\n";
101 } elsif ($status == 404) {
102 print $socket "Content-Type: text/html\n\n";
103 print $socket '<html><body><h1>Not found</h1><body></html>';
104 } elsif ($status == 500) {
105 print $socket "Content-Type: text/html\n\n";
106 print $socket '<html><body><h1>Internal server error</h1><body></html>';
107 } else {
108 print $socket "\n\n";
109 }
110  
111 close($socket);
112 }
113 }
114  
115 sub _is_static {
116 my ($self, $uri) = @_;
117  
118 my $path = __normalize_path($uri);
119  
120 $self->{'static_locations'} ||= {'/qbit' => $self->{'__ORIG_OPTIONS__'}{'FrameworkPath'} . 'QBit/data'};
121  
122 foreach my $l (sort {length($b) <=> length($a)} keys(%{$self->{'static_locations'}})) {
123 my $location = $l;
124 $location =~ s/\/$//;
125 if ($path =~ /^$location\/(.+)$/) {
126 return "$self->{'static_locations'}{$l}/$1";
127 }
128 }
129  
130 return '';
131  
132 }
133  
134 sub __normalize_path {
135 my ($path) = @_;
136  
137 for ($path) {
138 s/\?.+$//;
139 s/[^\/a-zA-Z0-9_\.-]//g;
140 while (s/\/[a-zA-Z0-9_-]+?\/\.\.(\/|$)/$1/) { }
141 }
142  
143 return $path;
144 }
145  
146 TRUE;
147  
148 __END__