back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 10c768d8448b78801335fbad6c03acc8b8e84bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Simple HTTP/1.1 Web Server

<img src="http_server.png" />

## Description

This project is a lightweight web server implemented in C, using raw TCP sockets
and the POSIX API. It serves static files from a specified resource directory
and handles multiple clients concurrently in a non-blocking fashion.

### Features

* Implements the HTTP/1.1 protocol (basic subset).
* Handles GET requests for static files.
* Uses non-blocking I/O with select.
* Manages multiple client sessions via a custom session array.
* Basic request parsing with a finite state machine (FSM).
* Logs events using syslog.
* Sends appropriate HTTP responses, including 400 Bad Request and 404 Not Found.

## Technologies Used

* C (ISO C90/C99)
* POSIX sockets API (socket, bind, accept, etc.)
* Non-blocking I/O with select
* TCP/IP Networking
* Syslog logging

## Structure

`struct server`: represents the server and holds the listening socket and
session array. `struct session`: represents a connected client and holds state
and buffers. Custom FSM (`enum fsm_states`) for request parsing and response
generation. Defines for buffer sizes and constants allow easy tuning.

## Usage

Compile with:

```
git clone https://git.scratko.xyz/http-server
cd http-server
gcc -Wall -O2 http_server.c -o http_server
```

Run:
```
./http_server [port] [host] [resource_directory]
```

Example:
```
./http_server 8082 test.scratko.xyz:8082 /var/www/test
```

## Notes

Only basic HTTP/1.1 functionality is supported (no keep-alive, POST, etc.).
Designed for educational and experimental purposes.
Ensure the resource directory and files have proper read permissions.

The http-server directory contains a ``test_files`` subdirectory with the files:
`400.html`, `404.html`, and `index.html`. These are placeholder pages that can
be placed in the resource directory specified when launching the server
(`[resource_directory]`).  If the server starts correctly, you should see
`index.html` in your browser (as shown in the screenshot).

If the browser sends an incorrect GET request, or if you specify an incorrect
host when launching the server — for example:

```
server 8082 test.scrato.xyz:8082 /var/www/test
```

Note the intentional typo in the hostname (scrato instead of scratko).  Then,
when the browser accesses test.scratko.xyz:8082, it will receive the `400.html`
page (Bad Request).

Another case: if you try to access a nonexistent section or file of the site,
for example:

```
test.scratko.xyz:8082/test
```

you will receive the `404.html` page (Page Not Found).