-
Notifications
You must be signed in to change notification settings - Fork 16
/
HttpStorage.Tc
363 lines (320 loc) · 7.81 KB
/
HttpStorage.Tc
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <math.h>
#include "logging.h"
#include "arpc.h"
#include "tame_aio.h"
#include "libconfig.h++"
#include "HttpStorage.h"
#include "tame.h"
#include "tame_io.h"
#include "async.h"
using namespace std;
using namespace tame;
const unsigned int MAX_BUF = 4096;
HttpStorage::HttpStorage(log4cpp::Appender *app, int lighttpd_port)
{
LOG.setAdditivity(false);
LOG.setAppender(app);
port = lighttpd_port;
}
HttpStorage::~HttpStorage()
{
}
tamed void HttpStorage::getFD(cbi ret_int) {
tvars {
int fd;
}
if (conn_pool.empty()) {
twait { tcpconnect ("127.0.0.1", port, mkevent(fd)); }
LOG_DEBUG << "fd: " << fd << "\n";
TRIGGER(ret_int, fd);
} else {
fd = conn_pool.front();
conn_pool.pop();
LOG_DEBUG << "fd: " << fd << "\n";
TRIGGER(ret_int, fd);
}
}
static blob make_blob(const char * str) {
blob to_ret;
bzero(&to_ret, sizeof(blob));
to_ret.setsize(strlen(str));
for(int i=0; i<strlen(str); i++) {
to_ret[i] = str[i];
}
return to_ret;
}
static int xtoi(char *str) {
LOG_INFO << "in xtoi";
int return_val = 0;
int length = strlen(str);
for (int i = 0; i < length; i++) {
int letter = str[length - i - 1];
if (letter <= '9') {
letter -= '0';
} else {
letter -='a';
letter += 10;
}
return_val += letter * (int) pow(16, i);
}
return return_val;
}
tamed void HttpStorage::get(ID_Value key, cb_blob ret_blob) {
tvars {
int fd;
strbuf data;
int rc;
strbuf resp;
int line_len;
char buff[MAX_BUF];
strbuf output;
char *s_out;
str str_out;
char *crlf;
char *crlf2;
char *content;
char *httpHeader;
blob value;
int prev_size;
int count;
int value_size;
}
LOG_INFO << "in http get";
prev_size = 0;
count = 0;
//attempt to connect to the server over and over again until we succeed
//the only reason we might not is that the file descriptor is expired
while (true) {
//get a new file descriptor
twait { getFD (mkevent(fd)); }
if (fd >= 0) {
LOG_DEBUG << "Connection succeeded!\n";
} else {
LOG_DEBUG << "Connection failed!\n";
continue;
}
//construct the GET request
data << "GET /";
data << key.toString().c_str();
data << " HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
LOG_DEBUG << "size of write: " << data.tosuio()->resid() << "\n";
//write/flush the GET request
while(data.tosuio()->resid()) {
twait { tame::waitwrite(fd, mkevent()); }
if (data.tosuio()->output(fd) < 0) {
LOG_DEBUG << "error writing to socket";
twait { getFD (mkevent(fd)); }
}
}
//read the response until we have all of it
while(true) {
twait { tame::waitread (fd, mkevent()); }
if( (rc = resp.tosuio()->input(fd)) < 0 && errno != EAGAIN) {
LOG_DEBUG << "error reading from socket"; //this happens if file descriptor has expired
break;
}
line_len = resp.tosuio()->linelen();
while(line_len > 0) {
if(line_len < MAX_BUF) {
resp.tosuio()->copyout(buff, line_len);
buff[line_len] = '\0';
output << buff;
} else {
LOG_FATAL << "exceeded buffer\n";
}
resp.tosuio()->rembytes(line_len);
line_len = resp.tosuio()->linelen();
}
str_out = str(output);
s_out = (char *) str_out.cstr();
if ((prev_size == strlen(s_out)) || (strlen(s_out) <= 0)) {
count++;
} else {
count = 0;
prev_size = strlen(s_out);
}
if (count > 10) {
LOG_DEBUG << "preemptive return\n";
break;
}
httpHeader = strstr(s_out, "HTTP/1.1");
if (httpHeader == NULL) {
continue;
}
crlf = strstr(httpHeader, "\r\n\r\n");
if (crlf == NULL) {
continue;
}
crlf2 = strstr(crlf + 4, "\r\n");
if (crlf2 == NULL) {
continue;
}
content = crlf2 + 2;
crlf += 4;
*crlf2 = '\0';
value_size = xtoi(crlf);
if (strlen(content) >= value_size) {
content[value_size] = '\0';
value = make_blob(str(content));
conn_pool.push(fd);
TRIGGER(ret_blob, New refcounted<blob>(value));
return;
}
}
}
}
tamed void HttpStorage::set(ID_Value key, const blob* data, cbb ret_blob) {
tvars {
int fd;
strbuf request;
int rc;
strbuf resp;
int line_len;
char buff[MAX_BUF];
strbuf output;
const char *s_out;
str str_out;
char *crlf;
const char *start;
char *end;
}
LOG_INFO << "in http set";
while (true) {
twait { getFD (mkevent(fd)); }
if (fd >= 0) {
LOG_WARN << "Connection succeeded!\n";
} else {
LOG_FATAL << "Connection failed!\n";
}
start = data->base();
end = (char *) (data->base() + data->size());
*end = '\0';
request << "PUT /";
request << key.toString().c_str();
request << " HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-length: ";
request << data->size();
request << "\r\n\r\n";
request << start;
while(request.tosuio()->resid()) {
twait { tame::waitwrite(fd, mkevent()); }
if(request.tosuio()->output(fd) < 0) {
LOG_DEBUG << "error writing to socket\n";
twait { getFD (mkevent(fd)); }
}
}
while(true) {
twait { tame::waitread (fd, mkevent()); }
if( (rc = resp.tosuio()->input(fd)) < 0 && errno != EAGAIN) {
LOG_DEBUG << "error reading from socket\n";
break;
}
line_len = resp.tosuio()->linelen();
while(line_len > 0) {
if(line_len < MAX_BUF) {
resp.tosuio()->copyout(buff, line_len);
buff[line_len] = '\0';
output << buff;
} else {
LOG_FATAL << "exceeded buffer\n";
}
resp.tosuio()->rembytes(line_len);
line_len = resp.tosuio()->linelen();
}
str_out = str(output);
s_out = (char *) str_out.cstr();
crlf = (char *) strstr(s_out, "\r\n\r\n");
if (crlf == NULL && strlen(s_out) > 0) {
cout << "crlf == NULL\n";
cout << s_out << "\n";
continue;
}
conn_pool.push(fd);
TRIGGER(ret_blob, strstr(s_out, "HTTP/1.1 200 OK") != NULL);
return;
}
}
}
tamed void HttpStorage::add(ID_Value key, const blob* data, cbb ret_blob) {
tvars {
ptr<blob> get_result;
bool set_result;
}
twait { get(key, mkevent(get_result)); }
if (get_result == NULL) {
twait { set(key, data, mkevent(set_result)); }
TRIGGER(ret_blob, set_result);
} else {
TRIGGER(ret_blob, false);
}
}
tamed void HttpStorage::replace(ID_Value key, const blob* data, cbb ret_blob) {
tvars {
ptr<blob> get_result;
bool set_result;
}
twait { get(key, mkevent(get_result)); }
if (get_result == NULL) {
TRIGGER(ret_blob, false);
} else {
twait { set(key, data, mkevent(set_result)); }
TRIGGER(ret_blob, set_result);
}
}
tamed void HttpStorage::del(ID_Value key, cbb ret_bool) {
tvars {
int fd;
strbuf request;
int rc;
strbuf resp;
int line_len;
char buff[MAX_BUF];
strbuf output;
const char *s_out;
char *crlf;
}
while (true) {
twait { getFD (mkevent(fd)); }
if (fd >= 0) {
LOG_WARN << "Connection succeeded!\n";
} else {
LOG_FATAL << "Connection failed!\n";
}
request << "DELETE /";
request << key.toString().c_str();
request << " HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
while(request.tosuio()->resid()) {
twait { tame::waitwrite(fd, mkevent()); }
if(request.tosuio()->output(fd) < 0) {
LOG_DEBUG << "error writing to socket\n";
twait { getFD (mkevent(fd)); }
}
}
while(true) {
twait { tame::waitread (fd, mkevent()); }
if( (rc = resp.tosuio()->input(fd)) < 0 && errno != EAGAIN) {
LOG_DEBUG << "error reading from socket\n";
break;
}
line_len = resp.tosuio()->linelen();
while(line_len > 0) {
if(line_len < MAX_BUF) {
resp.tosuio()->copyout(buff, line_len);
buff[line_len] = '\0';
output << buff;
} else {
LOG_FATAL << "exceeded buffer\n";
}
resp.tosuio()->rembytes(line_len);
line_len = resp.tosuio()->linelen();
}
s_out = str(output).cstr();
crlf = (char *) strstr(s_out, "\r\n\r\n");
if (crlf == NULL && strlen(s_out) > 0) {
continue;
}
conn_pool.push(fd);
TRIGGER(ret_bool, strstr(s_out, "HTTP/1.1 200 OK") != NULL);
return;
}
}
}