Http Server in C

Here’s the translation of the HTTP server example from Go to C, formatted in Markdown suitable for Hugo:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8090
#define BUFFER_SIZE 1024

void handle_hello(int client_socket) {
    char *response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhello\n";
    send(client_socket, response, strlen(response), 0);
}

void handle_headers(int client_socket, char *request) {
    char response[BUFFER_SIZE];
    char *header_start = strstr(request, "\r\n") + 2;
    char *header_end = strstr(header_start, "\r\n\r\n");
    
    snprintf(response, sizeof(response), "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
    send(client_socket, response, strlen(response), 0);
    
    char *header = strtok(header_start, "\r\n");
    while (header != NULL && header < header_end) {
        snprintf(response, sizeof(response), "%s\n", header);
        send(client_socket, response, strlen(response), 0);
        header = strtok(NULL, "\r\n");
    }
}

void handle_request(int client_socket) {
    char buffer[BUFFER_SIZE];
    recv(client_socket, buffer, sizeof(buffer), 0);
    
    if (strncmp(buffer, "GET /hello", 10) == 0) {
        handle_hello(client_socket);
    } else if (strncmp(buffer, "GET /headers", 12) == 0) {
        handle_headers(client_socket, buffer);
    } else {
        char *response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
        send(client_socket, response, strlen(response), 0);
    }
    
    close(client_socket);
}

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t addr_len = sizeof(client_addr);
    
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket == -1) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(PORT);
    
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("Bind failed");
        exit(EXIT_FAILURE);
    }
    
    if (listen(server_socket, 10) < 0) {
        perror("Listen failed");
        exit(EXIT_FAILURE);
    }
    
    printf("Server listening on port %d\n", PORT);
    
    while (1) {
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_len);
        if (client_socket < 0) {
            perror("Accept failed");
            continue;
        }
        
        handle_request(client_socket);
    }
    
    close(server_socket);
    return 0;
}

This C program implements a basic HTTP server similar to the original example. Let’s break down the key components:

  1. We include necessary headers for socket programming and standard I/O operations.

  2. The handle_hello function sends a simple “hello” response to the client.

  3. The handle_headers function reads the HTTP request headers and echoes them back to the client.

  4. The handle_request function processes incoming requests, routing them to the appropriate handler based on the URL path.

  5. In the main function, we set up a socket, bind it to a port, and enter a loop to accept and handle incoming connections.

To compile and run the server:

$ gcc http_server.c -o http_server
$ ./http_server
Server listening on port 8090

You can then access the server using curl or a web browser:

$ curl localhost:8090/hello
hello

$ curl localhost:8090/headers
User-Agent: curl/7.68.0
Accept: */*

This C implementation provides similar functionality to the original example, demonstrating how to create a basic HTTP server using socket programming in C.