forked from riolet/WAFer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nopeutils.c
402 lines (354 loc) · 9.78 KB
/
nopeutils.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
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
/* This is a Swiss army knife of utilities that make
* writing a network application a wee bit easier
* Rohana Rezel
* Riolet Corporation
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdarg.h>
#include "nopeutils.h"
#include "nope.h"
/* String functions */
bool stringEqualsLitLen(const char *varStr,const char *litStr,int litStrLen)
{
int i;
for (i=0;i<litStrLen-1;i++) {
if (varStr[i]==0)
return false;
if (varStr[i]!=litStr[i])
return false;
}
return true;
}
/**********************************************************************/
/* Return the value of a query parameter.
* Parameters: the query string
* the name of the parameter
* Returns: the value of the query parameter */
/**********************************************************************/
char * getQueryParam(const char * queryString, const char *name)
{
char bufferAmpersand[MAX_BUFFER_SIZE];
char bufferQuestion[MAX_BUFFER_SIZE];
snprintf(bufferQuestion,MAX_BUFFER_SIZE-1,"?%s=",name);
snprintf(bufferAmpersand,MAX_BUFFER_SIZE-1,"&%s=",name);
char *buffer;
char *pos1;
char *value = malloc(MAX_BUFFER_SIZE*sizeof(char));
if (value == NULL) {
printf("Could not allocate memory.");
exit(EXIT_FAILURE);
}
int i;
buffer=bufferQuestion;
pos1 = strstr(queryString,bufferQuestion);
printf ("Buffer %s Pos %s\n",buffer,pos1);
if (!pos1) {
buffer=bufferAmpersand;
pos1 = strstr(queryString, bufferAmpersand);
}
if (pos1) {
pos1 += strlen(buffer);
i=0;
while (*pos1 && *pos1 != '&' && i<MAX_BUFFER_SIZE) {
if (*pos1 == '%') {
value[i]= (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
pos1 += 3;
} else if( *pos1=='+' ) {
value[i] = ' ';
pos1++;
} else {
value[i] = *pos1++;
}
i++;
}
value[i] = '\0';
return value;
}
strcpy(value, UNDEFINED);
return value;
}
/**********************************************************************/
/* Return the query path. For example the query path for
* http://nopedotc.com/faq is /faq
* Note the / at the beginning
* Parameters: the query string
* Returns: the query path */
/**********************************************************************/
char * getQueryPath(const char * reqString)
{
char * queryPath;
queryPath = strdup(reqString);
u_int i;
for (i=0;i<strlen(reqString) && (queryPath[i] != '?') && (queryPath[i] != '\0');i++) {
}
queryPath[i] = '\0';
return queryPath;
}
/* This is like dprintf on post 2008 POSIX
* You can use FDPRINTF which might use dprintf if it is available
* Parameters: the client, format
* Returns: the number of characters printed
*/
long nprintf (int client, const char *format, ...)
{
/* initial buffer large enough for most cases, will resize if required */
char *buf = malloc(MAX_BUFFER_SIZE);
int len;
va_list arg;
long done;
va_start (arg, format);
len = vsnprintf (buf, MAX_BUFFER_SIZE, format, arg);
va_end (arg);
if (len > MAX_BUFFER_SIZE) {
/* buffer size was not enough */
free(buf);
buf = malloc(len+1);
if (buf == NULL) {
printf("Could not allocate memory.");
exit(EXIT_FAILURE);
}
va_start (arg, format);
vsnprintf (buf, len+1, format, arg);
va_end (arg);
}
/* printf("Buffer length %d",strlen(buf)); */
if (len<MAX_BUFFER_SIZE) {
done = (int) send(client, buf, len, 0);
} else {
done = writeLongString(client,buf,len);
}
free(buf);
return done;
}
char * getHeader(char **headers, char *header)
{
char * current_header, * matching_header, *value;
// Not sure if MAX_BUFFER_SIZE is right.
char * retval = malloc(MAX_BUFFER_SIZE*sizeof(char));
int i;
for (i=0;headers[i]!=NULL;i++) {
current_header = headers[i];
if ((matching_header = strstr(current_header, header))) {
value = matching_header+strlen(header);
if (*value == ':') {
while (*value == ' ' || *value == ':') {
value++;
}
strcpy(retval,value);
return retval;
}
}
}
memcpy(retval, UNDEFINED, sizeof(UNDEFINED));
return retval;
}
void sendHeadersTypeEncoding(Request request,const char *type, const char * encoding)
{
int client=request.client;
STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0);
STATIC_SEND(client, SERVER_STRING, 0);
FDPRINTF(client, "Content-Type: %s\r\n", type,0);
if (encoding!=NULL) {
FDPRINTF(client, "Content-Encoding: %s\r\n", encoding,0);
}
STATIC_SEND(client, "\r\n", 0);
}
void writeStandardHeaders(int client)
{
STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0);
STATIC_SEND(client, SERVER_STRING, 0);
STATIC_SEND(client, "Content-Type: text/html\r\n", 0);
STATIC_SEND(client, "\r\n", 0);
}
void cat(int client, FILE *pFile)
{
char buf[1024];
int result = fread (buf,1,1024,pFile);
while (result!=0) {
send(client, buf, result, 0);
result = fread (buf,1,1024,pFile);
}
}
/* Serve downloadable file. */
void serveDownloadableFile(int client, const char *filename, const char *displayFilename, const char * type)
{
FILE *resource = NULL;
STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0);
STATIC_SEND(client, SERVER_STRING, 0);
FDPRINTF(client, "Content-Type: %s\r\n",type);
FDPRINTF(client, "Content-Disposition: attachment; filename=\"%s\"\r\n",displayFilename);
STATIC_SEND(client, "\r\n", 0);
resource = fopen(filename, "r");
if (resource == NULL) {
not_found(client);
} else {
cat(client, resource);
}
fclose(resource);
}
/* Serve and entire file. */
void serveFile(int client, const char *filename, const char * type)
{
FILE *resource = NULL;
FDPRINT(client, "HTTP/1.0 200 OK\r\n");
FDPRINT(client, SERVER_STRING);
FDPRINTF(client, "Content-Type: %s\r\n",type);
FDPRINT(client, "\r\n");
resource = fopen(filename, "r");
if (resource == NULL) {
not_found(client);
} else {
cat(client, resource);
}
fclose(resource);
}
/* Write strings that are two big for our buffer */
ssize_t writeLongString(int client,const char* longString, size_t len)
{
size_t remain = len;
size_t sent=0;
while (remain)
{
size_t toCpy = remain > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : remain;
ssize_t sent_once = send(client, longString, toCpy, 0);
if (sent_once <= 0) return -1;
sent += sent_once;
longString += sent_once;
remain -= sent_once;
}
return sent;
}
/**********************************************************************/
/* Inform the client that the requested web method has not been
* implemented.
* Parameter: the client socket */
/**********************************************************************/
void unimplemented(int client)
{
FDPRINT(client, "HTTP/1.0 501 Method Not Implemented\r\n");
FDPRINT(client, SERVER_STRING);
FDPRINT(client, "Content-Type: text/html\r\n");
FDPRINT(client, "\r\n");
FDPRINT(client, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
FDPRINT(client, "</TITLE></HEAD>\r\n");
FDPRINT(client, "<BODY><P>HTTP request method not supported.\r\n");
FDPRINT(client, "</P></BODY></HTML>\r\n");
}
/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
void not_found(int client)
{
FDPRINT(client, "HTTP/1.0 404 NOT FOUND\r\n");
FDPRINT(client, "Content-Type: text/html\r\n");
FDPRINT(client, "\r\n");
FDPRINT(client, "<HTML><TITLE>Not Found</TITLE>\r\n");
FDPRINT(client, "<BODY><P>The server could not fulfill\r\n");
FDPRINT(client, "your request because the resource specified\r\n");
FDPRINT(client, "is unavailable or nonexistent.\r\n");
FDPRINT(client, "</P></BODY></HTML>\r\n");
}
char * _hscanIfEmpty(int client, const char * reqStr, const char *msg,const char * inputstr)
{
char * qpath=getQueryPath(reqStr);
char * qparam=getQueryParam(reqStr,"q");
if (strcmp(qparam,UNDEFINED)==0) {
FDPRINTF(client,
OTAGA(form,action="%s")
"%s%s"
STAG(input,type="submit")
CTAG(form)
,qpath,msg,inputstr);
}
free(qpath);
return qparam;
}
char * _hscan(int client, const char * reqStr, const char *msg,const char * inputstr)
{
char * qpath=getQueryPath(reqStr);
char * qparam=getQueryParam(reqStr,"q");
FDPRINTF(client,
OTAGA(form,action="%s")
"%s%s"
STAG(input,type="submit")
CTAG(form)
,qpath,msg,inputstr);
free(qpath);
return qparam;
}
bool nope_route(Request request, const char * path, void (* function)(Request),bool send_headers)
{
char * queryPath = getQueryPath(request.reqStr);
if (strcmp(queryPath,path)==0) {
free(queryPath);
if (send_headers)
writeStandardHeaders(request.client);
if (function!=NULL)
function(request);
return true;
} else {
free(queryPath);
return false;
}
}
/* Deprecated functions */
bool route(Request request, const char * path)
{
char * queryPath = getQueryPath(request.reqStr);
if (strcmp(queryPath,path)==0) {
free(queryPath);
return true;
} else {
free(queryPath);
return false;
}
}
bool routeh(Request request, const char * path)
{
char * queryPath = getQueryPath(request.reqStr);
if (strcmp(queryPath,path)==0) {
free(queryPath);
writeStandardHeaders(request.client);
return true;
} else {
free(queryPath);
return false;
}
}
bool routef(Request request, const char * path, void (* function)(int,const char *, const char*))
{
char * queryPath = getQueryPath(request.reqStr);
if (strcmp(queryPath,path)==0) {
free(queryPath);
function(request.client,request.reqStr,request.method);
return true;
} else {
free(queryPath);
return false;
}
}
bool routefh(Request request, const char * path, void (* function)(int,const char *, const char*))
{
char * queryPath = getQueryPath(request.reqStr);
if (strcmp(queryPath,path)==0) {
free(queryPath);
writeStandardHeaders(request.client);
function(request.client,request.reqStr,request.method);
return true;
} else {
free(queryPath);
return false;
}
}
/* End deprecated functions */