#libmicrohttpd

Beni HB9HNTbeni@wirebug.ch
2025-02-21
Today I discovered GNU's #libmicrohttpd. I guess with my mediocre C skills I shouldn't use this for anything facing the web. However, using it for a internal test script that only tells me whether I connected via IPv6 or IPv4 is fun.
I found where the memory leak in GnuTLS happened! I traced it for quite long time to trace it down in multithreaded calls of multiple libraries. it took some time and getting into the libraries internals.

so, the leak happens when gnutls_handshake returns an error. then the internal buffers for handshake headers are not freed properly and leak.
the cheap and dirty solution is to call gnutls_deinit and gnutls_init each time when a session failed a handshake (they may be numerous in long sessions).
this happens only when multiple threads call gnutls_handshake. on a single thread I didn't get any leaks. though, maybe there were no errors because I just had not enough traffic on the socket. though, they claim that gnutls is thread safe. so I'm not sure if everything is all right with such a use. I debug it in libmicrohttpd where I discovered the problem.
the internal fix for GnuTLS doesn't seem that easy. but I will to think on it. maybe the reinitialization from the outside is not that bad.
I will try to make a test example for showing the problem and send bug reports and possible patches to upstream.
#programming #gnutls #libmicrohttpd #memory leaks #bugs #hint #bugfix
I made a patch for libhttpd-http2 that fixes the problem of connection reset after every request in HTTP/1.1 protocol. I'm not sure it's proper, I think I need to discuss this with developers of libmicrohttpd first (the problem exists in the current trunk version).
anyway, the patch:
--- ./src/microhttpd/connection.c       2021-02-06 10:39:20.630410269 +0500
+++ ./src/microhttpd/connection.c       2021-02-06 14:15:02.148711030 +0500
@@ -4539,7 +4539,7 @@
     {
       /* response was queued "early", refuse to read body / footers or
          further requests! */
-      connection->read_closed = true;
+      //connection->read_closed = true; /* fix for HTTP/1.1 connection reuse
       connection->state = MHD_CONNECTION_FOOTERS_RECEIVED;
     connection->remaining_upload_size = 0;
     }

#libmicrohttpd #bugfix #programming #C #patch #opensource
I merged HTTP/2 realization of libmicrohttpd-http2 with up to date libmicrohttpd master branch.
for a while only the library itself is merged, tests are not merged yet. I will merge them later, I think.

I tested the merged library with my AP prototype, it seems working. though, I didn't check all options (they're many!) and this is non-stable master branch of libmicrohttpd, taken as is. and I tested it under Linux, I didn't merge Windows configs (it's not difficult to do but I have no use for them anyway).

though, with this update the speed of HTTP/1.1 and HTTP/2 download tests became relatively equal. they were too different and I thought it was due to outdated version of libmicrohttpd base realization used in libmicrohttpd-http2 variant. so I decided to update the code and merged both realizations.

the merged code is available here:
git.ironbug.org/libmicrohttpd-…

#C # software #libmicrohttpd #http/2 #libmicrohttpd-http2 #opensource #http #server #library #programming #Linux

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst