Performance Zone is brought to you in partnership with:

A Programmer, blogger and a keen enthusiast of Big Data and Data Science. A huge fan of massive open online courses (moocs). Pavithra is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

A Web Server in C

  • submit to reddit

I implemented a web server in C language using only the standard libraries and thought it would be useful for you guys if I share the code.

The server runs on Linux and includes features like handling HTTP GET request, handling content types(txt, html, jpg, zip. rar, pdf, php etc.), sending proper HTTP error codes, serving the files from a web root, change in web root in a config file, zero copy optimization using sendfile method and php file handling. A port number should be provided as a command line argument.

After the server is up and running you can request for files using a web browser like Firefox.

For an example assume port number is "9000" and if you want to request a file called "test.php" which is in the webroot, use


 * WebServer.c
 *  Created on: Nov 3, 2012
 *      Author: pavithra
 * A web server in C language using only the standard libraries.
 * The port number is passed as an argument.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>

#define EOL "\r\n"
#define EOL_SIZE 2

typedef struct {
 char *ext;
 char *mediatype;
} extn;

//Possible media types
extn extensions[] ={
 {"gif", "image/gif" },
 {"txt", "text/plain" },
 {"jpg", "image/jpg" },
 {"png", "image/png" },
 {"ico", "image/ico" },
 {"zip", "image/zip" },
 {"gz",  "image/gz"  },
 {"tar", "image/tar" },
 {"htm", "text/html" },
 {"html","text/html" },
 {"php", "text/html" },
 {0,0} };

 A helper function
void error(const char *msg) {

 A helper function
int get_file_size(int fd) {
 struct stat stat_struct;
 if (fstat(fd, &stat_struct) == -1)
  return (1);
 return (int) stat_struct.st_size;

 A helper function
void send_new(int fd, char *msg) {
 int len = strlen(msg);
 if (send(fd, msg, len, 0) == -1) {
  printf("Error in send\n");

 This function recieves the buffer
 until an "End of line(EOL)" byte is recieved
int recv_new(int fd, char *buffer) {
 char *p = buffer; // Use of a pointer to the buffer rather than dealing with the buffer directly
 int eol_matched = 0; // Use to check whether the recieved byte is matched with the buffer byte or not
 while (recv(fd, p, 1, 0) != 0) // Start receiving 1 byte at a time
  if (*p == EOL[eol_matched]) // if the byte matches with the first eol byte that is '\r'
   if (eol_matched == EOL_SIZE) // if both the bytes matches with the EOL
    *(p + 1 - EOL_SIZE) = '\0'; // End the string
    return (strlen(buffer)); // Return the bytes recieved
  } else {
   eol_matched = 0;
  p++; // Increment the pointer to receive next byte
 return (0);

 A helper function: Returns the
 web root location.
char* webroot() {
 // open the file "conf" for reading
 FILE *in = fopen("conf", "rt");
 // read the first line from the file
 char buff[1000];
 fgets(buff, 1000, in);
 // close the stream
 char* nl_ptr = strrchr(buff, '\n');
 if (nl_ptr != NULL)
  *nl_ptr = '\0';
 return strdup(buff);

 Handles php requests
void php_cgi(char* script_path, int fd) {
 send_new(fd, "HTTP/1.1 200 OK\n Server: Web Server in C\n Connection: close\n");
 dup2(fd, STDOUT_FILENO);
 char script[500];
 strcpy(script, "SCRIPT_FILENAME=");
 strcat(script, script_path);
 execl("/usr/bin/php-cgi", "php-cgi", NULL);

 This function parses the HTTP requests,
 arrange resource locations,
 check for supported media types,
 serves files in a web root,
 sends the HTTP error codes.
int connection(int fd) {
 char request[500], resource[500], *ptr;
 int fd1, length;
 if (recv_new(fd, request) == 0) {
  printf("Recieve Failed\n");
 printf("%s\n", request);
 // Check for a valid browser request
 ptr = strstr(request, " HTTP/");
 if (ptr == NULL) {
  printf("NOT HTTP !\n");
 } else {
  *ptr = 0;
  ptr = NULL;

  if (strncmp(request, "GET ", 4) == 0) {
   ptr = request + 4;
  if (ptr == NULL) {
   printf("Unknown Request ! \n");
  } else {
   if (ptr[strlen(ptr) - 1] == '/') {
    strcat(ptr, "index.html");
   strcpy(resource, webroot());
   strcat(resource, ptr);
   char* s = strchr(ptr, '.');
   int i;
   for (i = 0; extensions[i].ext != NULL; i++) {
    if (strcmp(s + 1, extensions[i].ext) == 0) {
     fd1 = open(resource, O_RDONLY, 0);
     printf("Opening \"%s\"\n", resource);
     if (fd1 == -1) {
      printf("404 File not found Error\n");
      send_new(fd, "HTTP/1.1 404 Not Found\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      send_new(fd, "<html><head><title>404 Not Found</head></title>");
      send_new(fd, "<body><p>404 Not Found: The requested resource could not be found!</p></body></html>");
      //Handling php requests
     } else if (strcmp(extensions[i].ext, "php") == 0) {
      php_cgi(resource, fd);
     } else {
      printf("200 OK, Content-Type: %s\n\n",
      send_new(fd, "HTTP/1.1 200 OK\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      if (ptr == request + 4) // if it is a GET request
       if ((length = get_file_size(fd1)) == -1)
        printf("Error in getting size !\n");
       size_t total_bytes_sent = 0;
       ssize_t bytes_sent;
       while (total_bytes_sent < length) {
        //Zero copy optimization
        if ((bytes_sent = sendfile(fd, fd1, 0,
          length - total_bytes_sent)) <= 0) {
         if (errno == EINTR || errno == EAGAIN) {
         return -1;
        total_bytes_sent += bytes_sent;

    int size = sizeof(extensions) / sizeof(extensions[0]);
    if (i == size - 2) {
     printf("415 Unsupported Media Type\n");
     send_new(fd, "HTTP/1.1 415 Unsupported Media Type\r\n");
     send_new(fd, "Server : Web Server in C\r\n\r\n");
     send_new(fd, "<html><head><title>415 Unsupported Media Type</head></title>");
     send_new(fd, "<body><p>415 Unsupported Media Type!</p></body></html>");

 shutdown(fd, SHUT_RDWR);

int main(int argc, char *argv[]) {
 int sockfd, newsockfd, portno, pid;
 socklen_t clilen;
 struct sockaddr_in serv_addr, cli_addr;

 if (argc < 2) {
  fprintf(stderr, "ERROR, no port provided\n");
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 bzero((char *) &serv_addr, sizeof(serv_addr));
 portno = atoi(argv[1]);
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = INADDR_ANY;
 serv_addr.sin_port = htons(portno);
 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  error("ERROR on binding");
 listen(sockfd, 5);
 clilen = sizeof(cli_addr);
  Server runs forever, forking off a separate
  process for each connection.
 while (1) {
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  if (newsockfd < 0)
   error("ERROR on accept");
  pid = fork();
  if (pid < 0)
   error("ERROR on fork");
  if (pid == 0) {
  } else
 } /* end of while */
 return 0; /* we never get here */

Make sure you have installed php and there's exist a "conf" file consisted of the webroot before running this.


Published at DZone with permission of Pavithra Gunasekara, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Claude Lalyre replied on Mon, 2012/11/19 - 5:51am

 And what about Apache ?

Dean Sands replied on Wed, 2012/11/21 - 7:00am

@Claude: What about it? This is a simple web server, implemented in straight C, made to be as lean as possible. That way, folks can look at the code and get an idea on how to write one themselves. This is for educational purposes only. If you think its educational value is insufficient, please, by all means, download the Apache or nginx sources, study them over, and write your own darn article.

Me, I think it's neat. She's even made sure to fork the sessions into different processes which is standard procedure for a socket server. Now, every time some kid wanders into a forum asking how to do a web server (that happens a lot) I can point them to this article here.

Phil Peko replied on Wed, 2012/11/21 - 10:31am in response to: Dean Sands


At least, this code looks nice and neat to me, but I am no Web server expert.

However, it also seems to be a _very_small_part_ of a web server. 

CMIIAW, core web server process is delegated to php libraries, isn't it?

Anyway, sharing this code was interesting.

It should be shared in github/sourceforge, and become an operational open-source project, don't you think so, Pavithra ?

-- Phil

Dean Sands replied on Wed, 2012/11/21 - 11:12pm in response to: Phil Peko


For the PHP bit, well, that's where it get interesting. You need to register the php extension as a known type, then create a process using something like execve to call the php interpreter with the chosen php file. Passing the information depends on whether it's a GET or a POST.

If I remember correctly, GET commands pass the information via environment variables whereas POST is passed into the new process' stdin. The PHP interpreter processes it then passes the information back out through stdout.

This is passed back to the web server which passes it back to the client.

If you want to try it yourself, setup a simple Apache/nginx server, then configure a home-made program as an interpreter for a silly made-up file type like .artichoke. Have a look at what gets set in the environment variables and what is passed into stdin.

Oh, hey. I didn't notice she'd already set it up for PHP. Pretty cool!

Jeremy Sandell replied on Wed, 2012/11/21 - 6:57pm

It would appear that you have a memory leak, and a pretty nasty one, at that. Each time a request is made, char* webroot(void); is called, which returns the result of strdup - but the result is never passed to void free(void*); 

It's also worth mentioning that there's no need to open the config file (and allocate memory from the heap to store its contents) for each request; this sort of thing could easily be cached once, when the server is run. Moreover, I noticed that there's no check to see if the file was successfully opened (e.g., something like if((in = fopen("conf", "rt")) == NULL) { /* handle error... */ })  

All in all, I'd recommend curious readers take a look at nginx's source. While rolling your own server is certainly a worthwhile experiment, it's usually best to learn from code that's been vetted and tested thoroughly. To the author, thanks for the hard work - but next time, please point valgrind at your compiled source prior to publishing it as learning/reference material. (:

Victor Flores replied on Fri, 2014/06/13 - 2:02am

I Know, I Know, this post is very old. But I think that it's very important to warn: You should've used strncpy() and strncat() instead of strcpy() and strcat(), just to avoid security problems! I know, the project have educational purpose, but if a student or a kid are trying to learn how to do that and take a look at your code, that kid will not perceive the mistake and will reproduce it! Anyway, congratulations for this program and you should put it onto github! 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.