-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.c
153 lines (120 loc) · 3.29 KB
/
connection.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "connection.h"
#include "logger.h"
#include "server.h"
struct connection *new_connection(int fd, struct server_thread *thread)
{
struct connection *conn;
bug_on(fd < 0);
conn = memory_pool_alloc(&thread->conn_pool);
if (!conn)
return NULL;
list_init(&conn->list);
conn->fd = fd;
conn->users = 1;
conn->thread = thread;
http_request_init(&conn->request);
http_response_init(&conn->response);
log_debug("new connection 0x%p %d\n", conn, fd);
return conn;
}
static void __close_connection(struct connection *conn)
{
if (conn->fd >= 0) {
log_debug("close connection 0x%p %d\n", conn, conn->fd);
close(conn->fd);
conn->fd = -1;
}
}
void close_connection(struct connection *conn)
{
__close_connection(conn);
}
static void free_connection(struct connection *conn)
{
log_debug("free_connection 0x%p\n", conn);
bug_on(!list_empty(&conn->list));
bug_on(conn->users);
close_connection(conn);
http_request_reset(&conn->request);
http_response_reset(&conn->response);
bug_on(conn->fd >= 0);
memory_pool_free(&conn->thread->conn_pool, conn);
}
void get_connection(struct connection *conn)
{
conn->users++;
}
void put_connection(struct connection *conn)
{
bug_on(!conn->users);
conn->users--;
if (!conn->users)
free_connection(conn);
}
int connection_on_read(struct connection *conn, int *close)
{
int r;
*close = 0;
if (conn->fd < 0) {
r = EINVAL;
goto out;
}
while (conn->request.state == HTTP_STATE_READING) {
r = http_request_read(&conn->request, conn->fd, close);
if (*close)
break;
if (r)
break;
}
if (conn->request.state == HTTP_STATE_READ_COMPLETE) {
r = http_handler(&conn->request, &conn->response, close);
if (r)
goto out;
while (conn->response.state == HTTP_STATE_WRITING) {
r = http_response_write(&conn->response, conn->fd, close);
if (*close)
goto out;
if (r)
goto out;
}
if (conn->response.state == HTTP_STATE_WRITE_COMPLETE) {
if (strncmp(conn->request.connection, "close", strlen("close") + 1) == 0) {
*close = 1;
}
http_stat_request(&conn->thread->stats, &conn->request, &conn->response);
http_request_reset(&conn->request);
http_response_reset(&conn->response);
}
}
out:
return r;
}
int connection_on_write(struct connection *conn, int *close)
{
int r;
*close = 0;
r = 0;
if (conn->fd < 0) {
r = EINVAL;
goto out;
}
while (conn->response.state == HTTP_STATE_WRITING) {
r = http_response_write(&conn->response, conn->fd, close);
if (*close)
break;
if (r)
break;
}
if (conn->response.state == HTTP_STATE_WRITE_COMPLETE) {
if (strncmp(conn->request.connection, "close", strlen("close") + 1) == 0) {
*close = 1;
}
#ifdef __STATS__
http_stat_request(&conn->thread->stats, &conn->request, &conn->response);
#endif
http_request_reset(&conn->request);
http_response_reset(&conn->response);
}
out:
return r;
}