Skip to content

Commit f252d3e

Browse files
committed
veb: remove commented code, cleanup formatting
1 parent 0b7b150 commit f252d3e

File tree

5 files changed

+8
-112
lines changed

5 files changed

+8
-112
lines changed

vlib/veb/context.v

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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`
9595
pub 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
@@ -392,7 +363,6 @@ pub:
392363
pub 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
}

vlib/veb/tests/veb_test_server.v

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ pub fn (mut app ServerApp) user_repo_settings(mut ctx ServerContext, username st
9393

9494
@['/json_echo'; post]
9595
pub fn (mut app ServerApp) json_echo(mut ctx ServerContext) veb.Result {
96-
// eprintln('>>>>> received http request at /json_echo is: $app.req')
9796
ctx.set_content_type(ctx.req.header.get(.content_type) or { '' })
9897
return ctx.ok(ctx.req.data)
9998
}
@@ -127,7 +126,6 @@ pub fn (mut app ServerApp) query_echo(mut ctx ServerContext, a string, b int) ve
127126
// Make sure [post] works without the path
128127
@[post]
129128
pub fn (mut app ServerApp) json(mut ctx ServerContext) veb.Result {
130-
// eprintln('>>>>> received http request at /json is: $app.req')
131129
ctx.set_content_type(ctx.req.header.get(.content_type) or { '' })
132130
return ctx.ok(ctx.req.data)
133131
}

vlib/veb/tr.v

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ pub fn raw(s string) RawHtml {
1111
return RawHtml(s)
1212
}
1313

14-
/*
15-
struct TrData {
16-
data
17-
}
18-
m map[string]TrData
19-
*/
20-
2114
// This function is run once, on app startup. Setting the `tr_map` const.
2215
// m['en']['house'] == 'House'
2316
fn load_tr_map() map[string]map[string]string {
@@ -32,14 +25,10 @@ fn load_tr_map() map[string]map[string]string {
3225
}
3326
x := text.split('-----\n')
3427
for s in x {
35-
// println('val="${val}"')
3628
nl_pos := s.index('\n') or { continue }
3729
key := s[..nl_pos]
3830
val := s[nl_pos + 1..]
39-
// v := vals[i + 1]
40-
// println('key="${key}" => val="${v}"')
4131
res[lang][key] = val
42-
// println(val)
4332
}
4433
}
4534
return res

vlib/veb/veb.v

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ mut:
144144
}
145145

146146
fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string, routes &map[string]Route) {
147-
// println('\n\nhandle_route() url=${url} routes=${routes}')
148147
mut route := Route{}
149148
mut middleware_has_sent_response := false
150149
mut not_found := false
@@ -220,11 +219,6 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
220219
}
221220
}
222221

223-
// defer {
224-
// println('USER CONTEXT at end of handle_route')
225-
// println(user_context)
226-
//}
227-
228222
// Route matching and match route specific middleware as last step
229223
$for method in A.methods {
230224
$if method.return_type is Result {
@@ -253,7 +247,6 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
253247
return
254248
}
255249
}
256-
257250
if method.args.len > 1 && can_have_data_args {
258251
// Populate method args with form or query values
259252
mut args := []string{cap: method.args.len + 1}
@@ -262,21 +255,16 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
262255
} else {
263256
user_context.Context.form
264257
}
265-
266258
for param in method.args[1..] {
267259
args << data[param.name]
268260
}
269-
270-
// println('m1')
271261
app.$method(mut user_context, args)
272262
} else {
273-
// println('m2')
274263
app.$method(mut user_context)
275264
}
276265
return
277266
}
278267

279-
// println('route_words=${route_words} method=${method}')
280268
if url_words.len == 0 && route_words == ['index'] && method.name == 'index' {
281269
$if A is MiddlewareApp {
282270
if validate_middleware[X](mut user_context, route.middlewares) == false {
@@ -288,21 +276,16 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
288276
if method.args.len > 1 && can_have_data_args {
289277
// Populate method args with form or query values
290278
mut args := []string{cap: method.args.len + 1}
291-
292279
data := if user_context.Context.req.method == .get {
293280
user_context.Context.query
294281
} else {
295282
user_context.Context.form
296283
}
297-
298284
for param in method.args[1..] {
299285
args << data[param.name]
300286
}
301-
302-
// println('m3')
303287
app.$method(mut user_context, args)
304288
} else {
305-
// println('m4')
306289
app.$method(mut user_context)
307290
}
308291
return
@@ -315,12 +298,10 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
315298
return
316299
}
317300
}
318-
319301
method_args := params.clone()
320302
if method_args.len + 1 != method.args.len {
321303
eprintln('[veb] warning: uneven parameters count (${method.args.len}) in `${method.name}`, compared to the veb route `${method.attrs}` (${method_args.len})')
322304
}
323-
// println('m5')
324305
app.$method(mut user_context, method_args)
325306
return
326307
}
@@ -335,7 +316,6 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
335316
}
336317

337318
fn route_matches(url_words []string, route_words []string) ?[]string {
338-
// println('route_matches(url_words:${url_words} route_words:${route_words}')
339319
// URL path should be at least as long as the route path
340320
// except for the catchall route (`/:path...`)
341321
if route_words.len == 1 && route_words[0].starts_with(':') && route_words[0].ends_with('...') {
@@ -344,7 +324,6 @@ fn route_matches(url_words []string, route_words []string) ?[]string {
344324
if url_words.len < route_words.len {
345325
return none
346326
}
347-
348327
mut params := []string{cap: url_words.len}
349328
if url_words.len == route_words.len {
350329
for i in 0 .. url_words.len {
@@ -474,9 +453,6 @@ fn send_string_ptr(mut conn net.TcpConn, ptr &u8, len int) !int {
474453
$if trace_send_string_conn ? {
475454
eprintln('> send_string: conn: ${ptr_str(conn)}')
476455
}
477-
// $if trace_response ? {
478-
// eprintln('> send_string:\n${s}\n')
479-
// }
480456
if voidptr(conn) == unsafe { nil } {
481457
return error('connection was closed before send_string')
482458
}

0 commit comments

Comments
 (0)