« blib/lib/Magpie/Plugin/Resource/Cache.pm
↑ Summary
blib/lib/Magpie/Transformer/ServiceUnavailable.pm »
| 1 | package Magpie::Resource::Proxy; | ||
| 2 | # ABSTRACT: . | ||
| 3 | $Magpie::Resource::Proxy::VERSION = '1.141660'; | ||
| 4 | use Moose; | ||
| 5 | extends 'Magpie::Resource'; | ||
| 6 | use Magpie::Constants; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use HTTP::Request; | ||
| 9 | use Plack::Response; | ||
| 10 | |||
| 11 | has user_agent => ( | ||
| 12 | is => 'ro', | ||
| 13 | isa => 'LWP::UserAgent', | ||
| 14 | default => sub { LWP::UserAgent->new }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | has [qw(scheme host path method port)] => ( | ||
| 18 | is => 'rw', | ||
| 19 | isa => 'Str', | ||
| 20 | lazy_build => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has url => ( | ||
| 24 | is => 'rw', | ||
| 25 | isa => 'Str', | ||
| 26 | lazy_build => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has headers => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'HTTP::Headers', | ||
| 32 | lazy_build => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has content => ( | ||
| 36 | is => 'rw', | ||
| 37 | lazy_build => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub _build_scheme { | ||
| 41 | shift->plack_request->scheme; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _build_host { | ||
| 45 | shift->plack_request->uri->host; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _build_path { | ||
| 49 | shift->plack_request->path; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _build_method { | ||
| 53 | shift->plack_request->method; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _build_content { | ||
| 57 | shift->plack_request->content; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _build_port { | ||
| 61 | shift->plack_request->port; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_headers { | ||
| 65 | shift->plack_request->headers; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _build_url { | ||
| 69 | my $self = shift; | ||
| 70 | $self->scheme . '://' . $self->host . ':' . $self->port . $self->path; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub proxy { | ||
| 75 | my $self = shift; | ||
| 76 | my $ctxt = shift; | ||
| 77 | my $url = $self->url; | ||
| 78 | my $request = HTTP::Request->new( $self->method, $self->url, $self->headers, $self->content ); | ||
| 79 | my $ua = $self->user_agent; | ||
| 80 | my $lwp_response = $ua->request($request); | ||
| 81 | my $resp = Plack::Response->new($lwp_response->code, $lwp_response->headers->clone, $lwp_response->content); | ||
| 82 | $self->data($resp); | ||
| 83 | return OK; | ||
| 84 | } | ||
| 85 | |||
| 86 | around [HTTP_METHODS] => sub { | ||
| 87 | my $orig = shift; | ||
| 88 | my $self = shift; | ||
| 89 | $self->parent_handler->resource($self); | ||
| 90 | return $self->proxy(@_); | ||
| 91 | }; | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | =pod | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | Magpie::Resource::Proxy - . | ||
| 102 | |||
| 103 | =head1 VERSION | ||
| 104 | |||
| 105 | version 1.141660 | ||
| 106 | |||
| 107 | # SEALSO: Magpie, Magpie::Resource | ||
| 108 | |||
| 109 | =head1 AUTHORS | ||
| 110 | |||
| 111 | =over 4 | ||
| 112 | |||
| 113 | =item * | ||
| 114 | |||
| 115 | Kip Hampton <kip.hampton@tamarou.com> | ||
| 116 | |||
| 117 | =item * | ||
| 118 | |||
| 119 | Chris Prather <chris.prather@tamarou.com> | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head1 COPYRIGHT AND LICENSE | ||
| 124 | |||
| 125 | This software is copyright (c) 2011 by Tamarou, LLC. | ||
| 126 | |||
| 127 | This is free software; you can redistribute it and/or modify it under | ||
| 128 | the same terms as the Perl 5 programming language system itself. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | |||
| 133 | __END__ |