AnyEvent-HTTP-Message

 view release on metacpan or  search on metacpan

lib/AnyEvent/HTTP/Response.pm  view on Meta::CPAN

sub pseudo_headers { $_[0]->{pseudo_headers} ||= {} }


sub to_http_message {
  my ($self) = @_;
  require HTTP::Response;

  my $res = HTTP::Response->new(
    @{ $self->pseudo_headers }{qw(Status Reason)},
    [ %{ $self->headers } ],
    $self->body
  );
  if( my $v = $self->pseudo_headers->{HTTPVersion} ){
    $res->protocol("HTTP/$v")
  }
  return $res;
}

1;

__END__

=pod

=encoding utf-8

=for :stopwords Randy Stauner ACKNOWLEDGEMENTS TODO featureful http

=head1 NAME

AnyEvent::HTTP::Response - HTTP Response object for AnyEvent::HTTP

=head1 VERSION

version 0.302

=head1 SYNOPSIS

  # parses argument list passed to AnyEvent::HTTP::http_request callback
  AnyEvent::HTTP::http_request(
    GET => $uri,
    sub {
      my $res = AnyEvent::HTTP::Response->new(@_);

      # inspect attributes
      print $res->header('Content-Type');
      print $res->body;

      # upgrade to HTTP::Response
      my $http_res = $res->to_http_message;
      if( !$http_res->is_success ){
        print $http_res->status_line;
      }
    }
  );

=head1 DESCRIPTION

This object represents an HTTP response from L<AnyEvent::HTTP>.

This is a companion class to L<AnyEvent::HTTP::Request>.

It parses the arguments passed to the final callback in
L<AnyEvent::HTTP/http_request>
(or produces the arguments that should be passed to that,
depending on how you'd like to use it).
and wraps them in an object.

It can also be converted L<from|/from_http_message> or L<to|/to_http_message>
the more featureful
L<HTTP::Response>.

=head1 CLASS METHODS

=head2 new

Accepts an argument list like the callback provided to
L<AnyEvent::HTTP/http_request>
(see L</parse_args>):

  AnyEvent::HTTP::Response->new($body, \%headers);

Alternatively accepts an instance of
L<HTTP::Response>
(see L</from_http_message>):

  AnyEvent::HTTP::Response->new(
    HTTP::Response->new( $code, $reason, $headers, $body )
  );

Also accepts a single hashref of named attributes
(see L</ATTRIBUTES>):

  AnyEvent::HTTP::Response->new({
    body    => $body,
    headers => \%headers,
    pseudo_headers => \%pseudo,
  });

=head2 parse_args

Called by the constructor
to parse the argument list
passed to the callback in
L<AnyEvent::HTTP/http_request>
and return a hashref which will be the basis for the object.

The list should look like
C<< ($body, \%headers) >>.

This will separate the "pseudo" headers
from the regular http headers
as described by
L<AnyEvent::HTTP/http_request>
(http headers are lower-cased
and pseudo headers start with an upper case letter).

=head2 from_http_message

Called by the constructor
when L</new> is passed an instance of L<HTTP::Response>.



( run in 0.917 second using v1.01-cache-2.11-cpan-ceb78f64989 )