1

I am writing a very simple web server in C. I was wondering what HTTP requests I should handle from a browser. I have looked at websites such as Mozilla's HTTP Header docs, but this doesn't tell me exactly what browsers are going to be sending. And handling every request header field is very overkill for what I am attempting to do.

If someone could just point me in the right direction on what I need to handle that would be perfect.

One of the basic headers I have seen requested is:

GET / HTTP/1.1
Host: localhost:3000
Connection: keep-alive
sec-ch-ua: "Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

But the header is different for things like images or files. So I was wondering if there is a standard of what fields will be used for these different requests?

Any help, or even just a link, would be much appreciated.

3
  • 3
    datatracker.ietf.org/doc/html/rfc2616 Commented Dec 13, 2023 at 22:07
  • When in doubt, open the RFC. Iłya has a link. Commented Dec 13, 2023 at 22:31
  • 1
    You don't need to handle every header, but you do need to handle and reply to every request. But you don't have to support every request you are given. If a client sends you something you don't recognize or implement, simply reply back with an error saying so. Commented Dec 13, 2023 at 22:37

2 Answers 2

1

Be warned, it is not an easy task to write a http server from scratch. If you still insist to implement one, then make sure to make the best out of the request and respond accordingly (based on the recommendation, aka standard).

Sign up to request clarification or add additional context in comments.

1 Comment

To make easier, implement only HTTP/1.0.
1

Regarding the question on what requests you might receive, you might need to look at the RFCs for the HTTP protocol[1], in special RFC 9110[2].

There's no easy way of doing it, you'll have to implement a bare minimum of request methods (see section 9 of the RFC 9110), I think at least GET, HEAD, etc. Maybe POST you could treat like GET to avoid the burden, and in fact some web servers leave it to the upper level (i.e. CGI interface, PHP, etc) to treat the processing of POST requests.

Regarding the HTTP fields, the 18.4 section of the RFC tells you what are the existing ones, but you might use it according to your needs to provide a minimum response. I mean, you don't have to treat each of them, you'll do it according to your request. If the browser send you a GET /index.html, you won't bother about the User-Agent field, unless you need to respond according to it.

In the reality, the HTTP fields you'll take into account to form your response are quite dependent on what you're trying to achieve. For instance, if you want to support CGI, all you're going to do is to send this information to the CGI program through the standard input. If you have a mod_php of sorts, it's going to work the same. But if you're just serving static HTML, you can just ignore most of them.

You can use encoding and language information to decide which files to serve, for instance, but that's only if you need on an application level.

However, I think you could use a library already implementing it, like libhttp[1], for instance. It will do the hard work for you so you can worry about your application instead of protocol implementation. I'm not sure it would attend to your requirements, though, since they are not clear.

REFERENCES

[1] HTTP Protocol. Wikipedia. https://en.wikipedia.org/wiki/HTTP

[2] RFC 9110. https://datatracker.ietf.org/doc/html/rfc9110

[3] libhttp homepage. https://www.libhttp.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.