Wombat/Util/RequestUtil.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 212 CC 20 SCAR 28.2
CC
20
Cov
0%
decodeURI
42.0
makeSessionCookie
20.0
parseParameters
20.0
CRAP 16.8
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Util::RequestUtil;
4  
5 use strict;
6 use warnings;
7  
8 use Servlet::Http::Cookie ();
9 use Servlet::Http::HttpServlet ();
10 use URI::Escape ();
11 use Wombat::Globals ();
12  
13 sub decodeURI {
14 my $class = shift;
15 my $uri = shift;
16  
17 $uri = $$uri if ref $uri;
18  
19 my $sessionID;
20 my ($pre, $post) = split(Wombat::Globals::SESSION_PARAMETER_NAME, $uri);
21 if (defined $post) {
22 $pre =~ s/[;&]$//;
23 $post =~ s/^=([^;&]+)[;&]?//;
24 $sessionID = $1;
25  
26 my $uri;
27 if ($post) {
28 $uri = ($pre =~ /\?$/) ?
29 join('', $pre, $post) :
30 join('&', $pre, $post);
31 } else {
32 $pre =~ s/\?$//;
33 $uri = $pre;
34 }
35 }
36  
37 return $sessionID ?
38 __PACKAGE__->unescapeURIPart($sessionID) :
39 undef;
40 }
41  
42 sub makeSessionCookie {
43 my $class = shift;
44 my $request = shift;
45  
46 my $application = $request->getApplication();
47 return undef unless $application && $application->isSessionCookie();
48  
49 my $cookie;
50 my $session = $request->getSession();
51 if ($session && $session->isNew()) {
52 my $name = Wombat::Globals::SESSION_COOKIE_NAME;
53 my $value = $session->getId();
54 $cookie = Servlet::Http::Cookie->new($name, $value);
55  
56 my $contextPath = $application->getPath() if $application;
57 $contextPath ||= '/';
58 $cookie->setPath($contextPath);
59 $cookie->setMaxAge(-1);
60 $cookie->setSecure($request->isSecure());
61  
62 # send cookie to the subdomain to which this host belongs
63 my $domain = $request->getServerName();
64 $domain =~ s|^[^.]+\.|.|;
65 $cookie->setDomain($domain);
66 }
67  
68 return $cookie;
69 }
70  
71 sub parseCharacterEncoding {
72 my $class = shift;
73 my $hdr = shift;
74  
75 return '' unless $hdr;
76  
77 my $encoding;
78 my ($primary, @attrs) = split /;\s*/, $hdr;
79 for my $attr (@attrs) {
80 my ($name, $value) = split /=/, $attr;
81 next unless lc $name eq 'charset';
82  
83 $encoding = $value;
84 last;
85 }
86  
87 return $encoding;
88 }
89  
90 sub parseContentType {
91 my $class = shift;
92 my $hdr = shift;
93  
94 return '' unless $hdr;
95  
96 my $encoding;
97 my ($primary) = split /;\s*/, $hdr;
98  
99 return $primary;
100 }
101  
102 sub parseCookies {
103 my $class = shift;
104 my $hdr = shift || '';
105  
106 my @list;
107 for my $chunk (split /;\s*/, $hdr) {
108 my ($key, $val) = split /=/, $chunk;
109  
110 $key = __PACKAGE__->unescapeURIPart($key);
111 $val = __PACKAGE__->unescapeURIPart($val);
112  
113 my $cookie;
114 eval {
115 $cookie = Servlet::Http::Cookie->new($key, $val);
116 };
117 push @list, $cookie if $cookie;
118 }
119  
120 return wantarray ? @list : \@list;
121 }
122  
123 sub parseLocales {
124 my $class = shift;
125 my $hdr = shift || '';
126  
127 my $locales = {};
128 for my $chunk (split /,\s*/, $hdr) {
129 my ($entry, $quality) = split /;q=/, $chunk;
130 next if $quality && $quality eq '*'; # XXX
131 $quality ||= '0.0';
132  
133 $locales->{$quality} ||= [];
134 push @{ $locales->{$quality} }, $entry;
135 }
136  
137 my @list;
138 for my $quality (sort { $b <=> $a } keys %$locales) {
139 for my $locale (@{ $locales->{$quality} }) {
140 push @list, $locale;
141 }
142 }
143  
144 return wantarray ? @list : \@list;
145 }
146  
147 sub parseParameters {
148 my $class = shift;
149 my $str = shift;
150  
151 my @uriwords;
152 if ($str) {
153 for my $chunk (split /[;&]/, $str) {
154 my ($key, $val) = split /=/, $chunk;
155  
156 if (defined $key && defined $val) {
157 $key = __PACKAGE__->unescapeURIPart($key);
158 $val = __PACKAGE__->unescapeURIPart($val);
159  
160 push @uriwords, [$key, $val];
161 } else {
162 # malformed parameter, skip
163 }
164 }
165 }
166  
167 return wantarray ? @uriwords : \@uriwords;
168 }
169  
170 sub unescapeURIPart {
171 my $class = shift;
172 my $str = shift;
173  
174 if ($str) {
175 $str =~ tr/+/ /;
176 $str = URI::Escape::uri_unescape($str);
177 }
178  
179 return $str;
180 }
181  
182 1;
183 __END__