Skip to content
This repository has been archived by the owner on Jul 23, 2022. It is now read-only.
/ web-server-in-c Public archive

A concurrent web server from scratch in C with effecting logging and error handling.

License

Notifications You must be signed in to change notification settings

sheharyaar/web-server-in-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

forthebadge forthebadge forthebadge

The project is now archived as it was a learning project.

Building

  1. Go to src/ folder
  2. Run make : make
  3. Run server with the proper options : ./server host port server-logfile client-logfile
  4. To clean : make clean

forthebadge

Supported capabilities

Server capabilities

  • HTTP Request Pipelining
  • Concurrent connections upto a limit defined by MAX_CLIENT
  • Client timeout
  • Logging for both Server and Client events
  • Error handler for both server and client errors
  • Exit handler to free the client list and data

HTTP Capabilities

  • GET, HEAD and POST Methods
  • Supported HTTP Codes : 200, 400, 404, 411, 413, 414, 500, 501, 505

Codebase Notes

Symbols used

// Universal Defines
#define TRUE 1
#define FALSE 0

// Server Defines
#define MAX_CLIENT 1024
#define REQ_PIPELINE 10

// Logger Defines
#define MAX_LOG_BUF 512

// Request Defines
#define REQ_LEN 8096
#define URI_LEN 1024
#define METHOD_LEN 10
#define VERSION_LEN 10

// Response Defines
#define RESP_LEN 8096
#define RESP_CODE_LEN 56
#define RESP_LINE_LEN (RESP_CODE_LEN + 16)
#define DATE_LEN 30

// Error defines
#define INFO 0
#define WARN 1
#define ERR 2

// Define Methods here
#define GET 0
#define HEAD 1
#define POST 2

Data Structures

Linked List to store connected Client information. Linked List seamed feasible as the max concurrent clients are by default 1024, so it seems not much effect on performance.

typedef struct client{
	int cfd;
	ssize_t readData;
	struct client *client_next;
	char host[NI_MAXHOST];
	char respBuf[RESP_LEN];
	char reqBuf[REQ_PIPELINE*REQ_LEN];
} CLIENT;

cfd : File descriptor for the client
readDate : Number of bytes of data read from the descriptor
host : String containing the hostname respBuf : Buffer to contain the response message reqBuf : Buffer to contain the request messagea client_next : Pointer to the next client node

Structure to store request messages :

typedef struct request {
	char method[METHOD_LEN];
	char url[URI_LEN];
	char version[VERSION_LEN];
	char **headers;
} REQUEST;

method : Contains Request method string - GET, HEAD, POST, etc.
url : URI for the source
version : String to contain HTTP Version
headers : a pointer to an array of strings containing the remaining headers

Structure to store response messages :

typedef struct response{
	int version;
	int code;
	char code_str[RESP_CODE_LEN];
	char **headers;
} RESPONSE;

version : HTTP Version of the response used to construct response message line
code : Response code
code_str : Description for the code
headers : a pointer to an array of strings containing the remaining headers

Functions

  • wait_on_client: implements select() for synchronous connections.
  • get_client() : implements accept() and adds the client to the linked list.
  • drop_client() : remove client on error and remove from the client list.
  • send_code() : generic function to send error codes as response.

About

A concurrent web server from scratch in C with effecting logging and error handling.

Topics

Resources

License

Stars

Watchers

Forks