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 | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | import multiprocessing |
| 9 | import os |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 10 | import traceback |
| 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) |
| 18 | self.serve_forever() |
| 19 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 20 | class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | |
| 22 | def log_message(self, format_str, *args): |
| 23 | pass |
| 24 | |
| 25 | class HTTPService(object): |
| 26 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 27 | def __init__(self, root_dir, host='', port=0, logger=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | self.root_dir = root_dir |
| 29 | self.host = host |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 30 | self.port = port |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 31 | self.logger = logger |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 33 | def start(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | if not os.path.exists(self.root_dir): |
| 35 | self.logger.info("Not starting HTTPService for directory %s which doesn't exist" % (self.root_dir)) |
| 36 | return |
| 37 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | self.server = HTTPServer((self.host, self.port), HTTPRequestHandler) |
| 39 | if self.port == 0: |
| 40 | self.port = self.server.server_port |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 41 | self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir, self.logger]) |
| 42 | |
| 43 | # The signal handler from testimage.bbclass can cause deadlocks here |
| 44 | # if the HTTPServer is terminated before it can restore the standard |
| 45 | #signal behaviour |
| 46 | orig = signal.getsignal(signal.SIGTERM) |
| 47 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | self.process.start() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 49 | signal.signal(signal.SIGTERM, orig) |
| 50 | |
| 51 | if self.logger: |
| 52 | self.logger.info("Started HTTPService on %s:%s" % (self.host, self.port)) |
| 53 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | |
| 55 | def stop(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 56 | if hasattr(self, "server"): |
| 57 | self.server.server_close() |
| 58 | if hasattr(self, "process"): |
| 59 | self.process.terminate() |
| 60 | self.process.join() |
| 61 | if self.logger: |
| 62 | self.logger.info("Stopped HTTPService on %s:%s" % (self.host, self.port)) |
| 63 | |