Added simple HTTPD server in C

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-03-11 22:32:56 +0100
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-03-11 22:32:56 +0100
Commit 29fb1c172b2b235a6484f23f8d5b391f877e98eb (patch)
-rw-r--r-- README.md 1
-rw-r--r-- c-httpd/.gitignore 1
-rw-r--r-- c-httpd/Makefile 7
-rw-r--r-- c-httpd/httpd.c 25
-rw-r--r-- c-httpd/httpserver.h 2818
5 files changed, 2852 insertions, 0 deletions
diff --git a/README.md b/README.md
...
22
| [c-embed](./c-embed)                   | clang-17   | Embedding external resources in compiled binary.               |
22
| [c-embed](./c-embed)                   | clang-17   | Embedding external resources in compiled binary.               |
23
| [c-signals](./c-signals)               | clang-17   | Uses SIGUSR1 and SIGUSR2 as IPC mechanism.                     |
23
| [c-signals](./c-signals)               | clang-17   | Uses SIGUSR1 and SIGUSR2 as IPC mechanism.                     |
24
| [c-structs](./c-structs)               | clang-17   | Saves and reads structs in/from binary files.                  |
24
| [c-structs](./c-structs)               | clang-17   | Saves and reads structs in/from binary files.                  |
  
25
| [c-httpd](./c-httpd)                   | clang-17   | Simple HTTP server written in C.                               |
25
| [zig-c-interop](./zig-c-interop)       | zig-0.11.0 | Uses functions written in C from Zig code.                     |
26
| [zig-c-interop](./zig-c-interop)       | zig-0.11.0 | Uses functions written in C from Zig code.                     |
26
| [zig-ppm](./zig-ppm)                   | zig-0.11.0 | Creates an image with random pixels in PPM image format.       |
27
| [zig-ppm](./zig-ppm)                   | zig-0.11.0 | Creates an image with random pixels in PPM image format.       |
27
| [zig-struct-json](./zig-struct-json)   | zig-0.11.0 | Serialization of a struct into JSON and then reading it back.  |
28
| [zig-struct-json](./zig-struct-json)   | zig-0.11.0 | Serialization of a struct into JSON and then reading it back.  |
...
diff --git a/c-httpd/.gitignore b/c-httpd/.gitignore
  
1
httpd
diff --git a/c-httpd/Makefile b/c-httpd/Makefile
  
1
all: httpd
  
2
  
  
3
httpd: httpd.c
  
4
	gcc httpd.c -o httpd
  
5
  
  
6
clean:
  
7
	-rm httpd
diff --git a/c-httpd/httpd.c b/c-httpd/httpd.c
  
1
#define EPOLL
  
2
#define HTTPSERVER_IMPL
  
3
#include "httpserver.h"
  
4
  
  
5
#define SERVER_PORT 6970
  
6
#define RESPONSE "default response"
  
7
  
  
8
void handle_request(struct http_request_s* request) {
  
9
	http_string_t url = http_request_target(request);
  
10
  
  
11
	printf("%s\n", url.buf);
  
12
  
  
13
	struct http_response_s* response = http_response_init();
  
14
	http_response_status(response, 200);
  
15
	http_response_header(response, "Content-Type", "text/html");
  
16
	http_response_body(response, RESPONSE, sizeof(RESPONSE) - 1);
  
17
	http_respond(request, response);
  
18
}
  
19
  
  
20
int main() {
  
21
	printf("> starting server on %d\n", SERVER_PORT);
  
22
  
  
23
	struct http_server_s* server = http_server_init(SERVER_PORT, handle_request);
  
24
	http_server_listen(server);
  
25
}
diff --git a/c-httpd/httpserver.h b/c-httpd/httpserver.h
Changes too big to display