From 9df891b409a462bdc210ff447a84c974464b7a2e Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Sun, 20 Jul 2025 20:44:04 +0200 Subject: [PATCH] Update README --- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b28b0d4..e125d66 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,65 @@ This is an HTTP client and server library for C. -# Use Cases +Here are some examples of how it looks like on the client and server. If you want to learn more, go through the files in `examples/` (they are intended to be skimmed in order). + +Here is a client performing a GET request: +```c +#include + +int main(void) +{ + http_global_init(); + + HTTP_String headers[] = { + HTTP_STR("User-Agent: cHTTP"), + }; + + HTTP_RequestHandle handle; + HTTP_Response *res = http_get( + HTTP_STR("http://example.com/index.html"), + headers, HTTP_COUNT(headers), + &handle + ); + + fwrite(res->body.ptr, 1, res->body.ptr, stdout); + + http_request_free(handle); + http_global_free(); + return 0; +} +``` + +And this is an HTTP server: +```c +#include + +int main(void) +{ + HTTP_Server *server = http_server_init(HTTP_STR("127.0.0.1"), 8080); + + for (;;) { + + HTTP_Request *req; + HTTP_ResponseHandle res; + http_server_wait(server, &res, &res); + + HTTP_String path = req->url.path; + printf("requested path [%.*s]\n", HTTP_UNPACK(req->url.path)); + + http_response_status(res, 200); + http_response_header(res, "Content-Type: text/plain"); + http_response_body(res, HTTP_STR("Hello")); + http_response_body(res, HTTP_STR(", world!")); + http_response_done(res); + } + + http_server_free(server); + return 0; +} +``` + +## Use Cases cHTTP is perfect for tooling or production environments of limited scale (up to about 1000 concurrent connections). To scale it further, users can take cHTTP's I/O independant HTTP state machine and use it in conjunction with more scalable I/O solutions (see examples/engine).