cal-docs-server 3.0.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cal_docs_server/.gitignore +1 -0
- cal_docs_server/__init__.py +1 -0
- cal_docs_server/__main__.py +64 -0
- cal_docs_server/_version.py +4 -0
- cal_docs_server/api.py +643 -0
- cal_docs_server/async_file.py +112 -0
- cal_docs_server/auth.py +253 -0
- cal_docs_server/cache_render_index.py +106 -0
- cal_docs_server/index_docs.py +449 -0
- cal_docs_server/main.py +191 -0
- cal_docs_server/render_index.py +174 -0
- cal_docs_server/render_md.py +90 -0
- cal_docs_server/resources/__init__.py +1 -0
- cal_docs_server/resources/help.md +171 -0
- cal_docs_server/resources/index_template.html +612 -0
- cal_docs_server/resources/md_template.html +244 -0
- cal_docs_server/resources/openapi.yaml +281 -0
- cal_docs_server/version.py +38 -0
- cal_docs_server/web_server.py +217 -0
- cal_docs_server-3.0.0b1.dist-info/METADATA +12 -0
- cal_docs_server-3.0.0b1.dist-info/RECORD +24 -0
- cal_docs_server-3.0.0b1.dist-info/WHEEL +4 -0
- cal_docs_server-3.0.0b1.dist-info/entry_points.txt +2 -0
- cal_docs_server-3.0.0b1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# ----------------------------------------------------------------------------------------
|
|
2
|
+
# web_server
|
|
3
|
+
# ----------
|
|
4
|
+
#
|
|
5
|
+
# Runs the web server using aiohttp
|
|
6
|
+
#
|
|
7
|
+
# License
|
|
8
|
+
# -------
|
|
9
|
+
# MIT License - Copyright 2025-2026 Cyber Assessment Labs
|
|
10
|
+
#
|
|
11
|
+
# Authors
|
|
12
|
+
# -------
|
|
13
|
+
# bena
|
|
14
|
+
#
|
|
15
|
+
# Version History
|
|
16
|
+
# ---------------
|
|
17
|
+
# Mar 2024 - Created
|
|
18
|
+
# Dec 2025 - New version 2
|
|
19
|
+
# ----------------------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
# ----------------------------------------------------------------------------------------
|
|
22
|
+
# Imports
|
|
23
|
+
# ----------------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
import importlib.resources
|
|
26
|
+
import mimetypes
|
|
27
|
+
import os
|
|
28
|
+
from collections.abc import Awaitable
|
|
29
|
+
from collections.abc import Callable
|
|
30
|
+
from functools import partial
|
|
31
|
+
from aiohttp import web
|
|
32
|
+
from . import api
|
|
33
|
+
from . import async_file
|
|
34
|
+
from . import render_md
|
|
35
|
+
from .cache_render_index import cache_render_index
|
|
36
|
+
from .version import VERSION_STR
|
|
37
|
+
|
|
38
|
+
# ----------------------------------------------------------------------------------------
|
|
39
|
+
# Types
|
|
40
|
+
# ----------------------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
Handler = Callable[[web.Request], Awaitable[web.StreamResponse]]
|
|
43
|
+
|
|
44
|
+
# ----------------------------------------------------------------------------------------
|
|
45
|
+
# Functions
|
|
46
|
+
# ----------------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# ----------------------------------------------------------------------------------------
|
|
50
|
+
async def launch_web_server(
|
|
51
|
+
root_directory: str,
|
|
52
|
+
port: int,
|
|
53
|
+
server_name: str = "Documents Server",
|
|
54
|
+
tokens_file: str | None = None,
|
|
55
|
+
base_url: str | None = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""
|
|
58
|
+
Launches the web server listening on `port` and serving from `root_directory`
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
app = web.Application(middlewares=[_set_server_name])
|
|
62
|
+
app["server_name"] = server_name
|
|
63
|
+
app["tokens_file"] = tokens_file # Tokens loaded dynamically with 5s cache
|
|
64
|
+
# Strip trailing slash from base_url if provided
|
|
65
|
+
app["base_url"] = base_url.rstrip("/") if base_url else ""
|
|
66
|
+
|
|
67
|
+
# API routes (must be registered before the catch-all static route)
|
|
68
|
+
api.setup_api_routes(app, root_directory)
|
|
69
|
+
|
|
70
|
+
app.router.add_get("/", partial(_serve_index, root_directory=root_directory))
|
|
71
|
+
app.router.add_get("/help", _serve_help)
|
|
72
|
+
|
|
73
|
+
app.router.add_get(
|
|
74
|
+
"/{path:.*}", partial(_serve_static, root_directory=root_directory)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
runner = web.AppRunner(app)
|
|
78
|
+
await runner.setup()
|
|
79
|
+
server = web.TCPSite(runner, "0.0.0.0", port)
|
|
80
|
+
await server.start()
|
|
81
|
+
|
|
82
|
+
print(f"CAL Docs Server started on port: {port}")
|
|
83
|
+
|
|
84
|
+
if not app["tokens_file"]:
|
|
85
|
+
print("Warning: No API tokens configured - uploads will be rejected")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# ----------------------------------------------------------------------------------------
|
|
89
|
+
# Private Functions
|
|
90
|
+
# ----------------------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# ----------------------------------------------------------------------------------------
|
|
94
|
+
@web.middleware
|
|
95
|
+
async def _set_server_name(
|
|
96
|
+
request: web.Request, handler: Handler
|
|
97
|
+
) -> web.StreamResponse:
|
|
98
|
+
"""
|
|
99
|
+
This replaces the Server header with our name. Odd that its not just a parameter
|
|
100
|
+
to pass to `web.Application` but apparently it isn't.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
response = await handler(request)
|
|
104
|
+
response.headers["Server"] = f"cal-docs-server/{VERSION_STR}"
|
|
105
|
+
return response
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ----------------------------------------------------------------------------------------
|
|
109
|
+
async def _serve_static(request: web.Request, root_directory: str) -> web.Response:
|
|
110
|
+
"""
|
|
111
|
+
Handles requests for the static files.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
path: str = request.match_info.get("path", "").replace("%20", " ")
|
|
115
|
+
if ".." in path:
|
|
116
|
+
return web.Response(status=400, text="Invalid URL\n")
|
|
117
|
+
|
|
118
|
+
# Check if this path should be redirected to a versioned directory
|
|
119
|
+
# Get the first component of the path (the package name)
|
|
120
|
+
path_parts = path.split("/")
|
|
121
|
+
base_package = path_parts[0] if path_parts else ""
|
|
122
|
+
|
|
123
|
+
# Check if we have a version redirect for this package
|
|
124
|
+
version_redirects = request.app.get("version_redirects", {})
|
|
125
|
+
if base_package and base_package in version_redirects:
|
|
126
|
+
# Redirect to the versioned directory
|
|
127
|
+
versioned_dir = version_redirects[base_package]
|
|
128
|
+
# Replace the first path component with the versioned directory
|
|
129
|
+
path_parts[0] = versioned_dir
|
|
130
|
+
path = "/".join(path_parts)
|
|
131
|
+
|
|
132
|
+
file_path: str = os.path.join(root_directory, path)
|
|
133
|
+
|
|
134
|
+
if os.path.isdir(file_path):
|
|
135
|
+
if not request.path.endswith("/"):
|
|
136
|
+
# If URL doesn't end with a slash we send it a redirect
|
|
137
|
+
return web.HTTPFound(location=request.path + "/")
|
|
138
|
+
|
|
139
|
+
# A directory specified means we want to get index.html or index.md
|
|
140
|
+
html_file_path = os.path.join(file_path, "index.html")
|
|
141
|
+
md_file_path = os.path.join(file_path, "index.md")
|
|
142
|
+
if os.path.isfile(html_file_path):
|
|
143
|
+
file_path = html_file_path
|
|
144
|
+
elif os.path.isfile(md_file_path):
|
|
145
|
+
file_path = md_file_path
|
|
146
|
+
|
|
147
|
+
if os.path.isfile(file_path):
|
|
148
|
+
try:
|
|
149
|
+
async with async_file.open_binary(file_path, "rb") as f:
|
|
150
|
+
file_contents = await f.read()
|
|
151
|
+
if file_path.endswith(".md"):
|
|
152
|
+
# Convert MarkDown to html on the fly
|
|
153
|
+
content_type = "text/html"
|
|
154
|
+
file_contents = await render_md.md_to_html(file_contents)
|
|
155
|
+
else:
|
|
156
|
+
content_type = (
|
|
157
|
+
mimetypes.guess_type(file_path)[0] or "application/octet-stream"
|
|
158
|
+
)
|
|
159
|
+
response_page = web.Response(body=file_contents, content_type=content_type)
|
|
160
|
+
return response_page
|
|
161
|
+
except OSError:
|
|
162
|
+
# File became unavailable between check and read
|
|
163
|
+
return web.HTTPNotFound(text="404 Not found\n")
|
|
164
|
+
|
|
165
|
+
# We haven't served a response yet. So return a 404
|
|
166
|
+
return web.HTTPNotFound(text="404 Not found\n")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# ----------------------------------------------------------------------------------------
|
|
170
|
+
async def _serve_index(request: web.Request, root_directory: str) -> web.Response:
|
|
171
|
+
"""
|
|
172
|
+
Handles requests for the main index page.
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
server_name = request.app.get("server_name", "Documents Server")
|
|
177
|
+
text, version_redirects = await cache_render_index(
|
|
178
|
+
root_directory=root_directory, server_name=server_name
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# Store version redirects in the app for use by _serve_static
|
|
182
|
+
request.app["version_redirects"] = version_redirects
|
|
183
|
+
|
|
184
|
+
response_page = web.Response(body=text, content_type="text/html")
|
|
185
|
+
return response_page
|
|
186
|
+
except Exception as e:
|
|
187
|
+
return web.Response(
|
|
188
|
+
text=f"Error loading index: {e}\n",
|
|
189
|
+
status=500,
|
|
190
|
+
content_type="text/plain",
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# ----------------------------------------------------------------------------------------
|
|
195
|
+
async def _serve_help(_request: web.Request) -> web.Response:
|
|
196
|
+
"""
|
|
197
|
+
Handles requests for the /help page - serves the help markdown file from resources.
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
try:
|
|
201
|
+
file_folder = importlib.resources.files("cal_docs_server.resources")
|
|
202
|
+
file_path = os.path.join(str(file_folder), "help.md")
|
|
203
|
+
|
|
204
|
+
async with async_file.open_binary(file_path, "rb") as f:
|
|
205
|
+
file_contents = await f.read()
|
|
206
|
+
|
|
207
|
+
# Render markdown to HTML
|
|
208
|
+
html_content = await render_md.md_to_html(file_contents)
|
|
209
|
+
|
|
210
|
+
response_page = web.Response(body=html_content, content_type="text/html")
|
|
211
|
+
return response_page
|
|
212
|
+
except Exception as e:
|
|
213
|
+
return web.Response(
|
|
214
|
+
text=f"Error loading help: {e}\n",
|
|
215
|
+
status=500,
|
|
216
|
+
content_type="text/plain",
|
|
217
|
+
)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cal-docs-server
|
|
3
|
+
Version: 3.0.0b1
|
|
4
|
+
License-Expression: MIT
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.14
|
|
7
|
+
Requires-Dist: aiohttp>=3.9.3
|
|
8
|
+
Requires-Dist: json5>=0.9.18
|
|
9
|
+
Requires-Dist: markdown>=3.5.2
|
|
10
|
+
Requires-Dist: pyyaml>=6.0.1
|
|
11
|
+
Requires-Dist: types-markdown>=3.5
|
|
12
|
+
Requires-Dist: types-pyyaml>=6.0.12.12
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
cal_docs_server/.gitignore,sha256=M4fmG7SHXNkkyUIiMjvf8SOuIU6bfWxJC-85lUBOxaM,23
|
|
2
|
+
cal_docs_server/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
3
|
+
cal_docs_server/__main__.py,sha256=Cfaum1RNvOfZ5ISXMc39h1pjICeMdyWL4Vu7lej3l7A,1611
|
|
4
|
+
cal_docs_server/_version.py,sha256=h20xb9c3QtHKsuqaoIU5c8oFHkh-1bywwxPhXtsg7qo,173
|
|
5
|
+
cal_docs_server/api.py,sha256=QWJsMtNFkdHCaMJHq0hykZxLQYwN5ByWm621BHsoMMo,22080
|
|
6
|
+
cal_docs_server/async_file.py,sha256=QdfKZawneR72KmRE93uZJBbaMwWiIKmm7_7gFPtSxEU,3648
|
|
7
|
+
cal_docs_server/auth.py,sha256=G0jdYXoQctybWOOVbY0MlxbQ0n-TjWP6D0gz1M9tGjA,7994
|
|
8
|
+
cal_docs_server/cache_render_index.py,sha256=72MnpqGnUUzJ4_ezyrObQAHGPE3Qg1N5HdjGo0_xw3A,3451
|
|
9
|
+
cal_docs_server/index_docs.py,sha256=QfkKgTojXRoC9kwwpJ29ArB_qZrn203T6PAu6M0OpO4,15560
|
|
10
|
+
cal_docs_server/main.py,sha256=djePJTuz_XHrzECA6ewFnjOfwe6mSc7lZikDi6R37So,5890
|
|
11
|
+
cal_docs_server/render_index.py,sha256=SRBMrVoY76XjWoj-K7lgiI5fadNkNLVDvU7EQE4M_wg,5842
|
|
12
|
+
cal_docs_server/render_md.py,sha256=j4fm0M1xgMJNiz1yhcUV4bm8duC-3TN2bbzAIhYLjJI,2813
|
|
13
|
+
cal_docs_server/version.py,sha256=9Pa48sDghqw_GgZCOjHyyvC2zYJvy0lVImJw3sxceug,1111
|
|
14
|
+
cal_docs_server/web_server.py,sha256=eBLoPJCJbaDIzwkjt7zCzaHMpBIZU0DUpZvzBQkgQUc,7826
|
|
15
|
+
cal_docs_server/resources/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
16
|
+
cal_docs_server/resources/help.md,sha256=pcfQWd0ZX3AhBESzSyAM8JfBFPZh2m7c_Gettyz2wpQ,4849
|
|
17
|
+
cal_docs_server/resources/index_template.html,sha256=6EvV4sH8JpEaWBWXzhOXZoBuHJUOf301BCjk4TxHlj8,15797
|
|
18
|
+
cal_docs_server/resources/md_template.html,sha256=HZdx_qnnbWbvnTR1kmDGhKNODJOySPF9NTaRP_tbkfQ,5399
|
|
19
|
+
cal_docs_server/resources/openapi.yaml,sha256=wsUTcx31WE6oyv7Usev7pYp8KV2v9paqcy_KpScY_Ro,7931
|
|
20
|
+
cal_docs_server-3.0.0b1.dist-info/METADATA,sha256=kbR9iZ0U3fMgnoxYwHFe7MYW27dOyYcgrXHn1SDWb58,324
|
|
21
|
+
cal_docs_server-3.0.0b1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
22
|
+
cal_docs_server-3.0.0b1.dist-info/entry_points.txt,sha256=irb15ryE9DMciKfJvVWP25b3zTxiotoTeVvwtl4ZYJU,61
|
|
23
|
+
cal_docs_server-3.0.0b1.dist-info/licenses/LICENSE,sha256=2QIKIIT_RVuBkU1cGnZ7wZ-E6j1WOqFDnfzofJfiH64,1083
|
|
24
|
+
cal_docs_server-3.0.0b1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Cyber Assessment Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|