~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-04-26 10:41:48 UTC
  • mfrom: (2420.1.22 bzr.http.auth)
  • Revision ID: pqm@pqm.ubuntu.com-20070426104148-4l5wq2zemlzv0shg
http authentication and other integrated fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
                    # trivial request
104
104
                    self.excess_buffer = self.in_buffer
105
105
                    self.in_buffer = ''
106
 
                    self._send_response(self.request.response.args,
107
 
                        self.request.response.body)
 
106
                    self._send_response(self.request.response)
108
107
            except KeyboardInterrupt:
109
108
                raise
110
109
            except Exception, exception:
111
110
                # everything else: pass to client, flush, and quit
112
 
                self._send_response(('error', str(exception)))
 
111
                self._send_response(request.FailedSmartServerResponse(
 
112
                    ('error', str(exception))))
113
113
                return
114
114
 
115
115
        if self.has_dispatched:
130
130
                assert self.request.finished_reading, \
131
131
                    "no more body, request not finished"
132
132
            if self.request.response is not None:
133
 
                self._send_response(self.request.response.args,
134
 
                    self.request.response.body)
 
133
                self._send_response(self.request.response)
135
134
                self.excess_buffer = self.in_buffer
136
135
                self.in_buffer = ''
137
136
            else:
138
137
                assert not self.request.finished_reading, \
139
138
                    "no response and we have finished reading."
140
139
 
141
 
    def _send_response(self, args, body=None):
 
140
    def _send_response(self, response):
142
141
        """Send a smart server response down the output stream."""
143
142
        assert not self._finished, 'response already sent'
 
143
        args = response.args
 
144
        body = response.body
144
145
        self._finished = True
145
146
        self._write_protocol_version()
 
147
        self._write_success_or_failure_prefix(response)
146
148
        self._write_func(_encode_tuple(args))
147
149
        if body is not None:
148
150
            assert isinstance(body, str), 'body must be a str'
155
157
        Version one doesn't send protocol versions.
156
158
        """
157
159
 
 
160
    def _write_success_or_failure_prefix(self, response):
 
161
        """Write the protocol specific success/failure prefix.
 
162
 
 
163
        For SmartServerRequestProtocolOne this is omitted but we
 
164
        call is_successful to ensure that the response is valid.
 
165
        """
 
166
        response.is_successful()
 
167
 
158
168
    def next_read_size(self):
159
169
        if self._finished:
160
170
            return 0
170
180
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
171
181
    """
172
182
 
 
183
    def _write_success_or_failure_prefix(self, response):
 
184
        """Write the protocol specific success/failure prefix."""
 
185
        if response.is_successful():
 
186
            self._write_func('success\n')
 
187
        else:
 
188
            self._write_func('failed\n')
 
189
 
173
190
    def _write_protocol_version(self):
174
191
        r"""Write any prefixes this protocol requires.
175
192
        
347
364
 
348
365
    def _recv_tuple(self):
349
366
        """Receive a tuple from the medium request."""
 
367
        return _decode_tuple(self._recv_line())
 
368
 
 
369
    def _recv_line(self):
 
370
        """Read an entire line from the medium request."""
350
371
        line = ''
351
372
        while not line or line[-1] != '\n':
352
373
            # TODO: this is inefficient - but tuples are short.
353
374
            new_char = self._request.read_bytes(1)
354
375
            line += new_char
355
376
            assert new_char != '', "end of file reading from server."
356
 
        return _decode_tuple(line)
 
377
        return line
357
378
 
358
379
    def query_version(self):
359
380
        """Return protocol version number of the server."""
392
413
        version = self._request.read_line()
393
414
        if version != RESPONSE_VERSION_TWO:
394
415
            raise errors.SmartProtocolError('bad protocol marker %r' % version)
 
416
        response_status = self._recv_line()
 
417
        if response_status not in ('success\n', 'failed\n'):
 
418
            raise errors.SmartProtocolError(
 
419
                'bad protocol status %r' % response_status)
 
420
        self.response_status = response_status == 'success\n'
395
421
        return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body)
396
422
 
397
423
    def _write_protocol_version(self):