Readit News logoReadit News
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
Joker_vD · 4 months ago

    // it doesn't seem to love piping or redirecting output without this, even
    // with the newlines above
    fflush(stdout);
Ah, the full buffering mode. I believe it can be fixed by calling

    setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
once at the start.

On the whole, it actually almost implements the minimally required amount of HTTP/1.1: I would suggest adding support for HEAD requests, it's just a single flag that you need to set in the try_parse_request_path(), and check in generate_response(). Also, probably check that the request path is followed by "HTTP/1." before sending the response? And I'd really recommend finishing reading out all of the request from the socket (that is, until you've seen "\r\n\r\n"), or you may run into the problem of your clients not being sent the complete response [0].

But other than that, yeah, it is an HTTP server. The HTTP protocol is decently well thought out so that you can be oblivious of most of the features you don't want to support.

[0] https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-... — the tl;dr is that if you do close() on a socket that still has the data from the client you haven't recv()d, the client will be sent an RST.

GSGBen · 4 months ago
Ah yep, I read about the TCP RST problem in one of the RFC docs, then promptly forgot about it and never implemented anything to avoid it. Thankyou for the detailed notes.
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
lionkor · 4 months ago
I've got a similar one, but with http 1.0 and partial 1.1 support, multi threaded, etc. in C

https://GitHub.com/lionkor/http

GSGBen · 4 months ago
Noice!
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
kjellsbells · 4 months ago
around line 663. there's a call to strrchr, checking for a period in the filename. then immediately after that, there's a strlen that uses the results.

Which is fine, unless the first call returns NULL, because there was no period in the name, and then the program will crash.

GSGBen · 4 months ago
Oof, thanks.
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
GSGBen · 4 months ago
Should be back up now with a very temporary workaround in place.
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
GSGBen · 4 months ago
Found the issue - a use after free in send_response() if I close the session early due to an error. Was continuing to the next bit. Put a temp fix in place, will push a proper one later.
GSGBen · 4 months ago
Still seems to have an issue, but no output before the crash. Will have to do some more debugging. Thanks for the test HN!

Source is here btw: https://github.com/GSGBen/unsafehttp/blob/main/src/main.c

GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
joncfoo · 4 months ago
Doesn't seem to be up =\
GSGBen · 4 months ago
Found the issue - a use after free in send_response() if I close the session early due to an error. Was continuing to the next bit. Put a temp fix in place, will push a proper one later.
GSGBen commented on Show HN: unsafehttp – tiny web server from scratch in C, running on an orange pi   unsafehttp.benren.au... · Posted by u/GSGBen
joncfoo · 4 months ago
Doesn't seem to be up =\
GSGBen · 4 months ago
Whoops, should be back up now. I'll have to check logs later to see why it went down.

u/GSGBen

KarmaCake day261January 26, 2020
About
https://twitter.com/GSGBen
View Original