-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhttpd.h
50 lines (38 loc) · 1.48 KB
/
httpd.h
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
#ifndef _HTTPD_H___
#define _HTTPD_H___
#include <stdio.h>
#include <string.h>
// Client request
extern char *method, // "GET" or "POST"
*uri, // "/index.html" things before '?'
*qs, // "a=1&b=2" things after '?'
*prot, // "HTTP/1.1"
*payload; // for POST
extern int payload_size;
// Server control functions
void serve_forever(const char *PORT);
char *request_header(const char *name);
typedef struct {
char *name, *value;
} header_t;
static header_t reqhdr[17] = {{"\0", "\0"}};
header_t *request_headers(void);
// user shall implement this function
void route();
// Response
#define RESPONSE_PROTOCOL "HTTP/1.1"
#define HTTP_200 printf("%s 200 OK\n\n", RESPONSE_PROTOCOL)
#define HTTP_201 printf("%s 201 Created\n\n", RESPONSE_PROTOCOL)
#define HTTP_404 printf("%s 404 Not found\n\n", RESPONSE_PROTOCOL)
#define HTTP_500 printf("%s 500 Internal Server Error\n\n", RESPONSE_PROTOCOL)
// some interesting macro for `route()`
#define ROUTE_START() if (0) {
#define ROUTE(METHOD, URI) \
} \
else if (strcmp(URI, uri) == 0 && strcmp(METHOD, method) == 0) {
#define GET(URI) ROUTE("GET", URI)
#define POST(URI) ROUTE("POST", URI)
#define ROUTE_END() \
} \
else HTTP_500;
#endif