Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/enforce_main.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Linting

on:
push:
branches: dev
branches: '**'
pull_request:
branches: dev, main

Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.1.0
20 changes: 15 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))})
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -79,21 +87,23 @@ 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()


if __name__ == "__main__":
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")