]>
Dogcows Code - chaz/p5-Plack-App-Proxy-WebSocket/blob - lib/Plack/App/Proxy/WebSocket.pm
1 package Plack
::App
::Proxy
::WebSocket
;
2 # ABSTRACT: proxy HTTP and WebSocket connections
4 use warnings FATAL
=> 'all';
14 use parent
'Plack::App::Proxy';
18 use Plack::App::Proxy::WebSocket;
22 mount "/socket.io" => Plack::App::Proxy::WebSocket->new(
23 remote => "http://localhost:9000/socket.io",
24 preserve_host_header => 1,
30 This is a subclass of L<Plack::App::Proxy> that adds support for proxying
31 WebSocket connections. It has no extra dependencies or configuration options
32 beyond what L<Plack::App::Proxy> requires or provides, so it may be an easy
33 drop-in replacement. Read the documentation of that module for advanced usage
34 not covered by the L<SYNOPSIS>.
36 This subclass necessarily requires extra L<PSGI> server features in order to
37 work. The server must support C<psgi.streaming> and C<psgix.io>. It is also
38 highly recommended to choose a C<psgi.nonblocking> server, though that isn't
39 strictly required; performance may suffer greatly without it. L<Twiggy> is an
40 excellent choice for this application.
42 This module is B<EXPERIMENTAL>. I use it in development and it works
43 swimmingly for me, but it is completely untested in production scenarios.
47 Some servers (e.g. L<Starman>) ignore the C<Connection> HTTP response header
48 and use their own values, but WebSocket clients expect the value of that
49 header to be C<Upgrade>. This module cannot work on such servers. Your best
50 bet is to use a non-blocking server like L<Twiggy> that doesn't mess with the
56 my ($self, $env) = @_;
57 my $req = Plack
::Request-
>new($env);
59 # detect the websocket handshake or just proxy as usual
60 lc($req->header('Upgrade') || "") eq 'websocket' or return $self->SUPER::call
($env);
62 $env->{'psgi.streaming'} or die "Plack server support for psgi.streaming is required";
63 my $client_fh = $env->{'psgix.io'} or die "Plack server support for the psgix.io extension is required";
65 my $url = $self->build_url_from_env($env) or return [502, [], ["Bad Gateway"]];
66 my $uri = URI-
>new($url);
71 # set up an event loop if the server is blocking
73 unless ($env->{'psgi.nonblocking'}) {
74 $env->{'psgi.errors'}->print("Plack server support for psgi.nonblocking is highly recommended.\n");
78 tcp_connect
$uri->host, $uri->port, sub {
79 my $server_fh = shift;
81 # return 502 if connection to server fails
83 $res->([502, [], ["Bad Gateway"]]);
88 my $client = AnyEvent
::Handle-
>new(fh
=> $client_fh);
89 my $server = AnyEvent
::Handle-
>new(fh
=> $server_fh);
91 # forward request from the client, modifying the host and origin
92 my $headers = $req->headers->clone;
93 my $host = $uri->host_port;
94 $headers->header(Host
=> $host, Origin
=> "http://$host");
95 my $hs = HTTP
::Request-
>new('GET', $uri->path, $headers);
96 $hs->protocol($req->protocol);
97 $server->push_write($hs->as_string);
102 # buffer the exchange between the client and server
103 $client->on_read(sub {
105 my $buf = delete $hdl->{rbuf
};
106 $server->push_write($buf);
108 $server->on_read(sub {
110 my $buf = delete $hdl->{rbuf
};
113 $writer->write($buf);
117 if (($buffer .= $buf) =~ s/^(.+\r?\n\r?\n)//s) {
118 my $http = HTTP
::Response-
>parse($1);
120 $http->headers->remove_header('Status');
121 $http->headers->scan(sub { push @headers, @_ });
122 $writer = $res->([$http->code, [@headers]]);
123 $writer->write($buffer) if $buffer;
128 # shut down the sockets and exit the loop if an error occurs
129 $client->on_error(sub {
131 $server->push_shutdown;
133 $writer->close if $writer;
135 $server->on_error(sub {
137 # get the client handle's attention
This page took 0.043438 seconds and 5 git commands to generate.