Skip to content

Commit be0986d

Browse files
authored
net.http: fix panic on empty addr, cleanup listen_and_serve Server method (#21164)
1 parent a1b6360 commit be0986d

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

vlib/net/http/server.v

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,10 @@ pub fn (mut s Server) listen_and_serve() {
6161
eprintln('Failed getting listener address, err: ${err}')
6262
return
6363
}
64-
mut listening_address := s.addr.clone()
6564
if l.family() == net.AddrFamily.unspec {
66-
if listening_address == ':0' {
67-
listening_address = 'localhost:0'
68-
}
69-
mut listen_family := net.AddrFamily.ip
70-
// $if !windows {
71-
// listen_family = net.AddrFamily.ip6
72-
// }
65+
listening_address := if s.addr == '' || s.addr == ':0' { 'localhost:0' } else { s.addr }
66+
listen_family := net.AddrFamily.ip
67+
// listen_family := $if windows { net.AddrFamily.ip } $else { net.AddrFamily.ip6 }
7368
s.listener = net.listen_tcp(listen_family, listening_address) or {
7469
eprintln('Listening on ${s.addr} failed, err: ${err}')
7570
return
@@ -84,7 +79,6 @@ pub fn (mut s Server) listen_and_serve() {
8479

8580
// Create tcp connection channel
8681
ch := chan &net.TcpConn{cap: s.pool_channel_slots}
87-
8882
// Create workers
8983
mut ws := []thread{cap: s.worker_num}
9084
for wid in 0 .. s.worker_num {
@@ -101,14 +95,10 @@ pub fn (mut s Server) listen_and_serve() {
10195
if s.on_running != unsafe { nil } {
10296
s.on_running(mut s)
10397
}
104-
for {
105-
// break if we have a stop signal
106-
if s.state != .running {
107-
break
108-
}
98+
for s.state == .running {
10999
mut conn := s.listener.accept() or {
110100
if err.code() == net.err_timed_out_code {
111-
// just skip network timeouts, they are normal
101+
// Skip network timeouts, they are normal
112102
continue
113103
}
114104
eprintln('accept() failed, reason: ${err}; skipping')

vlib/net/http/server_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn test_my_counting_handler_on_random_port() {
226226
}
227227
mut server := &http.Server{
228228
show_startup_message: false
229-
port: 0
229+
addr: ''
230230
accept_timeout: atimeout
231231
handler: MyCountingHandler{}
232232
on_running: fn (mut server http.Server) {

0 commit comments

Comments
 (0)