test.c (2669B)
1 #include <stdio.h> 2 3 #define HTTPP_IMPLEMENTATION 4 #include "httpp.h" 5 6 void print(httpp_req_t* parsed) 7 { 8 if (parsed == NULL) { 9 printf("Couldn't parse!\n"); 10 return; 11 } 12 13 printf("Method: %i (%s)\n", parsed->method, httpp_method_to_string(parsed->method)); 14 printf("Route: %s\n", parsed->route); 15 16 printf("Parsed headers length: %lu\n", parsed->headers->length); 17 for (size_t i = 0; i < parsed->headers->length; i++) { 18 printf("Header %lu:\n", i); 19 printf(" Name: :%s:\n", parsed->headers->arr[i].name); 20 printf(" Value: :%s:\n", parsed->headers->arr[i].value); 21 } 22 23 printf("\n---- Parsed body: ----\n"); 24 printf("%s\n", parsed->body); 25 printf("-------- (%lu) --------\n", strlen(parsed->body)); 26 } 27 28 int main() 29 { 30 char* req1 = 31 "POST /api/items HTTP/1.1\r\n" 32 "Host: api.example.com\r\n" 33 "User-Agent: MyClient/1.0\r\n" 34 "Content-Type: application/json\r\n" 35 "Content-Length: 48\r\n" 36 "\r\n" 37 "{\"name\":\"Widget\",\"quantity\":10,\"price\":9.99}"; 38 39 httpp_req_t* parsed = httpp_parse_request(req1); 40 print(parsed); 41 httpp_req_free(parsed); 42 43 char* req2 = 44 "POST /api/items HTTP/1.1\r\n" 45 "Host: api.example.com\r\n" 46 "User-Agent: MyClient/1.0\r\n" 47 "Content-Type: application/json; charset=utf-8\r\n" 48 "Content-Length: 106\r\n" 49 "X-Trace-ID: ;;--TRACE--;;\r\n" 50 "X-Feature-Flags: ,enable-new, ,\r\n" 51 "\r\n" 52 "{\"name\":\"SpicyWidget\",\"quantity\":-1,\"price\":9.9900,\"tags\":[\"hot\",\"ßpecial\",\"null\",null],\"meta\":{\"note\":\"line1\\nline2\\r\\nline3\",\"empty\":\"\",\"unicode\":\"🔥🚀\",\"quote_test\":\"She said: \\\"Spicy!\\\"\"}}"; 53 54 parsed = httpp_parse_request(req2); 55 print(parsed); 56 httpp_req_free(parsed); 57 58 char* req3 = 59 "POST /api/items HTTP/1.1\r\n" 60 "Host: api.example.com\r\n" 61 "User-Agent: MyClient/1.0\r\n" 62 "Content-Type: application/json; charset=utf-8\r\n" 63 "Content-Length: 106\r\n" 64 "X-Trace-ID: ;;--TRACE--;;\r\n" 65 "X-Feature-Flags: ,enable-new, ,\r\n" 66 "\r\n"; 67 68 printf("--- req3 ---\n"); 69 parsed = httpp_parse_request(req3); 70 print(parsed); 71 printf("--- end ---\n"); 72 73 httpp_res_t* res = httpp_res_new(); 74 75 res->code = 200; 76 httpp_headers_arr_add(res->headers, "Host", "idk.me.com"); 77 httpp_headers_arr_add(res->headers, "Home", "pkeofkwekgfwktokwt9wt293430592304"); 78 httpp_headers_arr_add(res->headers, "SOmethin", "afkofkeokfoekfo"); 79 80 httpp_res_set_body(res, "{\"hello\": 123}\n"); 81 82 char* raw = httpp_res_to_raw(res); 83 printf("-----------\n"); 84 printf("%s", raw); 85 printf("-----------\n"); 86 87 return 0; 88 }