This is an automated email from the ASF dual-hosted git repository. bcall pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push: new 789d7a8 traffic_via.pl: Fixed bugs, added tests, and make the output match more like traffic_via. 789d7a8 is described below commit 789d7a8b798de5b0dee2c83c7ff27b4b201f8919 Author: Bryan Call <bc...@apache.org> AuthorDate: Fri Mar 15 13:59:24 2019 -0700 traffic_via.pl: Fixed bugs, added tests, and make the output match more like traffic_via. --- tools/traffic_via/test_traffic_via_pl | 47 ++++++ tools/traffic_via/tests/[u c s f p eS;tNc p s ] | 13 ++ tools/traffic_via/tests/[uIcRs f p eN;t cCHp s ] | 13 ++ tools/traffic_via/tests/[uIcRs f p eN;t cCNp s ] | 13 ++ tools/traffic_via/tests/[uScMsSf pSeN;t cCMp sS] | 13 ++ tools/traffic_via/tests/[uScRs f p eN;t cCHp s ] | 13 ++ tools/traffic_via/tests/long rubbish via code2 | 15 ++ tools/traffic_via/tests/rubbish | 6 + tools/traffic_via/tests/short | 5 + tools/{ => traffic_via}/traffic_via.pl | 197 +++++++++++++++-------- 10 files changed, 265 insertions(+), 70 deletions(-) diff --git a/tools/traffic_via/test_traffic_via_pl b/tools/traffic_via/test_traffic_via_pl new file mode 100755 index 0000000..ed4c6e1 --- /dev/null +++ b/tools/traffic_via/test_traffic_via_pl @@ -0,0 +1,47 @@ +#! /usr/bin/env bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e # exit on error + +TMPDIR=${TMPDIR:-/tmp} +tmpfile=$(mktemp "$TMPDIR/via.XXXXXX") +if [ ! -z "$srcdir"]; then + srcdir=$(cd $srcdir && pwd) +else + srcdir=$(pwd) +fi + +# test command line option +echo "Testing the command line option:" +for f in $srcdir/tests/*; do + name=$(basename "$f") + echo "testing $name" + ./traffic_via.pl -s "$name" > "$tmpfile" 2>&1 || true + diff -u "$tmpfile" "$srcdir/tests/$name" +done + +# test stdin +echo -e "\nTesting stdin:" +for f in $srcdir/tests/\[*; do + name=$(basename "$f") + echo "testing $name" + echo "$name" | ./traffic_via.pl > "$tmpfile" 2>&1 || true + diff -u "$tmpfile" "$srcdir/tests/$name" +done + +rm -f $tmpfile diff --git a/tools/traffic_via/tests/[u c s f p eS;tNc p s ] b/tools/traffic_via/tests/[u c s f p eS;tNc p s ] new file mode 100644 index 0000000..e662dc0 --- /dev/null +++ b/tools/traffic_via/tests/[u c s f p eS;tNc p s ] @@ -0,0 +1,13 @@ +Via header is [u c s f p eS;tNc p s ], Length is 22 +Via Header Details: +Request headers received from client :unknown +Result of Traffic Server cache lookup for URL :no cache lookup +Response information received from origin server :no server connection needed +Result of document write-to-cache: :no cache write performed +Proxy operation result :unknown +Error codes (if any) :server related error +Tunnel info :tunneling due to no forward +Cache Type :unknown +Cache Lookup Result :cache miss or no cache lookup +Parent proxy connection status :no parent proxy or unknown +Origin server connection status :no server connection needed diff --git a/tools/traffic_via/tests/[uIcRs f p eN;t cCHp s ] b/tools/traffic_via/tests/[uIcRs f p eN;t cCHp s ] new file mode 100644 index 0000000..0755883 --- /dev/null +++ b/tools/traffic_via/tests/[uIcRs f p eN;t cCHp s ] @@ -0,0 +1,13 @@ +Via header is [uIcRs f p eN;t cCHp s ], Length is 22 +Via Header Details: +Request headers received from client :IMS +Result of Traffic Server cache lookup for URL :in cache, fresh Ram hit (a cache "HIT") +Response information received from origin server :no server connection needed +Result of document write-to-cache: :no cache write performed +Proxy operation result :unknown +Error codes (if any) :no error +Tunnel info :no tunneling +Cache Type :cache +Cache Lookup Result :cache hit +Parent proxy connection status :no parent proxy or unknown +Origin server connection status :no server connection needed diff --git a/tools/traffic_via/tests/[uIcRs f p eN;t cCNp s ] b/tools/traffic_via/tests/[uIcRs f p eN;t cCNp s ] new file mode 100644 index 0000000..6ecfc64 --- /dev/null +++ b/tools/traffic_via/tests/[uIcRs f p eN;t cCNp s ] @@ -0,0 +1,13 @@ +Via header is [uIcRs f p eN;t cCNp s ], Length is 22 +Via Header Details: +Request headers received from client :IMS +Result of Traffic Server cache lookup for URL :in cache, fresh Ram hit (a cache "HIT") +Response information received from origin server :no server connection needed +Result of document write-to-cache: :no cache write performed +Proxy operation result :unknown +Error codes (if any) :no error +Tunnel info :no tunneling +Cache Type :cache +Cache Lookup Result :conditional hit (client sent conditional, doc fresh in cache, returned 304) +Parent proxy connection status :no parent proxy or unknown +Origin server connection status :no server connection needed diff --git a/tools/traffic_via/tests/[uScMsSf pSeN;t cCMp sS] b/tools/traffic_via/tests/[uScMsSf pSeN;t cCMp sS] new file mode 100644 index 0000000..6ee096a --- /dev/null +++ b/tools/traffic_via/tests/[uScMsSf pSeN;t cCMp sS] @@ -0,0 +1,13 @@ +Via header is [uScMsSf pSeN;t cCMp sS], Length is 22 +Via Header Details: +Request headers received from client :simple request (not conditional) +Result of Traffic Server cache lookup for URL :miss (a cache "MISS") +Response information received from origin server :connection opened successfully +Result of document write-to-cache: :no cache write performed +Proxy operation result :served or connection opened successfully +Error codes (if any) :no error +Tunnel info :no tunneling +Cache Type :cache +Cache Lookup Result :cache miss (url not in cache) +Parent proxy connection status :no parent proxy or unknown +Origin server connection status :connection opened successfully diff --git a/tools/traffic_via/tests/[uScRs f p eN;t cCHp s ] b/tools/traffic_via/tests/[uScRs f p eN;t cCHp s ] new file mode 100644 index 0000000..ea96fdb --- /dev/null +++ b/tools/traffic_via/tests/[uScRs f p eN;t cCHp s ] @@ -0,0 +1,13 @@ +Via header is [uScRs f p eN;t cCHp s ], Length is 22 +Via Header Details: +Request headers received from client :simple request (not conditional) +Result of Traffic Server cache lookup for URL :in cache, fresh Ram hit (a cache "HIT") +Response information received from origin server :no server connection needed +Result of document write-to-cache: :no cache write performed +Proxy operation result :unknown +Error codes (if any) :no error +Tunnel info :no tunneling +Cache Type :cache +Cache Lookup Result :cache hit +Parent proxy connection status :no parent proxy or unknown +Origin server connection status :no server connection needed diff --git a/tools/traffic_via/tests/long rubbish via code2 b/tools/traffic_via/tests/long rubbish via code2 new file mode 100644 index 0000000..470c5a1 --- /dev/null +++ b/tools/traffic_via/tests/long rubbish via code2 @@ -0,0 +1,15 @@ +Via header is [long rubbish via code2], Length is 22 +traffic_via: Invalid VIA header character: l +traffic_via: Invalid VIA header character: o +traffic_via: Invalid VIA header character: n +traffic_via: Invalid VIA header character: g +traffic_via: Invalid VIA header character: r +traffic_via: Invalid VIA header character: b +traffic_via: Invalid VIA header character: b +traffic_via: Invalid VIA header character: i +traffic_via: Invalid VIA header character: h +traffic_via: Invalid VIA header character: v +traffic_via: Invalid VIA header character: i +traffic_via: Invalid VIA header character: a +traffic_via: Invalid VIA header character: o +traffic_via: Invalid VIA header character: d diff --git a/tools/traffic_via/tests/rubbish b/tools/traffic_via/tests/rubbish new file mode 100644 index 0000000..dbeeab0 --- /dev/null +++ b/tools/traffic_via/tests/rubbish @@ -0,0 +1,6 @@ +Via header is [rubbish], Length is 7 +traffic_via: Invalid VIA header character: r +traffic_via: Invalid VIA header character: b +traffic_via: Invalid VIA header character: b +traffic_via: Invalid VIA header character: i +traffic_via: Invalid VIA header character: h diff --git a/tools/traffic_via/tests/short b/tools/traffic_via/tests/short new file mode 100644 index 0000000..384a483 --- /dev/null +++ b/tools/traffic_via/tests/short @@ -0,0 +1,5 @@ +Via header is [short], Length is 5 +traffic_via: Invalid VIA header character: h +traffic_via: Invalid VIA header character: o +traffic_via: Invalid VIA header character: r +traffic_via: Invalid VIA header character: t diff --git a/tools/traffic_via.pl b/tools/traffic_via/traffic_via.pl similarity index 62% rename from tools/traffic_via.pl rename to tools/traffic_via/traffic_via.pl index e5a0f29..a135364 100755 --- a/tools/traffic_via.pl +++ b/tools/traffic_via/traffic_via.pl @@ -40,28 +40,28 @@ my $help; #Proxy request header flags and titles my @proxy_header_array = ( { - "Request headers received from client:", { - 'I' => "If Modified Since (IMS)", + "Request headers received from client", { + 'I' => "IMS", 'C' => "cookie", 'E' => "error in request", 'S' => "simple request (not conditional)", 'N' => "no-cache", - ' ' => "unknown?", + ' ' => "unknown", }, }, { - "Result of Traffic Server cache lookup for URL:", { + "Result of Traffic Server cache lookup for URL", { 'A' => "in cache, not acceptable (a cache \"MISS\")", 'H' => "in cache, fresh (a cache \"HIT\")", 'S' => "in cache, stale (a cache \"MISS\")", 'R' => "in cache, fresh Ram hit (a cache \"HIT\")", 'M' => "miss (a cache \"MISS\")", - ' ' => "unknown?", + ' ' => "no cache lookup", }, }, { - "Response information received from origin server:", { + "Response information received from origin server", { 'E' => "error in response", ' ' => "no server connection needed", - 'S' => "served", + 'S' => "connection opened successfully", 'N' => "not-modified", } }, { @@ -72,14 +72,14 @@ my @proxy_header_array = ( ' ' => "no cache write performed", }, }, { - "Proxy operation result:", { + "Proxy operation result", { 'R' => "origin server revalidated", - ' ' => "unknown?", - 'S' => "served", + ' ' => "unknown", + 'S' => "served or connection opened successfully", 'N' => "not-modified", }, }, { - "Error codes (if any):", { + "Error codes (if any)", { 'A' => "authorization failure", 'H' => "header syntax unacceptable", 'C' => "connection to server failed", @@ -90,47 +90,42 @@ my @proxy_header_array = ( 'F' => "request forbidden", }, }, { - "Tunnel info:", { + "Tunnel info", { ' ' => "no tunneling", 'U' => "tunneling because of url (url suggests dynamic content)", 'M' => "tunneling due to a method (e.g. CONNECT)", 'O' => "tunneling because cache is turned off", 'F' => "tunneling due to a header field (such as presence of If-Range header)", + 'N' => "tunneling due to no forward", }, }, { - "Cache type:", { + "Cache Type", { + ' ' => "unknown", 'I' => "icp", - ' ' => "cache miss or no cache lookup", 'C' => "cache", }, }, { - "Cache lookup result:", { - ' ' => "no cache lookup", + "Cache Lookup Result", { + ' ' => "cache miss or no cache lookup", 'S' => "cache hit, but expired", 'U' => "cache hit, but client forces revalidate (e.g. Pragma: no-cache)", 'D' => "cache hit, but method forces revalidated (e.g. ftp, not anonymous)", 'I' => "conditional miss (client sent conditional, fresh in cache, returned 412)", 'H' => "cache hit", 'M' => "cache miss (url not in cache)", - 'C' => "cache hit, but config forces revalidate", + 'C' => "cache miss (url not in cache)", 'N' => "conditional hit (client sent conditional, doc fresh in cache, returned 304)", }, }, { - "ICP status:", { - ' ' => "no icp", - 'S' => "connection opened successfully", - 'F' => "connection open failed", - }, - }, { - "Parent proxy connection status:", { - ' ' => "no parent proxy", + "Parent proxy connection status", { + ' ' => "no parent proxy or unknown", 'S' => "connection opened successfully", 'F' => "connection open failed", }, }, { - "Origin server connection status:", { - ' ' => "no server connection", + "Origin server connection status", { + ' ' => "no server connection needed", 'S' => "connection opened successfully", 'F' => "connection open failed", }, @@ -138,7 +133,7 @@ my @proxy_header_array = ( ); ##Print script usage -sub usage +sub usage() { print "\nPass Via Header with -s option \n"; print "Usage: traffic_via [-s viaheader]"; @@ -149,41 +144,10 @@ sub usage exit; } -if (@ARGV == 0) { - #if passed through standard input - my @userinput = <STDIN>; - my $via_string; - - for my $element (@userinput) { - #Pattern matching for Via - if ($element =~ /Via:(.*)\[(.*)\]/) { - #Search and grep via header - $via_string = $2; - chomp($via_string); - print "Via Header is [$via_string]"; - decode_via_header($via_string); - } - } -} else { - usage() - if ( - !GetOptions( - 's=s' => \$via_header, - 'help|?' => \$help - ) - or defined $help - ); - - if (defined $via_header) { - #if passed through commandline dashed argument - print "Via Header is [$via_header]"; - decode_via_header($via_header); - } -} #Subroutine to decode via header -sub decode_via_header +sub decode_via_header($) { my ($header) = @_; my $hdrLength; @@ -194,45 +158,98 @@ sub decode_via_header #Get via header length $hdrLength = length($header); - # Valid Via header length is 24 or 6. - # When Via header length is 24, it will have both proxy request header result and operational results. - if ($hdrLength == 24) { + # Valid Via header length is 22 or 6. + # When Via header length is 22, it will have both proxy request header result and operational results. + if ($hdrLength == 22) { #Split via header: proxy result and operational result $newHeader = join('', split(':', $header)); } elsif ($hdrLength == 6) { $newHeader = $header; } elsif ($hdrLength == 5) { # When Via header length is 5, it might be missing last field. Fill it and decode header. - my $newHeader = "$header" . " "; + $newHeader = $header . " "; } else { # Invalid header size, come out. - print "\nInvalid VIA header. VIA header length should be 6 or 24 characters\n"; + print "\nInvalid VIA header. VIA header length should be 6 or 22 characters\n"; return; } + convert_header_to_array($newHeader); } } -sub convert_header_to_array +sub convert_header_to_array($) { my ($viaHeader) = @_; my @ResultArray; #Convert string header into character array while ($viaHeader =~ /(.)/g) { #Only capital letters indicate flags - if ($1 !~ m/[a-z]+/) { + if ($1 !~ m/[a-z;]+/) { push(@ResultArray, $1); } } - print "\nVia Header details: \n"; + print "Via Header Details:\n"; for (my $arrayIndex = 0; $arrayIndex < scalar(@ResultArray); $arrayIndex++) { get_via_header_flags(\@proxy_header_array, $arrayIndex, $ResultArray[$arrayIndex]); } } +my %valid_keys = ('main' => { + 'u' => 1, + 'c' => 1, + 's' => 1, + 'f' => 1, + 'p' => 1, + 'e' => 1, + }, + 'detail' => { + 't' => 1, + 'c' => 1, + 'p' => 1, + 's' => 1, + } +); + +sub valid_char ($$) +{ + my($char, $hash) = @_; + + return exists $hash->{$char} +} + +sub validate_keys($) +{ + my($viaHeader) = @_; + my($main, $detail) = split(';', $viaHeader); + my $running_main = 1; + my $return_value_valid = 1; + + foreach my $group ($main, $detail) { + next if !defined $group; + + while ($group =~ /([a-z])/g) { + my $char = $1; + + my $valid = 0; + if ($running_main) { + $valid = valid_char($char, $valid_keys{main}); + } else { + $valid = valid_char($char, $valid_keys{detail}); + } + if (! $valid) { + print "traffic_via: Invalid VIA header character: $char\n"; + $return_value_valid = 0; + } + } + $running_main = 0; + } + return $return_value_valid; +} + #Get values from header arrays -sub get_via_header_flags +sub get_via_header_flags($$$) { my ($arrayName, $inputIndex, $flag) = @_; @@ -253,9 +270,49 @@ sub get_via_header_flags foreach my $key (@keys) { if ($key =~ /$flag/) { #print $flags{$key}; - printf("%s", $flags{$key}); + printf(":%s", $flags{$key}); print "\n"; } } } } + +# main +{ + if (@ARGV == 0) { + #if passed through standard input + my @userinput = <STDIN>; + + for my $element (@userinput) { + #Pattern matching for Via + if ($element =~ /Via:\s+\[(.+)\]/i || $element =~ /\[(.+)\]/ ) { + #Search and grep via header + my $via_string = $1; + chomp($via_string); + print "Via header is [$via_string], Length is ", length($via_string), "\n"; + last unless validate_keys($via_string); + decode_via_header($via_string); + } + } + } else { + usage() + if ( + !GetOptions( + 's=s' => \$via_header, + 'help|?' => \$help + ) + or defined $help + ); + + if (defined $via_header) { + if ($via_header =~ /Via:\s+\[(.+)\]/i || $via_header =~ /\[(.+)\]/ || $via_header =~ /(.+)/) { + #if passed through commandline dashed argument + my $via_string = $1; + print "Via header is [$via_string], Length is ", length($via_string), "\n"; + last unless validate_keys($via_string); + decode_via_header($via_string); + } + } + + } +} \ No newline at end of file