forked from wangchuan3533/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.h
84 lines (76 loc) · 2.42 KB
/
http.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
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
#ifndef __HTTP_H_
#define __HTTP_H_
#define MAX_HTTP_REQUEST 128
#define HTTP_HEADER_HOST "Host:"
#define HTTP_HEADER_USER_AGENT "User-Agent:"
#define HTTP_HEADER_COOKIE "Cookie:"
#define HTTP_HEADER_CONNECTION "Connection:"
#define HTTP_HEADER_UPGRADE "Upgrade:"
#define HTTP_HEADER_SEC_WEBSOCKET_KEY "Sec-WebSocket-Key:"
#define HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL "Sec-WebSocket-Protocol:"
#define HTTP_HEADER_SEC_WEBSOCKET_VERSION "Sec-WebSocket-Version:"
#define HTTP_RESPONSE_200 \
"HTTP/1.1 200 OK\r\n"\
"Server: WebSocket\r\n"\
"Content-Length: 8\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html\r\n\r\n"\
"It works"
#define HTTP_RESPONSE_400 \
"HTTP/1.1 400 Bad Reqeust\r\n"\
"Server: WebSocket\r\n"\
"Content-Length: 0\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html\r\n\r\n"
#define HTTP_RESPONSE_403 \
"HTTP/1.1 403 Forbidden\r\n"\
"Server: WebSocket\r\n"\
"Content-Length: 0\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html\r\n\r\n"
#define HTTP_RESPONSE_404 \
"HTTP/1.1 404 Not Found\r\n"\
"Server: WebSocket\r\n"\
"Content-Length: 0\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html\r\n\r\n"
#define HTTP_RESPONSE_101 \
"HTTP/1.1 101 Switching Protocols\r\n"\
"Server: WebSocket\r\n"\
"Upgrade: websocket\r\n"\
"Connection: Upgrade\r\n"\
"Sec-WebSocket-Accept: %s\r\n"\
"\r\n"
enum http_request_state_s {
HTTP_REQUEST_STATE_STARTED = 0,
HTTP_REQUEST_STATE_PARSED_FIRST_LINE,
HTTP_REQUEST_STATE_HEADERS_PARSING,
HTTP_REQUEST_STATE_FINISHED,
HTTP_REQUEST_STATE_ERROR,
};
struct http_request_s {
//first line
char *method;
char *request_uri;
char *http_version;
// request
char *host;
char *user_agent;
char *cookie;
char *connection;
char *upgrade;
char *sec_websocket_key;
char *sec_websocket_protocol;
char *sec_websocket_version;
// buffers & buffers count
char *buffers[MAX_HTTP_REQUEST];
size_t count;
// state
http_request_state_t state;
};
http_request_t *http_request_create();
void http_request_destroy(http_request_t *h);
void print_http_request(http_request_t *h);
http_request_state_t http_request_parse(struct evbuffer *b, http_request_t *h);
int send_http_response(struct evbuffer *b, int response);
#endif