Skip to content

Commit b5c273a

Browse files
committed
various fixes
1 parent 059763f commit b5c273a

File tree

7 files changed

+74
-15
lines changed

7 files changed

+74
-15
lines changed

.github/workflows/pull-request-tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout code
13-
uses: actions/checkout@v2
13+
uses: actions/checkout@v4
1414
with:
15-
ref: ${{ github.head_ref }}
15+
repository: ${{ github.event.pull_request.head.repo.full_name }}
16+
ref: ${{ github.event.pull_request.head.ref }}
1617

1718
- name: Set up Python
1819
uses: actions/setup-python@v2
@@ -58,6 +59,7 @@ jobs:
5859
make
5960
6061
- name: Commit updated snapshots
62+
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
6163
uses: EndBug/add-and-commit@v9
6264
id: commit
6365
with:

backend/app/api/tests/test_apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def test_api_apps_source(async_client):
2828
assert response.status_code == 200, "Expected /api/apps/source to return status 200"
2929
assert 'app.nim' in data, "Expected 'app.nim' to be part of the returned sources"
3030
assert 'config.json' in data, "Expected 'config.json' to be part of the returned sources"
31+
assert 'app_loader.nim' not in data, "Autogenerated files should not be returned"
3132
assert 'AppRoot' in data['app.nim'], "Expected 'AppRoot' text within app.nim source"
3233

3334

backend/app/models/apps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def get_one_app_sources(keyword: str) -> dict[str, str]:
4646
local_app_path = os.path.join(local_apps_path, keyword)
4747
files = os.listdir(local_app_path)
4848
for file in files:
49+
if file == "app_loader.nim":
50+
continue
4951
full_path = os.path.join(local_app_path, file)
5052
if os.path.isfile(full_path):
5153
# TODO: also support folders and binary files

frameos/src/frameos/server.nim

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import times
44
import assets/web as webAssets
55
import asyncdispatch
66
import httpclient
7+
import httpcore
78
import threadpool
89
import jester
910
import locks
@@ -42,6 +43,18 @@ proc h(message: string): string =
4243
proc s(message: string): string =
4344
message.replace("'", "\\'").replace("\n", "\\n")
4445

46+
proc shouldReturnNotModified*(headers: HttpHeaders, lastUpdate: float): bool {.gcsafe.} =
47+
if lastUpdate <= 0.0:
48+
return false
49+
let ifModifiedSince = seq[string](headers.getOrDefault("if-modified-since")).join(", ")
50+
if ifModifiedSince == "":
51+
return false
52+
try:
53+
let ifModifiedTime = parse(ifModifiedSince, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", utc())
54+
return int64(lastUpdate) <= ifModifiedTime.toTime().toUnix()
55+
except CatchableError:
56+
return false
57+
4558
const AUTH_HEADER = "authorization"
4659
const AUTH_TYPE = "Bearer"
4760

@@ -125,20 +138,14 @@ router myrouter:
125138
log(%*{"event": "http", "get": request.pathInfo})
126139
{.gcsafe.}: # We're reading immutable globals and png data via a lock. It's fine.
127140
let (sceneId, _, _, lastUpdate) = getLastPublicState()
128-
let ifModifiedSince = seq[string](request.headers.getOrDefault("if-modified-since")).join(", ")
129-
if ifModifiedSince != "" and lastUpdate > 0.0:
130-
try:
131-
let ifModifiedTime = parse(ifModifiedSince, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", utc())
132-
if int64(lastUpdate) <= ifModifiedTime.toTime().toUnix():
133-
resp Http304, [("X-Scene-Id", $sceneId), ("Access-Control-Expose-Headers", "X-Scene-Id")], ""
134-
return
135-
except:
136-
discard # Invalid date, proceed
141+
if shouldReturnNotModified(request.headers, lastUpdate):
142+
resp Http304, [("X-Scene-Id", $sceneId), ("Access-Control-Expose-Headers", "X-Scene-Id")], ""
143+
return
137144
var headers = @[
138-
("Content-Type", "image/png"),
139-
("Content-Disposition", &"inline; filename=\"{sceneId}.png\""),
140-
("X-Scene-Id", $sceneId),
141-
("Access-Control-Expose-Headers", "X-Scene-Id")
145+
("Content-Type", "image/png"),
146+
("Content-Disposition", &"inline; filename=\"{sceneId}.png\""),
147+
("X-Scene-Id", $sceneId),
148+
("Access-Control-Expose-Headers", "X-Scene-Id")
142149
]
143150
if lastUpdate > 0.0:
144151
let lastModified = format(fromUnix(int64(lastUpdate)), "ddd, dd MMM yyyy HH:mm:ss 'GMT'", utc())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
test_config
22
test_logger
33
test_scheduler
4+
test_server
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
import times
3+
import httpcore
4+
5+
import ../server
6+
7+
proc buildHeaders(values: seq[string]): HttpHeaders =
8+
result = newHttpHeaders()
9+
for value in values:
10+
result.add("If-Modified-Since", value)
11+
12+
suite "Server If-Modified-Since handling":
13+
let referenceTime = parse("Wed, 21 Oct 2015 07:28:00 GMT", "ddd, dd MMM yyyy HH:mm:ss 'GMT'", utc())
14+
let referenceUnix = referenceTime.toTime().toUnix().float
15+
16+
test "matches single header value":
17+
let headers = buildHeaders(@["Wed, 21 Oct 2015 07:28:00 GMT"])
18+
check shouldReturnNotModified(headers, referenceUnix)
19+
20+
test "matches split header value":
21+
let headers = buildHeaders(@["Wed", "21 Oct 2015 07:28:00 GMT"])
22+
check shouldReturnNotModified(headers, referenceUnix)
23+
24+
test "newer frame timestamp bypasses cache":
25+
let headers = buildHeaders(@["Wed, 21 Oct 2015 07:28:00 GMT"])
26+
check not shouldReturnNotModified(headers, referenceUnix + 60.0)
27+
28+
test "older frame timestamp returns cached image":
29+
let headers = buildHeaders(@["Wed, 21 Oct 2015 07:28:00 GMT"])
30+
check shouldReturnNotModified(headers, referenceUnix - 60.0)
31+
32+
test "invalid header value is ignored":
33+
let headers = buildHeaders(@["not a real date"])
34+
check not shouldReturnNotModified(headers, referenceUnix)
35+
36+
test "missing header is ignored":
37+
let headers = newHttpHeaders()
38+
check not shouldReturnNotModified(headers, referenceUnix)
39+
40+
test "non-positive last update is ignored":
41+
let headers = buildHeaders(@["Wed, 21 Oct 2015 07:28:00 GMT"])
42+
check not shouldReturnNotModified(headers, 0.0)

frontend/src/scenes/frame/panels/EditApp/editAppLogic.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export const editAppLogic = kea<editAppLogicType>([
9797
const response = await apiFetch(`/api/apps/source?keyword=${encodeURIComponent(values.savedKeyword)}`)
9898
sources = await response.json()
9999
}
100+
if (sources['app_loader.nim'] !== undefined) {
101+
const { ['app_loader.nim']: _ignored, ...filteredSources } = sources
102+
sources = filteredSources
103+
}
100104
for (const file of files) {
101105
if (file in sources) {
102106
actions.setActiveFile(file)

0 commit comments

Comments
 (0)