-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
73 lines (62 loc) · 1.58 KB
/
utils.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
// ********************** Utilities Code ************************************
// Author : Sabyasachi Gupta ([email protected])
// Organization : Texas A&M University, CS for ECEN 602 Assignment 4
// Description : Contains utility function definitions
// Last_Modified : 11/26/2017
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CACHE_ENTRY 10
#define MAX_LEN 1024
int parseHDR(const char* hdr, char* buf, char* op) {
char *st = strstr(buf, hdr);
if(!st) {
return 0;
}
char *end = strstr(st, "\r\n");
st += strlen(hdr);
while(*st == ' ')
++st;
while(*(end - 1) == ' ')
--end;
strncpy(op, st, end - st);
op[end - st] = '\0';
return 1;
}
int parse_URL (char* URL, char *hostname, int *port, char *path) {
char *token;
char *host_temp, *path_temp;
char *tmp1, *tmp2;
int num = 0;
char s[16];
if (strstr(URL,"http") != NULL){
token = strtok(URL, ":");
tmp1 = token + 7;
}
else{
tmp1 = URL;
}
tmp2 = malloc (64);
memcpy(tmp2, tmp1, 64);
if(strstr(tmp1, ":") != NULL){
host_temp = strtok(tmp1, ":");
*port = atoi(tmp1 + strlen(host_temp) + 1);
sprintf(s, "%d", *port);
path_temp = tmp1 + strlen(host_temp) + strlen(s) + 1;
}
else{
host_temp = strtok(tmp1, "/");
*port = 80;
path_temp = tmp2 + strlen(host_temp);
}
if (strcmp(path_temp, "") == 0)
strcpy(path_temp, "/");
memcpy(hostname, host_temp, 64);
memcpy(path, path_temp, 256);
return(0);
}
int err_sys(const char* x) // Error display source code
{
perror(x);
exit(1);
}