| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Net::IMP::HTTP::Request; | ||
| 5 | 1 | ▶ | use base 'Net::IMP::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use fields qw(dispatcher pos); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Net::IMP::HTTP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Net::IMP; | ||
| 9 | use Carp 'croak'; | ||
| 10 | |||
| 11 | |||
| 12 | # just define a typical set, maybe need to be redefined in subclass | ||
| 13 | sub RTYPES { | ||
| 14 | my $factory = shift; | ||
| 15 | return (IMP_PASS, IMP_PREPASS, IMP_REPLACE, IMP_DENY, IMP_LOG) | ||
| 16 | } | ||
| 17 | |||
| 18 | sub INTERFACE { | ||
| 19 | my $factory = shift; | ||
| 20 | my @rt = $factory->RTYPES; | ||
| 21 | return ( | ||
| 22 | [ IMP_DATA_HTTPRQ, \@rt ], | ||
| 23 | [ IMP_DATA_STREAM, \@rt, 'Net::IMP::Adaptor::STREAM2HTTPReq' ], | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | |||
| 27 | # we can overide data to handle the types directly, but per default we | ||
| 28 | # dispatch to seperate methods | ||
| 29 | sub data { | ||
| 30 | my ($self,$dir,$data,$offset,$type) = @_; | ||
| 31 | |||
| 32 | $self->{pos}[$dir] = $offset if $offset; | ||
| 33 | $self->{pos}[$dir] += length($data); | ||
| 34 | |||
| 35 | my $disp = $self->{dispatcher} ||= { | ||
| 36 | IMP_DATA_HTTPRQ_HEADER+0 => [ | ||
| 37 | $self->can('request_hdr'), | ||
| 38 | $self->can('response_hdr'), | ||
| 39 | ], | ||
| 40 | IMP_DATA_HTTPRQ_CONTENT+0 => [ | ||
| 41 | $self->can('request_body'), | ||
| 42 | $self->can('response_body'), | ||
| 43 | ], | ||
| 44 | IMP_DATA_HTTPRQ_DATA+0 => $self->can('any_data') | ||
| 45 | }; | ||
| 46 | my $sub = $disp->{$type+0} or croak("cannot dispatch type $type"); | ||
| 47 | if ( ref($sub) eq 'ARRAY' ) { | ||
| 48 | $sub = $sub->[$dir] or croak("cannot dispatch type $type dir $dir"); | ||
| 49 | $sub->($self,$data,$offset); | ||
| 50 | } else { | ||
| 51 | $sub->($self,$dir,$data,$offset); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub offset { | ||
| 56 | my ($self,$dir) = @_; | ||
| 57 | return $self->{pos}[$dir] // 0; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | ########################################################################### | ||
| 62 | # public interface | ||
| 63 | # most of these methods need to be implemented in subclass | ||
| 64 | ########################################################################### | ||
| 65 | |||
| 66 | for my $subname ( | ||
| 67 | 'request_hdr', # ($self,$hdr) | ||
| 68 | 'request_body', # ($self,$data,[$offset]) | ||
| 69 | 'response_hdr', # ($self,$hdr) | ||
| 70 | 'response_body', # ($self,$data,[$offset]) | ||
| 71 | 'any_data', # ($self,$dir,$data,[$offset]) | ||
| 72 | ) { | ||
| 73 | no strict 'refs'; | ||
| 74 | *$subname = sub { croak("$subname needs to be implemented in $_[0]") } | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | 1; | ||
| 79 | __END__ | ||