/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Andrew Bennetts
  • Date: 2008-09-08 12:59:00 UTC
  • mfrom: (3695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080908125900-8ywtsr7jqyyatjz0
Merge from 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
 
40
49
    care to call `translate_client_path` and `transport_from_client_path` as
41
50
    appropriate when dealing with paths received from the client.
42
51
    """
 
52
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
 
53
    # *handler* is a different concept to the request.
43
54
 
44
55
    def __init__(self, backing_transport, root_client_path='/'):
45
56
        """Constructor.
92
103
        
93
104
        Must return a SmartServerResponse.
94
105
        """
95
 
        # TODO: if a client erroneously sends a request that shouldn't have a
96
 
        # body, what to do?  Probably SmartServerRequestHandler should catch
97
 
        # this NotImplementedError and translate it into a 'bad request' error
98
 
        # to send to the client.
99
106
        raise NotImplementedError(self.do_body)
 
107
 
 
108
    def do_chunk(self, chunk_bytes):
 
109
        """Called with each body chunk if the request has a streamed body.
 
110
 
 
111
        The do() method is still called, and must have returned None.
 
112
        """
 
113
        raise NotImplementedError(self.do_chunk)
 
114
 
 
115
    def do_end(self):
 
116
        """Called when the end of the request has been received."""
 
117
        pass
100
118
    
101
119
    def translate_client_path(self, client_path):
102
120
        """Translate a path received from a network client into a local
117
135
        if client_path.startswith(self._root_client_path):
118
136
            path = client_path[len(self._root_client_path):]
119
137
            relpath = urlutils.joinpath('/', path)
120
 
            assert relpath.startswith('/')
 
138
            if not relpath.startswith('/'):
 
139
                raise ValueError(relpath)
121
140
            return '.' + relpath
122
141
        else:
123
142
            raise errors.PathNotChild(client_path, self._root_client_path)
238
257
        try:
239
258
            command = self._commands.get(cmd)
240
259
        except LookupError:
241
 
            raise errors.SmartProtocolError("bad request %r" % (cmd,))
 
260
            raise errors.UnknownSmartMethod(cmd)
242
261
        self._command = command(self._backing_transport, self._root_client_path)
243
262
        self._run_handler_code(self._command.execute, args, {})
244
263
 
291
310
            else:
292
311
                raise
293
312
 
 
313
    def headers_received(self, headers):
 
314
        # Just a no-op at the moment.
 
315
        pass
 
316
 
 
317
    def args_received(self, args):
 
318
        cmd = args[0]
 
319
        args = args[1:]
 
320
        try:
 
321
            command = self._commands.get(cmd)
 
322
        except LookupError:
 
323
            raise errors.UnknownSmartMethod(cmd)
 
324
        self._command = command(self._backing_transport)
 
325
        self._run_handler_code(self._command.execute, args, {})
 
326
 
 
327
    def prefixed_body_received(self, body_bytes):
 
328
        """No more body data will be received."""
 
329
        self._run_handler_code(self._command.do_body, (body_bytes,), {})
 
330
        # cannot read after this.
 
331
        self.finished_reading = True
 
332
 
 
333
    def body_chunk_received(self, chunk_bytes):
 
334
        self._run_handler_code(self._command.do_chunk, (chunk_bytes,), {})
 
335
 
 
336
    def end_received(self):
 
337
        self._run_handler_code(self._command.do_end, (), {})
 
338
 
294
339
 
295
340
class HelloRequest(SmartServerRequest):
296
341
    """Answer a version request with the highest protocol version this server
341
386
request_handlers.register_lazy(
342
387
    'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
343
388
request_handlers.register_lazy(
 
389
    'Branch.set_last_revision_info', 'bzrlib.smart.branch',
 
390
    'SmartServerBranchRequestSetLastRevisionInfo')
 
391
request_handlers.register_lazy(
 
392
    'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
 
393
    'SmartServerBranchRequestSetLastRevisionEx')
 
394
request_handlers.register_lazy(
344
395
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
345
396
request_handlers.register_lazy(
346
397
    'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
383
434
                               'bzrlib.smart.repository',
384
435
                               'SmartServerRepositoryGetParentMap')
385
436
request_handlers.register_lazy(
386
 
    'Repository.stream_knit_data_for_revisions',
387
 
    'bzrlib.smart.repository',
388
 
    'SmartServerRepositoryStreamKnitDataForRevisions')
389
 
request_handlers.register_lazy(
390
 
    'Repository.stream_revisions_chunked',
391
 
    'bzrlib.smart.repository',
392
 
    'SmartServerRepositoryStreamRevisionsChunked')
393
 
request_handlers.register_lazy(
394
437
    'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
395
438
request_handlers.register_lazy(
396
439
    'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')