/
proxy.c
286 lines (246 loc) · 7.42 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
/*
* Name : Dalong Cheng, Fan Xiang
* Andrew ID: dalongc, fanx
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "csapp.h"
#include "cache.h"
#define MAX_OBJECT_SIZE ((1 << 10) * 100)
#define state_offset 9
#define move_offset 9
int min(int a, int b) {
return a > b ? b : a;
}
typedef struct {
int client_fd;
} Thread_Input;
void proxy_error(char *msg) {
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
}
static inline void copy_single_line_str(rio_t *client_rio, char *buffer) {
if (rio_readlineb(client_rio, buffer, MAXLINE) < 0) {
proxy_error("copy_single_lienstr error");
}
buffer[strlen(buffer)] = '\0';
}
static inline void extract_hostname(char *src, char *desc) {
int copy_length;
copy_length = strlen(src + 6) - 2;
strncpy(desc, src + 6, copy_length);
desc[copy_length] = '\0';
}
void modify_request_header(Request *request) {
char *str;
if ((str = strstr(request->request_str, "HTTP/1.1")) != NULL) {
strncpy(str, "HTTP/1.0", 8);
} else {
fprintf(stderr, "Warning: HTTP/1.1 not found in request\n");
}
#ifdef DEBUG
printf("modify request str:%s", request->request_str);
#endif
}
void send_client(int client_fd, Response *response) {
#ifdef DEBUG
printf("send_client !!!!!!!!!!\n");
#endif
if (response != NULL) {
if (response->content != NULL) {
if (rio_writen(client_fd, response->content,
response->content_size) < 0) {
proxy_error("rio_writen in send_client error");
}
} else {
fprintf(stderr, "error in send_client, content is NULL\n");
abort();
}
} else {
fprintf(stderr, "error in send_client, response is NULL\n");
abort();
}
}
static inline int extract_port_number(char *resquest_str) {
return 80;
}
void forward_response(int client_fd, int server_fd, Response *response) {
#ifdef DEBUG
printf("enter forward_response\n");
#endif
size_t n;
int length = -1;
char header_buffer[MAXLINE];
char content_buffer[MAX_OBJECT_SIZE];
int read_size;
rio_t server_rio;
int header_line = 0;
int move_flag = 0;
char move_new_address[MAXLINE];
rio_readinitb(&server_rio, server_fd);
while ((n = rio_readlineb(&server_rio, header_buffer, MAXLINE)) != 0) {
strcat(response->header, header_buffer);
//parse response
//if return status code is 3XX
//address moved.
//need to send another request to the indicated place
//until get 2XX or 4XX or 5XX.
header_line ++;
//status code 3XX
if((header_line == 1) && (header_buffer[state_offset] == '3')) {
move_flag =1;
}
if((move_flag == 1) && strstr(header_buffer,"Location:")) {
//moved to new address.
//I suggest change the address in request and return. then send request again.
strcat(move_new_address, (header_buffer+move_offset));
}
//add exit here
/*
if (rio_writen(client_fd, header_buffer, n) < 0) {
proxy_error("rio_writen in forward_response header error");
}*/
if (strstr(header_buffer, "Content-Length: ")) {
sscanf(header_buffer + 16, "%d", &length);
}
if (!strcmp(header_buffer, "\r\n")) {
break;
}
}
if (length == -1)
read_size = MAX_OBJECT_SIZE;
else
read_size = min(length, MAX_OBJECT_SIZE);
#ifdef DEBUG
printf("finish response header\n");
#endif
int sum = 0;
while ((n = rio_readnb(&server_rio, content_buffer, read_size)) != 0) {
if (rio_writen(client_fd, content_buffer, n) < 0) {
proxy_error("rio_writen in forward_response content error");
Close(client_fd);
}
sum += n;
}
#ifdef DEBUG
printf("read byte size:%u\n", sum);
#endif
if (sum <= MAX_OBJECT_SIZE) {
response->content = Malloc(sizeof(char) * sum);
memcpy(response->content, content_buffer, sum * sizeof(char));
response->content_size = sum;
} else {
response->content_size = sum;
}
#ifdef DEBUG
printf("leave forward_response\n");
#endif
}
int forward_request(int client_fd, Request *request, Response *response) {
rio_t server_rio;
int server_fd;
char hostname[MAXLINE];
int port = 80;
port = extract_port_number(request->request_str);
extract_hostname(request->host_str, hostname);
#ifdef DEBUG
printf("hostname:%s\n", hostname);
printf("port:%d\n", port);
#endif
if ((server_fd = open_clientfd(hostname, port)) < 0) {
fprintf(stderr, "Warning connection refused !\n");
return -1;
}
rio_readinitb(&server_rio, server_fd);
#ifdef DEBUG
printf("request_str:%s", request->request_str);
#endif
rio_writen(server_fd, request->request_str, strlen(request->request_str));
rio_writen(server_fd, request->host_str, strlen(request->host_str));
rio_writen(server_fd, "\r\n", strlen("\r\n"));
forward_response(client_fd, server_fd, response);
Close(server_fd);
return 1;
}
void parse_request_header(int client_fd, Request *request) {
size_t n;
rio_t client_rio;
char buffer[MAXLINE];
rio_readinitb(&client_rio, client_fd);
copy_single_line_str(&client_rio, request->request_str);
copy_single_line_str(&client_rio, request->host_str);
#ifdef DEBUG
printf("request str:%s", request->request_str);
printf("host str: %s", request->host_str);
#endif
while ((n = rio_readlineb(&client_rio, buffer, MAXLINE)) != 0) {
#ifdef DEBUG
printf("%s", buffer);
#endif
if (!strcmp(buffer, "\r\n")) {
break;
}
}
}
void *request_handler(void *ptr) {
#ifdef DEBUG
printf("enter request_handler\n");
#endif
int client_fd = ((Thread_Input*)ptr)->client_fd;
Request request;
Response response;
parse_request_header(client_fd, &request);
modify_request_header(&request);
if (check_cache(&request, &response)) {
send_client(client_fd, &response);
} else {
if (forward_request(client_fd, &request, &response) < 0) {
Close(client_fd);
return NULL;
} else {
if (response.content_size <= MAX_OBJECT_SIZE)
save_to_cache(&request, &response);
}
}
free(ptr);
Close(client_fd);
#ifdef DEBUG
printf("connection close\n\n");
printf("leave request_handler\n");
#endif
return NULL;
}
/*
* scheduler -
*/
void scheduler(int client_fd) {
pthread_t tid;
Thread_Input *thread_input = (Thread_Input*)malloc(sizeof(Thread_Input));
thread_input->client_fd = client_fd;
Pthread_create(&tid, NULL, request_handler, thread_input);
Pthread_join(tid, NULL);
//Pthread_detach(tid);
}
int main (int argc, char *argv []) {
int listen_fd, port;
int client_fd;
socklen_t socket_length;
struct sockaddr_in client_socket;
pthread_mutex_init(&mutex_lock, NULL);
init_cache();
Signal (SIGPIPE, SIG_IGN);
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
port = atoi(argv[1]);
listen_fd = Open_listenfd(port);
socket_length = sizeof(client_socket);
while (1) {
client_fd = Accept(listen_fd, (SA*)&client_socket, &socket_length);
scheduler(client_fd);
}
pthread_mutex_destroy(&mutex_lock);
clean_cache();
}