ncteisen | 65a45ae | 2016-12-15 14:35:17 -0800 | [diff] [blame] | 1 | # Copyright 2016, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 30 | import logging |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 31 | import messages_pb2 |
| 32 | import struct |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 33 | |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 34 | import h2 |
| 35 | import h2.connection |
| 36 | import twisted |
| 37 | import twisted.internet |
| 38 | import twisted.internet.protocol |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 39 | |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 40 | _READ_CHUNK_SIZE = 16384 |
| 41 | _GRPC_HEADER_SIZE = 5 |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 42 | _MIN_SETTINGS_MAX_FRAME_SIZE = 16384 |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 43 | |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 44 | class H2ProtocolBaseServer(twisted.internet.protocol.Protocol): |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 45 | def __init__(self): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 46 | self._conn = h2.connection.H2Connection(client_side=False) |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 47 | self._recv_buffer = {} |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 48 | self._handlers = {} |
Makarand Dharmapurikar | 28d1980 | 2016-12-01 10:56:52 -0800 | [diff] [blame] | 49 | self._handlers['ConnectionMade'] = self.on_connection_made_default |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 50 | self._handlers['DataReceived'] = self.on_data_received_default |
| 51 | self._handlers['WindowUpdated'] = self.on_window_update_default |
| 52 | self._handlers['RequestReceived'] = self.on_request_received_default |
| 53 | self._handlers['SendDone'] = self.on_send_done_default |
| 54 | self._handlers['ConnectionLost'] = self.on_connection_lost |
| 55 | self._handlers['PingAcknowledged'] = self.on_ping_acknowledged_default |
| 56 | self._stream_status = {} |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 57 | self._send_remaining = {} |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 58 | self._outstanding_pings = 0 |
| 59 | |
| 60 | def set_handlers(self, handlers): |
| 61 | self._handlers = handlers |
| 62 | |
| 63 | def connectionMade(self): |
Makarand Dharmapurikar | 28d1980 | 2016-12-01 10:56:52 -0800 | [diff] [blame] | 64 | self._handlers['ConnectionMade']() |
| 65 | |
| 66 | def connectionLost(self, reason): |
| 67 | self._handlers['ConnectionLost'](reason) |
| 68 | |
| 69 | def on_connection_made_default(self): |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 70 | logging.info('Connection Made') |
| 71 | self._conn.initiate_connection() |
| 72 | self.transport.setTcpNoDelay(True) |
| 73 | self.transport.write(self._conn.data_to_send()) |
| 74 | |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 75 | def on_connection_lost(self, reason): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 76 | logging.info('Disconnected %s' % reason) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 77 | |
| 78 | def dataReceived(self, data): |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 79 | try: |
| 80 | events = self._conn.receive_data(data) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 81 | except h2.exceptions.ProtocolError: |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 82 | # this try/except block catches exceptions due to race between sending |
| 83 | # GOAWAY and processing a response in flight. |
| 84 | return |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 85 | if self._conn.data_to_send: |
| 86 | self.transport.write(self._conn.data_to_send()) |
| 87 | for event in events: |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 88 | if isinstance(event, h2.events.RequestReceived) and self._handlers.has_key('RequestReceived'): |
| 89 | logging.info('RequestReceived Event for stream: %d' % event.stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 90 | self._handlers['RequestReceived'](event) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 91 | elif isinstance(event, h2.events.DataReceived) and self._handlers.has_key('DataReceived'): |
| 92 | logging.info('DataReceived Event for stream: %d' % event.stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 93 | self._handlers['DataReceived'](event) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 94 | elif isinstance(event, h2.events.WindowUpdated) and self._handlers.has_key('WindowUpdated'): |
| 95 | logging.info('WindowUpdated Event for stream: %d' % event.stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 96 | self._handlers['WindowUpdated'](event) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 97 | elif isinstance(event, h2.events.PingAcknowledged) and self._handlers.has_key('PingAcknowledged'): |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 98 | logging.info('PingAcknowledged Event') |
| 99 | self._handlers['PingAcknowledged'](event) |
| 100 | self.transport.write(self._conn.data_to_send()) |
| 101 | |
| 102 | def on_ping_acknowledged_default(self, event): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 103 | logging.info('ping acknowledged') |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 104 | self._outstanding_pings -= 1 |
| 105 | |
| 106 | def on_data_received_default(self, event): |
| 107 | self._conn.acknowledge_received_data(len(event.data), event.stream_id) |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 108 | self._recv_buffer[event.stream_id] += event.data |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 109 | |
| 110 | def on_request_received_default(self, event): |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 111 | self._recv_buffer[event.stream_id] = '' |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 112 | self._stream_id = event.stream_id |
| 113 | self._stream_status[event.stream_id] = True |
| 114 | self._conn.send_headers( |
| 115 | stream_id=event.stream_id, |
| 116 | headers=[ |
| 117 | (':status', '200'), |
| 118 | ('content-type', 'application/grpc'), |
| 119 | ('grpc-encoding', 'identity'), |
| 120 | ('grpc-accept-encoding', 'identity,deflate,gzip'), |
| 121 | ], |
| 122 | ) |
| 123 | self.transport.write(self._conn.data_to_send()) |
| 124 | |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 125 | def on_window_update_default(self, _, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE): |
| 126 | # try to resume sending on all active streams (update might be for connection) |
| 127 | for stream_id in self._send_remaining: |
| 128 | self.default_send(stream_id, pad_length=pad_length, read_chunk_size=read_chunk_size) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 129 | |
| 130 | def send_reset_stream(self): |
| 131 | self._conn.reset_stream(self._stream_id) |
| 132 | self.transport.write(self._conn.data_to_send()) |
| 133 | |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 134 | def setup_send(self, data_to_send, stream_id, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 135 | logging.info('Setting up data to send for stream_id: %d' % stream_id) |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 136 | self._send_remaining[stream_id] = len(data_to_send) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 137 | self._send_offset = 0 |
| 138 | self._data_to_send = data_to_send |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 139 | self.default_send(stream_id, pad_length=pad_length, read_chunk_size=read_chunk_size) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 140 | |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 141 | def default_send(self, stream_id, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE): |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 142 | if not self._send_remaining.has_key(stream_id): |
| 143 | # not setup to send data yet |
| 144 | return |
| 145 | |
| 146 | while self._send_remaining[stream_id] > 0: |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 147 | lfcw = self._conn.local_flow_control_window(stream_id) |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 148 | padding_bytes = pad_length + 1 if pad_length is not None else 0 |
| 149 | if lfcw - padding_bytes <= 0: |
| 150 | logging.info('Stream %d. lfcw: %d. padding bytes: %d. not enough quota yet' % (stream_id, lfcw, padding_bytes)) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 151 | break |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 152 | chunk_size = min(lfcw - padding_bytes, read_chunk_size) |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 153 | bytes_to_send = min(chunk_size, self._send_remaining[stream_id]) |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 154 | logging.info('flow_control_window = %d. sending [%d:%d] stream_id %d. includes %d total padding bytes' % |
| 155 | (lfcw, self._send_offset, self._send_offset + bytes_to_send + padding_bytes, |
| 156 | stream_id, padding_bytes)) |
| 157 | # The receiver might allow sending frames larger than the http2 minimum |
| 158 | # max frame size (16384), but this test should never send more than 16384 |
| 159 | # for simplicity (which is always legal). |
| 160 | if bytes_to_send + padding_bytes > _MIN_SETTINGS_MAX_FRAME_SIZE: |
| 161 | raise ValueError("overload: sending %d" % (bytes_to_send + padding_bytes)) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 162 | data = self._data_to_send[self._send_offset : self._send_offset + bytes_to_send] |
Makarand Dharmapurikar | ecc3288 | 2016-12-01 15:22:50 -0800 | [diff] [blame] | 163 | try: |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 164 | self._conn.send_data(stream_id, data, end_stream=False, pad_length=pad_length) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 165 | except h2.exceptions.ProtocolError: |
| 166 | logging.info('Stream %d is closed' % stream_id) |
Makarand Dharmapurikar | ecc3288 | 2016-12-01 15:22:50 -0800 | [diff] [blame] | 167 | break |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 168 | self._send_remaining[stream_id] -= bytes_to_send |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 169 | self._send_offset += bytes_to_send |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 170 | if self._send_remaining[stream_id] == 0: |
| 171 | self._handlers['SendDone'](stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 172 | |
| 173 | def default_ping(self): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 174 | logging.info('sending ping') |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 175 | self._outstanding_pings += 1 |
| 176 | self._conn.ping(b'\x00'*8) |
| 177 | self.transport.write(self._conn.data_to_send()) |
| 178 | |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 179 | def on_send_done_default(self, stream_id): |
| 180 | if self._stream_status[stream_id]: |
| 181 | self._stream_status[stream_id] = False |
| 182 | self.default_send_trailer(stream_id) |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 183 | else: |
| 184 | logging.error('Stream %d is already closed' % stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 185 | |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 186 | def default_send_trailer(self, stream_id): |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 187 | logging.info('Sending trailer for stream id %d' % stream_id) |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 188 | self._conn.send_headers(stream_id, |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 189 | headers=[ ('grpc-status', '0') ], |
| 190 | end_stream=True |
| 191 | ) |
| 192 | self.transport.write(self._conn.data_to_send()) |
| 193 | |
| 194 | @staticmethod |
| 195 | def default_response_data(response_size): |
| 196 | sresp = messages_pb2.SimpleResponse() |
| 197 | sresp.payload.body = b'\x00'*response_size |
| 198 | serialized_resp_proto = sresp.SerializeToString() |
| 199 | response_data = b'\x00' + struct.pack('i', len(serialized_resp_proto))[::-1] + serialized_resp_proto |
| 200 | return response_data |
| 201 | |
Makarand Dharmapurikar | 4350e74 | 2016-12-01 14:24:22 -0800 | [diff] [blame] | 202 | def parse_received_data(self, stream_id): |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 203 | """ returns a grpc framed string of bytes containing response proto of the size |
| 204 | asked in request """ |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 205 | recv_buffer = self._recv_buffer[stream_id] |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 206 | grpc_msg_size = struct.unpack('i',recv_buffer[1:5][::-1])[0] |
Makarand Dharmapurikar | 8c57917 | 2016-12-12 13:58:15 -0800 | [diff] [blame] | 207 | if len(recv_buffer) != _GRPC_HEADER_SIZE + grpc_msg_size: |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 208 | return None |
| 209 | req_proto_str = recv_buffer[5:5+grpc_msg_size] |
| 210 | sr = messages_pb2.SimpleRequest() |
| 211 | sr.ParseFromString(req_proto_str) |
Alexander Polcyn | 50fdc8a | 2017-02-09 21:06:08 -0800 | [diff] [blame] | 212 | logging.info('Parsed simple request for stream %d' % stream_id) |
Makarand Dharmapurikar | af37b80 | 2016-11-29 12:28:51 -0800 | [diff] [blame] | 213 | return sr |