-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.c
325 lines (288 loc) · 8.63 KB
/
proxy.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
/*
* proxy.c - CS:APP Web proxy
*
* Fan Wentao 5140379060
*
*
* IMPORTANT: Give a high level description of your code here. You
* must also provide a header comment at the beginning of each
* function that describes what that function does.
*/
#include "csapp.h"
/*
* Function prototypes
*/
int parse_uri(char *uri, char *target_addr, char *path, int *port);
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr, char *uri, int size);
//void echo(int connfd);
void doit(int connfd, struct sockaddr_in *sockaddr);
void *init(void *arg);
int Open_clientfd_ts(char *hostname, int port);
int open_clientfd_ts(char *hostname, int port);
/*global variable*/
sem_t mutex;
sem_t mutex_log;
FILE *logFile;
//FILE *cache;
typedef struct
{
int connfd;
struct sockaddr_in sockaddr;
} Arg;
/*
* main - Main routine for the proxy program
*/
int main(int argc, char **argv)
{
/* variable claim */
int listenfd, port, clientlen;
struct sockaddr_in clientaddr;
//struct hostent *hp;
//char *haddrp;
/* Check arguments */
if (argc != 2)
{
fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
exit(0);
}
Signal(SIGPIPE, SIG_IGN);
logFile = fopen("proxy.log","w");
//cache = fopen("cache","w");
port = atoi(argv[1]);
sem_init(&mutex, 0, 1);
sem_init(&mutex_log, 0, 1);
listenfd = Open_listenfd(port);
pthread_t tid;
while (1){
clientlen = sizeof(clientaddr);
//connfd = malloc(sizeof(int));
/*prepare arguments*/
Arg *arg = malloc(sizeof(Arg));
arg->connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
arg->sockaddr = clientaddr;
printf("connfd in main = %d\n", arg->connfd);
/*handle in a thread*/
Pthread_create(&tid,NULL,init,(void *)arg);
//doit(connfd, &clientaddr);
printf("line 61\n");
}
exit(0);
}
void *init(void *arg)
{
Pthread_detach(pthread_self());
Arg *p = (Arg *)arg;
int connfd = p->connfd;
struct sockaddr_in sockaddr;
memcpy(&sockaddr,&(p->sockaddr),sizeof(struct sockaddr_in));
printf("connfd in init = %d\n", connfd);
free(arg);
doit(connfd, &sockaddr);
//Close(connfd);
}
void doit(int connfd, struct sockaddr_in *sockaddr)
{
printf("IN doit\n");
printf("connfd in doit = %d\n", connfd);
char request[MAXLINE];
char method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char hostname[MAXLINE], pathname[MAXLINE];
char buf[MAXLINE];
char logstring[MAXLINE];
int port;
int res_size = 0;
int size;
rio_t client_rio, serv_rio,cache_rio;
Rio_readinitb(&client_rio, connfd);
Rio_readlineb(&client_rio, request, MAXLINE);
sscanf(request, "%s %s %s", method, uri, version);
parse_uri(uri, hostname, pathname, &port);
printf("line 94 %s %s %d\n", hostname, pathname, port);
int servfd = open_clientfd_ts(hostname, port);
char path[MAXLINE];
sprintf(path,"%s/%s","./cache/",pathname);
int cachefd = open(path,O_RDWR|O_CREAT,DEF_MODE);
Rio_readinitb(&cache_rio,cachefd);
if (servfd < 0){
/*connect server fail, find in cache*/
while (Rio_readlineb(&cache_rio, buf, MAXLINE) > 2)
{
/*header*/
printf("%s\n", buf);
//Rio_writen(connfd, buf, strlen(buf));
}
printf(logFile, "Hello! We are in cache.\n");
fflush(logFile);
while ((size = Rio_readnb(&cache_rio, buf, MAXLINE)) != 0)
{
res_size += size;
printf("%s\n", buf);
Rio_writen(connfd, buf, size);
}
format_log_entry(logstring, sockaddr, uri, res_size);
P(&mutex_log);
fprintf(logFile, "%s\n", logstring);
fflush(logFile);
V(&mutex_log);
Close(servfd);
Close(connfd);
Close(cachefd);
return;
}
Rio_readinitb(&serv_rio, servfd);
strcpy(request, method);
strcat(request, " /");
strcat(request, pathname);
strcat(request, " ");
strcat(request, version);
strcat(request, "\r\n");
printf("line 106 request = %s\n", request);
/* send request to server*/
Rio_writen(servfd, request, strlen(request));
/* send request header*/
printf("request header :\n");
while (Rio_readlineb(&client_rio, buf, MAXLINE) > 2)
{
printf("%s\n", buf);
Rio_writen(servfd, buf, strlen(buf));
}
Rio_writen(servfd, "\r\n", 2);
printf("line 117\n");
/*send response to client*/
printf("response header :\n");
while (Rio_readlineb(&serv_rio, buf, MAXLINE) > 2)
{
printf("%s\n", buf);
Rio_writen(cachefd, buf,strlen(buf));
Rio_writen(connfd, buf, strlen(buf));
}
printf("line 135 \n");
//send a empty line
Rio_writen(connfd, "\r\n", 2);
Rio_writen(cachefd, "\r\n", 2);
printf("line 147\n");
//send back response data
while ((size = Rio_readnb(&serv_rio, buf, MAXLINE)) != 0)
{
res_size += size;
printf("%s\n", buf);
Rio_writen(cachefd, buf, size);
Rio_writen(connfd, buf, size);
}
printf("line 157\n");
format_log_entry(logstring, sockaddr, uri, res_size);
P(&mutex_log);
int temp = fprintf(logFile, "%s\n", logstring);
fflush(logFile);
V(&mutex_log);
printf("line 162 %d logstring = %s\n", temp, logstring);
//fclose(logFile);
Close(servfd);
Close(connfd);
Close(cachefd);
}
/*
* parse_uri - URI parser
*
* Given a URI from an HTTP proxy GET request (i.e., a URL), extract
* the host name, path name, and port. The memory for hostname and
* pathname must already be allocated and should be at least MAXLINE
* bytes. Return -1 if there are any problems.
*/
int parse_uri(char *uri, char *hostname, char *pathname, int *port)
{
char *hostbegin;
char *hostend;
char *pathbegin;
int len;
if (strncasecmp(uri, "http://", 7) != 0) {
hostname[0] = '\0';
return -1;
}
/* Extract the host name */
hostbegin = uri + 7;
hostend = strpbrk(hostbegin, " :/\r\n\0");
len = hostend - hostbegin;
strncpy(hostname, hostbegin, len);
hostname[len] = '\0';
/* Extract the port number */
*port = 80; /* default */
if (*hostend == ':')
*port = atoi(hostend + 1);
/* Extract the path */
pathbegin = strchr(hostbegin, '/');
if (pathbegin == NULL) {
pathname[0] = '\0';
}
else {
pathbegin++;
strcpy(pathname, pathbegin);
}
return 0;
}
/*
* format_log_entry - Create a formatted log entry in logstring.
*
* The inputs are the socket address of the requesting client
* (sockaddr), the URI from the request (uri), and the size in bytes
* of the response from the server (size).
*/
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr,
char *uri, int size)
{
time_t now;
char time_str[MAXLINE];
unsigned long host;
unsigned char a, b, c, d;
/* Get a formatted time string */
now = time(NULL);
strftime(time_str, MAXLINE, "%a %d %b %Y %H:%M:%S %Z", localtime(&now));
/*
* Convert the IP address in network byte order to dotted decimal
* form. Note that we could have used inet_ntoa, but chose not to
* because inet_ntoa is a Class 3 thread unsafe function that
* returns a pointer to a static variable (Ch 13, CS:APP).
*/
host = ntohl(sockaddr->sin_addr.s_addr);
a = host >> 24;
b = (host >> 16) & 0xff;
c = (host >> 8) & 0xff;
d = host & 0xff;
/* Return the formatted log entry string */
sprintf(logstring, "%s: %d.%d.%d.%d %s %d", time_str, a, b, c, d, uri, size);
}
int Open_clientfd_ts(char *hostname, int port)
{
int rc;
if ((rc = open_clientfd_ts(hostname, port)) < 0) {
if (rc == -1)
unix_error("Open_clientfd Unix error");
else
dns_error("Open_clientfd DNS error");
}
return rc;
}
int open_clientfd_ts(char *hostname, int port)
{
int clientfd;
struct hostent *hp;
struct sockaddr_in serveraddr;
if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1; /* check errno for cause of error */
/* Fill in the server's IP address and port */
P(&mutex);
if ((hp = gethostbyname(hostname)) == NULL){
V(&mutex);
return -2; /* check h_errno for cause of error */
}
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)hp->h_addr_list[0],
(char *)&serveraddr.sin_addr.s_addr, hp->h_length);
V(&mutex);
serveraddr.sin_port = htons(port);
/* Establish a connection with the server */
if (connect(clientfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0)
return -1;
return clientfd;
}