| 1 | package Arriba::Connection::SPDY; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use HTTP::Date; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use HTTP::Status qw(status_message); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use IO::Socket qw(:crlf); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Net::SPDY::Session; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Plack::Util; | ||
| 11 | |||
| 12 | use Arriba::Request; | ||
| 13 | |||
| 14 | use base 'Arriba::Connection'; | ||
| 15 | |||
| 16 | # The current version of Net::SPDY::Session has some debugging code in the | ||
| 17 | # process_frame method, which breaks things. Until that gets fixed, we'll use | ||
| 18 | # our own version of this method. | ||
| 19 | { | ||
| 20 | ## no critic | ||
| 21 | no strict 'refs'; | ||
| 22 | no warnings 'redefine'; | ||
| 23 | *{"Net::SPDY::Session::process_frame"} = sub { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | my %frame = $self->{framer}->read_frame (); | ||
| 27 | return () unless %frame; | ||
| 28 | |||
| 29 | if (not $frame{control}) { | ||
| 30 | #warn 'Not implemented: Data frame received'; | ||
| 31 | return %frame; | ||
| 32 | } | ||
| 33 | |||
| 34 | if ($frame{type} == Net::SPDY::Framer::SYN_STREAM) { | ||
| 35 | } elsif ($frame{type} == Net::SPDY::Framer::SETTINGS) { | ||
| 36 | $self->got_settings (%frame); | ||
| 37 | } elsif ($frame{type} == Net::SPDY::Framer::PING) { | ||
| 38 | $self->{framer}->write_ping ( | ||
| 39 | flags => 0, | ||
| 40 | id => $frame{id}, | ||
| 41 | ); | ||
| 42 | } elsif ($frame{type} == Net::SPDY::Framer::GOAWAY) { | ||
| 43 | $self->close (0); | ||
| 44 | } elsif ($frame{type} == Net::SPDY::Framer::HEADERS) { | ||
| 45 | # We should remember values gotten here for stream | ||
| 46 | #warn 'Not implemented: Got headers frame' | ||
| 47 | } elsif ($frame{type} == Net::SPDY::Framer::WINDOW_UPDATE) { | ||
| 48 | } else { | ||
| 49 | die 'Unknown frame type '.$frame{type}; | ||
| 50 | } | ||
| 51 | |||
| 52 | return %frame; | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my $class = shift; | ||
| 58 | my $self = $class->SUPER::new(@_); | ||
| 59 | |||
| 60 | $self->{_spdy} = { | ||
| 61 | session => Net::SPDY::Session->new($self->{client}), | ||
| 62 | streams => {}, | ||
| 63 | }; | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub read_request { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $session = $self->{_spdy}->{session}; | ||
| 72 | my $streams = $self->{_spdy}->{streams}; | ||
| 73 | |||
| 74 | my $req; | ||
| 75 | |||
| 76 | while (my %frame = $session->process_frame) { | ||
| 77 | if (exists $frame{type}) { | ||
| 78 | if ($frame{type} == Net::SPDY::Framer::SYN_STREAM) { | ||
| 79 | # Request initiated | ||
| 80 | $req = Arriba::Request->new($self); | ||
| 81 | $req->{_spdy} = { | ||
| 82 | # Keep track of this request's stream ID | ||
| 83 | stream_id => $frame{stream_id}, | ||
| 84 | # Save a reference to the frame that initiated this request | ||
| 85 | frame => \%frame, | ||
| 86 | }; | ||
| 87 | |||
| 88 | $streams->{$frame{stream_id}} = { req => $req }; | ||
| 89 | |||
| 90 | my %frame_headers = @{$frame{headers}}; | ||
| 91 | my @http_headers = @{$frame{headers}}; | ||
| 92 | |||
| 93 | my $headers = ''; | ||
| 94 | # Construct the HTTP request line | ||
| 95 | $headers .= $frame_headers{':method'} . ' ' . | ||
| 96 | $frame_headers{':path'} . ' ' . | ||
| 97 | $frame_headers{':version'} . $CRLF; | ||
| 98 | |||
| 99 | $req->{scheme} = $frame_headers{':scheme'}; | ||
| 100 | |||
| 101 | if ($frame_headers{':host'}) { | ||
| 102 | $headers .= 'host: ' . $frame_headers{':host'} . $CRLF; | ||
| 103 | } | ||
| 104 | |||
| 105 | for (my $i = 0; $i < $#http_headers; $i += 2) { | ||
| 106 | if ($http_headers[$i] !~ /^:/) { | ||
| 107 | $headers .= "$http_headers[$i]: $http_headers[$i+1]$CRLF"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | $headers .= $CRLF; | ||
| 112 | |||
| 113 | $req->{headers} = $headers; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | # FIXME: Other cases | ||
| 117 | next; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | # Data frame - check for existing stream ID | ||
| 121 | elsif (my $stream = $streams->{$frame{stream_id}}) { | ||
| 122 | # Grab the corresponding request | ||
| 123 | $req = $stream->{req}; | ||
| 124 | } | ||
| 125 | # Unknown stream ID | ||
| 126 | else { | ||
| 127 | # TODO: Error | ||
| 128 | next; | ||
| 129 | } | ||
| 130 | |||
| 131 | if (!$frame{control}) { | ||
| 132 | # TODO: Move the above check for existing stream ID in here? | ||
| 133 | # Data frame | ||
| 134 | if (!$req->{body_stream}) { | ||
| 135 | # TODO: Initialize with content length? | ||
| 136 | $req->{body_stream} = Stream::Buffered->new; | ||
| 137 | } | ||
| 138 | $req->{body_stream}->print($frame{data}); | ||
| 139 | } | ||
| 140 | |||
| 141 | if ($frame{flags} & Net::SPDY::Framer::FLAG_FIN) { | ||
| 142 | # Last frame on this stream | ||
| 143 | $req->{complete} = 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | return $req; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub write_response { | ||
| 151 | my $self = shift; | ||
| 152 | my $req = shift; | ||
| 153 | my $res = shift; | ||
| 154 | |||
| 155 | my $status = $res->[0]; | ||
| 156 | |||
| 157 | my %frame_headers = ( | ||
| 158 | ':version' => 'HTTP/1.1', | ||
| 159 | ':status' => "$status " . status_message($status), | ||
| 160 | 'date' => HTTP::Date::time2str(), | ||
| 161 | 'server' => 'arriba', # FIXME | ||
| 162 | ); | ||
| 163 | |||
| 164 | Plack::Util::header_iter($res->[1], sub { | ||
| 165 | my ($k, $v) = @_; | ||
| 166 | if (exists $frame_headers{lc $k}) { | ||
| 167 | if (ref $frame_headers{lc $k} ne 'ARRAY') { | ||
| 168 | $frame_headers{lc $k} = [ $frame_headers{lc $k} ]; | ||
| 169 | } | ||
| 170 | push @{$frame_headers{lc $k}}, $v; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | $frame_headers{lc $k} = $v; | ||
| 174 | } | ||
| 175 | }); | ||
| 176 | |||
| 177 | my %frame = ( | ||
| 178 | type => Net::SPDY::Framer::SYN_REPLY, | ||
| 179 | stream_id => $req->{_spdy}->{stream_id}, | ||
| 180 | headers => [ %frame_headers ], | ||
| 181 | control => 1, | ||
| 182 | flags => 0, | ||
| 183 | ); | ||
| 184 | |||
| 185 | my $framer = $self->{_spdy}->{session}->{framer}; | ||
| 186 | |||
| 187 | if (defined $res->[2]) { | ||
| 188 | Plack::Util::foreach($res->[2], sub { | ||
| 189 | # Send the previous frame | ||
| 190 | $framer->write_frame(%frame); | ||
| 191 | |||
| 192 | %frame = ( | ||
| 193 | stream_id => $req->{_spdy}->{stream_id}, | ||
| 194 | data => $_[0], | ||
| 195 | control => 0, | ||
| 196 | flags => 0, | ||
| 197 | ); | ||
| 198 | }); | ||
| 199 | |||
| 200 | $frame{flags} |= Net::SPDY::Framer::FLAG_FIN; | ||
| 201 | $framer->write_frame(%frame); | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | return Plack::Util::inline_object | ||
| 205 | write => sub { | ||
| 206 | # Send the previous frame | ||
| 207 | $framer->write_frame(%frame); | ||
| 208 | |||
| 209 | %frame = ( | ||
| 210 | stream_id => $req->{_spdy}->{stream_id}, | ||
| 211 | data => $_[0], | ||
| 212 | control => 0, | ||
| 213 | flags => 0, | ||
| 214 | ); | ||
| 215 | }, | ||
| 216 | close => sub { | ||
| 217 | $frame{flags} |= Net::SPDY::Framer::FLAG_FIN; | ||
| 218 | $framer->write_frame(%frame); | ||
| 219 | }; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | |||