-
Notifications
You must be signed in to change notification settings - Fork 0
/
jorgeNetwork.cpp
327 lines (292 loc) · 10.5 KB
/
jorgeNetwork.cpp
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
#include <jorgeLua.h>
#include <jorgeNetwork.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
// get sockaddr, IPv4 or IPv6:
void *jnet_get_req_ip(struct sockaddr *sa){
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
// Linked list of buffers to store recv data
struct buffer_node{
char data[JNET_REQ_BUFF_SIZE];
struct buffer_node* next;
};
// Receives a connected socket and receives all data until timeout
// or until no data is coming.
struct jnet_request_data jnet_read_request(int conn, char **request){
ssize_t totalBytes = 0;
struct buffer_node *first_buffer = (struct buffer_node*) malloc(sizeof *first_buffer);
struct buffer_node *last_buffer = first_buffer;
last_buffer->next = NULL;
last_buffer->data[0] = '\0';
int loops = 0;
struct timeval begin;
gettimeofday(&begin , NULL);
while (true) {
loops++;
struct timeval now;
gettimeofday(&now , NULL);
double elapsedSeconds = (now.tv_sec - begin.tv_sec) + 1e-6 * (now.tv_usec - begin.tv_usec);
//if you got some data, then wait a little more time
if( totalBytes > 0 && elapsedSeconds > JNET_RECV_TIMEEXTRA ) break;
//if you got no data at all, wait timeout
else if( elapsedSeconds > JNET_RECV_TIMEOUT) break;
if(!last_buffer->next){
last_buffer->next = (struct buffer_node*) malloc(sizeof *last_buffer->next);
last_buffer->next->next = NULL;
last_buffer->next->data[0] = '\0';
}
ssize_t recvBytes = recv(conn, last_buffer->data, JNET_REQ_BUFF_SIZE-1, MSG_DONTWAIT);
// NONPORTABLE MSG_DONTWAIT makes single call non blocking, but is only supported on linux
if(recvBytes > 0) {
totalBytes += recvBytes;
// printf("%d: \n%s\n(%zu out of %d) -1:%d\n", loops, last_buffer->data, recvBytes, JNET_REQ_BUFF_SIZE-1, last_buffer->data[recvBytes]);
last_buffer->data[recvBytes] = '\0';
last_buffer = last_buffer->next;
gettimeofday(&begin, NULL);
}
else usleep(100000); // Wait a little if nothing was received
}
//printf("bytes: %zu\n", totalBytes);
*request = (char *)malloc((totalBytes+1) * (sizeof *request));
char *endofstring = (*request);
struct buffer_node *walker;
walker = first_buffer;
while(walker != NULL){
strcpy(endofstring, walker->data);
endofstring += strlen(walker->data);
// printf("----------------------------------\n");
// printf("\nSOURCE := %s\n", walker->data);
// printf("\nTHIS := %s\n", endofstring);
// printf("\nFULL := %s\n", (*request));
last_buffer = walker;
walker = walker->next;
free(last_buffer);
}
free(walker);
// TODO Http parser
/* At this point the whole request should be in `char *request`
* In jorgeNetwork.h there is both the request struct and the enum
* for the parser state machine.
* The main idea here is to take advantage of the contiguous request string
* and only set the pointers on the struct to those memory locations.
* TODO Also refactor this function so it returns the struct instead of the string
*/
struct jnet_request_data req_data;
req_data.verb = NULL;
req_data.path = NULL;
req_data.header = NULL;
req_data.body = NULL;
struct jnet_request_header *req_header_walker = NULL;
int httpParserState = jnet_parser_state_verb_begin;
int parserIndex = 0;
while(httpParserState != jnet_parser_state_halt){
char *thisChar = &((*request)[parserIndex]);
char caractere = *thisChar;
switch (httpParserState){
case jnet_parser_state_verb_begin:
if(*thisChar == ' ' || *thisChar == '\t'){}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z')
){
req_data.verb = thisChar;
httpParserState = jnet_parser_state_verb_end;
}else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_verb_end:
if(*thisChar == ' ' || *thisChar == '\t'){
*thisChar = '\0';
httpParserState = jnet_parser_state_path_begin;
}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z')
){}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_path_begin:
if(*thisChar == ' ' || *thisChar == '\t'){}
else if(
(*thisChar == '/')
){
req_data.path = thisChar;
httpParserState = jnet_parser_state_path_end;
}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_path_end:
if(*thisChar == ' ' || *thisChar == '\t'){
*thisChar = '\0';
httpParserState = jnet_parser_state_version;
}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z') ||
(*thisChar >= '0' && *thisChar <= '9') ||
(*thisChar == '/')||(*thisChar == '.') ||
(*thisChar == '+')||(*thisChar == '-') ||
(*thisChar == '=')||(*thisChar == '?') ||
(*thisChar == '~')||(*thisChar == '%')
){}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_version:
if(*thisChar == ' ' || *thisChar == '\t') parserIndex++;
else if(
toupper(*(thisChar)) == 'H' &&
toupper(*(thisChar+1)) == 'T' &&
toupper(*(thisChar+2)) == 'T' &&
toupper(*(thisChar+3)) == 'P' &&
*(thisChar+4) == '/' &&
*(thisChar+5) == '1' &&
*(thisChar+6) == '.' &&
((*(thisChar+7)=='0') || (*(thisChar+7)=='1')) && // 0 or 1
*(thisChar+8) == '\r' &&
*(thisChar+9) == '\n'
){
req_data.version = *(thisChar+7);
httpParserState = jnet_parser_state_field_begin;
parserIndex += 10;
}
else{
httpParserState = jnet_parser_state_err;
parserIndex++;
}
break;
case jnet_parser_state_field_begin:
if(*thisChar == ' ' || *thisChar == '\t'){}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z') ||
(*thisChar >= '0' && *thisChar <= '9') ||
(*thisChar == '-')||(*thisChar == '.') ||
(*thisChar == '+')||(*thisChar == '*')
){
if(req_data.header == NULL){
req_data.header = (struct jnet_request_header*) malloc(sizeof(*req_data.header));
req_header_walker = req_data.header;
}else{
req_header_walker->next = (struct jnet_request_header*) malloc(sizeof(*req_data.header));
req_header_walker = req_header_walker->next;
}
req_header_walker->next = NULL;
req_header_walker->field = thisChar;
httpParserState = jnet_parser_state_field_end;
}
else if((*thisChar == '\r' && *(thisChar+1) == '\n')){
req_data.body = thisChar+2;
parserIndex += 2;
httpParserState = jnet_parser_state_halt;
}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_field_end:
if(*thisChar == ' ' || *thisChar == '\t'){
*thisChar = '\0';
}
else if(*thisChar == ':'){
*thisChar = '\0';
httpParserState = jnet_parser_state_value_begin;
}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z') ||
(*thisChar >= '0' && *thisChar <= '9') ||
(*thisChar == '-')||(*thisChar == '.') ||
(*thisChar == '+')||(*thisChar == '*')
){}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_value_begin:
if(*thisChar == ' ' || *thisChar == '\t'){}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z') ||
(*thisChar >= '0' && *thisChar <= '9') ||
(*thisChar == '-')||(*thisChar == '.') ||
(*thisChar == '/')||(*thisChar == '?') ||
(*thisChar == '+')||(*thisChar == '*')
){
req_header_walker->value = thisChar;
httpParserState = jnet_parser_state_value_end;
}
else{
httpParserState = jnet_parser_state_err;
}
parserIndex++;
break;
case jnet_parser_state_value_end:
if(*thisChar == '\r' && *(thisChar+1) == '\n'){
*thisChar = '\0';
httpParserState = jnet_parser_state_field_begin;
parserIndex += 2;
}
else if(
(*thisChar >= 'A' && *thisChar <= 'Z') ||
(*thisChar >= 'a' && *thisChar <= 'z') ||
(*thisChar >= '0' && *thisChar <= '9') ||
(*thisChar == '-')||(*thisChar == '.') ||
(*thisChar == '/')||(*thisChar == '?') ||
(*thisChar == '+')||(*thisChar == '*') ||
(*thisChar == ':')||(*thisChar == '=') ||
(*thisChar == '%')||(*thisChar == '&') ||
(*thisChar == '(')||(*thisChar == ')') ||
(*thisChar == '_')||(*thisChar == ';') ||
(*thisChar == ' ')||(*thisChar == '\t')||
(*thisChar == ',')||(*thisChar == '\n')
) parserIndex++;
else{
httpParserState = jnet_parser_state_err;
parserIndex++;
}
break;
case jnet_parser_state_err:
// Cleanup
printf("Error parsing request\n");
default:
httpParserState = jnet_parser_state_halt;
}
}
return req_data;
}
// Receives string of data and a connected socket and sends it all.
// This is necessary because sometimes the packets don't get sent entirely.
ssize_t jnet_send_all(int connection, char *buffer, size_t *length, int isLast){
size_t bytesSent = 0;
ssize_t bytesNow = 0;
while(bytesSent < *length){
bytesNow = send(connection,
buffer+bytesSent, (*length)-bytesSent ,
isLast?0:MSG_MORE);
if(bytesNow == -1) break;
bytesSent += bytesNow;
}
*length = bytesSent;
return bytesNow==-1?-1:0;
}