xref: /aosp_15_r20/external/autotest/client/cros/cellular/test_endpoint.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
10import six.moves.BaseHTTPServer
11import six.moves.urllib.parse
12
13class TestEndpointHandler(six.moves.BaseHTTPServer.BaseHTTPRequestHandler):
14    """
15    A web server that is used by cellular tests.  It serves up the following
16    pages:
17            - http://<ip>/generate_204
18              This URL is used by shill's portal detection.
19
20            - http://<ip>/download?size=%d
21              Tests use this URL to download an arbitrary amount of data.
22
23    """
24    GENERATE_204_PATH = '/generate_204'
25    DOWNLOAD_URL_PATH = '/download'
26    SIZE_PARAM = 'size'
27
28    def do_GET(self):
29        """Handles GET requests."""
30        url = six.moves.urllib.parse.urlparse(self.path)
31        print('URL: %s' % url.path)
32        if url.path == self.GENERATE_204_PATH:
33            self.send_response(204)
34        elif url.path == self.DOWNLOAD_URL_PATH:
35            parsed_query = six.moves.urllib.parse.parse_qs(url.query)
36            if self.SIZE_PARAM not in parsed_query:
37                pass
38            self.send_response(200)
39            self.send_header('Content-type', 'application/octet-stream')
40            self.end_headers()
41            self.wfile.write('0' * int(parsed_query[self.SIZE_PARAM][0]))
42        else:
43            print('Unsupported URL path: %s' % url.path)
44
45
46def main():
47    """Main entry point when this script is run from the command line."""
48    try:
49        server = six.moves.BaseHTTPServer.HTTPServer(('', 80), TestEndpointHandler)
50        server.serve_forever()
51    except KeyboardInterrupt:
52        server.socket.close()
53
54
55if __name__ == '__main__':
56    main()
57