~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

remove all trailing whitespace from bzr source

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
        for start, length in offsets:
110
110
            txt.append('%d,%d' % (start, length))
111
111
        return '\n'.join(txt)
112
 
        
 
112
 
113
113
 
114
114
class SmartServerRequestProtocolOne(SmartProtocolBase):
115
115
    """Server-side encoding and decoding logic for smart version 1."""
116
 
    
 
116
 
117
117
    def __init__(self, backing_transport, write_func, root_client_path='/'):
118
118
        self._backing_transport = backing_transport
119
119
        self._root_client_path = root_client_path
127
127
 
128
128
    def accept_bytes(self, bytes):
129
129
        """Take bytes, and advance the internal state machine appropriately.
130
 
        
 
130
 
131
131
        :param bytes: must be a byte string
132
132
        """
133
133
        if not isinstance(bytes, str):
169
169
 
170
170
        if self._has_dispatched:
171
171
            if self._finished:
172
 
                # nothing to do.XXX: this routine should be a single state 
 
172
                # nothing to do.XXX: this routine should be a single state
173
173
                # machine too.
174
174
                self.unused_data += self.in_buffer
175
175
                self.in_buffer = ''
211
211
 
212
212
    def _write_protocol_version(self):
213
213
        """Write any prefixes this protocol requires.
214
 
        
 
214
 
215
215
        Version one doesn't send protocol versions.
216
216
        """
217
217
 
234
234
 
235
235
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
236
236
    r"""Version two of the server side of the smart protocol.
237
 
   
 
237
 
238
238
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
239
239
    """
240
240
 
250
250
 
251
251
    def _write_protocol_version(self):
252
252
        r"""Write any prefixes this protocol requires.
253
 
        
 
253
 
254
254
        Version two sends the value of RESPONSE_VERSION_TWO.
255
255
        """
256
256
        self._write_func(self.response_marker)
412
412
        self.chunks = collections.deque()
413
413
        self.error = False
414
414
        self.error_in_progress = None
415
 
    
 
415
 
416
416
    def next_read_size(self):
417
417
        # Note: the shortest possible chunk is 2 bytes: '0\n', and the
418
418
        # end-of-body marker is 4 bytes: 'END\n'.
506
506
                self.chunks.append(self.chunk_in_progress)
507
507
            self.chunk_in_progress = None
508
508
            self.state_accept = self._state_accept_expecting_length
509
 
        
 
509
 
510
510
    def _state_accept_reading_unused(self):
511
511
        self.unused_data += self._get_in_buffer()
512
512
        self._in_buffer_list = []
514
514
 
515
515
class LengthPrefixedBodyDecoder(_StatefulDecoder):
516
516
    """Decodes the length-prefixed bulk data."""
517
 
    
 
517
 
518
518
    def __init__(self):
519
519
        _StatefulDecoder.__init__(self)
520
520
        self.state_accept = self._state_accept_expecting_length
521
521
        self.state_read = self._state_read_no_data
522
522
        self._body = ''
523
523
        self._trailer_buffer = ''
524
 
    
 
524
 
525
525
    def next_read_size(self):
526
526
        if self.bytes_left is not None:
527
527
            # Ideally we want to read all the remainder of the body and the
537
537
        else:
538
538
            # Reading excess data.  Either way, 1 byte at a time is fine.
539
539
            return 1
540
 
        
 
540
 
541
541
    def read_pending_data(self):
542
542
        """Return any pending data that has been decoded."""
543
543
        return self.state_read()
564
564
                self._body = self._body[:self.bytes_left]
565
565
            self.bytes_left = None
566
566
            self.state_accept = self._state_accept_reading_trailer
567
 
        
 
567
 
568
568
    def _state_accept_reading_trailer(self):
569
569
        self._trailer_buffer += self._get_in_buffer()
570
570
        self._set_in_buffer(None)
574
574
            self.unused_data = self._trailer_buffer[len('done\n'):]
575
575
            self.state_accept = self._state_accept_reading_unused
576
576
            self.finished_reading = True
577
 
    
 
577
 
578
578
    def _state_accept_reading_unused(self):
579
579
        self.unused_data += self._get_in_buffer()
580
580
        self._set_in_buffer(None)
721
721
    def _response_is_unknown_method(self, result_tuple):
722
722
        """Raise UnexpectedSmartServerResponse if the response is an 'unknonwn
723
723
        method' response to the request.
724
 
        
 
724
 
725
725
        :param response: The response from a smart client call_expecting_body
726
726
            call.
727
727
        :param verb: The verb used in that call.
734
734
            # The response will have no body, so we've finished reading.
735
735
            self._request.finished_reading()
736
736
            raise errors.UnknownSmartMethod(self._last_verb)
737
 
        
 
737
 
738
738
    def read_body_bytes(self, count=-1):
739
739
        """Read bytes from the body, decoding into a byte stream.
740
 
        
741
 
        We read all bytes at once to ensure we've checked the trailer for 
 
740
 
 
741
        We read all bytes at once to ensure we've checked the trailer for
742
742
        errors, and then feed the buffer back as read_body_bytes is called.
743
743
        """
744
744
        if self._body_buffer is not None:
782
782
 
783
783
    def _write_protocol_version(self):
784
784
        """Write any prefixes this protocol requires.
785
 
        
 
785
 
786
786
        Version one doesn't send protocol versions.
787
787
        """
788
788
 
789
789
 
790
790
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
791
791
    """Version two of the client side of the smart protocol.
792
 
    
 
792
 
793
793
    This prefixes the request with the value of REQUEST_VERSION_TWO.
794
794
    """
795
795
 
823
823
 
824
824
    def _write_protocol_version(self):
825
825
        """Write any prefixes this protocol requires.
826
 
        
 
826
 
827
827
        Version two sends the value of REQUEST_VERSION_TWO.
828
828
        """
829
829
        self._request.accept_bytes(self.request_marker)
977
977
            self.message_handler.headers_received(decoded)
978
978
        except:
979
979
            raise errors.SmartMessageHandlerError(sys.exc_info())
980
 
    
 
980
 
981
981
    def _state_accept_expecting_message_part(self):
982
982
        message_part_kind = self._extract_single_byte()
983
983
        if message_part_kind == 'o':
1069
1069
        for start, length in offsets:
1070
1070
            txt.append('%d,%d' % (start, length))
1071
1071
        return '\n'.join(txt)
1072
 
        
 
1072
 
1073
1073
    def _write_protocol_version(self):
1074
1074
        self._write_func(MESSAGE_VERSION_THREE)
1075
1075
 
1151
1151
                self._write_prefixed_body(chunk)
1152
1152
                self.flush()
1153
1153
        self._write_end()
1154
 
        
 
1154
 
1155
1155
 
1156
1156
class ProtocolThreeRequester(_ProtocolThreeEncoder, Requester):
1157
1157
 
1162
1162
 
1163
1163
    def set_headers(self, headers):
1164
1164
        self._headers = headers.copy()
1165
 
        
 
1165
 
1166
1166
    def call(self, *args):
1167
1167
        if 'hpss' in debug.debug_flags:
1168
1168
            mutter('hpss call:   %s', repr(args)[1:-1])