In the era of IoT and edge computing, embedded devices require lightweight and efficient HTTP service capabilities.
Traditional HTTP servers like Nginx and Apache, while powerful, consume too many resources and are difficult to meet the demands of embedded scenarios. Today, we introduce an open source HTTP library designed specifically for embedded systems—libevhtp.
Project address: https://github.com/Yellow-Camper/libevhtp

1. Introduction to libevhtp
libevhtp is a high-performance HTTP library developed based on libevent, written in C, and designed for resource-constrained environments. It provides a simple API interface and supports building efficient HTTP servers and clients, particularly suitable for embedded Linux environments.


Core Features
-
High-performance event-driven: Asynchronous I/O based on libevent
-
Minimal resource usage: Only a few hundred KB after compilation
-
Zero-copy design: Efficient handling of network data
-
Flexible routing system: Supports RESTful style routing
-
HTTPS support: Integrated OpenSSL for encrypted communication
Advantages and Disadvantages
Advantages:
- Millisecond-level startup time
- Low memory usage (<1MB)
- High concurrency connection handling capability
- Simple and easy-to-use API design
Disadvantages:
- Basic functionality (no advanced HTTP features)
- Relatively small community ecosystem
- Documentation completeness needs improvement
2. Practical Use of libevhtp
1. Install Dependencies
# Install dependencies
sudo apt install libevent-dev libssl-dev
# Compile and install
git clone https://github.com/Yellow-Camper/libevhtp.git
cd libevhtp/build
cmake .. -DEVHTP_DISABLE_SSL=ON # Disable SSL to simplify compilation
make
sudo make install
2. Simple HTTP Server
Flowchart:

Code:
#include <evhtp.h>
#include <string.h>
#include <stdio.h>
// Request handling callback
void home_cb(evhtp_request_t* req, void* arg)
{
const char* response = "<h1>Welcome to libevhtp!</h1>"
"<p>Embedded HTTP Server</p>";
printf("Received request!!!\n");
evbuffer_add(req->buffer_out, response, strlen(response));
evhtp_send_reply(req, EVHTP_RES_OK);
}
int main(void)
{
printf("================= Embedded HTTP Server Demo =================\n");
// Create base event loop
struct event_base* base = event_base_new();
// Initialize evhtp instance
evhtp_t* htp = evhtp_new(base, NULL);
// Register root path handler/route registration
evhtp_set_cb(htp, "/", home_cb, NULL);
// Bind port
evhtp_bind_socket(htp, "0.0.0.0", 8080, 1024);
// Start event loop
event_base_loop(base, 0);
// Resource cleanup
evhtp_unbind_socket(htp);
evhtp_free(htp);
event_base_free(base);
return 0;
}
-
Create base event loop: event_base_new() creates the base event loop for libevent, which is the core environment for libevhtp to run and supports subsequent network event handling.
-
Initialize evhtp instance: evhtp_new() initializes the HTTP processing context and associates it with the previously created event_base, establishing the foundation for the entire HTTP service.
-
Route registration: evhtp_set_cb() binds the URL path to the handler function, mapping the root path / to the home_cb function, allowing the corresponding handling logic to be triggered when accessing the root path.
-
Server binding: evhtp_bind_socket() specifies the IP and port the server listens on, with the last parameter being the backlog queue size, which sets the maximum number of waiting connections.
-
Start event loop: libevhtp runs.
-
Request handling callback: The content to be responded to is added to the output buffer using evbuffer_add, and then evhtp_send_reply is used to send the response, completing the handling of a request.
Compile command:
gcc demo.c -o demo -levhtp -levent
Run the demo to start the HTTP server, then enter the address in the browser: http://localhost:8080.

This concludes our sharing. In the next article, we will learn about the code of libevhtp together.

END
Source:Embedded Miscellany
Copyright belongs to the original author. If there is any infringement, please contact for deletion..▍Recommended ReadingWhy C++ is Rarely Used in Microcontroller Development?5 Recommended Lightweight Microcontroller File SystemsCrash! Encountered Another Microcontroller Crash Case→ Follow for more updates ←