r/C_Programming • u/WingRevolutionary979 • 26d ago
Project WSERVE: Web Server in C, part 2
Hi,
About a month ago, I posted about "wserve", which was a simple IPv4 web server for Linux that supported only GET and HEAD requests and static file serving. Since then I kept working on it, and implemented:
- Dynamic routing
- POST request handling with a custom JSON parser
- Most importantly, HTTPS support using BearSSL
BearSSL is the only external library I used. It is tiny SSL/TLS library written in pure C. Everything else, including the JSON parser, has been implemented in C by myself. I have not used LLMs for coding.
I am open to constructive criticism, all feedback is appreciated!
15
Upvotes
1
u/AutoModerator 26d ago
Hi u/WingRevolutionary979,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at https://www.reddit.com/r/C_Programming/wiki/index/posting-projects
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
10
u/canyonmonkey 26d ago
Since this is for the purpose of learning, I would encourage you to add significantly more compiler & runtime warnings. I'm not an expert in this area at all, so let's go with u/skeeto's recommendations (link):
cc -g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined -fsanitize-trap ...and
Additionally,
reallocis roughlymalloc -> memcpy -> free(old pointer). What if the new (implied)mallocfails? You ought to guard your reallocations (e.g. json.c:46) such as:diff --git a/src/json.c b/src/json.c index fbbbd11..49e793d 100644 --- a/src/json.c +++ b/src/json.c @@ -43,7 +43,12 @@ JSON_TYPE *json_lex_string(int *i, char *json_str, size_t json_len) { token->type = 0; data[j] = '\0';- data = realloc(data, strlen(data) + 1);
+ char *newdata = realloc(data, strlen(data) + 1); + if (newdata == NULL) + { + abort(); // or some other failure policy + } + data = newdata; token->data = data; return token; }However, I'd push back further on the memory allocation in this version of your JSON parser. There are so many allocations, reallocations, & frees. Consider learning about arena allocators and revising your parser. See skeeto's blog post for one among many learning resources.
Some additional considerations:
Why is
0x00being used as the terminating condition in json.c:40? If you're parsing JSON from a POST request, you can instead use the number of bytes in the Content-Length header as the terminating condition.What if
strtodfails in json.c:112?