shttp

A small http server written in C

git clone git://git.janzachar.dev/shttp.git


0#pragma once
1#include <sys/socket.h>
2#include <netinet/in.h>
3#include <stdio.h>
4
5#define printe(...) fprintf(stderr, __VA_ARGS__)
6
7#define push(len, ...) { \
8	ptr += snprintf(ptr, (len) - (ptr-buff), __VA_ARGS__); \
9	if (ptr - buff > (len)) return -1; \
10}
11
12
13struct sock {
14	int fd;
15	struct sockaddr_in addr;
16	socklen_t len;
17};
18
19void sock_fill(struct sock*, const char*, int);
20void sock_open(struct sock*);
21void sock_on(struct sock*, int (*func)(int));
22
23
24struct hs {
25	struct header {
26		const char *key;
27		const char *value;
28	} *item;
29
30	int head;
31	int size;
32};
33
34void header_push(struct hs*, const char*, const char*);
35const char *header_find(struct hs*, const char*);
36struct header header_pop(struct hs*);
37void header_free(struct hs*);
38
39
40struct HTTP_request {
41	const char *method;
42	char *path;
43	const char *version;
44
45	struct hs headers;
46};
47
48
49struct HTTP_response {
50	int code;
51	const char *version;
52	struct hs headers;
53};
54
55int request(struct HTTP_request*, char**);
56
57int reply(int, int, struct header[]);
58int freply(int, int, struct header[], FILE*);
59int sreply(int, int, struct header[], char*, int);
60
61long getsize(FILE*);
62
63int handle(int);
64int GET(int, struct HTTP_request*, FILE*, const char*);
65int HEAD(int, struct HTTP_request*, FILE*, const char*);
66
67
68// settings.c
69extern const char *IP;
70extern const int PORT;
71extern const int LISTEN_COUNT;
72extern const char *ROOT_DIR;
73
74extern const int HEADER_BLOCK;
75extern const char *TIMESTAMP_FORMAT;
76