SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
makefsdata Namespace Reference

Functions

 process_file (input_dir, file)
 process_file_list (fd, input, base_dirs)
 run_tool ()

Variables

dict response_types
int PAYLOAD_ALIGNMENT = 4
str HTTPD_SERVER_AGENT = "lwIP/2.2.0d (http://savannah.nongnu.org/projects/lwip)"
list LWIP_HTTPD_SSI_EXTENSIONS = [".shtml", ".shtm", ".ssi", ".xml", ".json"]

Function Documentation

◆ process_file()

makefsdata.process_file ( input_dir,
file )

Definition at line 20 of file httpd/makefsdata.py.

20def process_file(input_dir, file):
21 results = []
22
23 # Check content type
24 content_type, content_encoding = mimetypes.guess_type(file)
25 if content_type is None:
26 content_type = "application/octet-stream"
27
28 # file name with posix directory separators
29 file_path_posix = file.relative_to(input_dir).as_posix()
30 data = f"/{file_path_posix}\x00"
31 comment = f"\"/{file_path_posix}\" ({len(data)} chars)"
32 while(len(data) % PAYLOAD_ALIGNMENT != 0):
33 data += "\x00"
34 results.append({'data': bytes(data, "utf-8"), 'comment': comment});
35
36 # Header
37 response_type = 200
38 for response_id in response_types:
39 if file.name.startswith(f"{response_id}."):
40 response_type = response_id
41 break
42 data = f"{response_types[response_type]}\r\n"
43 comment = f"\"{response_types[response_type]}\" ({len(data)} chars)"
44 results.append({'data': bytes(data, "utf-8"), 'comment': comment});
45
46 # user agent
47 data = f"Server: {HTTPD_SERVER_AGENT}\r\n"
48 comment = f"\"Server: {HTTPD_SERVER_AGENT}\" ({len(data)} chars)"
49 results.append({'data': bytes(data, "utf-8"), 'comment': comment});
50
51 if file.suffix not in LWIP_HTTPD_SSI_EXTENSIONS:
52 # content length
53 file_size = file.stat().st_size
54 data = f"Content-Length: {file_size}\r\n"
55 comment = f"\"Content-Length: {file_size}\" ({len(data)} chars)"
56 results.append({'data': bytes(data, "utf-8"), 'comment': comment});
57
58 # content type and content encoding
59 content_type_header = f"Content-Type: {content_type}"
60 if content_encoding is None:
61 data = f"{content_type_header}\r\n\r\n"
62 comment = f"\"{content_type_header}\" ({len(data)} chars)"
63 else:
64 content_encoding_header = f"Content-Encoding: {content_encoding}"
65 data = f"{content_type_header}\r\n{content_encoding_header}\r\n\r\n"
66 comment = f"\"{content_type_header} {content_encoding_header}\" ({len(data)} chars)"
67 results.append({'data': bytes(data, "utf-8"), 'comment': comment});
68
69 # file contents
70 data = file.read_bytes()
71 comment = f"raw file data ({len(data)} bytes)"
72 results.append({'data': data, 'comment': comment});
73
74 return results;
75

Referenced by process_file_list().

Here is the caller graph for this function:

◆ process_file_list()

makefsdata.process_file_list ( fd,
input,
base_dirs )

Definition at line 76 of file httpd/makefsdata.py.

76def process_file_list(fd, input, base_dirs):
77 data = []
78 fd.write("#include \"lwip/apps/fs.h\"\n")
79 fd.write("\n")
80 # generate the page contents
81 for file in input:
82 if not file.is_file():
83 raise RuntimeError(f"File not found: {file}")
84
85 # Take the input directory relative to matching base folder
86 input_dir = file.parent
87 for base in base_dirs:
88 if file.is_relative_to(base):
89 input_dir = base if base.is_dir() else base.parent
90 break
91
92 results = process_file(input_dir, file)
93
94 # make a variable name
95 var_name = str(file.relative_to(input_dir))
96 var_name = re.sub(r"\W+", "_", var_name, flags=re.ASCII)
97
98 # Add a suffix if the variable name is used already
99 if any(d["data_var"] == f"data_{var_name}" for d in data):
100 var_name += f"_{len(data)}"
101
102 data_var = f"data_{var_name}"
103 file_var = f"file_{var_name}"
104
105 # variable containing the raw data
106 fd.write(f"static const unsigned char {data_var}[] = {{\n")
107 for entry in results:
108 fd.write(f"\n /* {entry['comment']} */\n")
109 byte_count = 0
110 for b in entry['data']:
111 if byte_count % 16 == 0:
112 fd.write(" ")
113 byte_count += 1
114 fd.write(f"0x{b:02x},")
115 if byte_count % 16 == 0:
116 fd.write("\n")
117 if byte_count % 16 != 0:
118 fd.write("\n")
119 fd.write(f"}};\n\n")
120
121 # set the flags
122 flags = "FS_FILE_FLAGS_HEADER_INCLUDED"
123 if file.suffix not in LWIP_HTTPD_SSI_EXTENSIONS:
124 flags += " | FS_FILE_FLAGS_HEADER_PERSISTENT"
125 else:
126 flags += " | FS_FILE_FLAGS_SSI"
127
128 # add variable details to the list
129 data.append({'data_var': data_var, 'file_var': file_var, 'name_size': len(results[0]['data']), 'flags': flags})
130
131 # generate the page details
132 last_var = "NULL"
133 for entry in data:
134 fd.write(f"const struct fsdata_file {entry['file_var']}[] = {{{{\n")
135 fd.write(f" {last_var},\n")
136 fd.write(f" {entry['data_var']},\n")
137 fd.write(f" {entry['data_var']} + {entry['name_size']},\n")
138 fd.write(f" sizeof({entry['data_var']}) - {entry['name_size']},\n")
139 fd.write(f" {entry['flags']},\n")
140 fd.write(f"}}}};\n\n")
141 last_var = entry['file_var']
142 fd.write(f"#define FS_ROOT {last_var}\n")
143 fd.write(f"#define FS_NUMFILES {len(data)}\n")
144

References process_file().

Referenced by run_tool().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_tool()

makefsdata.run_tool ( )

Definition at line 145 of file httpd/makefsdata.py.

145def run_tool():
146 parser = argparse.ArgumentParser(prog="makefsdata.py", description="Generates a source file for the lwip httpd server")
147 parser.add_argument(
148 "-i",
149 "--input",
150 help="input files or directories to add as http content",
151 required=True,
152 nargs='+'
153 )
154 parser.add_argument(
155 "-o",
156 "--output",
157 help="name of the source file to generate",
158 required=True,
159 )
160 args = parser.parse_args()
161
162 mimetypes.init()
163 for ext in [".shtml", ".shtm", ".ssi"]:
164 mimetypes.add_type("text/html", ext)
165
166 base_dirs = [Path(item).resolve() for item in args.input]
167 file_list = []
168
169 for item in args.input:
170 path = Path(item).resolve()
171 if path.is_dir():
172 # Recursively find all files in subdirectories (sorted to ensure deterministic build)
173 for file_path in sorted(path.rglob("*")):
174 if file_path.is_file() and file_path not in file_list:
175 file_list.append(file_path)
176 elif path.is_file():
177 if path not in file_list:
178 file_list.append(path)
179 else:
180 raise RuntimeError(f"Input path does not exist: {item}")
181
182 if not file_list:
183 raise RuntimeError("No valid input files found to process.")
184
185 # Write output into an in-memory buffer to verify MD5 checksum before disk updates
186 buffer = io.StringIO()
187 process_file_list(buffer, file_list, base_dirs)
188 generated_content = buffer.getvalue()
189
190 output_path = Path(args.output).resolve()
191
192 # Compare checksum against existing target file
193 if output_path.exists():
194 existing_hash = hashlib.md5(output_path.read_bytes()).hexdigest()
195 new_hash = hashlib.md5(generated_content.encode("utf-8")).hexdigest()
196
197 if existing_hash == new_hash:
198 print(f"No changes detected. '{args.output}' kept unchanged.")
199 return
200
201 output_path.write_text(generated_content, encoding="utf-8", newline="")
202 print(f"Updated '{args.output}'.")
203

References process_file_list(), and run_tool().

Referenced by run_tool().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ HTTPD_SERVER_AGENT

str makefsdata.HTTPD_SERVER_AGENT = "lwIP/2.2.0d (http://savannah.nongnu.org/projects/lwip)"

Definition at line 17 of file httpd/makefsdata.py.

◆ LWIP_HTTPD_SSI_EXTENSIONS

list makefsdata.LWIP_HTTPD_SSI_EXTENSIONS = [".shtml", ".shtm", ".ssi", ".xml", ".json"]

Definition at line 18 of file httpd/makefsdata.py.

◆ PAYLOAD_ALIGNMENT

int makefsdata.PAYLOAD_ALIGNMENT = 4

Definition at line 16 of file httpd/makefsdata.py.

◆ response_types

dict makefsdata.response_types
Initial value:
1= {
2 200: "HTTP/1.0 200 OK",
3 400: "HTTP/1.0 400 Bad Request",
4 404: "HTTP/1.0 404 File not found",
5 501: "HTTP/1.0 501 Not Implemented",
6}

Definition at line 9 of file httpd/makefsdata.py.