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