Skip to content

Commit 7db5fb5

Browse files
peterharperuklurch
andauthored
Allow response to be specified in makefsdata script (#3000)
* Allow response to be changed in makefsdata script Currently the response contents are hardcoded. This prevents using the makefsdata script to implement a captive portal where we have to send a redirect when a 404 result would be returned. When loading file contents, look for a file with the same name with a "response" extension and use its contents for the response. We can then use this for the access point example which can then be simplified. Fixes #2646 * Change .hdr file to .response Only return on zero contents if a .response file is found. * Apply suggestion from @lurch Co-authored-by: Andrew Scheller <lurch@durge.org> --------- Co-authored-by: Andrew Scheller <lurch@durge.org>
1 parent 3ebd10c commit 7db5fb5

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

src/rp2_common/pico_lwip/tools/makefsdata.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
def process_file(input_dir, file):
1919
results = []
2020

21+
# Ignore response files
22+
if file.suffix == ".response":
23+
return None
24+
2125
# Check content type
2226
content_type, content_encoding = mimetypes.guess_type(file)
2327
if content_type is None:
@@ -31,16 +35,27 @@ def process_file(input_dir, file):
3135
data += "\x00"
3236
results.append({'data': bytes(data, "utf-8"), 'comment': comment});
3337

34-
# Header
35-
response_type = 200
36-
for response_id in response_types:
37-
if file.name.startswith(f"{response_id}."):
38-
response_type = response_id
39-
break
40-
data = f"{response_types[response_type]}\r\n"
41-
comment = f"\"{response_types[response_type]}\" ({len(data)} chars)"
38+
# If we find a file with the same name and a "response" extension - use its contents for the response
39+
response_file = file.with_suffix('.response')
40+
if response_file.is_file():
41+
data = response_file.read_text()
42+
comment = f"content from {response_file.name} ({len(data)} chars)"
43+
else:
44+
# response result
45+
response_type = 200
46+
for response_id in response_types:
47+
if file.name.startswith(f"{response_id}."):
48+
response_type = response_id
49+
break
50+
data = f"{response_types[response_type]}\r\n"
51+
comment = f"\"{response_types[response_type]}\" ({len(data)} chars)"
4252
results.append({'data': bytes(data, "utf-8"), 'comment': comment});
4353

54+
# load file contents
55+
file_contents = file.read_bytes()
56+
if len(file_contents) == 0 and response_file.is_file():
57+
return results
58+
4459
# user agent
4560
data = f"Server: {HTTPD_SERVER_AGENT}\r\n"
4661
comment = f"\"Server: {HTTPD_SERVER_AGENT}\" ({len(data)} chars)"
@@ -64,10 +79,9 @@ def process_file(input_dir, file):
6479
comment = f"\"{content_type_header} {content_encoding_header}\" ({len(data)} chars)"
6580
results.append({'data': bytes(data, "utf-8"), 'comment': comment});
6681

67-
# file contents
68-
data = file.read_bytes()
69-
comment = f"raw file data ({len(data)} bytes)"
70-
results.append({'data': data, 'comment': comment});
82+
# add file contents
83+
comment = f"raw file data ({len(file_contents)} bytes)"
84+
results.append({'data': file_contents, 'comment': comment});
7185

7286
return results;
7387

@@ -85,6 +99,8 @@ def process_file_list(fd, input):
8599
if input_dir is None:
86100
input_dir = file.parent
87101
results = process_file(input_dir, file)
102+
if not results:
103+
continue
88104

89105
# make a variable name
90106
var_name = str(file.relative_to(input_dir))

0 commit comments

Comments
 (0)