-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
704 lines (537 loc) · 14.7 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// utils.c
//
// contains a bunch of stuff I need.
//
// Written in C but compiled with gcc, so I can use // comments.
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include "utils.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_PATH_LENGTH 120
#define MAX_HOSTNAME_LENGTH 60
#define UTILS_DEBUG 0
// This is a dirty way of doing some enums with an array
#define NUMBER_OF_PROTOCOLS 5
char * PROTOCOL_NAMES[] = {"http", "ftp", "gopher", "mailto", "telnet"};
int initialize_url(url u)
{
if (!u) // url is null
{
printf("initialize_url: passed NULL\n");
return -1;
}
// Put in a bunch of defaults
u -> protocol = HTTP;
u -> hostname = (char *) malloc((MAX_HOSTNAME_LENGTH+1) * sizeof(char));
strcpy(u -> hostname, "guppy.cs.unc.edu");
u -> port = 80;
u -> path = (char *) malloc((MAX_PATH_LENGTH+1) * sizeof(char));
strcpy(u -> path, "/index.html");
return 0;
}
void pretty_print(url u)
{
printf("Protocol is '%s'\n", PROTOCOL_NAMES [u -> protocol]);
printf("hostname is '%s'\n", u -> hostname);
printf(" port is %d\n", u -> port);
printf(" path is '%s'\n", u -> path);
if (u -> port >= 0)
{
printf("So the entire URL is ");
printf("%s://%s:%d%s\n",
PROTOCOL_NAMES [u -> protocol],
u -> hostname,
u -> port,
u -> path);
}
else
{
printf("So the entire URL is ");
printf("%s://%s%s\n",
PROTOCOL_NAMES [u -> protocol],
u -> hostname,
u -> path);
}
printf("\n");
}
int parse_as_url(char * in, url u)
{
char * result;
char port_as_string[10];
int temp;
// Get the first token, try to treat it as a protocol.
result = strtok(in, ":/");
if (UTILS_DEBUG)
printf("token is %s -- this should be the protocol\n", result);
u -> protocol = parse_as_protocol(result);
if (u -> protocol < 0)
{
printf("I'm sorry, I couldn't parse ""%s"" as a protocol.\n", result);
return (-1);
}
else if (u -> protocol != HTTP)
{
printf("I'm sorry, only HTTP is supported in this client.\n");
return(-1);
}
if (UTILS_DEBUG)
printf("u -> protocol is %d\n", u -> protocol);
// Get the second token. See whether it has a : in it.
// If so, treat it as a hostname and port;
// If not, treat it as just a hostname.
if (u -> hostname != NULL)
free(u -> hostname);
result = strtok(NULL, "/");
if (UTILS_DEBUG)
printf("result is %s\n", result);
if (result == NULL)
{
printf("Oops, ran out of URL to parse.\n");
return(-1);
}
// I'll look for a : using strcspn, because I am not sure what using
// strtok or strstr would do to the secret magic index variable. C
// string functions are weird that way.
int charstilcolon = strcspn(result, ":");
int resultlength = strlen(result);
if (UTILS_DEBUG)
printf("token is %s -- this should include the port if present\n", result);
if (charstilcolon == resultlength) // Just the hostname.
{
// Copy the hostname into the struct, and set port to default.
if (UTILS_DEBUG)
printf("rest of string is %s -- port not present\n", result);
u -> hostname = (char *) malloc((resultlength+3) * sizeof(char));
strcpy(u -> hostname, result);
// u -> hostname = strdup(result);
if (UTILS_DEBUG)
{
printf("--------------\n");
pretty_print(u);
printf("--------------\n");
}
set_default_port(u);
if (UTILS_DEBUG)
printf("port number is %d (default)\n", u -> port);
}
else
{
// First put in the hostname... that's chars 0 through temp-1,
// and slap a null after.
if (UTILS_DEBUG)
{
printf("rest of string is %s -- port is present\n", result);
printf("number of chars til colon is %d\n", charstilcolon);
}
u -> hostname = (char *) malloc( (charstilcolon+1) * sizeof(char));
strncpy(u -> hostname, result, charstilcolon);
(u -> hostname)[charstilcolon] = '\0';
if (UTILS_DEBUG)
printf("Allocated hostname ok, it is %s\n", u -> hostname);
strcpy(port_as_string, result+charstilcolon+1);
u -> port = atoi(port_as_string);
if (UTILS_DEBUG)
printf("port number is %d\n", u -> port);
}
// And everything else should be the path.
if (UTILS_DEBUG) printf("1\n");
if (u -> path != NULL)
{
if (UTILS_DEBUG) printf("2\n");
// free(u -> path);
}
if (UTILS_DEBUG) printf("3\n");
result = strtok(NULL, "");
if (UTILS_DEBUG) printf("4\n");
if (result == NULL || strlen(u -> path) == 0)
{
if (UTILS_DEBUG) printf("5\n");
u -> path = (char *) malloc (4 * sizeof(char));
u -> path = "/";
}
else
{
if (UTILS_DEBUG) printf("6\n");
char * foobar = strdup("/");
strcat(foobar, result);
int fbl = strlen(foobar);
if (UTILS_DEBUG) printf("foobar = %s, length is %d\n", foobar, fbl);
u -> path = (char *) malloc ((fbl+3) * sizeof(char));
if (UTILS_DEBUG) printf("Allocated u -> path ok...\n");
strcpy(u -> path, foobar);
}
if (UTILS_DEBUG) printf("7\n");
return 0;
}
int parse_as_protocol(char * in)
{
int i;
for (i = 0; i<NUMBER_OF_PROTOCOLS; i++)
{
if (strcasecmp(in, PROTOCOL_NAMES[i]) == 0)
{
return i;
}
}
return -1;
}
int set_default_port(url u)
{
if (u -> protocol == HTTP)
{
u -> port = 80;
}
else if (u -> protocol == TELNET)
{
u -> port = 23;
}
else
return (u -> port = -1);
}
int get_ip_int(url u)
{
struct hostent * h;
int i = 0;
int result;
char *temp;
h = (struct hostent *) malloc (sizeof (struct hostent));
if (UTILS_DEBUG)
{
printf("hostname looking for is %s \n", u -> hostname);
printf("g1\n");
pretty_print(u);
}
h = gethostbyname(u -> hostname);
if (UTILS_DEBUG) printf("g2\n");
if (h == NULL)
{
printf("Host ""%s"": \n", u -> hostname);
herror("Got an error trying to look up the host ");
return(-1);
}
if (UTILS_DEBUG) printf("Host name is %s \n", h -> h_name);
temp = (char *) inet_ntoa(*((struct in_addr *) h -> h_addr));
if (UTILS_DEBUG) printf("IP address is %s\n", temp);
result = inet_addr(temp);
if (UTILS_DEBUG) printf("IP as an int is %x\n", result);
return(result);
}
void allocate_and_fill_http_request(char ** addr_of_buffer, url u)
{
if (UTILS_DEBUG) printf("Entered allocate_and_fill_http_request\n");
char buffer[1000];
buffer[0] = '\0';
// Method
strcat(buffer, "GET ");
if (UTILS_DEBUG) printf("buffer is %s, length is %ld\n", buffer, strlen(buffer));
// Request-URI
strcat(buffer, u -> path);
if (UTILS_DEBUG) printf("buffer is %s, length is %ld\n", buffer, strlen(buffer));
// HTTP-Version
strcat(buffer, " HTTP/1.0");
if (UTILS_DEBUG) printf("buffer is %s, length is %ld\n", buffer, strlen(buffer));
// CRLF
strcat(buffer, CRLF); // #defined as "\015\012\000"
if (UTILS_DEBUG) printf("buffer is %s, length is %ld\n", buffer, strlen(buffer));
// User-Agent (and another CRLF)
strcat(buffer, "User-Agent:MatuszekClient/1.0");
strcat(buffer, CRLF);
strcat(buffer, CRLF);
if (UTILS_DEBUG) printf("buffer is %s, length is %ld\n", buffer, strlen(buffer));
// Allocate only what we need; return that pointer.
int length = strlen(buffer);
char * result;
result = (char *) malloc ((length+1) * sizeof(char));
strncpy(result, buffer, length);
result[length] = '\0';
*addr_of_buffer = result;
}
void allocate_and_fill_http_response(char ** addr_of_buffer, int status, int contentlength)
{
// HTTP/1.1 200 OK
// Date: Mon, 13 Sep 1999 01:03:58 GMT
// Server: Apache/1.3.4 (Unix)
// Last-Modified: Thu, 26 Aug 1999 14:32:38 GMT
// ETag: "4b1a1504-1865-37c55006"
// Accept-Ranges: bytes
// Content-Length: 6245
// Connection: close
// Content-Type: text/html
char temp[100];
if (UTILS_DEBUG) printf("Entered allocate_and_fill_http_request\n");
char buffer[1000];
buffer[0] = '\0';
// if (UTILS_DEBUG) printf("buffer is %s, length is %d", buffer, strlen(buffer));
// HTTP status
char * statusline;
switch (status)
{
case 200: statusline = "HTTP/1.0 200 OK";
break;
case 400: statusline = "HTTP/1.0 400 Bad Request";
break;
case 403: statusline = "HTTP/1.0 403 Forbidden";
break;
case 404: statusline = "HTTP/1.0 404 Not Found";
break;
case 500: statusline = "HTTP/1.0 500 Internal Server Error";
break;
case 501: statusline = "HTTP/1.0 501 Not Implemented";
break;
case 503: statusline = "HTTP/1.0 503 Service Unavailable";
break;
default:
{
sprintf(temp, "HTTP/1.0 %d Some Other Error", status);
statusline = temp;
break;
}
}
strcat(buffer, statusline);
strcat(buffer, CRLF);
// Date
strcat(buffer, "Date: Mon, 13 Sep 1999 01:03:58 GMT");
strcat(buffer, CRLF);
// Content-length
sprintf(temp, "Content-Length: %d", contentlength);
strcat(buffer, temp);
strcat(buffer, CRLF);
// Connection
strcat(buffer, "Connection: close");
strcat(buffer, CRLF);
// Content-Type
strcat(buffer, "Content-Type: text/plain");
strcat(buffer, CRLF);
// CRLF
strcat(buffer, CRLF);
// Allocate only what we need; return that pointer.
int length = strlen(buffer);
char * result;
result = (char *) malloc ((length+1) * sizeof(char));
strncpy(result, buffer, length);
result[length] = '\0';
*addr_of_buffer = result;
}
int content_offset_of(char * in)
{
int last_was = 0;
int i, length;
length = strlen(in);
for (i=0; i<length; i++)
{
if (in[i] == 13)
{
continue;
}
if (in[i] == 10)
{
if (last_was)
{
return (i+1);
}
else
{
last_was = 1;
}
}
else
{
last_was = 0;
}
}
return(-1);
}
int parse_response_header(char * in, int length, int *content_length)
{
char * mycopy;
char * currentline;
char * currenttoken;
char * temp;
char ** placeincopy = &mycopy;
char ** placeinline = ¤tline;
int responsecode = 0;
mycopy = (char *) malloc ((length+1) * sizeof(char));
strncpy(mycopy, in, length);
// Handle first line.
temp = strtok_r(mycopy, "\n", placeincopy);
if (temp) currentline = strdup(temp);
if (UTILS_DEBUG)
{
printf("The first line of the header is: ");
printf("%s\n", currentline);
}
currenttoken = strtok_r(currentline, " ", placeinline);
if (!strstr(currenttoken, "http"))
{
printf("Got an HTTP response.\n");
}
else
{
printf("Whoops! We got a non-HTTP response. The first line was:\n");
printf("%s\n", currentline);
exit(0);
}
currenttoken = strtok_r(currentline, " ", placeinline);
responsecode = atoi(currenttoken);
currenttoken = strtok_r(currentline, "\015\012", placeinline);
switch (responsecode)
{
case 200: printf("Connection is okay.\n");
break;
case 400: printf("Whoops -- it seems there was something ");
printf("wrong with our request. My fault.\n");
// return(400);
break;
case 403: printf("I'm sorry, the server says we can't ");
printf("have that resource.\n");
//return(403);
break;
case 404: printf("I'm sorry, the server says it can't ");
printf("find that resource.\n");
// return(404);
break;
case 500: printf("Hmm -- the server is having some problem ");
printf("fulfilling that request.\n");
//return(500);
break;
case 501: printf("The server says that method isn't ");
printf("implemented.\n");
//return(501);
break;
case 503: printf("The service is unavailable -- the server might");
printf(" have too many connections or something.\n");
//return(503);
break;
default:
{
if (responsecode >= 400)
{
printf("Got an unknown response code of %d.\n",
responsecode);
printf("The explanation is [%s].\n", currenttoken);
return(responsecode);
}
else
{
printf("Got an unknown, but non-fatal, response code of %d.\n",
responsecode);
printf("The explanation is [%s].\n", currenttoken);
}
}
}
// Handle remaining headers. Ignore most.
int found_content_length = 0;
temp = strtok_r(mycopy, "\n", placeincopy);
if (temp) currentline = strdup(temp);
while (temp)
{
if (UTILS_DEBUG)
{
printf("The next line of the header is: ");
printf("%s\n", currentline);
}
currenttoken = strtok_r(currentline, " ", placeinline);
if (!strcasecmp(currenttoken, "content-type:"))
{
currenttoken = strtok_r(currentline, " ", placeinline);
if (1 || UTILS_DEBUG)
printf("Content type is %s\n", currenttoken);
if (strncasecmp(currenttoken, "text", 4))
{
printf("Sorry, but this client can only view text/* files.\n");
return(600);
}
}
else if (!strcasecmp(currenttoken, "content-length:"))
{
currenttoken = strtok_r(currentline, " ", placeinline);
*content_length = atoi(currenttoken);
found_content_length = 1;
if (1 || UTILS_DEBUG)
{
// printf("Content length is %s\n", currenttoken);
printf("Content length is %d\n", *content_length);
}
}
else
{
while (currenttoken)
{
if (UTILS_DEBUG)
printf("\tThe next token in the first line is %s\n",
currenttoken);
currenttoken = strtok_r(currentline, " ", placeinline);
}
}
temp = strtok_r(mycopy, "\n", placeincopy);
if (temp) currentline = strdup(temp);
}
if (!found_content_length)
{
*content_length = IGNORE_THIS_VALUE;
// printf("I'm sorry, but this client can only receive a resource if\n");
// printf("the server tells it the Content-Length (that is, how big the\n");
// printf("file is). The reason for this is that otherwise, the client\n");
// printf(".... actually, let's see whether we can get around that.\n");
}
if (UTILS_DEBUG)
{
printf("Finished parsing...\n");
printf("Returning responsecode == %d\n", responsecode);
}
return (responsecode);
}
int parse_request_header(char * in, char **filename, int max_filename_length)
{
// First off, anything that's not a GET is ignored.
char * mycopy;
char * currentline;
char * currenttoken;
char * temp;
char ** placeincopy = &mycopy;
char ** placeinline = ¤tline;
mycopy = (char *) malloc ((strlen(in)+4) * sizeof(char));
strncpy(mycopy, in, max_filename_length);
// Handle first line.
temp = strtok_r(mycopy, "\n", placeincopy);
if (temp) currentline = strdup(temp);
if (1 || UTILS_DEBUG)
{
printf("The first line of the header is: ");
printf("%s\n", currentline);
}
currenttoken = strtok_r(currentline, " ", placeinline);
printf("First token is: %s\n", currenttoken);
if (!strcasecmp(currenttoken, "GET"))
{
printf("Got a GET request.\n");
}
else
{
printf("Whoops! We got a non-GET method.\n");
return(-1);
}
currenttoken = strtok_r(currentline, " ", placeinline);
printf("Next token is: %s\n", currenttoken);
if (currenttoken[0] == '/')
{
printf("Looks like a filename, let's copy it in.\n");
strcpy(*filename, currenttoken);
printf("*filename set to %s\n", *filename);
return(0);
}
else
{
printf("That doesn't sound like a URI to me, I'm afraid.\n");
return(-1);
}
// Yes, we're just going to ignore anything else that may
// exist in the header.
return 0;
}