Changeset 35
- Timestamp:
- 06/18/08 17:49:51 (5 years ago)
- Files:
-
- trunk/lib/Mungo.pm (modified) (7 diffs)
- trunk/lib/Mungo/Cookie.pm (modified) (6 diffs)
- trunk/lib/Mungo/Request.pm (modified) (7 diffs)
- trunk/lib/Mungo/Response.pm (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/Mungo.pm
r33 r35 82 82 # Use a scalar reference! 83 83 $Response->Include(\$asp, @args); 84 %> 85 86 <!-- Cookie facilities --> 87 <% 88 # Read cookie 89 $single_value = $Request->Cookies($cookie_name); 90 $hashref = $Request->Cookies($cookie_name); 91 92 # Set cookie 93 $Response->Cookies($cookie_name, $single_value); 94 $Response->Cookies($cookie_name, $hash_ref); 84 95 %> 85 96 … … 296 307 sub Response { return Mungo::Response->new($_[0]); } 297 308 309 =head2 $encoded = $mungo->URLEncode($string); 310 311 =head2 $encoded = Mungo->URLEncode($string); 312 313 Encodes a string to escape characters that are not permitted in a URL. 314 315 =cut 316 298 317 sub URLEncode { 299 318 my $self = shift; … … 302 321 return $s; 303 322 } 323 324 =head2 $string = $mungo->URLDecode($encoded); 325 326 =head2 $string = Mungo->URLDecode($encoded); 327 328 Decodes a string to unescape characters that are not permitted in a URL. 329 330 =cut 331 304 332 sub URLDecode { 305 333 my $self = shift; … … 318 346 return HTML::Entities::decode_entities( $s ); 319 347 } 348 349 350 # Private? 320 351 sub demangle_name { 321 352 my $self = shift; … … 336 367 } 337 368 369 # Private? 338 370 sub filename2packagename { 339 371 my ($self, $filename) = @_; … … 368 400 $page->content(@_); 369 401 } 402 # Private? 370 403 sub include_file { 371 404 my $self = shift; … … 402 435 $page->content(@_); 403 436 } 437 # Private? 404 438 sub packagize { 405 439 my $self = shift; trunk/lib/Mungo/Cookie.pm
r17 r35 4 4 # For information on licensing see: 5 5 # https://labs.omniti.com/zetaback/trunk/LICENSE 6 7 =head1 NAME 8 9 Mungo::Cookie - Cookie support class 10 11 =head1 SYNOPSIS 12 13 # Use via Mungo::Request->Cookies 14 my $value = $Request->cookies($cookie_name, $key_name); 15 16 =head1 DESCRIPTION 17 18 Represents one or more 19 20 =cut 6 21 7 22 use strict; … … 26 41 ); 27 42 43 # Private constructor. 44 # $cookiejar = Mungo::Cookie->new($apache_req); 45 # $cookiejar = Mungo::Cookie->new($raw_cookie_string); 46 # Splits the incoming cookie string on ; to find sub-cookies 47 # If subcookie has a single value (with no key): 48 # $self->{$subcookie_name}->{Value} = $value 49 # If subcookie has a key-value pairs: 50 # $self->{$subcookie_name}->{Value}->{$key} = $value; 28 51 sub new { 29 52 my $class = shift; … … 36 59 my $self = bless {}, $class; 37 60 foreach my $cookie (split /;\s*/, $cstr) { # ; seperated cookies 61 # $cname = subcookie name 62 # @lk = list of key, value pairs in subcookie 63 # @kv = a key, value pair in a subcookie 38 64 my ($cname, $rest) = split /=/, $cookie, 2; # cookie=OPAQUE_STRING 39 65 my @lk = ($rest !~ /[=&]/) ? # single value ? … … 94 120 return $cstring; 95 121 } 122 123 96 124 sub inject_headers { 97 125 my $self = shift; … … 115 143 } 116 144 145 # Sigh. "friend" method. 146 147 # Why??? 148 # $cookie_object = $cookie_object->__get(); 149 150 # If $cookie_name is single-valued.... 151 # $value = $cookie_object->__get($cookie_name); 152 153 # If $cookie_name is multi-valued.... 154 # $hashref = $cookie_object->__get($cookie_name); 155 156 # If $cookie_name is multi-valued.... 157 # $value = $cookie_object->__get($cookie_name, $key); 158 117 159 sub __get { 118 160 my $self = shift; … … 120 162 return $self unless(@_); 121 163 122 my $ key= shift;164 my $cookie_name = shift; 123 165 if(@_) { 124 my $ part= shift;125 return (exists $self->{$ key} && ref $self->{$key}->{Value} eq 'HASH' &&126 exists $self->{$ key}->{Value}->{$part}) ?127 $self->{$ key}->{Value}->{$part} :166 my $key = shift; 167 return (exists $self->{$cookie_name} && ref $self->{$cookie_name}->{Value} eq 'HASH' && 168 exists $self->{$cookie_name}->{Value}->{$key}) ? 169 $self->{$cookie_name}->{Value}->{$key} : 128 170 undef; 129 171 } 130 return (exists $self->{$key}->{Value}) ? $self->{$key}->{Value} : undef; 131 } 172 return (exists $self->{$cookie_name}->{Value}) ? $self->{$cookie_name}->{Value} : undef; 173 } 174 175 176 # $cookie->__set($cookie_name, $value); 177 # $cookie->__set($cookie_name, $key, $value); 178 # $cookie->__set($cookie_name, 'Expires', $value); 179 # $cookie->__set($cookie_name, 'Path', $value); 180 # $cookie->__set($cookie_name, 'Domain', $value); 181 # $cookie->__set($cookie_name, 'Secure', $value); 132 182 133 183 sub __set { 134 184 my $self = shift; 135 185 my $cname = shift; 186 my $value = shift; 136 187 my $key = undef; 137 my $value = shift;138 188 if(@_) { 139 189 $key = $value; trunk/lib/Mungo/Request.pm
r34 r35 10 10 eval "use APR::Table;"; 11 11 our $AUTOLOAD; 12 13 =head1 NAME 14 15 Mungo::Request - represent an HTTP request context 16 17 =head1 SYNOPSIS 18 19 <!-- Within your HTML, you get a Request object for free --> 20 <% if (defined $Request) { ... } %> 21 22 <!-- Get params --> 23 <% 24 my $value = $Request->Params('param_name'); 25 my %params = $Request->Params(); 26 %> 27 28 <!-- Get cookies --> 29 <% 30 # for single-valued cookies 31 my $value = $Request->Cookies($cookie_name); 32 33 # for multi-valued cookies 34 my $hashref = $Request->Cookies($cookie_name); 35 36 # for multi-valued cookies 37 my $value = $Request->Cookies($cookie_name, $key); 38 39 %> 40 41 =head1 DESCRIPTION 42 43 Represents the request side of a Mungo request cycle. 44 45 See Mungo, and Mungo::Request. 46 47 =cut 48 12 49 13 50 sub new { … … 58 95 } 59 96 97 =head2 $value = $Request->Cookies($cookie_name); 98 99 =head2 $hashref = $Request->Cookies($cookie_name); 100 101 =head2 $value = $Request->Cookies($cookie_name, $key); 102 103 Reads and parses incoming cookie data. Behavior depends on whether 104 the cookie contained name-value pairs. 105 106 If not, the first form simply returns the value set in the given cookie name, or undef. 107 108 If name-value pairs are present, the second form returns a hashref of all the name-value pairs. 109 110 If name-value pairs are present, the third form returns the value for the given key. 111 112 If no such cookie with the given name exists, returns undef. 113 114 =cut 115 60 116 sub Cookies { 61 117 my $self = shift; 62 my $c cls = 'Mungo::Cookie';63 my $cookie = $self->{$c cls} ||= $ccls->new($self->{'Apache::Request'});118 my $cookie_class = 'Mungo::Cookie'; 119 my $cookie = $self->{$cookie_class} ||= $cookie_class->new($self->{'Apache::Request'}); 64 120 return $cookie->__get(@_); 65 121 } 122 123 =head2 $value = $Request->QueryString($name); 124 125 =head2 %params = $Request->QueryString(); 126 127 =head2 $params_hashref = $Request->QueryString(); 128 129 Returns one value (first form) or all values (second and third forms) 130 from the submitted query string. 131 132 Params() is preferred. 133 134 =cut 135 66 136 sub QueryString { 67 137 my $self = shift; … … 81 151 return \%qs; 82 152 } 153 83 154 sub decode_form { 84 155 my $class = ref $_[0] ? ref $_[0] : $_[0]; … … 100 171 return $form; 101 172 } 173 174 =head2 $value = $Request->Form($name); 175 176 =head2 %params = $Request->Form(); 177 178 =head2 $params_hashref = $Request->Form(); 179 180 Returns one value (first form) or all values (second and third forms) 181 from the submitted POST data. 182 183 Params() is preferred. 184 185 =cut 186 102 187 sub Form { 103 188 my $self = shift; … … 119 204 } 120 205 206 =head2 $value = $Request->Params($name); 207 208 =head2 %params = $Request->Params(); 209 210 =head2 $params_hashref = $Request->Params(); 211 212 Returns one value (first form) or all values (second and third forms) 213 from the submitted CGI parameters, whether that was via the query string or via POST data. 214 215 This method is recommended over Form and QueryString, because it is independent 216 of how the data was submitted. 217 218 If both methods provide data, Form overrides QueryString. 219 220 =cut 221 121 222 sub Params { 122 223 my $self = shift; … … 131 232 } 132 233 234 =head2 $value = $Request->ServerVariables($variable_name); 235 236 Returns information about the request or the server. Only certain 237 variables are supported: 238 239 REFERER, REFERRER, DOCUMENT_ROOT, HTTP_HOST 240 241 =cut 242 133 243 sub ServerVariables { 134 244 my $self = shift; … … 156 266 } 157 267 268 =head1 AUTHOR 269 270 Theo Schlossnagle (code) 271 272 Clinton Wolfe (docs) 273 274 275 =cut 276 158 277 1; trunk/lib/Mungo/Response.pm
r33 r35 4 4 # For information on licensing see: 5 5 # https://labs.omniti.com/zetaback/trunk/LICENSE 6 7 =head1 NAME 8 9 Mungo::Response - Represent response side of HTTP request cycle 10 11 =head1 SYNOPSIS 12 13 <!-- You get a Response object for free when you use Mungo --> 14 <% if ($Response) { ... } %> 15 16 <!-- Read and Mungo-process other files --> 17 <% 18 # Prints to browser 19 $Response->Include('/some/file.html', $arg1); 20 21 # Caputured 22 my $output = $Response->TrapInclude('/some/file.html'); 23 24 # Can also print to browser via Response 25 print $Response "Hello world!"; 26 %> 27 28 <!-- May also set headers --> 29 <% 30 $Response->AddHeader('header_name' => $value); 31 %> 32 33 <!-- Halt processing and jump out of the handler --> 34 <% 35 # With a 302 36 $Response->Redirect('/new/url/'); 37 38 # Just end 39 $Response->End(); 40 %> 41 42 <!-- Cookie facility --> 43 <% 44 # Single valued cookies 45 $Response->Cookies($cookie_name, $cookie_value); 46 47 # Multivalued cookies 48 $Response->Cookies($cookie_name, $key, $value); 49 50 # Cookie options 51 $Response->Cookies($cookie_name, 'Domain', $value); 52 $Response->Cookies($cookie_name, 'Expires', $value); 53 $Response->Cookies($cookie_name, 'Path', $value); 54 $Response->Cookies($cookie_name, 'Secure', $value); 55 %> 56 57 =head1 DESCRIPTION 58 59 Represents the response side of the Mungo request cycle. 60 61 =cut 62 6 63 7 64 use strict; … … 103 160 } 104 161 162 =head2 $Response->AddHeader('header_name' => 'header_value'); 163 164 Adds an HTTP header to the response. 165 166 Dies if headers (or any other output) has already been sent. 167 168 =cut 169 105 170 sub AddHeader { 106 171 my $self = shift; … … 115 180 $cookie->__set(@_); 116 181 } 182 183 =head2 $Response->Redirect($url); 184 185 Issues a 302 redirect with the new location as $url. 186 187 Dies if headers (or any other output) has already been sent. 188 189 =cut 190 117 191 sub Redirect { 118 192 my $self = shift; … … 208 282 } 209 283 284 =head2 $output = $Response->TrapInclude($filename, @args); 285 286 Like Include(), but results are returned as a string, instead of being printed. 287 288 =cut 289 210 290 sub TrapInclude { 211 291 my $self = shift; … … 226 306 } 227 307 308 =head2 $Response->End() 309 310 Stops processing the current response, shuts down the 311 output handle, and jumps out of the response handler. 312 No further processing will occur. 313 314 =cut 315 228 316 sub End { 229 317 my $self = shift; … … 302 390 sub UNTIE { } 303 391 392 =head1 AUTHOR 393 394 Theo Schlossnagle 395 396 Clinton Wolfe (docs) 397 398 =cut 399 304 400 1;
