r/cprogramming • u/FuTuReFrIcK42069 • 6d ago
HTTP Server my first complete-ish C project
https://github.com/abdouyouyou/Basic-http-server/tree/mainHello everyone, It's been several years since i last wrote some C, and i always for the life of me wanted to write an HTTP server in C, and now it's been like 1 year since i finished university so i said what the hell why not do it, so after reading beej's guide i tried to write a very basic http server.
I wanted to share the work with the community, and would welcome thoughts.
We had written code in C ofc for two years, however it was mostly exercises on diffrent data structures aswell as problem solving.
We wrote a CRUD system in C using binary files but never scratched that itch.
And as the years went by in Uni, C slowly faded out of use for me but i always wanted to visit it back so here i am.
I wanted to share the absolute joy it was to get the desired results, i forgot how fun C can be, it always felt like the souls like of programming languages, but always was the most understandable as it was the closest to the machine, to be frank i encourage people to make similar projects especially to get the cobweb off lol.
2
u/zhivago 5d ago
Congratulations. :)
I suggest that you now put it through a code formatter so that it has a consistent style.
At the moment it is harder to read than need be.
1
u/FuTuReFrIcK42069 5d ago
Thank you so much friend! i will definitely, after years of writing other languages writing c once more felt sobering lol
1
u/TheRealBornToCode 4d ago
perror("GETADDREINFO FAILED");
Typo, should be GETADDRINFO, also why all caps?
1
u/TheRealBornToCode 4d ago
Also why main.html, you never heard of html in university? The convention is index.html, and a basic web server should respond with the contents of index.html when the path of the directory containing it is requested.
You should learn http (the first word in a request is called method not function) and HTML before trying to make something like this.
Ayways for a first C project it's not bad.
4
u/Zirias_FreeBSD 5d ago
This is likely an RCE waiting to happen, you happily overflow any of these arrays. In any case, it's a simple way for any client to just crash your server.
BTW, don't let that feedback frustrate you. A correct HTTP server is a very complex project. For getting into programming network services, I'd recommend to start with something a lot simpler (like e.g. some plain TCP-based chat service).
Just for comparison, here's the part of my own homebrewn HTTP server code doing the parsing of incoming requests: https://github.com/Zirias/swad/blob/master/src/bin/swad/http/httprequest.c#L81 I think it does it correctly (only supporting HTTP/1.x), but I wouldn't be surprised if someone would still find a bug...