cserver

Simple C server

Home | Concept | Usage | Tests


Project maintained by Deybacsi Hosted on GitHub Pages — Theme by mattgraham

C language

One of the main purposes of this project is to demonstrare C language, its possibilities, and its usage.

Because of this, the project was separated into multiple source code files to demonstrate:

The number of files and functions has been kept as minimal as possible, to maintain code readability and help its easy understanding.

There is a compile.sh script to easily compile and run the server

Configurability

The server’s main properties should be (somewhat) easily reconfigured in the future.

The below properties can be freely modified directly in http.c

// server main configuration 
const int   PORT=8080,                  // listening port
            MAXCONNS=10,                // max no of connections
            BUFFERLENGTH=10000;         // buffer size for request/response and key value read

const char *KEYSPACEDIR="./serverdb";   // directory path for storing key-value pairs

Concurrent connections

The server uses the fork() function to duplicate itself on an incoming request, and the main instance continues with listening.

The forked copy (worker) will continue running with checkRequest(), and hopefully with the sendResponse() function if no fatal errors occured. (For eg. client unexpectedly closed connection)

Parsing HTTP requests - checkRequest()

Accepted request body format:

Incoming HTTP request are in plain text, so strtok() was used to split the input string, and parse the GET/PUT request type, and the key=value from the request body.

Storing / reading the data - checkRequest()

The key storage is accessible (by default) in the serverdb directory. As the project’s specification states, key names can contain only alphanumerical characters, so the easiest and fastest storing method was implemented:

(This method should not be used on NTFS or FAT filesystems, it can cause heavy slowdowns)

PUT requests always overwrite existing key values.

This method makes it easy to check / edit / delete the stored keys with external programs.

Sending response - sendResponse()

It composes the final response string (HTTP headers + body) and sends it back to the client.

The response is as simple as possible (1 line string), but JSON or XML output can be generated easily.

Error handling

The code tries to handle the following issues:

Except of the first 2 cases, every error both sends