| 1 | package PAGI::App::Proxy; | ||
| 2 | $PAGI::App::Proxy::VERSION = '0.002002'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Future::AsyncAwait; | ||
| 6 | use IO::Socket::INET; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | PAGI::App::Proxy - HTTP reverse proxy (DEMO ONLY - NOT FOR PRODUCTION) | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use PAGI::App::Proxy; | ||
| 15 | |||
| 16 | my $app = PAGI::App::Proxy->new( | ||
| 17 | backend => 'http://localhost:8080', | ||
| 18 | )->to_app; | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ($class, %args) = @_; | ||
| 24 | |||
| 25 | my $backend = $args{backend} // 'http://localhost:8080'; | ||
| 26 | my ($host, $port) = $backend =~ m{://([^:/]+)(?::(\d+))?}; | ||
| 27 | $port //= 80; | ||
| 28 | |||
| 29 | return bless { | ||
| 30 | host => $host, | ||
| 31 | port => $port, | ||
| 32 | timeout => $args{timeout} // 30, | ||
| 33 | headers => $args{headers} // {}, | ||
| 34 | }, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub to_app { | ||
| 38 | my ($self) = @_; | ||
| 39 | |||
| 40 | my $host = $self->{host}; | ||
| 41 | my $port = $self->{port}; | ||
| 42 | my $timeout = $self->{timeout}; | ||
| 43 | my $extra_headers = $self->{headers}; | ||
| 44 | |||
| 45 | return async sub { | ||
| 46 | my ($scope, $receive, $send) = @_; | ||
| 47 | die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http'; | ||
| 48 | |||
| 49 | # Build request | ||
| 50 | my $method = $scope->{method}; | ||
| 51 | my $path = $scope->{path}; | ||
| 52 | $path .= "?$scope->{query_string}" if $scope->{query_string}; | ||
| 53 | |||
| 54 | # Collect body | ||
| 55 | my $body = ''; | ||
| 56 | while (1) { | ||
| 57 | my $event = await $receive->(); | ||
| 58 | last if $event->{type} ne 'http.request'; | ||
| 59 | $body .= $event->{body} // ''; | ||
| 60 | last unless $event->{more}; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Build headers | ||
| 64 | my @headers; | ||
| 65 | for my $h (@{$scope->{headers} // []}) { | ||
| 66 | next if lc($h->[0]) eq 'host'; # Replace host | ||
| 67 | push @headers, "$h->[0]: $h->[1]"; | ||
| 68 | } | ||
| 69 | push @headers, "Host: $host:$port"; | ||
| 70 | |||
| 71 | # Add X-Forwarded headers | ||
| 72 | push @headers, "X-Forwarded-For: $scope->{client}[0]" if $scope->{client}; | ||
| 73 | push @headers, "X-Forwarded-Proto: $scope->{scheme}" if $scope->{scheme}; | ||
| 74 | |||
| 75 | # Add extra headers | ||
| 76 | for my $name (keys %$extra_headers) { | ||
| 77 | push @headers, "$name: $extra_headers->{$name}"; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (length $body) { | ||
| 81 | push @headers, "Content-Length: " . length($body); | ||
| 82 | } | ||
| 83 | |||
| 84 | my $request = "$method $path HTTP/1.1\r\n" . join("\r\n", @headers) . "\r\n\r\n" . $body; | ||
| 85 | |||
| 86 | # Connect to backend | ||
| 87 | my $sock = IO::Socket::INET->new( | ||
| 88 | PeerHost => $host, | ||
| 89 | PeerPort => $port, | ||
| 90 | Timeout => $timeout, | ||
| 91 | ); | ||
| 92 | |||
| 93 | unless ($sock) { | ||
| 94 | await $send->({ | ||
| 95 | type => 'http.response.start', | ||
| 96 | status => 502, | ||
| 97 | headers => [['content-type', 'text/plain']], | ||
| 98 | }); | ||
| 99 | await $send->({ type => 'http.response.body', body => 'Bad Gateway', more => 0 }); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | print $sock $request; | ||
| 104 | |||
| 105 | # Read response | ||
| 106 | my $response = ''; | ||
| 107 | while (my $chunk = <$sock>) { | ||
| 108 | $response .= $chunk; | ||
| 109 | } | ||
| 110 | close $sock; | ||
| 111 | |||
| 112 | # Parse response (simple parsing) | ||
| 113 | my ($status_line, $rest) = split /\r?\n/, $response, 2; | ||
| 114 | my ($proto, $status, $reason) = split / /, $status_line, 3; | ||
| 115 | |||
| 116 | my ($header_block, $resp_body) = split /\r?\n\r?\n/, $rest, 2; | ||
| 117 | my @resp_headers; | ||
| 118 | for my $line (split /\r?\n/, $header_block // '') { | ||
| 119 | my ($name, $value) = split /:\s*/, $line, 2; | ||
| 120 | next unless $name; | ||
| 121 | push @resp_headers, [lc($name), $value]; | ||
| 122 | } | ||
| 123 | |||
| 124 | await $send->({ | ||
| 125 | type => 'http.response.start', | ||
| 126 | status => $status, | ||
| 127 | headers => \@resp_headers, | ||
| 128 | }); | ||
| 129 | await $send->({ type => 'http.response.body', body => $resp_body // '', more => 0 }); | ||
| 130 | }; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ |