-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.h
430 lines (339 loc) · 8.28 KB
/
web.h
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef WEB_H
#define WEB_H
#include "stdio.h"
#include "stdlib.h"
#define WEB_MAX_QUERY_STRING 1024
//maximum permissible size of post data is 10 MB;
//TODO(GI): What is the right maximum size to use?
#define WEB_MAX_POST_DATA 10485760
#define TEC_MOST_SIG_BIT 128
#define TEC_2ND_MOST_SIG_BIT 64
#define web_print_header() printf("Content-Type: text/html; charset=utf-8\r\n\r\n")
#define web_print_relocate_header(X) printf("Status:308 Permanent Redirect\nLocation: %s\r\n\r\n", X)
/*web_get_query_string:
a query string typically looks something like this:
var1=abc&var2=def&var3=1%2B1
this function will retrieve the query string
it will split the query string where there is a '&'
it will convert the escaped symbols e.g. %2B -> '+'
this does modify the original string, make a copy if needed
*/
char** web_get_query_string();
/*web_get_post_string:
this does the same thing as web_get_query_string
but gets data sent with POST method
*/
char** web_get_post_string();
/*web_get_from_query_string:
returns a string which contains the value of variable var_name
this requires you first used web_get_query_string()
returns NULL if var_name was not found
char *qss: this is the output of web_get_query_string
char *var_name: name of variable you are seeking the value of
*/
char* web_get_from_query_string(char **qss, char *var_name);
/*tec_buf_begins:
returns 1 if the buffer begins with string str
returns 0 in all other cases, including errors and str being longer than buffer
*/
int tec_buf_begins(char *buffer, char *str);
/*tec_string_length:
returns the length of a string in bytes
check tec_string_utf8_length to know the number of codepoints
unlike strlen, this function does not segfault when you give it a NULL pointer
instead it returns zero because, well, you gave it an empty string ;-)
*/
int tec_string_length(char *str);
/*tec_string_find_char:
returns index of first instance of a character in a string
returns -1 if not found
*/
int tec_string_find_char(char *s, char to_find);
/*tec_string_shift:
removes an ascii char or unicode codepoint at front of string
assumes a valid utf8 string
*/
void tec_string_shift(char *str);
/*tec_string_split:
splits a string where char c does occur
num will give you the number of times char c did occur in the string
num is also index of last string in returned char**
CAUTION: there is one more string than occurences of char c, this might include empty strings
the string str will be modified so make sure to make a copy if needed
note that some character pointers may be NULL pointers
if 2 or more characters c are right next to each other
free the returned char ** when done
returns NULL in case of error
*/
char** tec_string_split(char *str, char c, int *num);
/*tec_string_to_int:
converts a string to an integer
string may not contain anything in front of number except '-' or '+'
does not safeguard against overflow
*/
int tec_string_to_int(char *s);
/*tec_char_is_white_space:
returns 1 if c is a white space character (e.g. space)
returns 0 otherwise
assumes 7 bit ascii character
there are more white space characters within unicode
they are not so commonly used and could not all be considered in just an 8 bit character
*/
int tec_char_is_white_space(char c);
void w_process_query_string(char *qs);
/////////////////////////////////////////////////////////////////////////////////////
//Implementation below
/////////////////////////////////////////////////////////////////////////////////////
char** web_get_query_string(){
char *qs = getenv("QUERY_STRING");
if(!qs)
return NULL;
if(tec_string_length(qs) > WEB_MAX_QUERY_STRING){
return NULL;
}
int i = 0;
int num = 0;
char **qss = tec_string_split(qs, '&', &num);
while(i <= num){
w_process_query_string(qss[i]);
i += 1;
}
return qss;
}//web_get_query_string*/
char** web_get_post_string(){
char *cont_len = getenv("CONTENT_LENGTH");
if(!cont_len){
return NULL;
}
int len = tec_string_to_int(cont_len);
if(len > WEB_MAX_POST_DATA){
return NULL;
}
len += 1;
char *buffer = (char *) malloc(sizeof(char) * len );
fread(buffer, sizeof(char), len, stdin);
int i = 0;
int num = 0;
char **qss = tec_string_split(buffer, '&', &num);
while(i <= num){
w_process_query_string(qss[i]);
i += 1;
}
return qss;
}//web_get_post_string*/
char* web_get_from_query_string(char **qss, char *var_name){
if(!qss)
return NULL;
int i = 0;
while(qss[i]){
if(tec_buf_begins(qss[i], var_name)){
char *str = qss[i];
str += tec_string_length(var_name);
str += 1; //skip over '='
return str;
}
i += 1;
}
return NULL;
}//web_get_from_query_string*/
int tec_buf_begins(char *buffer, char *str){
if(!buffer)
return 0;
if(!str)
return 0;
while(*str && *buffer == *str){
str += 1;
buffer += 1;
}
if(*str){
return 0;
}
return 1;
}//tec_buf_begins*/
int tec_string_length(char *str){
//TODO(GI): go through string one WORD at a time and check for empty characters in the word
if(!str)
return 0;
if(!*str)
return 0;
int len = 0;
while(*str){
len += 1;
str += 1;
}
return len;
}//tec_string_length*/
int tec_string_find_char(char *s, char to_find){
if(!s)
return -1;
int i = 0;
while(s[i] && to_find != s[i]){
i += 1;
}
if(s[i]){
return i;
}else{
return -1;
}
}//tec_string_find_char*/
void tec_string_shift(char *str){
if(!str)
return;
int len = tec_string_length(str);
int i = 1;
int j = 1;
if( (str[i-j] & TEC_MOST_SIG_BIT) && (str[i-j] & TEC_2ND_MOST_SIG_BIT) ){
j += 1;
i += 1;
while( (str[i] & TEC_MOST_SIG_BIT) && !(str[i] & TEC_2ND_MOST_SIG_BIT) ){
j += 1;
i += 1;
}
}
while(i < len){
str[ i - j ] = str[i];
i += 1;
}
str[i - j] = '\0';
}//tec_string_shift*/
char** tec_string_split(char *str, char c, int *num){
if(!str)
return NULL;
if(!c)
return NULL;
// we start by assuming that there will be no more than 32 instances of c
int original_limit = 32;
int limit = original_limit;
char **table[26];
int table_index = 0;
int tmp_num = 0;
*num = 0;
char **res = (char **) malloc(sizeof(char *) * limit);
if(!res){
return NULL;
}
table[table_index] = res;
res[0] = str;
while(*str){
if(*str == c){
tmp_num += 1;
*num += 1;
if(tmp_num == limit){
limit *= 2;
table_index += 1;
res = (char **) malloc(sizeof(char *) * limit);
if(!res){
return NULL;
}
table[table_index] = res;
tmp_num = 0;
}
*str = '\0';
str += 1;
if(*str){
res[tmp_num] = str;
}else{
//Note(GI) this deals with case where char c is last character in the string
res[tmp_num] = NULL;
}
}else{
str += 1;
}
}
if(*num < original_limit){
res[(*num)+1] = NULL;
return res;
}
char **real_res = (char **) malloc(sizeof(char *) * (*num + 2));
int ti = 0;
int n = 0;
int n2 = 0;
limit = original_limit;
while(ti <= table_index){
res = table[ti];
n2 = 0;
while(n2 < limit && n <= *num){
real_res[n] = res[n2];
n2 += 1;
n += 1;
}
free(res);
limit *= 2;
ti += 1;
}
real_res[(*num) + 1] = NULL;
return real_res;
}//tec_string_split*/
int tec_string_to_int(char *s){
if(!s)
return 0;
int sign = 1;
int result = 0;
while(tec_char_is_white_space(*s)){
s += 1;
}
if(*s == '-'){
sign = -1;
s += 1;
}else{
if(*s == '+'){
s += 1;
}
}
while(*s){
if(*s > '9' || *s < '0'){
return result * sign;
}
result *= 10;
result += *s - '0';
s += 1;
}
return result * sign;
}//tec_string_to_int*/
int tec_char_is_white_space(char c){
if(c == 32 || c == 9 || c == 10 || c == 11 || c == 12 || c == 13)
return 1;
return 0;
}//tec_char_is_white_space*/
void w_process_query_string(char *qs){
//first replacing a '+' with a space
char *tmp = qs;
int n = tec_string_find_char(tmp, '+');
while( n != -1 ){
tmp += n;
*tmp = ' ';
n = tec_string_find_char(tmp, '+');
}
int i = 0;
tmp = qs;
while(qs[i]){
while(qs[i] && qs[i] != '%'){
i += 1;
}
if(qs[i] == '%'){
tmp = &(qs[i]);
int hex = 0;
i += 1;
int j = 1;
while(j >= 0){
if(qs[i] >= 'A' && qs[i] <= 'F'){
hex += (qs[i] - 'A') + 10;
}
if(qs[i] >= '0' && qs[i] <= '9'){
hex += (qs[i] - '0');
}
if(j){
hex *= 16;
}
j -= 1;
i += 1;
}
tmp[0] = hex;
tmp += 1;
tec_string_shift(tmp);
tec_string_shift(tmp);
i -= 2;
}
}
}//w_process_query_string*/
#endif