From 514ffc164a971eabeecbd05afa3ae4f7bba226f4 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:23:05 +0300 Subject: [PATCH 1/7] feature: add CORS support --- README.md | 13 ++++++++----- VERSION | 2 +- src/main.py | 20 +++++++++++++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1dda6d2..c5f19e9 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,18 @@ No additional dependencies required. One-file bundled executable can be downloaded from the **Releases** section. +Tested on Windows 10/11, Ubuntu 22.04. + ## Usage ```txt -bphs [-h] [-p] [-d] [-l] +bphs [-h] [-p PORT] [-d PATH] [-l] [--cors] --h, --help show help message and exit --p, --port port to use [8080] --d, --dir directory to serve [current directory] --l, --listing enable directory listing +-h, --help show help message and exit +-p PORT, --port PORT port to use [8080] +-d PATH, --dir PATH directory to serve [current directory] +-l, --listing enable directory listing +--cors enable CORS headers ``` [1]: https://github.com/python/cpython/blob/3.12/Lib/http/server.py diff --git a/VERSION b/VERSION index 3eefcb9..9084fa2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 +1.1.0 diff --git a/src/main.py b/src/main.py index 955b5d8..a5db42f 100644 --- a/src/main.py +++ b/src/main.py @@ -32,9 +32,16 @@ class BasicHTTPRequestHandler(SimpleHTTPRequestHandler): def __init__(self, *handler_args, **handler_kwargs) -> None: self.dir_listing = handler_kwargs.pop('dir_listing', False) + self.enable_cors = handler_kwargs.pop('enable_cors', False) super().__init__(*handler_args, **handler_kwargs) self.follow_symlinks = False + def end_headers(self): + """ Allow requests from any origin """ + if self.enable_cors: + self.send_header('Access-Control-Allow-Origin', '*') + super().end_headers() + # https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes _control_char_table = str.maketrans({c: fr'\x{c:02x}' for c in itertools.chain(range(0x20), range(0x7f, 0xa0))}) @@ -55,7 +62,7 @@ def list_directory(self, path: str | os.PathLike[str]) -> BytesIO | None: return super().list_directory(path) -def basic_http_server(port: int, public_dir: Path, dir_listing: bool) -> None: +def basic_http_server(port: int, public_dir: Path, dir_listing: bool, cors: bool) -> None: """ Starts a basic HTTP server """ if not public_dir.exists() or not public_dir.is_dir(): logging.error("Directory \"%s\" doesn't exist", public_dir) @@ -64,7 +71,8 @@ def basic_http_server(port: int, public_dir: Path, dir_listing: bool) -> None: logging.info("Initializing Basic HTTP Server") try: httpd = ThreadingBasicServer(("", port), partial( - BasicHTTPRequestHandler, directory=public_dir, dir_listing=dir_listing)) + BasicHTTPRequestHandler, directory=public_dir, + dir_listing=dir_listing, enable_cors=cors)) logging.info("Available on port %s", port) httpd.serve_forever() @@ -79,14 +87,16 @@ def parse_arguments() -> argparse.Namespace: formatter_class=CustomHelpFormatter ) - parser.add_argument("-p", "--port", metavar="", + parser.add_argument("-p", "--port", default=8080, type=int, help="port to use [8080]") - parser.add_argument("-d", "--dir", metavar="", + parser.add_argument("-d", "--dir", metavar="PATH", default=Path(os.getcwd()), type=Path, help="directory to serve [current directory]") parser.add_argument("-l", "--listing", action="store_true", help="enable directory listing") + parser.add_argument("--cors", action="store_true", + help="enable CORS headers") return parser.parse_args() @@ -94,6 +104,6 @@ def parse_arguments() -> argparse.Namespace: args = parse_arguments() try: - basic_http_server(args.port, args.dir, args.listing) + basic_http_server(args.port, args.dir, args.listing, args.cors) except KeyboardInterrupt: logging.info("Basic HTTP Server stopped") From cb2c679a50e61cdcaddb062abc3e6857f1673116 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:33:18 +0300 Subject: [PATCH 2/7] chore: update workflow --- .github/workflows/linting.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index cf09865..9d6cc12 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -2,7 +2,7 @@ name: Linting on: push: - branches: dev + branches: "*" pull_request: branches: dev, main From a2d944bf1866a902cf6a412b72a1567c4db7d3f4 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:34:41 +0300 Subject: [PATCH 3/7] chore: update workflow --- .github/workflows/linting.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 9d6cc12..61c614b 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -2,7 +2,7 @@ name: Linting on: push: - branches: "*" + branches: '*' pull_request: branches: dev, main From 41bf163ef1e67a0967957aebf72ddc166d1db941 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:35:48 +0300 Subject: [PATCH 4/7] chore: update workflow --- .github/workflows/linting.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 61c614b..bbdcb3b 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -2,7 +2,8 @@ name: Linting on: push: - branches: '*' + branches: + - "*" pull_request: branches: dev, main From e6190a95431a54ed07ddc3d95813e7f76f4b5401 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:36:21 +0300 Subject: [PATCH 5/7] chore: update workflow --- .github/workflows/linting.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index bbdcb3b..3bce817 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -3,7 +3,7 @@ name: Linting on: push: branches: - - "*" + - '*' pull_request: branches: dev, main From 4bfd7f4261016e8e7dd6d468ac792e1f71978e71 Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 12:37:47 +0300 Subject: [PATCH 6/7] chore: update workflow --- .github/workflows/linting.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 3bce817..11d3225 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -2,8 +2,7 @@ name: Linting on: push: - branches: - - '*' + branches: '**' pull_request: branches: dev, main From b1a8ec38d359caa0208b204d0cad802492e0629b Mon Sep 17 00:00:00 2001 From: evgenii-d Date: Sun, 17 Mar 2024 13:12:37 +0300 Subject: [PATCH 7/7] chore: update add new workflow --- .github/workflows/enforce_main.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/workflows/enforce_main.yml diff --git a/.github/workflows/enforce_main.yml b/.github/workflows/enforce_main.yml new file mode 100644 index 0000000..d13fe95 --- /dev/null +++ b/.github/workflows/enforce_main.yml @@ -0,0 +1,16 @@ +name: Enforce Main Branch + +on: + pull_request: + branches: main + +jobs: + check_branch: + runs-on: ubuntu-latest + steps: + - name: Enforce main branch on pull requests + if: github.head_ref != 'dev' + run: | + echo "Merge to main branch allowed only from dev" + exit 1 + \ No newline at end of file