• 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 http2_base_server
16import logging
17import messages_pb2
18
19# Set the number of padding bytes per data frame to be very large
20# relative to the number of data bytes for each data frame sent.
21_LARGE_PADDING_LENGTH = 255
22_SMALL_READ_CHUNK_SIZE = 5
23
24
25class TestDataFramePadding(object):
26    """
27    In response to an incoming request, this test sends headers, followed by
28    data, followed by a reset stream frame. Client asserts that the RPC failed.
29    Client needs to deliver the complete message to the application layer.
30  """
31
32    def __init__(self, use_padding=True):
33        self._base_server = http2_base_server.H2ProtocolBaseServer()
34        self._base_server._handlers['DataReceived'] = self.on_data_received
35        self._base_server._handlers['WindowUpdated'] = self.on_window_update
36        self._base_server._handlers[
37            'RequestReceived'] = self.on_request_received
38
39        # _total_updates maps stream ids to total flow control updates received
40        self._total_updates = {}
41        # zero window updates so far for connection window (stream id '0')
42        self._total_updates[0] = 0
43        self._read_chunk_size = _SMALL_READ_CHUNK_SIZE
44
45        if use_padding:
46            self._pad_length = _LARGE_PADDING_LENGTH
47        else:
48            self._pad_length = None
49
50    def get_base_server(self):
51        return self._base_server
52
53    def on_data_received(self, event):
54        logging.info('on data received. Stream id: %d. Data length: %d' %
55                     (event.stream_id, len(event.data)))
56        self._base_server.on_data_received_default(event)
57        if len(event.data) == 0:
58            return
59        sr = self._base_server.parse_received_data(event.stream_id)
60        stream_bytes = ''
61        # Check if full grpc msg has been read into the recv buffer yet
62        if sr:
63            response_data = self._base_server.default_response_data(
64                sr.response_size)
65            logging.info('Stream id: %d. total resp size: %d' %
66                         (event.stream_id, len(response_data)))
67            # Begin sending the response. Add ``self._pad_length`` padding to each
68            # data frame and split the whole message into data frames each carrying
69            # only self._read_chunk_size of data.
70            # The purpose is to have the majority of the data frame response bytes
71            # be padding bytes, since ``self._pad_length`` >> ``self._read_chunk_size``.
72            self._base_server.setup_send(response_data,
73                                         event.stream_id,
74                                         pad_length=self._pad_length,
75                                         read_chunk_size=self._read_chunk_size)
76
77    def on_request_received(self, event):
78        self._base_server.on_request_received_default(event)
79        logging.info('on request received. Stream id: %s.' % event.stream_id)
80        self._total_updates[event.stream_id] = 0
81
82    # Log debug info and try to resume sending on all currently active streams.
83    def on_window_update(self, event):
84        logging.info('on window update. Stream id: %s. Delta: %s' %
85                     (event.stream_id, event.delta))
86        self._total_updates[event.stream_id] += event.delta
87        total = self._total_updates[event.stream_id]
88        logging.info('... - total updates for stream %d : %d' %
89                     (event.stream_id, total))
90        self._base_server.on_window_update_default(
91            event,
92            pad_length=self._pad_length,
93            read_chunk_size=self._read_chunk_size)
94