Merge lp:~jelmer/brz/aiohttp into lp:brz

Proposed by Jelmer Vernooij
Status: Needs review
Proposed branch: lp:~jelmer/brz/aiohttp
Merge into: lp:brz
Diff against target: 86 lines (+82/-0)
1 file modified
breezy/transport/http/aiohttp.py (+82/-0)
To merge this branch: bzr merge lp:~jelmer/brz/aiohttp
Reviewer Review Type Date Requested Status
Breezy developers Pending
Review via email: mp+384197@code.launchpad.net

Description of the change

Add a breezy.transport.http.aiohttp module.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'breezy/transport/http/aiohttp.py'
2--- breezy/transport/http/aiohttp.py 1970-01-01 00:00:00 +0000
3+++ breezy/transport/http/aiohttp.py 2020-05-19 17:33:15 +0000
4@@ -0,0 +1,82 @@
5+# Copyright (C) 2020 Breezy Developers
6+#
7+# This program is free software; you can redistribute it and/or modify
8+# it under the terms of the GNU General Public License as published by
9+# the Free Software Foundation; either version 2 of the License, or
10+# (at your option) any later version.
11+#
12+# This program is distributed in the hope that it will be useful,
13+# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+# GNU General Public License for more details.
16+#
17+# You should have received a copy of the GNU General Public License
18+# along with this program; if not, write to the Free Software
19+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+"""aiohttp application for bzr HTTP smart server.
22+"""
23+
24+from __future__ import absolute_import
25+
26+from io import BytesIO
27+
28+from aiohttp import web
29+
30+from ...bzr.smart import medium
31+from ...transport import chroot
32+
33+
34+async def handle_smart_request(backing_transport, request):
35+ """Server an smart protocol request using aiohttp.
36+
37+ :param backing_transport: A backing transport
38+ :param request: an aiohttp web.Request
39+ :returns: an aiohttp web.Response
40+ """
41+ if request.method != 'POST':
42+ raise web.HTTPMethodNotAllowed()
43+
44+ suffix = '/.bzr/smart'
45+ if not request.path.endswith(suffix):
46+ raise web.HTTPNotFound()
47+ relpath = request.path[:-len(suffix)].lstrip('/')
48+
49+ transport = backing_transport.clone(relpath)
50+
51+ out_buffer = BytesIO()
52+ request_data_bytes = await request.read()
53+
54+ protocol_factory, unused_bytes = medium._get_protocol_factory_for_bytes(
55+ request_data_bytes)
56+ smart_protocol_request = protocol_factory(
57+ transport, out_buffer.write, '.', backing_transport)
58+ smart_protocol_request.accept_bytes(unused_bytes)
59+ if smart_protocol_request.next_read_size() != 0:
60+ # The request appears to be incomplete, or perhaps it's just a
61+ # newer version we don't understand. Regardless, all we can do
62+ # is return an error response in the format of our version of the
63+ # protocol.
64+ response_data = b'error\x01incomplete request\n'
65+ else:
66+ response_data = out_buffer.getvalue()
67+ # TODO(jelmer): Use StreamResponse
68+ return web.Response(
69+ status=200, body=response_data,
70+ content_type='application/octet-stream')
71+
72+
73+if __name__ == '__main__':
74+ import argparse
75+ from functools import partial
76+ from breezy.transport import get_transport
77+ parser = argparse.ArgumentParser()
78+ parser.add_argument(
79+ '--directory', '-d', type=str, default='.',
80+ help='Directory to serve.')
81+ args = parser.parse_args()
82+ app = web.Application()
83+ transport = get_transport(args.directory)
84+ app.router.add_post(
85+ '/{path_info:.*}', partial(handle_smart_request, transport))
86+ web.run_app(app)

Subscribers

People subscribed via source and target branches