Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 7 | import http.server |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 8 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | import multiprocessing |
| 10 | import os |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | import signal |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | from socketserver import ThreadingMixIn |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | class HTTPServer(ThreadingMixIn, http.server.HTTPServer): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 16 | def server_start(self, root_dir, logger): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | os.chdir(root_dir) |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 18 | self.logger = logger |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | self.serve_forever() |
| 20 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | def log_message(self, format_str, *args): |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 24 | self.server.logger.info(format_str, *args) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 26 | class HTTPService: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 28 | def __init__(self, root_dir, host='', port=0, logger=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | self.root_dir = root_dir |
| 30 | self.host = host |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 31 | self.port = port |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 32 | if logger: |
| 33 | self.logger = logger.getChild("HTTPService") |
| 34 | else: |
| 35 | self.logger = logging.getLogger("HTTPService") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
| 37 | def start(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 38 | if not os.path.exists(self.root_dir): |
| 39 | self.logger.info("Not starting HTTPService for directory %s which doesn't exist" % (self.root_dir)) |
| 40 | return |
| 41 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | self.server = HTTPServer((self.host, self.port), HTTPRequestHandler) |
| 43 | if self.port == 0: |
| 44 | self.port = self.server.server_port |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 45 | self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir, self.logger]) |
| 46 | |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 47 | def handle_error(self, request, client_address): |
| 48 | import traceback |
| 49 | exception = traceback.format_exc() |
| 50 | self.logger.warn("Exception when handling %s: %s" % (request, exception)) |
| 51 | self.server.handle_error = handle_error |
| 52 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 53 | # The signal handler from testimage.bbclass can cause deadlocks here |
| 54 | # if the HTTPServer is terminated before it can restore the standard |
| 55 | #signal behaviour |
| 56 | orig = signal.getsignal(signal.SIGTERM) |
| 57 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | self.process.start() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 59 | signal.signal(signal.SIGTERM, orig) |
| 60 | |
| 61 | if self.logger: |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 62 | self.logger.info("Started HTTPService for %s on %s:%s" % (self.root_dir, self.host, self.port)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 63 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
| 65 | def stop(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 66 | if hasattr(self, "server"): |
| 67 | self.server.server_close() |
| 68 | if hasattr(self, "process"): |
| 69 | self.process.terminate() |
| 70 | self.process.join() |
| 71 | if self.logger: |
| 72 | self.logger.info("Stopped HTTPService on %s:%s" % (self.host, self.port)) |
| 73 | |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 74 | if __name__ == "__main__": |
| 75 | import sys, logging |
| 76 | |
| 77 | logger = logging.getLogger(__name__) |
| 78 | logging.basicConfig(level=logging.DEBUG) |
| 79 | httpd = HTTPService(sys.argv[1], port=8888, logger=logger) |
| 80 | httpd.start() |