emikulic / darkhttpd

When you need a web server in a hurry.
https://unix4lyfe.org/darkhttpd/
ISC License
1.05k stars 87 forks source link

`maxconn` option does not limit simultaneous connections #9

Open tdryer opened 3 years ago

tdryer commented 3 years ago

The --maxconn N option does not limit the number of simultaneous connections as described.

This value is being used as the backlog parameter to listen. At least on Linux, this does not limit the number of concurrent connections, because once a connection is accepted, it's no long part of the pending connection queue.

One way to fix this would be to count the number of open connections, and avoid adding the listening socket to the file descriptor set if the count reaches the maximum.

Here's a Python script to reproduce the issue:

import socket

PORT = 8080

def main():
  request = b'GET /darkhttpd.c HTTP/1.1\r\n\r\n'
  sockets = []
  while True:
      s = socket.socket()
      s.settimeout(1)
      s.connect(("", PORT))
      s.send(request)
      data = s.recv(1024)
      sockets.append(s)
      print('{} connections open...'.format(len(sockets)))

if __name__ == '__main__':
    main()
emikulic commented 3 years ago

Yep, you're right.