@@ -93,12 +93,6 @@ pub fn (mut ctx Context) set_custom_header(key string, value string) ! {
9393// send_response_to_client finalizes the response headers and sets Content-Type to `mimetype`
9494// and the response body to `response`
9595pub fn (mut ctx Context) send_response_to_client (mimetype string , response string ) Result {
96- // println('send_response_to_client')
97- // print_backtrace()
98- // println('ctx=')
99- // println(ctx)
100- // println('sending resp=')
101- // println(response)
10296 if ctx.done && ! ctx.takeover {
10397 eprintln ('[veb] a response cannot be sent twice over one connection' )
10498 return Result{}
@@ -112,7 +106,6 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin
112106 ctx.res.body = response.replace ('</html>' , '<script src="/veb_livereload/${veb_livereload_server_start} /script.js"></script>\n </html>' )
113107 }
114108 }
115-
116109 // set Content-Type and Content-Length headers
117110 mut custom_mimetype := if ctx.content_type.len == 0 { mimetype } else { ctx.content_type }
118111 if custom_mimetype != '' {
@@ -133,7 +126,6 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin
133126 if ctx.res.status_code == 0 {
134127 ctx.res.set_status (.ok)
135128 }
136-
137129 if ctx.takeover {
138130 println ('calling fast send resp' )
139131 fast_send_resp (mut ctx.conn, ctx.res) or {}
@@ -171,9 +163,7 @@ pub fn (mut ctx Context) file(file_path string) Result {
171163 eprintln ('[veb] file "${file_path} " does not exist' )
172164 return ctx.not_found ()
173165 }
174-
175166 ext := os.file_ext (file_path)
176-
177167 mut content_type := ctx.content_type
178168 if content_type.len == 0 {
179169 if ct := ctx.custom_mime_types[ext] {
@@ -182,12 +172,10 @@ pub fn (mut ctx Context) file(file_path string) Result {
182172 content_type = mime_types[ext]
183173 }
184174 }
185-
186175 if content_type.len == 0 {
187176 eprintln ('[veb] no MIME type found for extension "${ext} "' )
188177 return ctx.server_error ('' )
189178 }
190-
191179 return ctx.send_file (content_type, file_path)
192180}
193181
@@ -197,7 +185,6 @@ fn (mut ctx Context) send_file(content_type string, file_path string) Result {
197185 ctx.res.set_status (.not_found)
198186 return ctx.text ('resource does not exist' )
199187 }
200-
201188 // seek from file end to get the file size
202189 file.seek (0 , .end) or {
203190 eprintln ('[veb] error while trying to read file: ${err.msg()} ' )
@@ -208,24 +195,19 @@ fn (mut ctx Context) send_file(content_type string, file_path string) Result {
208195 return ctx.server_error ('could not read resource' )
209196 }
210197 file.close ()
211-
212198 // Check which encodings the client accepts
213199 accept_encoding := ctx.req.header.get (.accept_encoding) or { '' }
214200 client_accepts_zstd := accept_encoding.contains ('zstd' )
215201 client_accepts_gzip := accept_encoding.contains ('gzip' )
216-
217202 max_size_bytes := ctx.static_compression_max_size
218-
219203 // Determine which compression modes are enabled
220204 use_zstd := (ctx.enable_static_zstd && client_accepts_zstd)
221205 || (ctx.enable_static_compression && client_accepts_zstd)
222206 use_gzip := (ctx.enable_static_gzip && client_accepts_gzip)
223207 || (ctx.enable_static_compression && client_accepts_gzip)
224-
225208 // Try to serve pre-compressed files if any compression is enabled
226209 if use_zstd || use_gzip {
227210 orig_mtime := os.file_last_mod_unix (file_path)
228-
229211 // Try zstd first if enabled (better compression), then gzip
230212 if use_zstd {
231213 if ctx.serve_precompressed_file (content_type, file_path, '.zst' , 'zstd' , orig_mtime) {
@@ -237,15 +219,13 @@ fn (mut ctx Context) send_file(content_type string, file_path string) Result {
237219 return Result{}
238220 }
239221 }
240-
241222 // No pre-compressed file available: create one if file is small enough
242223 if file_size < max_size_bytes {
243224 // Load, compress, save, and serve
244225 data := os.read_file (file_path) or {
245226 eprintln ('[veb] error while trying to read file: ${err.msg()} ' )
246227 return ctx.server_error ('could not read resource' )
247228 }
248-
249229 // Try zstd first if enabled, then gzip
250230 if use_zstd {
251231 if result := ctx.serve_compressed_static (content_type, file_path, data,
@@ -261,15 +241,13 @@ fn (mut ctx Context) send_file(content_type string, file_path string) Result {
261241 return result
262242 }
263243 }
264-
265244 // Compression failed: serve uncompressed in streaming mode
266245 ctx.return_type = .file
267246 ctx.return_file = file_path
268247 ctx.res.header.set (.content_length, file_size.str ())
269248 return ctx.send_response_to_client (content_type, '' )
270249 }
271250 }
272-
273251 // Takeover mode: load file in memory (backward compatibility)
274252 if ctx.takeover {
275253 data := os.read_file (file_path) or {
@@ -278,13 +256,11 @@ fn (mut ctx Context) send_file(content_type string, file_path string) Result {
278256 }
279257 return ctx.send_response_to_client (content_type, data)
280258 }
281-
282259 // Default: serve uncompressed file in streaming mode (zero-copy sendfile)
283260 ctx.return_type = .file
284261 ctx.return_file = file_path
285262 ctx.res.header.set (.content_length, file_size.str ())
286- ctx.send_response_to_client (content_type, '' )
287- return Result{}
263+ return ctx.send_response_to_client (content_type, '' )
288264}
289265
290266// serve_precompressed_file serves an existing pre-compressed file (.zst or .gz) if it exists and is fresh.
@@ -305,8 +281,8 @@ fn (mut ctx Context) serve_precompressed_file(content_type string, file_path str
305281 ctx.res.header.set (.vary, 'Accept-Encoding' )
306282 compressed_size := os.file_size (compressed_path)
307283 ctx.res.header.set (.content_length, compressed_size.str ())
308- ctx.send_response_to_client (content_type, '' )
309284 ctx.already_compressed = true
285+ ctx.send_response_to_client (content_type, '' )
310286 return true
311287}
312288
@@ -323,33 +299,28 @@ fn (mut ctx Context) serve_compressed_static(content_type string, file_path stri
323299 c, '.gz' , 'gzip'
324300 }
325301 }
326-
327302 compressed_path := '${file_path}${ext} '
328-
329303 // Try to save compressed version for future requests
330304 mut write_success := true
331305 os.write_file (compressed_path, compressed.bytestr ()) or {
332306 eprintln ('[veb] warning: could not save ${ext} file (readonly filesystem?): ${err.msg()} ' )
333307 write_success = false
334308 }
335-
309+ ctx. already_compressed = true
336310 if write_success {
337311 // Serve the newly cached file in streaming mode (zero-copy)
338312 ctx.return_type = .file
339313 ctx.return_file = compressed_path
340314 ctx.res.header.set (.content_encoding, encoding_name)
341315 ctx.res.header.set (.vary, 'Accept-Encoding' )
342316 ctx.res.header.set (.content_length, compressed.len.str ())
343- ctx.send_response_to_client (content_type, '' )
344- ctx.already_compressed = true
317+ return ctx.send_response_to_client (content_type, '' )
345318 } else {
346319 // Fallback: serve compressed content from memory (no caching)
347320 ctx.res.header.set (.content_encoding, encoding_name)
348321 ctx.res.header.set (.vary, 'Accept-Encoding' )
349- ctx.send_response_to_client (content_type, compressed.bytestr ())
350- ctx.already_compressed = true
322+ return ctx.send_response_to_client (content_type, compressed.bytestr ())
351323 }
352- return Result{}
353324}
354325
355326// Response HTTP_OK with s as payload
392363pub fn (mut ctx Context) redirect (url string , params RedirectParams) Result {
393364 status := http.Status (params.typ)
394365 ctx.res.set_status (status)
395-
396366 ctx.res.header.add (.location, url)
397367 return ctx.send_response_to_client ('text/plain' , status.str ())
398368}
0 commit comments