Context from core#19420:
> This PR calls evhttp_free before releasing event base. But according to evhttp_free docs:
>
> > Works only if no requests are currently being served.
>
> So this PR also tracks active requests with libevent and waits for last request to finish. This requires libevent 2.1 due to evhttp_request_set_on_complete_cb (https://github.com/libevent/libevent/blob/master/whatsnew-2.1.txt).
>
> Finally, the call to evhttp_free is done in the event base loop to avoid concurrency issues.
>
> Now test/functional/feature_abortnode.py quits normally, not due to socket timeouts.
>
> http: Track active requests and wait for last to finish
https://github.com/bitcoin/bitcoin/pull/26742/commits/8c6d007c80dc3fec5ce6c0196381444a5ed7e424
> http: Release server before waiting for event base loop exit
https://github.com/bitcoin/bitcoin/pull/26742/commits/660bdbf785a32024f0694915fa043968a0afb573
> test: Reduce extended timeout on abortnode test
>
> This was made obsolete by tracking the active requests and explicitly waiting for them to finish before shutdown.
https://github.com/bitcoin/bitcoin/pull/26742/commits/60978c8080ec13ff4571c8a89e742517b2aca692
> http: refactor: use encapsulated HTTPRequestTracker
>
> Introduces and uses a HTTPRequestTracker class to keep track of
> how many HTTP requests are currently active, so we don't stop the
> server before they're all handled.
>
> This has two purposes:
> 1. In a next commit, allows us to untrack all requests associated
> with a connection without running into lifetime issues of the
> connection living longer than the request
> (see https://github.com/bitcoin/bitcoin/pull/27909#discussion_r1265614783)
>
> 2. Improve encapsulation by making the mutex and cv internal members,
> and exposing just the WaitUntilEmpty() method that can be safely
> used.
https://github.com/bitcoin/bitcoin/pull/28551/commits/41f9027813f849a9fd6a1479bbb74b9037990c3c
> http: log connection instead of request count
>
> There is no significant benefit in logging the request count instead
> of the connection count. Reduces amount of code and computational
> complexity.
https://github.com/bitcoin/bitcoin/pull/28551/commits/68f23f57d77bc172ed39ecdd4d2d5cd5e13cf483
> http: bugfix: track closed connection
>
> It is possible that the client disconnects before the request is
> handled. In those cases, evhttp_request_set_on_complete_cb is never
> called, which means that on shutdown the server we'll keep waiting
> endlessly.
>
> By adding evhttp_connection_set_closecb, libevent automatically
> cleans up those dead connections at latest when we shutdown, and
> depending on the libevent version already at the moment of remote
> client disconnect. In both cases, the bug is fixed.
https://github.com/bitcoin/bitcoin/pull/28551/commits/68f23f57d77bc172ed39ecdd4d2d5cd5e13cf483#
This is a backport of core#26742 and core#28551
Depends on D17303