Skip to content

Commit cdb34d8

Browse files
committed
Resolve relative URLs in settings/config to same host:port
If `FlagsURLs` and/or `PostResult` are specified as relative URLs, then the same `scheme://host:port` will be prepended to which the request was issued that caused these files to be served. Closes: #2
1 parent c92340f commit cdb34d8

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

tptools/ext/squore/assets/config.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
shareAction = "PostResult"
55

66
# If shareAction == "PostResult", the URL where to POST the results.
7-
# Use the special value "me" such that results are posted back to tpsrv.
7+
# If specified as a relative URL, the same scheme/host/port will be used to which the client
8+
# send the request.
89
# TODO: interaction with MQTT?
9-
PostResult = "http://tc/tptools/v1/squore/result"
10+
PostResult = "/tptools/v1/squore/result"
1011

1112
# What to POST to the URL:
1213
# https://github.com/obbimi/Squore/blob/master/src/com/doubleyellow/scoreboard/prefs/PostDataPreference.java

tptools/ext/squore/assets/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"BackKeyBehaviour": "UndoScore",
33
"FCMEnabled": false,
4-
"FlagsURLs": "http://tc/squore/v1/flags/%1$s.png",
4+
"FlagsURLs": "/squore/v1/flags/%1$s.png",
55
"MQTTBrokerURL": "Custom",
66
"MQTTBrokerURL_Custom": "mqtt://mqtt",
77
"MQTTPublishJoinerLeaverTopic": "tptools/${Brand}/JoinerLeaver",

tptools/tpsrv/squoresrv.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,35 @@ def get_devmap_path(request: Request) -> pathlib.Path | Never:
215215
) from err
216216

217217

218+
def _relative_to_absolute_urls(urls_per_line: str, myurl: URL) -> str:
219+
urls: list[str] = []
220+
for urlstr in urls_per_line.splitlines():
221+
if URL(urlstr).is_absolute():
222+
continue
223+
urls.append(str(myurl.with_path(urlstr, encoded=True)))
224+
return "\n".join(urls)
225+
226+
218227
def get_settings(
219228
path: Annotated[pathlib.Path, Depends(get_settings_path)],
229+
myurl: Annotated[URL, Depends(get_url)],
220230
) -> dict[str, Any] | Never:
221231
try:
222232
with open(path, "rb") as f:
223233
# TODO:cache. There should be a file-like class that caches the output,
224234
# ideally even the output of a callable on the data, and on every
225235
# subsequent access, check the original file's mtime to decide whether
226236
# to serve the cache, or relaod.
227-
return cast(dict[str, Any], json.load(f))
237+
238+
settings: dict[str, Any] = json.load(f)
239+
240+
for setting in ("FlagsURLs",):
241+
if setting in settings:
242+
settings[setting] = _relative_to_absolute_urls(
243+
settings[setting], myurl
244+
)
245+
246+
return settings
228247

229248
except FileNotFoundError as err:
230249
raise HTTPException(
@@ -235,6 +254,7 @@ def get_settings(
235254

236255
def get_config(
237256
path: Annotated[pathlib.Path, Depends(get_config_path)],
257+
myurl: Annotated[URL, Depends(get_url)],
238258
) -> Config | Never:
239259
config: Config = {}
240260

@@ -259,6 +279,10 @@ def get_config(
259279
detail=f"Invalid key in config file {path}: {key}",
260280
)
261281

282+
for setting in ("PostResult",):
283+
if setting in config:
284+
config[setting] = _relative_to_absolute_urls(config[setting], myurl)
285+
262286
return config
263287

264288

0 commit comments

Comments
 (0)