File: flag_truncs.pl

package info (click to toggle)
squid3 3.0.PRE5-5%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,188 kB
  • ctags: 20,388
  • sloc: cpp: 119,851; ansic: 30,259; sh: 10,465; makefile: 3,289; perl: 1,267; awk: 84; xml: 58
file content (68 lines) | stat: -rwxr-xr-x 1,887 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/local/bin/perl

# flag_truncs.pl - martin hamilton <m.t.hamilton@lut.ac.uk>
#
# Check the CERN/Harvest/Netscape cache for truncated objects
# - i.e. those for which there is a "Content-length:" HTTP header,
#   and this does not match the size of the cached object

# $Id: flag_truncs.pl,v 1.2 2003/01/23 00:37:06 robertc Exp $

require "getopts.pl";
require "stat.pl";
&Getopts("cd");
# -c -> just count the number of objects with a Content-length header
# -d -> turn on debugging output

# pass filenames on command line or via STDIN
@things = $#ARGV >= 0 ? @ARGV : <STDIN>; 

$total_objects = 0, $content_length = 0;

# iterate through them
foreach $thing (@things) {
  chop $thing;

  $opt_d && (print STDERR ">> inspecting: $thing\n");
  next if -d "$thing"; # don't want directories

  $size = (stat($thing))[$ST_SIZE]||next;
  $opt_d && (print STDERR ">> stat: $size\n");
  print "$thing\n", next if ($size == 0);

  $total_objects++;

  $count = 0, $expected = 0;
  open(IN, "$thing") || die "Can't open cached object $thing: $!";
  while(<IN>) {
    $count += length($_);
    chop;
    print STDERR ">> inspecting $_\n" if $opt_d;
    last if /^(\s+|)$/; # drop out after the end of the HTTP headers

    # skip if cached file appeared since script started running
    if (-M $_ < 0) {
      print STDERR ">> skipping $_\n" if $opt_d;
      next;
    }
    
    if (/^Content-length:\s+(\d+)/i) {
      $expected = $1;
      $content_length++;
    }
  }
  close(IN);

  next if $opt_c;
  next if $expected == 0; # no Content-length header

  # looked at the headers now
  $difference = $size - $count;
  $opt_d && print STDERR ">> real: ", $difference, ", expected: $expected\n";
  if ($difference != $expected) {
    print "$thing (expected: $expected, got: $difference)\n";
  }
}

print "$content_length out of $total_objects had Content-length: header\n"
  if $opt_c;