Debian Codegraph @

src/.svn/text-base/cookie.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 ########################################################################
29
30 =item B<cookie_new_jar>
31
32 Params: none
33
34 Return: $jar
35
36 Create a new cookie jar, for use with the other functions. Even though
37 the jar is technically just a hash, you should still use this function
38 in order to be future-compatible (should the jar format change).
39
40 =cut
41
42 sub cookie_new_jar {
43 return {};
44 }
45
46 ########################################################################
47
48 =item B<cookie_read>
49
50 Params: $jar, \%response [, \%request, $reject ]
51
52 Return: $num_of_cookies_read
53
54 Read in cookies from an %response hash, and put them in $jar.
55
56 Notice: cookie_read uses internal magic done by http_do_request
57 in order to read cookies regardless of 'Set-Cookie[2]' header
58 appearance.
59
60 If the optional %request hash is supplied, then it will be used to
61 calculate default host and path values, in case the cookie doesn't
62 specify them explicitly. If $reject is set to 1, then the %request
63 hash values are used to calculate and reject cookies which are not
64 appropriate for the path and domains of the given request.
65
66 =cut
67
68 sub cookie_read {
69 my ( $count, $jarref, $hrs, $hrq, $rej ) = ( 0, @_ );
70
71 return 0 if ( !( defined $jarref && ref($jarref) ) );
72 return 0 if ( !( defined $hrs && ref($hrs) ) );
73 return 0
74 if (
75 !(
76 defined $$hrs{whisker}->{cookies}
77 && ref( $$hrs{whisker}->{cookies} )
78 )
79 );
80
81 my @opt;
82 if(defined $hrq && ref($hrq)){
83 push @opt, $hrq->{whisker}->{host};
84 my $u = $hrq->{whisker}->{uri};
85 $u=~s#/.*?$##;
86 $u='/' if($u eq '');
87 push @opt, $u, $rej;
88 }
89
90 foreach ( @{ $hrs->{whisker}->{cookies} } ) {
91 cookie_parse( $jarref, $_ , @opt);
92 $count++;
93 }
94 return $count;
95 }
96
97 ########################################################################
98
99 =item B<cookie_parse>
100
101 Params: $jar, $cookie [, $default_domain, $default_path, $reject ]
102
103 Return: nothing
104
105 Parses the cookie into the various parts and then sets the appropriate
106 values in the cookie $jar. If the cookie value is blank, it will delete
107 it from the $jar. See the 'docs/cookies.txt' document for a full
108 explanation of how Libwhisker parses cookies and what RFC aspects are
109 supported.
110
111 The optional $default_domain value is taken literally. Values with no
112 leading dot (e.g. 'www.host.com') are considered to be strict hostnames
113 and will only match the identical hostname. Values with leading dots (e.g.
114 '.host.com') are treated as sub-domain matches for a single domain level.
115 If the cookie does not indicate a domain, and a $default_domain is not
116 provided, then the cookie is considered to match all domains/hosts.
117
118 The optional $default_path is used when the cookie does not specify a path.
119 $default_path must be absolute (start with '/'), or it will be ignored. If
120 the cookie does not specify a path, and $default_path is not provided, then
121 the default value '/' will be used.
122
123 Set $reject to 1 if you wish to reject cookies based upon the provided
124 $default_domain and $default_path. Note that $default_domain and
125 $default_path must be specified for $reject to actually do something
126 meaningful.
127
128 =cut
129
130 sub cookie_parse {
131 my ( $jarref, $header ) = (shift, shift);
132 my ( $Dd, $Dp, $R ) = (shift, shift, shift||0);
133
134 return if ( !( defined $jarref && ref($jarref) ) );
135 return if ( !( defined $header && length($header) > 0 ) );
136
137 my @C = ( undef, undef, undef, undef, 0 );
138
139 $header =~ tr/\r\n//d;
140 my ($f,%seen,$n,$t) = (1);
141 while( length($header) ){
142 $header =~ s/^[ \t]+//;
143 last if(!($header =~ s/^([^ \t=;]+)//));
144 # LW2.5 change: cookie name is no longer lower-cased
145 # my $an = lc($1);
146 my $an = $1;
147 my $av = undef;
148 $header =~ s/^[ \t]+//;
149 if(substr($header,0,1) eq '='){
150 $header=~s/^=[ \t]*//;
151 if(substr($header,0,1) eq '"'){
152 my $p = index($header,'"',1);
153 last if($p == -1);
154 $av = substr($header,1,$p-1);
155 substr($header,0,$p+1)='';
156 } else {
157 $av = $1 if($header =~ s/^([^ \t;,]*)//);
158 }
159 } else {
160 my $p = index($header,';');
161 substr($header,0,$p)='';
162 }
163 $header =~ s/^.*?;//;
164 if($f){
165 return if(!defined $av);
166 ($f,$n,$C[0])=(0,$an,$av);
167 } else {
168 $seen{$an}=$av if(!exists $seen{$an});
169 }
170 }
171
172 return if(!defined $n || $n eq '');
173 my $del = 0;
174 $del++ if($C[0] eq '');
175 $del++ if(defined $seen{'max-age'} && $seen{'max-age'} eq '0');
176 if($del){
177 delete $$jarref{$n} if exists $$jarref{$n};
178 return;
179 }
180
181 if(defined $seen{domain} && $seen{domain} ne ''){
182 $t = $seen{domain};
183 $t='.'.$t if(substr($t,0,1) ne '.' && !_is_ip_address($t));
184 } else {
185 $t=$Dd;
186 }
187 $t=~s/\.+$// if(defined $t);
188 $C[1]=$t;
189
190 if(defined $seen{path}){
191 $t = $seen{path};
192 } else {
193 $t=$Dp || '/';
194 }
195 $t=~s#/+$##;
196 $t='/' if(substr($t,0,1) ne '/');
197 $C[2]=$t;
198
199 $C[4]=1 if(exists $seen{secure});
200
201 return if($R && !_is_valid_cookie_match($C[1], $C[2], $Dd, $Dp));
202 $$jarref{$n} = \@C;
203 }
204
205 ########################################################################
206
207 sub _is_ip_address {
208 my $n = shift;
209 return 1 if($n=~/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/);
210 return 0;
211 }
212
213 sub _is_valid_cookie_match {
214 my ($cd, $cp, $td, $tp) = @_;
215 return 0 if(index($tp,$cp)!=0);
216 if(substr($cd,0,1) eq '.'){
217 if( $td =~ /(.+)$cd$/ ){
218 return 1 if(index($1,'.') == -1);
219 }
220 return 0;
221 } else {
222 return 0 if($cd ne $td);
223 }
224 return 1;
225 }
226
227 ########################################################################
228
229 =item B<cookie_write>
230
231 Params: $jar, \%request, $override
232
233 Return: nothing
234
235 Goes through the given $jar and sets the Cookie header in %req pending the
236 correct domain and path. If $override is true, then the secure, domain and
237 path restrictions of the cookies are ignored and all cookies are essentially
238 included.
239
240 Notice: cookie expiration is currently not implemented. URL restriction
241 comparision is also case-insensitive.
242
243 =cut
244
245 sub cookie_write {
246 my ( $jarref, $hin, $override ) = @_;
247 my ( $name, $out ) = ( '', '' );
248
249 return if ( !( defined $jarref && ref($jarref) ) );
250 return if ( !( defined $hin && ref($hin) ) );
251
252 $override = $override || 0;
253 $$hin{'whisker'}->{'ssl'} = $$hin{'whisker'}->{'ssl'} || 0;
254
255 foreach $name ( keys %$jarref ) {
256 next if ( $name eq '' );
257 if($override){
258 $out .= "$name=$$jarref{$name}->[0];";
259 next;
260 }
261 next if ( $$hin{'whisker'}->{'ssl'} == 0 && $$jarref{$name}->[4] > 0 );
262 if ( $$hin{'whisker'}->{'host'} =~ /$$jarref{$name}->[1]$/i
263 && $$hin{'whisker'}->{'uri'} =~ /^$$jarref{$name}->[2])/ )
264 {
265 $out .= "$name=$$jarref{$name}->[0];";
266 }
267 }
268
269 if ( $out ne '' ) { $$hin{'Cookie'} = $out; }
270
271 }
272
273 ########################################################################
274
275 =item B<cookie_get>
276
277 Params: $jar, $name
278
279 Return: @elements
280
281 Fetch the named cookie from the $jar, and return the components. The
282 returned items will be an array in the following order:
283
284 value, domain, path, expire, secure
285
286 value = cookie value, should always be non-empty string
287 domain = domain root for cookie, can be undefined
288 path = URL path for cookie, should always be a non-empty string
289 expire = undefined (depreciated, but exists for backwards-compatibility)
290 secure = whether or not the cookie is limited to HTTPs; value is 0 or 1
291
292 =cut
293
294 sub cookie_get {
295 my ( $jarref, $name ) = @_;
296
297 return undef if ( !( defined $jarref && ref($jarref) ) );
298
299 if ( defined $$jarref{$name} ) {
300 return @{ $$jarref{$name} };
301 }
302
303 return undef;
304 }
305
306 ########################################################################
307
308 =item B<cookie_get_names>
309
310 Params: $jar
311
312 Return: @names
313
314 Fetch all the cookie names from the jar, which then let you cooke_get()
315 them individually.
316
317 =cut
318
319 sub cookie_get_names {
320 my ( $jarref, $name ) = @_;
321
322 return undef if ( !( defined $jarref && ref($jarref) ) );
323 return keys %$jarref;
324 }
325
326 ########################################################################
327
328 =item B<cookie_get_valid_names>
329
330 Params: $jar, $domain, $url, $ssl
331
332 Return: @names
333
334 Fetch all the cookie names from the jar which are valid for the given
335 $domain, $url, and $ssl values. $domain should be string scalar of the
336 target host domain ('www.example.com', etc.). $url should be the absolute
337 URL for the page ('/index.html', '/cgi-bin/foo.cgi', etc.). $ssl should be
338 0 for non-secure cookies, or 1 for all (secure and normal) cookies. The
339 return value is an array of names compatible with cookie_get().
340
341 =cut
342
343 sub cookie_get_valid_names {
344 my ( $jarref, $domain, $url, $ssl ) = @_;
345
346 return () if ( !( defined $jarref && ref($jarref) ) );
347 return () if ( !defined $domain || $domain eq '' );
348 return () if ( !defined $url || $url eq '' );
349 $ssl ||= 0;
350
351 my (@r, $name);
352 foreach $name ( keys %$jarref ) {
353 next if ( $name eq '' );
354 next if ( $$jarref{$name}->[4] > 0 && $ssl == 0 );
355 if ( $domain =~ /$$jarref{$name}->[1]$/i
356 && $url =~ /^$$jarref{$name}->[2])/i ) {
357 push @r, $name;
358 }
359 }
360
361 return @r;
362 }
363
364
365 ########################################################################
366
367 =item B<cookie_set>
368
369 Params: $jar, $name, $value, $domain, $path, $expire, $secure
370
371 Return: nothing
372
373 Set the named cookie with the provided values into the %jar. $name is
374 required to be a non-empty string. $value is required, and will delete
375 the named cookie from the $jar if it is an empty string. $domain and
376 $path can be strings or undefined. $expire is ignored (but exists
377 for backwards-compatibility). $secure should be the numeric value of
378 0 or 1.
379
380 =cut
381
382 sub cookie_set {
383 my ( $jarref, $name, $value, $domain, $path, $expire, $secure ) = @_;
384 my @construct;
385
386 return if ( !( defined $jarref && ref($jarref) ) );
387
388 return if ( $name eq '' );
389 if ( !defined $value || $value eq '' ) {
390 delete $$jarref{$name};
391 return;
392 }
393 $path = $path || '/';
394 $secure = $secure || 0;
395
396 @construct = ( $value, $domain, $path, undef, $secure );
397 $$jarref{$name} = \@construct;
398 }
399
400 ########################################################################
401