• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import logging
16import messages_pb2
17import struct
18
19import h2
20import h2.connection
21import twisted
22import twisted.internet
23import twisted.internet.protocol
24
25_READ_CHUNK_SIZE = 16384
26_GRPC_HEADER_SIZE = 5
27_MIN_SETTINGS_MAX_FRAME_SIZE = 16384
28
29
30class H2ProtocolBaseServer(twisted.internet.protocol.Protocol):
31
32    def __init__(self):
33        self._conn = h2.connection.H2Connection(client_side=False)
34        self._recv_buffer = {}
35        self._handlers = {}
36        self._handlers['ConnectionMade'] = self.on_connection_made_default
37        self._handlers['DataReceived'] = self.on_data_received_default
38        self._handlers['WindowUpdated'] = self.on_window_update_default
39        self._handlers['RequestReceived'] = self.on_request_received_default
40        self._handlers['SendDone'] = self.on_send_done_default
41        self._handlers['ConnectionLost'] = self.on_connection_lost
42        self._handlers['PingAcknowledged'] = self.on_ping_acknowledged_default
43        self._stream_status = {}
44        self._send_remaining = {}
45        self._outstanding_pings = 0
46
47    def set_handlers(self, handlers):
48        self._handlers = handlers
49
50    def connectionMade(self):
51        self._handlers['ConnectionMade']()
52
53    def connectionLost(self, reason):
54        self._handlers['ConnectionLost'](reason)
55
56    def on_connection_made_default(self):
57        logging.info('Connection Made')
58        self._conn.initiate_connection()
59        self.transport.setTcpNoDelay(True)
60        self.transport.write(self._conn.data_to_send())
61
62    def on_connection_lost(self, reason):
63        logging.info('Disconnected %s' % reason)
64
65    def dataReceived(self, data):
66        try:
67            events = self._conn.receive_data(data)
68        except h2.exceptions.ProtocolError:
69            # this try/except block catches exceptions due to race between sending
70            # GOAWAY and processing a response in flight.
71            return
72        if self._conn.data_to_send:
73            self.transport.write(self._conn.data_to_send())
74        for event in events:
75            if isinstance(event, h2.events.RequestReceived
76                         ) and self._handlers.has_key('RequestReceived'):
77                logging.info('RequestReceived Event for stream: %d' %
78                             event.stream_id)
79                self._handlers['RequestReceived'](event)
80            elif isinstance(event, h2.events.DataReceived
81                           ) and self._handlers.has_key('DataReceived'):
82                logging.info('DataReceived Event for stream: %d' %
83                             event.stream_id)
84                self._handlers['DataReceived'](event)
85            elif isinstance(event, h2.events.WindowUpdated
86                           ) and self._handlers.has_key('WindowUpdated'):
87                logging.info('WindowUpdated Event for stream: %d' %
88                             event.stream_id)
89                self._handlers['WindowUpdated'](event)
90            elif isinstance(event, h2.events.PingAcknowledged
91                           ) and self._handlers.has_key('PingAcknowledged'):
92                logging.info('PingAcknowledged Event')
93                self._handlers['PingAcknowledged'](event)
94        self.transport.write(self._conn.data_to_send())
95
96    def on_ping_acknowledged_default(self, event):
97        logging.info('ping acknowledged')
98        self._outstanding_pings -= 1
99
100    def on_data_received_default(self, event):
101        self._conn.acknowledge_received_data(len(event.data), event.stream_id)
102        self._recv_buffer[event.stream_id] += event.data
103
104    def on_request_received_default(self, event):
105        self._recv_buffer[event.stream_id] = ''
106        self._stream_id = event.stream_id
107        self._stream_status[event.stream_id] = True
108        self._conn.send_headers(
109            stream_id=event.stream_id,
110            headers=[
111                (':status', '200'),
112                ('content-type', 'application/grpc'),
113                ('grpc-encoding', 'identity'),
114                ('grpc-accept-encoding', 'identity,deflate,gzip'),
115            ],
116        )
117        self.transport.write(self._conn.data_to_send())
118
119    def on_window_update_default(self,
120                                 _,
121                                 pad_length=None,
122                                 read_chunk_size=_READ_CHUNK_SIZE):
123        # try to resume sending on all active streams (update might be for connection)
124        for stream_id in self._send_remaining:
125            self.default_send(stream_id,
126                              pad_length=pad_length,
127                              read_chunk_size=read_chunk_size)
128
129    def send_reset_stream(self):
130        self._conn.reset_stream(self._stream_id)
131        self.transport.write(self._conn.data_to_send())
132
133    def setup_send(self,
134                   data_to_send,
135                   stream_id,
136                   pad_length=None,
137                   read_chunk_size=_READ_CHUNK_SIZE):
138        logging.info('Setting up data to send for stream_id: %d' % stream_id)
139        self._send_remaining[stream_id] = len(data_to_send)
140        self._send_offset = 0
141        self._data_to_send = data_to_send
142        self.default_send(stream_id,
143                          pad_length=pad_length,
144                          read_chunk_size=read_chunk_size)
145
146    def default_send(self,
147                     stream_id,
148                     pad_length=None,
149                     read_chunk_size=_READ_CHUNK_SIZE):
150        if not self._send_remaining.has_key(stream_id):
151            # not setup to send data yet
152            return
153
154        while self._send_remaining[stream_id] > 0:
155            lfcw = self._conn.local_flow_control_window(stream_id)
156            padding_bytes = pad_length + 1 if pad_length is not None else 0
157            if lfcw - padding_bytes <= 0:
158                logging.info(
159                    'Stream %d. lfcw: %d. padding bytes: %d. not enough quota yet'
160                    % (stream_id, lfcw, padding_bytes))
161                break
162            chunk_size = min(lfcw - padding_bytes, read_chunk_size)
163            bytes_to_send = min(chunk_size, self._send_remaining[stream_id])
164            logging.info(
165                'flow_control_window = %d. sending [%d:%d] stream_id %d. includes %d total padding bytes'
166                % (lfcw, self._send_offset, self._send_offset + bytes_to_send +
167                   padding_bytes, stream_id, padding_bytes))
168            # The receiver might allow sending frames larger than the http2 minimum
169            # max frame size (16384), but this test should never send more than 16384
170            # for simplicity (which is always legal).
171            if bytes_to_send + padding_bytes > _MIN_SETTINGS_MAX_FRAME_SIZE:
172                raise ValueError("overload: sending %d" %
173                                 (bytes_to_send + padding_bytes))
174            data = self._data_to_send[self._send_offset:self._send_offset +
175                                      bytes_to_send]
176            try:
177                self._conn.send_data(stream_id,
178                                     data,
179                                     end_stream=False,
180                                     pad_length=pad_length)
181            except h2.exceptions.ProtocolError:
182                logging.info('Stream %d is closed' % stream_id)
183                break
184            self._send_remaining[stream_id] -= bytes_to_send
185            self._send_offset += bytes_to_send
186            if self._send_remaining[stream_id] == 0:
187                self._handlers['SendDone'](stream_id)
188
189    def default_ping(self):
190        logging.info('sending ping')
191        self._outstanding_pings += 1
192        self._conn.ping(b'\x00' * 8)
193        self.transport.write(self._conn.data_to_send())
194
195    def on_send_done_default(self, stream_id):
196        if self._stream_status[stream_id]:
197            self._stream_status[stream_id] = False
198            self.default_send_trailer(stream_id)
199        else:
200            logging.error('Stream %d is already closed' % stream_id)
201
202    def default_send_trailer(self, stream_id):
203        logging.info('Sending trailer for stream id %d' % stream_id)
204        self._conn.send_headers(stream_id,
205                                headers=[('grpc-status', '0')],
206                                end_stream=True)
207        self.transport.write(self._conn.data_to_send())
208
209    @staticmethod
210    def default_response_data(response_size):
211        sresp = messages_pb2.SimpleResponse()
212        sresp.payload.body = b'\x00' * response_size
213        serialized_resp_proto = sresp.SerializeToString()
214        response_data = b'\x00' + struct.pack(
215            'i', len(serialized_resp_proto))[::-1] + serialized_resp_proto
216        return response_data
217
218    def parse_received_data(self, stream_id):
219        """ returns a grpc framed string of bytes containing response proto of the size
220    asked in request """
221        recv_buffer = self._recv_buffer[stream_id]
222        grpc_msg_size = struct.unpack('i', recv_buffer[1:5][::-1])[0]
223        if len(recv_buffer) != _GRPC_HEADER_SIZE + grpc_msg_size:
224            return None
225        req_proto_str = recv_buffer[5:5 + grpc_msg_size]
226        sr = messages_pb2.SimpleRequest()
227        sr.ParseFromString(req_proto_str)
228        logging.info('Parsed simple request for stream %d' % stream_id)
229        return sr
230