Simple C server
Home | Concept | Usage | Tests
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
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
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)
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.
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.
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.
The code tries to handle the following issues:
GET requests
PUT requests
Except of the first 2 cases, every error both sends