-
Notifications
You must be signed in to change notification settings - Fork 9
/
lkcontext.c
261 lines (225 loc) · 6.02 KB
/
lkcontext.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "lklib.h"
#include "lknet.h"
/*** LKContext functions ***/
LKContext *lk_context_new() {
LKContext *ctx = lk_malloc(sizeof(LKContext), "lk_context_new");
ctx->selectfd = 0;
ctx->clientfd = 0;
ctx->type = 0;
ctx->next = NULL;
ctx->client_ipaddr = NULL;
ctx->client_port = 0;
ctx->req_line = NULL;
ctx->req_buf = NULL;
ctx->sr = NULL;
ctx->reqparser = NULL;
ctx->req = NULL;
ctx->resp = NULL;
ctx->buflist = NULL;
ctx->cgifd = 0;
ctx->cgi_outputbuf = NULL;
ctx->cgi_inputbuf = NULL;
ctx->proxyfd = 0;
ctx->proxy_respbuf = NULL;
return ctx;
}
LKContext *create_initial_context(int fd, struct sockaddr_in *sa) {
LKContext *ctx = lk_malloc(sizeof(LKContext), "create_initial_context");
ctx->selectfd = fd;
ctx->clientfd = fd;
ctx->type = CTX_READ_REQ;
ctx->next = NULL;
ctx->client_sa = *sa;
ctx->client_ipaddr = lk_get_ipaddr_string((struct sockaddr *) sa);
ctx->client_port = lk_get_sockaddr_port((struct sockaddr *) sa);
ctx->req_line = lk_string_new("");
ctx->req_buf = lk_buffer_new(0);
ctx->sr = lk_socketreader_new(fd, 0);
ctx->reqparser = lk_httprequestparser_new();
ctx->req = lk_httprequest_new();
ctx->resp = lk_httpresponse_new();
ctx->buflist = lk_reflist_new();
ctx->cgifd = 0;
ctx->cgi_outputbuf = NULL;
ctx->cgi_inputbuf = NULL;
ctx->proxyfd = 0;
ctx->proxy_respbuf = NULL;
return ctx;
}
void lk_context_free(LKContext *ctx) {
if (ctx->client_ipaddr) {
lk_string_free(ctx->client_ipaddr);
}
if (ctx->req_line) {
lk_string_free(ctx->req_line);
}
if (ctx->req_buf) {
lk_buffer_free(ctx->req_buf);
}
if (ctx->sr) {
lk_socketreader_free(ctx->sr);
}
if (ctx->reqparser) {
lk_httprequestparser_free(ctx->reqparser);
}
if (ctx->req) {
lk_httprequest_free(ctx->req);
}
if (ctx->resp) {
lk_httpresponse_free(ctx->resp);
}
if (ctx->buflist) {
lk_reflist_free(ctx->buflist);
}
if (ctx->cgi_outputbuf) {
lk_buffer_free(ctx->cgi_outputbuf);
}
if (ctx->cgi_inputbuf) {
lk_buffer_free(ctx->cgi_inputbuf);
}
if (ctx->proxy_respbuf) {
lk_buffer_free(ctx->proxy_respbuf);
}
ctx->selectfd = 0;
ctx->clientfd = 0;
ctx->next = NULL;
memset(&ctx->client_sa, 0, sizeof(struct sockaddr_in));
ctx->client_ipaddr = NULL;
ctx->req_line = NULL;
ctx->req_buf = NULL;
ctx->sr = NULL;
ctx->reqparser = NULL;
ctx->req = NULL;
ctx->resp = NULL;
ctx->buflist = NULL;
ctx->cgifd = 0;
ctx->cgi_outputbuf = NULL;
ctx->cgi_inputbuf = NULL;
ctx->proxyfd = 0;
ctx->proxy_respbuf = NULL;
lk_free(ctx);
}
// Add new client ctx to end of ctx linked list.
// Skip if ctx clientfd already in list.
void add_new_client_context(LKContext **pphead, LKContext *ctx) {
assert(pphead != NULL);
if (*pphead == NULL) {
// first client
*pphead = ctx;
} else {
// add client to end of clients list
LKContext *p = *pphead;
while (p->next != NULL) {
// ctx fd already exists
if (p->clientfd == ctx->clientfd) {
return;
}
p = p->next;
}
p->next = ctx;
}
}
// Add ctx to end of ctx linked list, allowing duplicate clientfds.
void add_context(LKContext **pphead, LKContext *ctx) {
assert(pphead != NULL);
if (*pphead == NULL) {
// first client
*pphead = ctx;
} else {
// add to end of clients list
LKContext *p = *pphead;
while (p->next != NULL) {
p = p->next;
}
p->next = ctx;
}
}
// Delete first ctx having clientfd from linked list.
// Returns 1 if context was deleted, 0 if no deletion made.
int remove_client_context(LKContext **pphead, int clientfd) {
assert(pphead != NULL);
if (*pphead == NULL) {
return 0;
}
// remove head ctx
if ((*pphead)->clientfd == clientfd) {
LKContext *tmp = *pphead;
*pphead = (*pphead)->next;
lk_context_free(tmp);
return 1;
}
LKContext *p = *pphead;
LKContext *prev = NULL;
while (p != NULL) {
if (p->clientfd == clientfd) {
assert(prev != NULL);
prev->next = p->next;
lk_context_free(p);
return 1;
}
prev = p;
p = p->next;
}
return 0;
}
// Delete all ctx's having clientfd.
//$$ todo: unused, remove this?
void remove_client_contexts(LKContext **pphead, int clientfd) {
int z = 1;
// Keep trying to remove matching clientfd's until none left.
while (z != 0) {
z = remove_client_context(pphead, clientfd);
}
}
// Return ctx matching selectfd.
LKContext *match_select_ctx(LKContext *phead, int selectfd) {
LKContext *ctx = phead;
while (ctx != NULL) {
if (ctx->selectfd == selectfd) {
break;
}
ctx = ctx->next;
}
return ctx;
}
// Delete first ctx having selectfd from linked list.
// Returns 1 if context was deleted, 0 if no deletion made.
int remove_selectfd_context(LKContext **pphead, int selectfd) {
assert(pphead != NULL);
if (*pphead == NULL) {
return 0;
}
// remove head ctx
if ((*pphead)->selectfd == selectfd) {
LKContext *tmp = *pphead;
*pphead = (*pphead)->next;
lk_context_free(tmp);
return 1;
}
LKContext *p = *pphead;
LKContext *prev = NULL;
while (p != NULL) {
if (p->selectfd == selectfd) {
assert(prev != NULL);
prev->next = p->next;
lk_context_free(p);
return 1;
}
prev = p;
p = p->next;
}
return 0;
}