Search code examples
chttpserverhttp-headers

Handling HTTP Headers in a Minimal C HTTP Server


I’m working on a minimal HTTP server in C that can listen, bind, accept connections, and handle basic HTTP requests. I understand that HTTP headers are important for communicating additional information between the client and server, but I’m unsure how to properly handle and respond to them.

Here’s a snippet of my code:

void HandleHeaders(int ClientSocket, char *request){
  
  char response[BUFFER_SIZE];
}

void HandleResponse(int ClientSocket, char *request){
  char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHOLA\n";
  write(ClientSocket, response, strlen(response));
}

void HandleRequest(int ServerSocket, int ClientSocket){
    //Obtener los datos recibidos del socket y los almacena en un buffer
    char request [BUFFER_SIZE]; //Buffer
    
    int DatosDelBuffer = recv(ClientSocket, request, sizeof(request), 0);

    //Verificamos el contenido del buffer
    if(DatosDelBuffer < 0 ){
      perror("Error leer el buffer!\n");
      exit(EXIT_FAILURE);
    } else if(strncmp(request,"GET / ",6) == 0){
      printf("%s\n",request); 
      HandleResponse(ClientSocket,request);
    } else if(strncmp(request, "GET /headers",12) == 0){
      HandleHeaders(ClientSocket, request);
    } else {
      char *response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
      write(ClientSocket, response, strlen(response)); 
    }    
    
    //Cierra el Socket y termina la conexion
    close(ClientSocket);
}
  

I’ve tried looking online for examples or explanations, but most results just define HTTP headers without showing how to process them in C.

Questions:

  • How can I properly parse and extract HTTP headers from the request?
  • How should I structure a response that includes custom headers?
  • Are there any best practices for handling HTTP headers in a simple C server?

I appreciate any help or examples!


Solution

  • First of all, don't confuse an HTTP HEAD request method with HTTP request and response headers—these are separate concepts.

    • The HEAD method is similar to GET, but it only requests the headers of a resource without retrieving the actual body content.
    • HTTP headers, on the other hand, are metadata sent along with requests and responses, influencing how the server and client interact.

    To properly handle HTTP headers in your server, refer to the HTTP specifications, which define how your server should react to different request headers. For example:

    1. General HTTP Headers

    • RFC 9110 – HTTP Semantics (Methods, Status Codes, General Headers)
    • RFC 9112 – HTTP/1.1 (Persistent Connections, Transfer-Encoding)
    • RFC 9113 – HTTP/2 (Binary Framing, Multiplexing)
    • RFC 9114 – HTTP/3 (QUIC-based HTTP)

    2. Caching & Conditional Requests

    • RFC 9111 – HTTP Caching (Cache-Control, Expires, ETag, Last-Modified)
    • RFC 7232 – Conditional Requests (If-Modified-Since, If-None-Match)
    • RFC 8297 – Early Hints (103 Early Hints)

    3. Security & Authentication

    • RFC 6797 – HSTS (Strict Transport Security) (Strict-Transport-Security)
    • RFC 7235 – Authentication (WWW-Authenticate, Authorization)
    • RFC 6265 – Cookies (Set-Cookie, Cookie)
    • RFC 9110 – Content Security (Content-Security-Policy, Referrer-Policy)

    4. Content Negotiation

    • RFC 9110 – Media Types & Encoding (Content-Type, Accept, Accept-Encoding)
    • RFC 7838 – Alternative Services (Alt-Svc)

    5. Client & Server Communication

    • RFC 7239 – Forwarded Headers (Forwarded, X-Forwarded-For)
    • RFC 8470 – Early Data (0-RTT) (Early-Data)
    • RFC 8594 – Retry-After (Retry-After)

    6. Miscellaneous

    To implement everything correctly, you will need to consult a multitude of RFCs and check the easily underestimated complexity of open source projects like C-Web-Server or Apache ;-).