~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: John Arbash Meinel
  • Date: 2008-05-28 21:53:46 UTC
  • mfrom: (3453 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3458.
  • Revision ID: john@arbash-meinel.com-20080528215346-l8plys9h9ps624pn
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Basic server-side logic for dealing with requests."""
18
 
 
 
17
"""Basic server-side logic for dealing with requests.
 
18
 
 
19
**XXX**:
 
20
 
 
21
The class names are a little confusing: the protocol will instantiate a
 
22
SmartServerRequestHandler, whose dispatch_command method creates an instance of
 
23
a SmartServerRequest subclass.
 
24
 
 
25
The request_handlers registry tracks SmartServerRequest classes (rather than
 
26
SmartServerRequestHandler).
 
27
"""
19
28
 
20
29
import tempfile
21
30
 
27
36
    urlutils,
28
37
    )
29
38
from bzrlib.bundle.serializer import write_bundle
30
 
from bzrlib.transport import get_transport
31
 
from bzrlib.transport.chroot import ChrootServer
32
39
 
33
40
 
34
41
class SmartServerRequest(object):
39
46
    care to call `translate_client_path` and `transport_from_client_path` as
40
47
    appropriate when dealing with paths received from the client.
41
48
    """
 
49
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
 
50
    # *handler* is a different concept to the request.
42
51
 
43
52
    def __init__(self, backing_transport, root_client_path='/'):
44
53
        """Constructor.
91
100
        
92
101
        Must return a SmartServerResponse.
93
102
        """
94
 
        # TODO: if a client erroneously sends a request that shouldn't have a
95
 
        # body, what to do?  Probably SmartServerRequestHandler should catch
96
 
        # this NotImplementedError and translate it into a 'bad request' error
97
 
        # to send to the client.
98
103
        raise NotImplementedError(self.do_body)
 
104
 
 
105
    def do_chunk(self, chunk_bytes):
 
106
        """Called with each body chunk if the request has a streamed body.
 
107
 
 
108
        The do() method is still called, and must have returned None.
 
109
        """
 
110
        raise NotImplementedError(self.do_chunk)
 
111
 
 
112
    def do_end(self):
 
113
        """Called when the end of the request has been received."""
 
114
        pass
99
115
    
100
116
    def translate_client_path(self, client_path):
101
117
        """Translate a path received from a network client into a local
238
254
        try:
239
255
            command = self._commands.get(cmd)
240
256
        except LookupError:
241
 
            raise errors.SmartProtocolError("bad request %r" % (cmd,))
 
257
            raise errors.UnknownSmartMethod(cmd)
242
258
        self._command = command(self._backing_transport, self._root_client_path)
243
259
        self._run_handler_code(self._command.execute, args, {})
244
260
 
291
307
            else:
292
308
                raise
293
309
 
 
310
    def headers_received(self, headers):
 
311
        # Just a no-op at the moment.
 
312
        pass
 
313
 
 
314
    def args_received(self, args):
 
315
        cmd = args[0]
 
316
        args = args[1:]
 
317
        try:
 
318
            command = self._commands.get(cmd)
 
319
        except LookupError:
 
320
            raise errors.UnknownSmartMethod(cmd)
 
321
        self._command = command(self._backing_transport)
 
322
        self._run_handler_code(self._command.execute, args, {})
 
323
 
 
324
    def prefixed_body_received(self, body_bytes):
 
325
        """No more body data will be received."""
 
326
        self._run_handler_code(self._command.do_body, (body_bytes,), {})
 
327
        # cannot read after this.
 
328
        self.finished_reading = True
 
329
 
 
330
    def body_chunk_received(self, chunk_bytes):
 
331
        self._run_handler_code(self._command.do_chunk, (chunk_bytes,), {})
 
332
 
 
333
    def end_received(self):
 
334
        self._run_handler_code(self._command.do_end, (), {})
 
335
 
294
336
 
295
337
class HelloRequest(SmartServerRequest):
296
338
    """Answer a version request with the highest protocol version this server