Changeset 42

Show
Ignore:
Timestamp:
09/17/08 03:32:19 (6 years ago)
Author:
jesus
Message:

refs #15. Need to test this extensively, but it behaves as expected in my cursory tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/Mungo/Arbiter.pm

    r14 r42  
    2323  my $parent = shift; 
    2424  my $data = shift; 
    25   return bless { 'Mungo' => $parent, data => $data }, $class; 
     25  return bless { '__Internal__' => $parent, data => $data }, $class; 
    2626} 
    2727sub UNTIE { 
    2828  my $self = shift; 
    29   delete $self->{'Mungo'}; 
     29  delete $self->{'__Internal__'}; 
    3030} 
    3131sub FIRSTKEY { 
     
    4848  return exists $self->{data}->{$key}; 
    4949} 
    50 sub DELETE { 
    51   my $self = shift; 
    52   my @details = caller(1); 
    53   my $key = shift; 
    54   if($details[3] =~ /::cleanse$/) { 
    55     delete $self->{data}->{$key}; 
    56   } 
    57 
     50sub DELETE { } 
    5851sub STORE { 
    5952  my ($self, $key, $value) = @_; 
  • trunk/lib/Mungo/Arbiter/Response.pm

    r14 r42  
    2121      die $@ if $@; 
    2222    } 
    23     return $self->{data}->{$class} = $class->new(); 
     23    return $self->{data}->{$class} = $self->{data}->{$key} = $class->new(); 
    2424  } 
    2525  return $self->SUPER::FETCH($key); 
     
    4646  if($key eq 'Buffer' && $value == 0 && 
    4747     $self->{data}->{$key} != 0) { 
    48     $self->{'Mungo'}->Flush(); 
     48    $self->{'__Internal__'}->Flush(); 
    4949    return 1; 
    5050  } 
  • trunk/lib/Mungo/Response.pm

    r39 r42  
    107107sub cleanse { 
    108108  my $self = shift; 
    109   if(ref $self->{'IO_stack'} eq 'ARRAY') { 
    110     while (@{$self->{'IO_stack'}}) { 
    111       my $fh = pop @{$self->{'IO_stack'}}; 
     109  my $_r = tied %$self; 
     110  if(ref $_r->{data}->{'IO_stack'} eq 'ARRAY') { 
     111    while (@{$_r->{data}->{'IO_stack'}}) { 
     112      my $fh = pop @{$_r->{data}->{'IO_stack'}}; 
    112113      close(select($fh)); 
    113114    } 
    114115  } 
    115   delete $self->{$_} for keys %$self; 
     116  delete $_r->{data}->{$_} for keys %$self; 
    116117  untie %$self if tied %$self; 
    117118} 
     
    119120sub send_http_header { 
    120121  my $self = shift; 
    121   my $r = $self->{'Apache::Request'}; 
    122   return if($self->{'__HEADERS_SENT__'}); 
    123   $self->{'__HEADERS_SENT__'} = 1; 
    124   if($self->{CacheControl} eq 'no-cache') { 
     122  my $_r = tied %$self; 
     123  my $r = $_r->{data}->{'Apache::Request'}; 
     124  return if($_r->{data}->{'__HEADERS_SENT__'}); 
     125  $_r->{data}->{'__HEADERS_SENT__'} = 1; 
     126  if($_r->{data}->{CacheControl} eq 'no-cache') { 
    125127    $r->no_cache(1); 
    126128  } 
    127129  else { 
    128130    if($r->can('headers_out')) { 
    129       $r->headers_out->set('Cache-Control' => $self->{CacheControl}); 
     131      $r->headers_out->set('Cache-Control' => $_r->{data}->{CacheControl}); 
    130132    } 
    131133    else { 
    132       $r->header_out('Cache-Control' => $self->{CacheControl}); 
    133     } 
    134   } 
    135   $self->{Cookies}->inject_headers($r); 
    136   $r->status($self->{Status}); 
     134      $r->header_out('Cache-Control' => $_r->{data}->{CacheControl}); 
     135    } 
     136  } 
     137  # Must use Internal as the tiehash is magic for cookies 
     138  $_r->{'__Internal__'}->{Cookies}->inject_headers($r); 
     139  $r->status($_r->{data}->{Status}); 
    137140  $r->can('send_http_header') ? 
    138     $r->send_http_header($self->{ContentType}) : 
    139     $r->content_type($self->{ContentType});; 
     141    $r->send_http_header($_r->{data}->{ContentType}) : 
     142    $r->content_type($_r->{data}->{ContentType});; 
    140143} 
    141144 
    142145sub start { 
    143146  my $self = shift; 
    144   return if(exists $self->{'IO_stack'} && 
    145             scalar(@{$self->{'IO_stack'}}) > 0); 
    146   $self->{'IO_stack'} = []; 
     147  my $_r = tied %$self; 
     148  return if(exists $_r->{data}->{'IO_stack'} && 
     149            scalar(@{$_r->{data}->{'IO_stack'}}) > 0); 
     150  $_r->{data}->{'IO_stack'} = []; 
    147151  tie *DIRECT, ref $self, $self; 
    148   push @{$self->{'IO_stack'}}, select(DIRECT); 
     152  push @{$_r->{data}->{'IO_stack'}}, select(DIRECT); 
    149153} 
    150154 
    151155sub finish { 
    152156  my $self = shift; 
    153   # Unbuffer outselves, this will actually induce a flush 
    154   $self->{Buffer} = 0; 
     157  my $_r = tied %$self; 
     158  # Unbuffer outselves, this will actually induce a flush (must go through tiehash) 
     159  $_r->{'__Internal__'}->{Buffer} = 0; 
    155160  untie *DIRECT if tied *DIRECT; 
    156   return unless(exists $self->{'IO_stack'}); 
    157   my $fh = $self->{'IO_stack'}->[0]; 
    158   delete $self->{'IO_stack'}; 
    159   die __PACKAGE__." IO stack of wrong depth" if(scalar(@{$self->{'IO_stack'}}) != 1); 
     161  return unless(exists $_r->{data}->{'IO_stack'}); 
     162  my $fh = $_r->{data}->{'IO_stack'}->[0]; 
     163  die __PACKAGE__." IO stack of wrong depth" if(scalar(@{$_r->{data}->{'IO_stack'}}) != 1); 
    160164} 
    161165 
     
    170174sub AddHeader { 
    171175  my $self = shift; 
    172   my $r = $self->{'Apache::Request'}; 
    173   die "Headers already sent." if($self->{'__HEADERS_SENT__'}); 
     176  my $_r = tied %$self; 
     177  my $r = $_r->{data}->{'Apache::Request'}; 
     178  die "Headers already sent." if($_r->{data}->{'__HEADERS_SENT__'}); 
    174179  $r->can('headers_out') ? $r->headers_out->set(@_) : $r->header_out(@_); 
    175180} 
    176181sub Cookies { 
    177182  my $self = shift; 
    178   die "Headers already sent." if($self->{'__HEADERS_SENT__'}); 
    179   my $cookie = $self->{'Cookies'}; 
     183  my $_r = tied %$self; 
     184  die "Headers already sent." if($_r->{data}->{'__HEADERS_SENT__'}); 
     185  # Must use Internal as the tiehash is magic for cookies 
     186  my $cookie = $_r->{'__Internal__'}->{'Cookies'}; 
    180187  $cookie->__set(@_); 
    181188} 
     
    192199  my $self = shift; 
    193200  my $url = shift; 
    194   die "Cannot redirect, headers already sent\n" if($self->{'__HEADERS_SENT__'}); 
    195   $self->{Status} = shift || 302; 
    196   my $r = $self->{'Apache::Request'}; 
     201  my $_r = tied %$self; 
     202  die "Cannot redirect, headers already sent\n" if($_r->{data}->{'__HEADERS_SENT__'}); 
     203  $_r->{data}->{Status} = shift || 302; 
     204  my $r = $_r->{data}->{'Apache::Request'}; 
    197205  $r->can('headers_out') ? $r->headers_out->set('Location', $url) : 
    198206                           $r->header_out('Location', $url); 
     
    217225  my $self = shift; 
    218226  my $subject = shift; 
     227  my $_r = tied %$self; 
    219228  my $rv; 
    220229  eval { 
    221230    local $SIG{__DIE__} = \&Mungo::MungoDie; 
    222231    if(ref $subject) { 
    223       $rv = $self->{'Mungo'}->include_mem($subject, @_); 
     232      $rv = $_r->{data}->{Mungo}->include_mem($subject, @_); 
    224233    } 
    225234    else { 
    226       $rv = $self->{'Mungo'}->include_file($subject, @_); 
     235      $rv = $_r->{data}->{Mungo}->include_file($subject, @_); 
    227236    } 
    228237  }; 
    229238  if($@) { 
    230239    # If we have more than 1 item in the IO stack, we should just re-raise. 
    231     if (scalar(@{$self->{'IO_stack'} || []}) > 1) { 
     240    if (scalar(@{$_r->{data}->{'IO_stack'} || []}) > 1) { 
    232241      local $SIG{__DIE__} = undef; 
    233242      die $@; 
     
    235244    my $hashref = $@; 
    236245    eval { 
    237       if($self->{OnError}) { 
    238         $self->{OnError}->($self, $hashref, $subject); 
     246      if($_r->{data}->{OnError}) { 
     247        $_r->{data}->{OnError}->($self, $hashref, $subject); 
    239248      } 
    240249      else { 
     
    258267  my $href = shift; # Our Error 
    259268  my $subject = shift; 
     269  my $_r = tied %$self; 
    260270  print "Error in Include($subject):<br />\n"; 
    261271  my $pkg = $href->{callstack}->[0]->[0]; 
     
    275285 
    276286  if($contents) { 
    277     if($self->{'Apache::Request'}->dir_config('Debug')) { 
     287    if($_r->{data}->{'Apache::Request'}->dir_config('Debug')) { 
    278288      print Mungo::Utils::pretty_print_code($preamble, $contents, $postamble, $href->{callstack}->[0]->[2]); 
    279289    } 
     
    291301sub TrapInclude { 
    292302  my $self = shift; 
     303  my $_r = tied %$self; 
    293304  my $output; 
    294305  my $handle = \do { local *HANDLE }; 
    295306  tie *{$handle}, 'Mungo::Response::Trap', \$output; 
    296   push @{$self->{'IO_stack'}}, select(*{$handle}); 
     307  push @{$_r->{data}->{'IO_stack'}}, select(*{$handle}); 
    297308  eval { 
    298309    $self->Include(@_); 
    299310  }; 
    300311  untie *{$handle} if tied *{$handle}; 
    301   select(pop @{$self->{'IO_stack'}}); 
     312  select(pop @{$_r->{data}->{'IO_stack'}}); 
    302313  if($@) { 
    303314    local $SIG{__DIE__} = undef; 
     
    317328sub End { 
    318329  my $self = shift; 
    319   while(scalar(@{$self->{'IO_stack'} || []}) > 1) { 
    320     my $oldfh = select(pop @{$self->{'IO_stack'}}); 
     330  my $_r = tied %$self; 
     331  while(scalar(@{$_r->{data}->{'IO_stack'} || []}) > 1) { 
     332    my $oldfh = select(pop @{$_r->{data}->{'IO_stack'}}); 
    321333    if(my $obj = tied *{$oldfh}) { 
    322334      untie *{$oldfh}; 
     
    330342sub Flush { 
    331343  my $self = shift; 
     344  my $_r = tied %$self; 
    332345  # Flush doesn't apply unless we're immediately above STDOUT 
    333   return if(scalar(@{$self->{'IO_stack'} || []}) > 1); 
    334   unless($self->{'__OUTPUT_STARTED__'}) { 
     346  return if(scalar(@{$_r->{data}->{'IO_stack'} || []}) > 1); 
     347  unless($_r->{data}->{'__OUTPUT_STARTED__'}) { 
    335348    $self->send_http_header; 
    336     $self->{'__OUTPUT_STARTED__'} = 1; 
    337   } 
    338   if (@{$self->{'IO_stack'} || []}) { 
    339       $self->{'IO_stack'}->[-1]->print($one_true_buffer); 
     349    $_r->{data}->{'__OUTPUT_STARTED__'} = 1; 
     350  } 
     351  if (@{$_r->{data}->{'IO_stack'} || []}) { 
     352      $_r->{data}->{'IO_stack'}->[-1]->print($one_true_buffer); 
    340353  } else { 
    341354      print $one_true_buffer; 
     
    360373  my $self = shift; 
    361374  my $output = shift; 
    362   if(scalar(@{$self->{'IO_stack'} || []}) == 1) { 
     375  my $_r = tied %$self; 
     376  if(scalar(@{$_r->{data}->{'IO_stack'} || []}) == 1) { 
    363377    # Buffering a just-in-time headers only applies if we 
    364378    # immediately above STDOUT 
    365     if($self->{Buffer}) { 
     379    if($_r->{data}->{Buffer}) { 
    366380      $one_true_buffer .= $output; 
    367381      return; 
    368382    } 
    369     unless($self->{'__OUTPUT_STARTED__'}) { 
    370       $self->{'__OUTPUT_STARTED__'} = 1; 
     383    unless($_r->{data}->{'__OUTPUT_STARTED__'}) { 
     384      $_r->{data}->{'__OUTPUT_STARTED__'} = 1; 
    371385      $self->send_http_header; 
    372386    } 
    373387  } 
    374   if (@{$self->{'IO_stack'} || []}) { 
    375       $self->{'IO_stack'}->[-1]->print($output); 
     388  if (@{$_r->{data}->{'IO_stack'} || []}) { 
     389      $_r->{data}->{'IO_stack'}->[-1]->print($output); 
    376390  } else { 
    377391      print $output; 
     
    380394sub PRINTF { 
    381395  my $self = shift; 
    382   if(scalar(@{$self->{'IO_stack'} || []}) == 1) { 
     396  my $_r = tied %$self; 
     397  if(scalar(@{$_r->{data}->{'IO_stack'} || []}) == 1) { 
    383398    # Buffering a just-in-time headers only applies if we 
    384399    # immediately above STDOUT 
    385     if($self->{Buffer}) { 
     400    if($_r->{data}->{Buffer}) { 
    386401      $one_true_buffer .= sprintf(@_); 
    387402      return; 
    388403    } 
    389     unless($self->{'__OUTPUT_STARTED__'}) { 
    390       $self->{'__OUTPUT_STARTED__'} = 1; 
     404    unless($_r->{data}->{'__OUTPUT_STARTED__'}) { 
     405      $_r->{data}->{'__OUTPUT_STARTED__'} = 1; 
    391406      $self->send_http_header; 
    392407    } 
    393408  } 
    394   if (@{$self->{'IO_stack'} || []}) { 
    395       $self->{'IO_stack'}->[-1]->printf(@_); 
     409  if (@{$_r->{data}->{'IO_stack'} || []}) { 
     410      $_r->{data}->{'IO_stack'}->[-1]->printf(@_); 
    396411  } else { 
    397412      printf(@_); 
     
    400415sub CLOSE { 
    401416  my $self = shift; 
    402   $self->{Buffer} = 0; 
     417  my $_r = tied %$self; 
     418  # Unbuffer outselves, this will actually induce a flush (must go through tiehash) 
     419  $_r->{data}->{Buffer} = 0; 
    403420} 
    404421sub UNTIE { }