SHARE
TWEET

Untitled

a guest Jan 12th, 2017 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use feature 'say';
  6. use bytes;
  7.  
  8. local $/ = "\r\n";
  9.  
  10. my $fh = \*STDIN;
  11.  
  12. my $frag = {};
  13.  
  14. sub D { return; say "DEBUG: ", @_ }
  15.  
  16. while (my $fragment_header = <$fh>) {
  17.  
  18.     chomp $fragment_header;
  19.     my ($length, $timestamp, $direction, $uuid, $count) =
  20.         split /\s+/, $fragment_header;
  21.  
  22.     $length = int eval "0x$length";
  23.     $count  = int eval "0x$count";
  24.  
  25.     if ($count == 0) { # first fragment
  26.         my $payload; # response headers, actually
  27.         read $fh, $payload, $length;
  28.         warn $! if $!;
  29.         $frag->{$uuid}[0] = $payload;
  30.         D "init fragment of [$uuid]";
  31.         D "payload len [", length($payload), "] data [$payload]";
  32.         my $empty_line = <$fh>;
  33.         next;
  34.     }
  35.  
  36.     if ($length == 0) { # last fragment
  37.         # no payload
  38.         if (1) {
  39.             shift @{ $frag->{$uuid} };
  40.             if (scalar @{ $frag->{$uuid} }) {
  41.                 open my $frag_fh, ">$uuid.json" or warn $!;
  42.                 print $frag_fh join '', @{ $frag->{$uuid} };
  43.                 close $frag_fh;
  44.             }
  45.         }
  46.         delete $frag->{$uuid};
  47.         D "last fragment of [$uuid]";
  48.         next;
  49.     }
  50.  
  51.     my $payload;
  52.     read $fh, $payload, $length;
  53.     warn $! if $!;
  54.     D "mid fragment of [$uuid]";
  55.     D "payload len [", length($payload), "] data [$payload]";
  56.     my $empty_line = <$fh>;
  57.     push @{ $frag->{$uuid} }, $payload;
  58. }
RAW Paste Data
Top