-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
320 lines (270 loc) · 9.47 KB
/
utils.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
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
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "httpProcessing/commonHTTP.h"
#include "httpProcessing/realtimePacketParser.h"
#include "net/networking.h"
#include "net/connection.h"
#include "commonDefines.h"
#include "google/googleAccessToken.h"
#include "httpProcessing/commonHTTP.h"
#include "httpProcessing/realtimePacketParser.h"
#include "httpProcessing/createHTTPHeader.h"
char* strstrn_nonConst(char* const haystack, const char* needle, const int haystackSize) {
if (haystackSize < strlen(needle))
return NULL;
int i;
for (i = 0; i < (haystackSize - (strlen(needle) - 1)); i++) {
int j;
for (j = 0; j < strlen(needle); j++)
if (needle[j] != haystack[i + j])
break;
if (j == strlen(needle))
return haystack + i;
}
return NULL;
}
const char* strstrn(const char* const haystack, const char* needle, const int haystackSize) {
if (haystackSize < strlen(needle))
return NULL;
int i;
for (i = 0; i < (haystackSize - (strlen(needle) - 1)); i++) {
int j;
for (j = 0; j < strlen(needle); j++)
if (needle[j] != haystack[i + j])
break;
if (j == strlen(needle))
return haystack + i;
}
return NULL;
}
//FIXME: this shouldn't be case sensitive
//just like utils_parseUrl but it gives you the protocol in type protocol_t
//returns 0 if valid url, -1 otherwise
//TODO: should work with lower case too and there shouldn't be so much hardcoded stuff
int utils_parseUrl_proto(const char *inputUrl, protocol_t *type, const char **domain,
int *domainLength, const char **fileUrl, int *fileUrlLength) {
int protoSize, protocolLength, returnCode;
const char *protocolStr;
returnCode = utils_parseUrl(inputUrl, &protocolStr, &protocolLength, domain,
domainLength, fileUrl, fileUrlLength);
if (returnCode == 0) {
return 0;
}
protoSize = protocolLength;
if (strncmp(protocolStr, "https",
(protoSize > strlen("https")) ? protoSize : strlen("https")) == 0) {
*type = PROTO_HTTPS;
} else if (strncmp(protocolStr, "http",
(protoSize > strlen("http")) ? protoSize : strlen("http")) == 0) {
*type = PROTO_HTTP;
} else {
printf("ERROR: BAD PROTOCOL\n");
*type = PROTO_HTTP;
}
return 1;
}
//returns 1 if valid url, 0 otherwise
//fileUrl can be 0 in length
//fileUrl is everything after ".com"
//inputUrl must be a null terminated string
int utils_parseUrl(const char *inputUrl, const char **protocol, int *protocolLength,
const char **domain, int *domainLength, const char **fileUrl, int *fileUrlLength) {
int remaining;
const char *dotPtr;
const char *protoEndPtr;
//get protocol
//go till '://'
protoEndPtr = strstrn(inputUrl, "://", strlen(inputUrl));
if (protoEndPtr == NULL) {
return 0;
}
*protocol = inputUrl;
*protocolLength = protoEndPtr - inputUrl;
*domain = protoEndPtr + strlen("://");
remaining = strlen(inputUrl) - (*domain - inputUrl);
//get the first occurrence of a "."
dotPtr = strstrn(*domain, ".", remaining);
if (dotPtr == NULL) {
return 0;
}
//now try to get the first occurrence of a single "/"
*fileUrl = strstrn(dotPtr, "/", remaining);
//if that failed then the url doesn't end in a "/"
//and the fileUrlLength will be zero
if (*fileUrl == NULL) {
*fileUrl = inputUrl + strlen(inputUrl);
*fileUrlLength = 0;
*domainLength = strlen(inputUrl) - (*domain - inputUrl);
} else {
*fileUrlLength = strlen(inputUrl) - (*fileUrl - inputUrl);
*domainLength = (*fileUrl - *domain);
}
return 1;
}
//creates a hIfno get request for the the url
void utils_setHInfoFromUrl(const char *inputUrl, HTTPHeaderState_t *hInfo,
const httpRequestTypes_t requestType, const char *extraHeaders) {
protocol_t type;
const char *fileUrl, *domain;
int fileUrlLength, domainLength;
utils_parseUrl_proto(inputUrl, &type, &domain, &domainLength, &fileUrl,
&fileUrlLength);
hInfo->isRequest = 1;
hInfo->requestType = requestType;
//FIXME: there should really be some sort of setUrlBuffer and setHostBuffer
memcpy(hInfo->urlBuffer, fileUrl, fileUrlLength);
hInfo->urlBuffer[fileUrlLength] = '\0';
memcpy(hInfo->hostBuffer, domain, domainLength);
hInfo->hostBuffer[domainLength] = '\0';
}
//FIXME: CONST THIS STUFF !
void getConnectionStructByUrl(const char *inputUrl, Connection_t *con) {
protocol_t type;
const char *fileUrl, *domain;
int fileUrlLength, domainLength;
utils_parseUrl_proto(inputUrl, &type, &domain, &domainLength, &fileUrl,
&fileUrlLength);
if (type == PROTO_HTTP) {
con->type = PROTO_HTTP;
} else if (type == PROTO_HTTPS) {
con->type = PROTO_HTTPS;
}
}
//returns an active tcp connection to the url
void utils_connectByUrl(const char *inputUrl, Connection_t *con) {
protocol_t type;
const char *fileUrl, *domain;
char domainBuffer[MAX_DOMAIN_SIZE];
int fileUrlLength, domainLength;
net_setupNewConnection(con);
getConnectionStructByUrl(inputUrl, con);
utils_parseUrl_proto(inputUrl, &type, &domain, &domainLength, &fileUrl,
&fileUrlLength);
sprintf(domainBuffer, "%.*s", domainLength, domain);
net_connect(con, domainBuffer);
}
//This function allows the content length to already be set,
//if the content length is not set then the packet must not have any data (other than the header)
int utils_createHTTPHeaderFromUrl(char *inputUrl, char *output,
int maxOutputLen, HTTPHeaderState_t *hInfo,
const httpRequestTypes_t requestType, char *extraHeaders) {
utils_setHInfoFromUrl(inputUrl, hInfo, requestType, extraHeaders);
return createHTTPHeader(output, maxOutputLen, hInfo, extraHeaders);
}
//gets the whole next http packet
//FIXME: handle cases where the outputBuffer isn't big enough
//returns the amount of data written to outputData
int utils_recvNextHttpPacket(Connection_t *con, HTTPHeaderState_t *outputHInfo,
char *outputBuffer, const int outputBufferMaxLength) {
char *tempPtr = outputBuffer;
char packetBuffer[MAX_PACKET_SIZE];
int received, outputDataLength;
HTTPParserState_t parserState;
set_new_header_info(outputHInfo);
set_new_parser_state_struct(&parserState);
while (parserState.currentState != packetEnd_s) {
received = net_recv(con, packetBuffer, MAX_PACKET_SIZE);
process_data(packetBuffer, received, &parserState, tempPtr, 10000,
&outputDataLength, packetEnd_s, outputHInfo);
tempPtr += outputDataLength;
}
return tempPtr - outputBuffer;
}
//returns the amount of data downloaded (excluding the header)
int utils_downloadHTTPFileSimple(char *outputBuffer, const int outputMaxLength,
char *inputUrl, HTTPHeaderState_t *hInfo, char *extraHeaders) {
HTTPHeaderState_t requestHeaderInfo;
HTTPParserState_t parserState;
Connection_t con;
char packetBuffer[MAX_PACKET_SIZE];
set_new_parser_state_struct(&parserState);
set_new_header_info(hInfo);
set_new_header_info(&requestHeaderInfo);
utils_connectByUrl(inputUrl, &con);
/* request the file */
utils_createHTTPHeaderFromUrl(inputUrl, packetBuffer, MAX_PACKET_SIZE,
&requestHeaderInfo, REQUEST_GET, extraHeaders);
net_send(&con, packetBuffer, strlen(packetBuffer));
return utils_recvNextHttpPacket(&con, hInfo, outputBuffer, outputMaxLength);
}
#define MAX_STRING_SIZE 20000
#define FORMAT "\"%s\" : "
#define FORMAT2 "\"%s\": "
int isJsonChar(char inputChar) {
if (('a' <= inputChar && inputChar <= 'z')
|| ('A' <= inputChar && inputChar <= 'Z')
|| ('0' <= inputChar && inputChar <= '9') || ('"' == inputChar)) {
return 1;
} else {
return 0;
}
}
char *utils_shittyGetAccessTokenHeader(AccessTokenState_t *tokenState) {
char headerStub[] = "Authorization: Bearer %s\r\n";
int size = strlen(headerStub) + strlen(tokenState->accessTokenStr);
char *tokenHeader = malloc(size);
sprintf(tokenHeader, headerStub, tokenState->accessTokenStr);
return tokenHeader;
}
//TEST: make sure count/the return value is as expected
//FIXME: check for buffer overflow
//returns the resulting length
//targetChunkSize isn't needed
int utils_chunkData(const void *inputData, const int inputDataLength,
void *outputBuffer) {
//calc the chunk length and get the string
char *tempPtr = outputBuffer;
sprintf(outputBuffer, "%x\r\n", inputDataLength);
tempPtr += strlen(outputBuffer);
memcpy(tempPtr, inputData, inputDataLength);
tempPtr += inputDataLength;
memcpy(tempPtr, "\r\n\0", strlen("\r\n") + 1);
tempPtr += strlen("\r\n");
return tempPtr - (const char *) outputBuffer;
}
int utils_chunkAndSend(Connection_t *clientCon, char *dataBuffer,
int dataBufferLen) {
char chunkBuffer[MAX_PACKET_SIZE + 100];
int chunkSize = utils_chunkData(dataBuffer, dataBufferLen, chunkBuffer);
return net_send(clientCon, chunkBuffer, chunkSize);
}
//FIXME: DIRTY HACKS EVERYWHERE !
char* shitty_get_json_value(const char* inputName, char* jsonData, int jsonDataSize) {
//find the area, get the value
char needle[MAX_STRING_SIZE];
sprintf(needle, FORMAT, inputName);
const char* ptr;
if ((ptr = strstrn_nonConst(jsonData, needle, jsonDataSize)) == NULL) {//fixme: dirty hack here with strstrn_nonConst
sprintf(needle, FORMAT2, inputName);
if ((ptr = strstrn(jsonData, needle, jsonDataSize)) == NULL) {
return NULL;
}
ptr += strlen(inputName) + strlen( FORMAT2) - strlen("%s");
} else {
ptr += strlen(inputName) + strlen( FORMAT) - strlen("%s");
}
//now find the value
//get the starting char
for (; !isJsonChar(*ptr); ptr++)
;
int isString = 0;
if (*ptr == '\"') {
isString = 1;
ptr++;
}
const char *endPtr;
if (isString) {
for (endPtr = ptr + 1; *endPtr != '\"'; endPtr++)
;
} else {
for (endPtr = ptr; *endPtr != ',' && *endPtr != '\n'; endPtr++)
;
}
int outputLength = endPtr - ptr;
char* output = malloc(outputLength + 1);
memcpy(output, ptr, outputLength);
output[outputLength] = '\0';
return output;
}