A minimal and educational HTTP web framework written in pure C using POSIX sockets.
It provides a simple foundation for building web servers and handling routing, from managing raw TCP connections to parsing HTTP requests and generating responses.
Inspired by the simplicity of Go’s net/http package.
I`ll be glad to any ideas or contributions — feel free to open an issue or PR!
#include "http.h"
void simple_handler(response *resp, request *req){
resp->status_code = HTTP_STATUS_OK;
if (strcmp(req->uri, "/home") == 0) {
int fd = open("index.html", O_RDONLY);
ssize_t n = read_all(fd, resp->body, sizeof(resp->body));
close(fd);
add_header(&resp->headers, "Content-Type", "text/html");
char len_buf[32];
sprintf(len_buf, "%zd", n);
add_header(&resp->headers, "Content-Length", len_buf);
}else{
send_404(resp, req);
}
}
int main(){
listen_and_serve("localhost:8080", simple_handler);
}microhttpc provides:
- A blocking TCP server using
accept()andpthreadfor concurrency. - A simple HTTP/1.1 parser that fills a
requeststruct with the method, URI, headers, and body. - A lightweight header map for O(1) lookups.
- A
handle_funccallback that receives bothrequest *reqandresponse *resp.
Inside the handler, you fill theresponse— setstatus_code, add headers, and write the body.
The framework automatically serializes and sends it back to the client. - Built-in helpers like
send_404(),send_file(), andsend_500(). - Optional Keep-Alive and per-connection read timeouts.
No external dependencies — just pure C and POSIX.
- Build a basic TCP server from scratch
- Handle multiple clients using
fork() - Implement HTTP/1.1 request parsing
- Implement persistent connections (Keep-Alive)
- Add MIME type detection
- Implement HTTP response helpers
void send_404(response *r);
void send_500(response *r);
void send_file(response *r, const char *path);
- Improve headers convenience
- Add routing system
- Add logging and configuration
- Graceful shutdown (SIGINT)
- Explore multithreading with
pthread