Changeset 3409

Show
Ignore:
Timestamp:
03/10/10 22:22:40 (2 years ago)
Author:
steve.schnepp
Message:

- Suppress debug messages
- Add cache headers for 304
- remove old graph file before generation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/master/_bin/munin-cgi-graph.in

    r3404 r3409  
    8888    # If a "Cache-Control: no-cache" header gets send, we regenerate the image in every case: 
    8989    my $no_cache = $pinpoint || defined($ENV{HTTP_CACHE_CONTROL}) && $ENV{HTTP_CACHE_CONTROL} =~ /no-cache/i; 
    90     print STDERR "no_cache:$no_cache\n"; 
    9190 
    9291    # Compute the cache values 
     
    102101                print "Status: 304\n"; 
    103102                print "Content-Type: image/png\n"; 
     103                print "Content-Length: 0\n"; 
     104                my $headers = get_headers_for_file($filename, $graph_ttl); 
     105                 
     106                # Conditionaly add timing informations 
     107                print "Expires: $headers->{Expires}\n"; 
     108                print "Last-Modified: " . $headers->{"Last-Modified"} . "\n"; 
     109 
    104110                print "\n"; 
    105                  
    106111                # We replied, continue with the next request  
    107112                next; 
     
    118123    } 
    119124 
    120     # At this time the file exists and should be served 
    121     my @stats       = stat ($filename); 
    122     my $mtime_epoch = $stats[9]; 
    123     my $last_modified = get_w3c_date_from_epoch($mtime_epoch); 
    124  
    125     # "Expires" has to use last modified time as base: 
    126     my $graph_next_expires = $mtime_epoch - ($mtime_epoch % $graph_ttl) + $graph_ttl; 
    127     my $expires       = get_w3c_date_from_epoch($graph_next_expires); 
    128125     
    129126 
     
    131128    print "Status: 200\n"; 
    132129    print "Content-Type: image/png\n"; 
    133     print "Content-Length: $stats[7]\n"
    134  
    135     # Conditionaly add timing informations 
    136     print "Expires: $expires\n" if $expires; 
    137     print "Last-Modified: $last_modified\n" if $last_modified; 
     130    my $headers = get_headers_for_file($filename, $graph_ttl)
     131    foreach my $header_name (keys %$headers) { 
     132            print "$header_name: $headers->{$header_name}\n"; 
     133    } 
     134 
    138135    print "\n"; 
    139136 
     
    143140# END FAST-CGI LOOP 
    144141 
     142sub get_headers_for_file { 
     143        my $filename = shift; 
     144        my $graph_ttl = shift; 
     145     
     146        # At this time the file exists and should be served 
     147        my @stats       = stat ($filename); 
     148        my $mtime_epoch = $stats[9]; 
     149        my $last_modified = get_w3c_date_from_epoch($mtime_epoch); 
     150 
     151        # "Expires" has to use last modified time as base: 
     152        my $graph_next_expires = $mtime_epoch - ($mtime_epoch % $graph_ttl) + $graph_ttl; 
     153        my $expires       = get_w3c_date_from_epoch($graph_next_expires); 
     154 
     155        return { 
     156                "Expires" => $expires, 
     157                "Last-Modified" => $last_modified, 
     158                "Content-Length" => $stats[7], 
     159        }; 
     160     
     161} 
     162 
    145163sub get_w3c_date_from_epoch { 
    146164        my $epoch = shift; 
    147         print STDERR "get_w3c_date_from_epoch($epoch)\n"; 
    148165        return strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($epoch)); 
    149166} 
     
    274291    my $logfile = shift; 
    275292 
     293    # remove old file 
     294    unlink($filename) or die("cannot remove old graph file: $!"); 
     295 
    276296    $serv =~ s{[^\w_\/"'\[\]\(\)+=-]}{_}g; $serv =~ /^(.+)$/; $serv = $1; #" 
    277297    # . needs to be legal in host names