File: pcchget.perl

package info (click to toggle)
libpoe-component-client-http-perl 0.949-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 468 kB
  • sloc: perl: 3,566; makefile: 10
file content (63 lines) | stat: -rw-r--r-- 1,502 bytes parent folder | download | duplicates (5)
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
#!perl

# A short program to dump requests and responses.
# Provided by Toby Ovod-Everett.  Thanks!

use strict;

sub POE::Kernel::ASSERT_DEFAULT () { 1 }

use HTTP::Request;
use POE qw(Component::Client::HTTP);

POE::Component::Client::HTTP->spawn(
  Alias     => 'ua',                  # defaults to 'weeble'
  Timeout   => 20,                    # defaults to 180 seconds
);

POE::Session->create(
  inline_states => {
    _start => sub {
      POE::Kernel->post(
        'ua',        # posts to the 'ua' alias
        'request',   # posts to ua's 'request' state
        'response',  # which of our states will receive the response
        HTTP::Request->new(GET => $ARGV[0]),    # an HTTP::Request object
      );
    },
    _stop => sub {},
    response => \&response_handler,
  },
);

POE::Kernel->run();
exit;

sub response_handler {
  my ($request_packet, $response_packet) = @_[ARG0, ARG1];
  my $request_object  = $request_packet->[0];
  my $response_object = $response_packet->[0];

  my $stream_chunk;

  if (!defined($response_object->content)) {
    $stream_chunk = $response_packet->[1];
  }

  print(
    "*" x 78, "\n",
    "*** my request:\n",
    "-" x 78, "\n",
    $request_object->as_string(),
    "*" x 78, "\n",
    "*** their response:\n",
    "-" x 78, "\n",
    $response_object->as_string(),
  );

  if (defined $stream_chunk) {
    print( "-" x 40, "\n", $stream_chunk, "\n" );
  }

  print "*" x 78, "\n";
}