README.md (2148B)
1 # httpp 2 Tiny, simple, yet useful header only dynamic HTTP/1.1 requests parser for C. 3 4 ## API 5 6 ```c 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #define HTTPP_IMPLEMENTATION 11 #include "httpp.h" 12 13 char* req = 14 "POST /api/items HTTP/1.1\r\n" 15 "Host: api.example.com\r\n" 16 "User-Agent: MyClient/1.0\r\n" 17 "Content-Type: application/json\r\n" 18 "Content-Length: 48\r\n" 19 "\r\n" 20 "{\"name\":\"Widget\",\"quantity\":10,\"price\":9.99}"; 21 22 int main() 23 { 24 httpp_req_t* parsed = httpp_parse_request(req); 25 printf("%s\n", parsed->body); // parsed, malloc'd body 26 printf("%i\n", parsed->method); // Method is an enum! 27 28 const char* method = httpp_method_to_string(parsed->method); // Method as string (e.g POST) 29 printf("%s\n", method); 30 31 httpp_header_t* host = httpp_find_header(parsed, "Host"); 32 printf("%s\n", host->value); // api.example.com 33 34 httpp_res_t* response = httpp_res_new(); 35 response->code = Ok; // or 200 36 httpp_add_header(response, "Host", "somehost.some.where"); 37 httpp_add_header(response, "Status", "ok"); 38 39 httpp_res_set_body(response, "Some body"); 40 char* raw = httpp_res_to_raw(response); // Convert to a malloc'd raw string 41 42 printf("\nComposed response: \n"); 43 printf("----\n%s\n----\n", raw); // Or write it to a socket 44 45 // Don't forget to free everything 46 httpp_req_free(parsed); // Free parsed request 47 httpp_res_free(response); // Free response structure 48 free(raw); // Free composed raw response 49 } 50 ``` 51 52 ## Benchmark 53 All benchmarks were compiled with -O3 flag using gcc _15.2.1 20251112_. Benchmarks were running on a Ryzen 7 with 4.79GHz peek frequency. The code can be found [here](./bench.c). Same benchmark was adapted for http-parser. Results of each one is the average of 5 runs 54 55  56 57 | http-parser | httpp | picohttpparser | 58 | ----------- | ----------- | -------------- | 59 | `5.344527s` | `3.058964s` | `2.005615s` | 60 61  62 63 | http-parser | httpp | picohttpparser | 64 | ------------- | ------------- | -------------- | 65 | `1871702.306` | `3269700.736` | `4986831.824` |