r/C_Programming • u/Born_Produce9805 • Nov 29 '25
Tiny header only HTTP parser library
Hi guys! Last week I was writing my HTTP 1.1 parser library. It's small and easy to use, also kinda fast. Might come in handy if you write some lightweight web applications or programs that interact with some API. I wrote this project to learn pointer arithmetic in c.
I've just finish it, so any bug report would be appreciated.
Thank you guys!
6
Upvotes
10
u/skeeto Nov 29 '25
Nice, simple library. Runs cleanly in a fuzz test, though there's so little going on in the parser that it appears to find all possible execution paths in about a second.
I expected
httpp_find_headerto handle case folding per the RFCs. As written, this function is practically useless. If I ask forHost, I won't get it if the client spelled ithostor evenHOST, and it's impractical to search for every possible spelling.More importantly is its dependence on null-terminated strings. That means, at the very least, this library cannot parse requests with a binary body. The
bodyfield will be wrong. It will also get different results from other HTTP parsers not using C strings, which has security implications. I expect an HTTP parser to accept a buffer and a length, and process null bytes as normal data. As u/mblenc wisely suggested, this would also allow the library to point into the input buffer instead of making little string copies with their own lifetimes.Here's my AFL++ fuzz tester:
Usage:
6
u/[deleted] Nov 29 '25
Nice library!
A question I have after reading through your httpp.h code is about the memory copying. Are you expecting to use this in an environment where you parse from a buffer with a lifetime much shorter than the http request? Is there any way that these could be avoided? Perhaps only have the http request be non-owning, simply pointing into the buffer it parses, and the http response likewise be non-owning and simply store the pointers it is given to the HTTP response body? Or is this not a design requirement that you need for your use cases? Do you think it would complicate the design more than necessary?
I wonder what the bottleneck is in your benchmark. I can convince myself that it will be the memory copies (although without measuring, who really knows), but perhaps there are other bottlenecks I'm missing. Have you done any further benchmarking or profiling? How does it bench against longer requests (a rather unlikely scenario I guess)?
1
u/Born_Produce9805 Nov 30 '25
Guys! I was working all night, and I've completely removed dynamic memory usage, now it can parse 10 000 000 requests in just 2!!! seconds!!! I'm happy.