-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.c
290 lines (251 loc) · 7.82 KB
/
Request.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
#include "Request.h"
#include "Cache.h"
#include "host_connection.h"
#define CBUFLEN 100000
const char* DEFAULT_PORT = "80";
const char* e403 = "403 - Forbidden";
const char* e405 = "405 Method Not Allowed";
const char* e500 = "500 - Internal Server Error";
const char* e504 = "504 Gateway Timeout";
const char* cat_expr_1 = "gsed -r -i 's_(.*<img src=\")([^\"]*)(.*)_\\1http://thecatapi.com/api/images/get\?format=src\\";
const char* cat_expr_2 = "&type=jpg\\3_g' ";
void parse_request(Request* r, char* buf, int buf_len)
{
// Ignore 'GET '
char* start = buf+4;
if (strstr(buf, "http://") != NULL){
char* tmp = start+7;
start = tmp;
}
char* end = start;
while(1){
if (*end=='/') break;
end++;
}
memcpy(r->server_host, start, end-start);
r->server_url[end-start] = '\0';
start = end;
while(1){
if (*end==':' || *end == ' '|| *end=='\n' || *end=='\r') break;
end++;
}
memcpy(r->server_url, start, end-start);
r->server_url[end-start] = '\0';
start = end;
if (*end == ':'){
start++;
while(1){
if (*end==' ') break;
end++;
}
memcpy(r->server_port, start, end-start);
r->server_port[end-start] = '\0';
} else {
strcpy(r->server_port, DEFAULT_PORT);
}
char* GET = "GET ";
char* HTTPVER = " HTTP/1.1\r\n";
char* END = "\r\n\r\n";
char* HOST = "Host: ";
char* COLON = ":";
strcat(r->formatted_request, GET);
strcat(r->formatted_request, r->server_url);
strcat(r->formatted_request, HTTPVER);
strcat(r->formatted_request, HOST);
strcat(r->formatted_request, r->server_host);
strcat(r->formatted_request, COLON);
strcat(r->formatted_request, r->server_port);
strcat(r->formatted_request, END);
// Format For File Name
strcat(r->absolute_url,r->server_host);
strcat(r->absolute_url,r->server_url);
strcat(r->absolute_url,COLON);
strcat(r->absolute_url,r->server_port);
printf("Server Host: %s\n",r->server_host);
printf("Server Resource: %s\n",r->server_url);
printf("Server Port: %s\n",r->server_port);
printf("Formatted Request: \n%s\n", r->formatted_request);
printf("Absolute Url: %s\n", r->absolute_url);
}
void* process_request(void* args)
{
Thread_Args* thread_args = (Thread_Args*)args;
int sd = thread_args->socket_descriptor;
Request* r = &(thread_args->r);
int client_len;
struct sockaddr_in client;
char sed_cmd[512];
FILE* blacklist = thread_args->blacklist;
char buf[BUFLEN];
int client_socket;
while(1)
{
if ((client_socket = accept(sd, (struct sockaddr *)&client, (socklen_t*)&client_len)) == -1) {
fprintf(stderr, "Can't accept client.\n");
exit(1);
}
/* Client Accepted */
memset(buf, 0, BUFLEN);
char* get = "GET";
char* bp;
bp = buf;
int bytes_to_read, n;
bytes_to_read = BUFLEN;
while((n = read(client_socket, bp, bytes_to_read)) > 0) {
if (*bp == '\n') {
break;
}
bp += n;
bytes_to_read -= n;
}
/* If Not Get Request Return 404 */
int valid;
valid = strncmp(buf, get, 3);
if ((valid = strncmp(buf, get, 3)) != 0){
write(client_socket, e405, strlen(e405));
close(client_socket);
continue;
}
/* Parse Client Request */
reset_request(r);
parse_request(r, buf, BUFLEN);
/* Validate Against Blacklist */
int cleared = check_blacklist(r->server_host, blacklist);
if (!cleared){
write(client_socket, e403, strlen(e403));
close(client_socket);
continue;
}
/* Check Cache Before Proceeding */
char cache_file_path[250];
memset(cache_file_path,0,250);
get_file_path(r->absolute_url, cache_file_path);
FILE* cache_file;
int cache_hit = file_exists(cache_file_path);
if (cache_hit)
{
cache_file = fopen(cache_file_path, "r");
if (!cache_file) {
perror("cache file error");
continue;
}
printf("cache hit!");
// Read File and Transmit Over Client Socket
read_cache(cache_file, client_socket);
close(client_socket);
}
else
{
/* Cache Miss */
/* Create a stream socket. */
int sockfd, numbytes;
char server_buf[BUFLEN];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* Get Address Information */
if ((rv = getaddrinfo(r->server_host, r->server_port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
freeaddrinfo(servinfo);
printf(e500);
write(client_socket, e500, strlen(e500));
close(client_socket);
continue;
}
/* Open First New Socket to Requested HTTP Server */
for (p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
freeaddrinfo(servinfo);
printf(e504);
write(client_socket, e504, strlen(e504));
close(client_socket);
continue;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
printf("client: connecting to %s\n", s);
freeaddrinfo(servinfo);
/* Write Original Client Request to Socket, Update Cache */
write(sockfd, r->formatted_request, strlen(r->formatted_request));
printf("Sent: %s\n", r->formatted_request);
/* Get Host Content From Server Response */
char clientBuffer[CBUFLEN];
memset(clientBuffer, 0, CBUFLEN);
int respValid;
respValid = getHostContent(sockfd, clientBuffer, CBUFLEN);
if (!respValid)
{
/* Write Response to Client (Without Caching) */
int cb_len = strlen(clientBuffer);
sendall(client_socket, clientBuffer, &cb_len);
close(client_socket);
continue;
}
/* Initialize cache_file */
char* temp_suffix = "_temp";
char temp_file_path[BUFLEN];
strcpy(temp_file_path, cache_file_path);
strcat(temp_file_path,temp_suffix);
/* Use Temporary Filename */
cache_file = fopen(temp_file_path,"w+");
if (!cache_file) perror("Error");
/* Write Retrieved Content to Cache File */
int bytes_cached = fwrite(clientBuffer, 1, strlen(clientBuffer), cache_file);
// Write to socket and send to the client.
int to_send = strlen(clientBuffer);
read_cache(cache_file, client_socket);
// /* Rename Cache File */
int renamed = rename(temp_file_path, cache_file_path);
if (renamed == -1) perror ("Error");
/* Clean up. */
close(client_socket);
}
}
}
/* Return True if host is Blacklisted */
int check_blacklist(char* host, FILE* blacklist){
int MAX_LENGTH = 101;
char *found, line[MAX_LENGTH];
while( fgets (line, MAX_LENGTH, blacklist) != NULL ) {
// Remove Trailing Crap From fgets
line[strcspn(line, "\r\n")] = 0;
found = strstr(host, line);
if (found){
printf("Request for host %s is blacklisted under entry %s\n", host, line);
return 0;
}
}
return 1;
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
/* Reset Request Information */
void reset_request(Request* request)
{
memset(request->absolute_url, 0, BUFLEN);
memset(request->server_host, 0, BUFLEN);
memset(request->server_url, 0, BUFLEN);
memset(request->server_port, 0, BUFLEN);
memset(request->formatted_request, 0, BUFLEN);
}