-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathproxy.c
More file actions
338 lines (293 loc) · 9.34 KB
/
proxy.c
File metadata and controls
338 lines (293 loc) · 9.34 KB
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
/*
* proxy.c - ICS Web proxy
* Kang Huquan
* 517021910839
*/
#include "csapp.h"
#include <stdarg.h>
#include <sys/select.h>
/* In debug mode, debug information such as printf will be not execute */
const int debug_mode = 0;
sem_t log_mutex;
/*
* Function prototypes
*/
int parse_uri(char *uri, char *target_addr, char *path, char *port);
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr, char *uri, size_t size);
int proxy_send(int clientfd, rio_t *conn_rio, char *requestheader, size_t content_length, char *method, char *pathname, char *version);
size_t proxy_receive(int connfd, rio_t *client_rio);
void doit(int connfd, struct sockaddr_in *clientaddr);
ssize_t Rio_readlineb_w(rio_t *rp, void *usrbuf, size_t maxlen)
{
ssize_t rc;
if ((rc = rio_readlineb(rp, usrbuf, maxlen)) < 0){
fprintf(stderr, "Rio_readlineb error");
rc = 0;
}
return rc;
}
ssize_t Rio_readnb_w(rio_t *rp, void *usrbuf, size_t n)
{
ssize_t rc;
if ((rc = rio_readnb(rp, usrbuf, n)) < 0){
fprintf(stderr, "Rio_readnb error");
rc = 0;
}
return rc;
}
int Rio_writen_w(int fd, void *usrbuf, size_t n)
{
if (rio_writen(fd, usrbuf, n) != n){
fprintf(stderr, "Rio_writen error");
return -1;
}
return 0;
}
typedef struct vargp{
int connfd;
struct sockaddr_in clientaddr;
} vargp_t;
void *thread(void *vargp){
Pthread_detach(Pthread_self());
vargp_t *vargp_self = (vargp_t *)vargp;
doit(vargp_self->connfd, &(vargp_self->clientaddr));
Close(vargp_self->connfd);
Free(vargp_self);
return NULL;
}
/*
* main - Main routine for the proxy program
*/
int main(int argc, char **argv)
{
int listenfd;
socklen_t clientlen = sizeof(struct sockaddr_in);
char client_hostname[MAXLINE], client_port[MAXLINE];
pthread_t tid;
vargp_t *vargp;
/* Check arguments */
if (argc != 2) {
fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
exit(0);
}
Sem_init(&log_mutex, 0, 1);
Signal(SIGPIPE, SIG_IGN);
/* As Server, Listen to Client */
listenfd = Open_listenfd(argv[1]);
while(1){
vargp = Malloc(sizeof(vargp_t));
vargp->connfd = Accept(listenfd, (SA *)&(vargp->clientaddr), &clientlen);
Pthread_create(&tid, NULL, thread, vargp);
Getnameinfo((SA *)&(vargp->clientaddr), clientlen, client_hostname, MAXLINE, client_port, MAXLINE, 0);
if(debug_mode)
printf("\n\n----------------Connected to (%s, %s)\n", client_hostname, client_port);
}
Close(listenfd);
exit(0);
}
void doit(int connfd, struct sockaddr_in *clientaddr){
char buf[MAXLINE], requestheader[MAXLINE];
char method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char hostname[MAXLINE], pathname[MAXLINE], port[MAXLINE];
int clientfd;
rio_t conn_rio, client_rio;
size_t n, byte_size = 0, content_length = 0;
/* Connect to Server */
Rio_readinitb(&conn_rio, connfd);
if(Rio_readlineb_w(&conn_rio, buf, MAXLINE) == 0){
fprintf(stderr, "error: read empty request line\n");
return;
}
if(sscanf(buf, "%s %s %s", method, uri, version) != 3){
fprintf(stderr, "error: mismatched parameters\n");
return;
}
if(parse_uri(uri, hostname, pathname, port) != 0 ){
fprintf(stderr, "error: parse uri wrong\n");
return;
}
/* Request Header */
sprintf(requestheader, "%s /%s %s\r\n", method, pathname, version);
while ((n = Rio_readlineb_w(&conn_rio, buf, MAXLINE)) != 0)
{
if(!strncasecmp(buf, "Content-Length", 14))
sscanf(buf+15, "%zu", &content_length);
sprintf(requestheader, "%s%s", requestheader, buf);
if(debug_mode)
printf("(%zu bytes) %s", strlen(buf), buf);
if(!strncmp(buf, "\r\n", 2))
break;
}
if(n == 0)
return;
/* Open Clientfd */
while ((clientfd = open_clientfd(hostname, port)) < 0){
fprintf(stderr, "error: open client fd wrong\n");
fprintf(stderr, "error: hostname-%s\n", hostname);
fprintf(stderr, "error: port-%s\n", port);
return;
}
Rio_readinitb(&client_rio, clientfd);
/* Debug Infomation */
if(debug_mode) {
printf("**********Connect to Server***\n");
printf("Parse uri: %s %s %s\n", hostname, pathname, port);
printf("Open clientfd %d", clientfd);
}
//Send By Proxy From Client to Server
if(proxy_send(clientfd, &conn_rio, requestheader, content_length, method, pathname, version) != -1)
//Send By Proxy From Server to Client
byte_size = proxy_receive(connfd, &client_rio);
/* Format Output */
format_log_entry(buf, clientaddr, uri, byte_size);
P(&log_mutex);
printf("%s\n", buf);
V(&log_mutex);
Close(clientfd);
}
int proxy_send(int clientfd, rio_t *conn_rio, char *requestheader, size_t content_length, char *method, char *pathname, char *version)
{
char buf[MAXLINE];
if(debug_mode) {
printf("**********Send By Proxy From Client to Server***\n");
printf(">>> Request headers:\n");
}
if(Rio_writen_w(clientfd, requestheader, strlen(requestheader)) == -1)
return -1;
if(debug_mode) {
printf("%s", requestheader);
printf("Send ALL Request Headers! Done!\n");
printf(">>> Request bodys:\n");
}
/* Request Body */
if(strcasecmp(method, "GET"))
for(int i=0; i<content_length; i++){
if(Rio_readnb_w(conn_rio, buf, 1) == 0)
return -1;
if(Rio_writen_w(clientfd, buf, 1) == -1)
return -1;
if(debug_mode)
printf("%c", *buf);
}
if(debug_mode) {
printf("\nSend ALL Request Bodys! Done! Content-Length: %zu\n", content_length);
}
return 0;
}
size_t proxy_receive(int connfd, rio_t *client_rio)
{
char buf[MAXLINE];
size_t n, byte_size = 0, content_length = 0;
/* Response Header */
if(debug_mode) {
printf("**********Send By Proxy From Server to Client***\n");
printf(">>> Response headers:\n");
}
while((n = Rio_readlineb_w(client_rio, buf, MAXLINE)) != 0){
byte_size += n;
if(!strncasecmp(buf, "Content-Length: ", 14))
sscanf(buf+15, "%zu", &content_length);
if(Rio_writen_w(connfd, buf, strlen(buf)) == -1)
return 0;
if(debug_mode)
printf("(%zu bytes) %s", strlen(buf), buf);
if(!strncmp(buf, "\r\n", 2))
break;
}
if(n == 0)
return 0;
if(debug_mode){
printf("Receive ALL Response Headers! Done!\n");
printf(">>> Response bodys:\n");
}
/* Response Body */
for(int i=0; i<content_length; i++){
if(Rio_readnb_w(client_rio, buf, 1) == 0)
return 0;
byte_size ++;
if(Rio_writen_w(connfd, buf, 1) == -1)
return 0;
if(debug_mode)
printf("%c", *buf);
}
if(debug_mode)
printf("\nReceive All Response Body! Done! Content-Length: %zu\n", content_length);
return byte_size;
}
/*
* 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, char *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");
if (hostend == NULL)
return -1;
len = hostend - hostbegin;
strncpy(hostname, hostbegin, len);
hostname[len] = '\0';
/* Extract the port number */
if (*hostend == ':') {
char *p = hostend + 1;
while (isdigit(*p))
*port++ = *p++;
*port = '\0';
} else {
strcpy(port, "80");
}
/* 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), the number of bytes
* from the server (size).
*/
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr,
char *uri, size_t 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 12, 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 %zu", time_str, a, b, c, d, uri, size);
}