commit f7d799c324344983f90b740e10efd3ff23c883f5
parent 3574bd278f24d75b079722a3b5ad263d12133ef9
Author: cebem1nt <mineewarik@gmail.com>
Date: Thu, 27 Nov 2025 23:24:16 -0300
Small changes and improvements. Trying to optimize everything
Diffstat:
3 files changed, 39 insertions(+), 35 deletions(-)
diff --git a/benchmark.c b/benchmark.c
@@ -21,7 +21,7 @@
"Cookie: name=wookie\r\n" \
"\r\n"
-#define ITERATIONS 1000000
+#define ITERATIONS 10000000
double now_seconds(void)
{
diff --git a/httpp.h b/httpp.h
@@ -10,7 +10,6 @@
*/
#include <stddef.h>
-#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -31,10 +30,6 @@
strcmp(s, "POST") == 0 ? 1 : \
strcmp(s, "DELETE") == 0 ? 2 : -1)
-#define HTTPP_ERRMEMRY 3
-#define HTTPP_ERRLOGIC 2
-#define HTTPP_ERRDEFLT 1
-
typedef enum {
GET,
POST,
@@ -118,34 +113,41 @@ int httpp_res_set_body(httpp_res_t* res, char* body); // strdup and res->body =
void httpp_res_free(httpp_res_t* res);
char* httpp_res_to_raw(httpp_res_t* res);
+#define HTTPP_IMPLEMENTATION
#ifdef HTTPP_IMPLEMENTATION
-#define trim(str) do { ltrim(str); rtrim(str); } while (0)
-
-#define ltrim(str) do { \
- while(*(str) && isspace(*(str))) { \
- (str)++; \
- } \
+#define ltrim(str) do { \
+ while (*(str) && *(str) == ' ' || *(str) == '\r' || *(str) == '\n') { \
+ (str)++; \
+ } \
} while (0)
-#define rtrim(str) do { \
- char* __str = (str); \
- char* end = __str + strlen(__str) - 1; \
- while (end >= __str && isspace(*end)) { \
- *end-- = '\0'; \
- } \
+#define lltrim(str, len) do { \
+ while (*(str) && *(str) == ' ' || *(str) == '\r' || *(str) == '\n') { \
+ (str)++; \
+ (len)--; \
+ } \
} while (0)
+#define rtrim(str, len) do { \
+ char* __str = (str); \
+ char* end = __str + len; \
+ while (end >= __str && *end == ' ' || *end == '\r' || *end == '\n') { \
+ *end-- = '\0'; \
+ len--; \
+ } \
+} while (0)
+
static int chop_until(char c, char** src, char* dest, size_t n)
{
char* pos = strchr(*src, c);
if (!pos)
- return HTTPP_ERRDEFLT;
+ return 1;
size_t chopped_size = pos - *src;
if (chopped_size >= n)
- return HTTPP_ERRDEFLT;
+ return 1;
memcpy(dest, *src, chopped_size);
dest[chopped_size] = '\0';
@@ -324,10 +326,10 @@ httpp_header_t* httpp_parse_header(httpp_headers_arr_t* hs, char* line)
memcpy(name, line, name_len);
name[name_len] = '\0';
- trim(name);
+ ltrim(name);
char* value_start = delim_pos + 1;
- trim(value_start);
+ ltrim(value_start);
char* value = strdup(value_start);
if (!value)
@@ -344,24 +346,24 @@ static int parse_start_line(char** itr, httpp_req_t* dest)
ltrim(*itr);
if (chop_until(' ', itr, method_buf, HTTPP_MAX_METHOD_LENGTH) != 0)
- return HTTPP_ERRDEFLT;
+ return 1;
ltrim(*itr); // Route might have extra spaces at the bginning, for our implementation thats fine
if ((route = dchop_until(' ', itr)) == NULL)
- return HTTPP_ERRDEFLT;
+ return 1;
ltrim(*itr);
- if (chop_until('\n', itr, version_buf, HTTPP_VERSION_BUFSIZE) != 0) {
+ if (chop_until('\r', itr, version_buf, HTTPP_VERSION_BUFSIZE) != 0) {
free(route);
- return HTTPP_ERRDEFLT;
+ return 1;
}
- rtrim(version_buf);
if (strcmp(version_buf, HTTPP_SUPPORTED_VERSION) != 0) {
free(route);
- return HTTPP_ERRDEFLT;
+ return 1;
}
+ ltrim(*itr); // remove \n left after version
dest->method = (httpp_method_t) httpp_string_to_method(method_buf);
dest->route = route;
return 0;
diff --git a/test.c b/test.c
@@ -50,23 +50,25 @@ int main()
"X-Feature-Flags: ,enable-new, ,\r\n"
"\r\n"
"{\"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!\\\"\"}}";
-
+
parsed = httpp_parse_request(req2);
print(parsed);
httpp_req_free(parsed);
char* req3 =
"POST /api/items HTTP/1.1\r\n"
- "Host: api.example.com\r\n"
- "User-Agent: MyClient/1.0\r\n"
- "Content-Type: application/json; charset=utf-8\r\n"
- "Content-Length: 106\r\n"
- "X-Trace-ID: ;;--TRACE--;;\r\n"
- "X-Feature-Flags: ,enable-new, ,\r\n"
+ " Host: api.example.com\r\n"
+ " User-Agent : MyClient/1.0\r\n"
+ " Content-Type: application/json; charset=utf-8\r\n"
+ " Content-Length: 106\r\n"
+ " X-Trace-ID: ;;--TRACE--;;\r\n"
+ " X-Feature-Flags: ,enable-new, ,\r\n"
"\r\n";
+ printf("--- req3 ---\n");
parsed = httpp_parse_request(req3);
print(parsed);
+ printf("--- end ---\n");
httpp_res_t* res = httpp_res_new();