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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
| #include <stdio.h> #include "csapp.h"
#define MAX_CACHE_SIZE 1049000 #define MAX_OBJECT_SIZE 102400
#define NTHREADS 4 #define SBUFSIZE 16
#define CACHE_OBJS_COUNT 20
typedef struct { int *buf; int n; int front; int rear; sem_t mutex; sem_t slots; sem_t items; } sbuf_t;
void sbuf_init(sbuf_t *sp, int n); void sbuf_deinit(sbuf_t *sp); void sbuf_insert(sbuf_t *sp, int item); int sbuf_remove(sbuf_t *sp);
typedef struct { char uri[MAXLINE]; char object[MAX_OBJECT_SIZE]; size_t size; int valid; unsigned long lru_time; } cache_block_t;
typedef struct { cache_block_t blocks[CACHE_OBJS_COUNT]; size_t total_size; unsigned long global_time; int readcnt; sem_t mutex; sem_t w; } cache_t;
void cache_init(void); int cache_find_and_send(const char *uri, int connfd); void cache_insert(const char *uri, const char *buf, size_t size);
struct Uri { char host[MAXLINE]; char port[MAXLINE]; char path[MAXLINE]; };
static const char *user_agent_hdr = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n";
sbuf_t sbuf; cache_t cache;
void *thread(void *vargp); void doit(int connfd); void parse_uri(char *uri, struct Uri *uri_data); void build_header(char *http_header, size_t max_len, struct Uri *uri_data, rio_t *client_rio);
int main(int argc, char **argv) { int listenfd, connfd; socklen_t clientlen; struct sockaddr_storage clientaddr; pthread_t tid;
if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); }
Signal(SIGPIPE, SIG_IGN);
listenfd = Open_listenfd(argv[1]);
sbuf_init(&sbuf, SBUFSIZE); cache_init(); for (int i = 0; i < NTHREADS; i++) { Pthread_create(&tid, NULL, thread, NULL); }
while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); sbuf_insert(&sbuf, connfd); }
sbuf_deinit(&sbuf); return 0; }
void *thread(void *vargp) { Pthread_detach(pthread_self()); while (1) { int connfd = sbuf_remove(&sbuf); doit(connfd); Close(connfd); } return NULL; }
void doit(int connfd) { char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char uri_copy[MAXLINE]; char server[MAXLINE * 2]; rio_t rio, server_rio;
Rio_readinitb(&rio, connfd); if (Rio_readlineb(&rio, buf, MAXLINE) <= 0) { return; } sscanf(buf, "%s %s %s", method, uri, version);
if (strcasecmp(method, "GET")) { printf("Proxy does not implement the method: %s\n", method); return; }
strncpy(uri_copy, uri, sizeof(uri_copy) - 1); uri_copy[sizeof(uri_copy) - 1] = '\0';
if (cache_find_and_send(uri_copy, connfd)) { return; }
struct Uri *uri_data = (struct Uri *)Malloc(sizeof(struct Uri)); parse_uri(uri, uri_data); build_header(server, sizeof(server), uri_data, &rio);
int serverfd = Open_clientfd(uri_data->host, uri_data->port); if (serverfd < 0) { printf("connection failed\n"); Free(uri_data); return; }
Rio_readinitb(&server_rio, serverfd); Rio_writen(serverfd, server, strlen(server));
char cache_buf[MAX_OBJECT_SIZE]; size_t total_bytes = 0; ssize_t n;
while ((n = Rio_readnb(&server_rio, buf, MAXLINE)) > 0) { Rio_writen(connfd, buf, n); if (total_bytes + n <= MAX_OBJECT_SIZE) { memcpy(cache_buf + total_bytes, buf, n); } total_bytes += n; }
if (total_bytes > 0 && total_bytes <= MAX_OBJECT_SIZE) { cache_insert(uri_copy, cache_buf, total_bytes); }
Close(serverfd); Free(uri_data); }
void parse_uri(char *uri, struct Uri *uri_data) { uri_data->host[0] = '\0'; strcpy(uri_data->port, "80"); strcpy(uri_data->path, "/");
char *hostpose = strstr(uri, "//"); if (hostpose == NULL) { char *pathpose = strstr(uri, "/"); if (pathpose != NULL) { strcpy(uri_data->path, pathpose); } return; } else { char *portpose = strstr(hostpose + 2, ":"); if (portpose != NULL) { int tmp; sscanf(portpose + 1, "%d%s", &tmp, uri_data->path); snprintf(uri_data->port, sizeof(uri_data->port), "%d", tmp); *portpose = '\0'; } else { char *pathpose = strstr(hostpose + 2, "/"); if (pathpose != NULL) { strcpy(uri_data->path, pathpose); *pathpose = '\0'; } } strcpy(uri_data->host, hostpose + 2); } }
void build_header(char *http_header, size_t max_len, struct Uri *uri_data, rio_t *client_rio) { char buf[MAXLINE], request_hdr[MAXLINE], other_hdr[MAXLINE], host_hdr[MAXLINE];
snprintf(request_hdr, sizeof(request_hdr), "GET %s HTTP/1.0\r\n", uri_data->path); other_hdr[0] = '\0'; host_hdr[0] = '\0';
while (Rio_readlineb(client_rio, buf, MAXLINE) > 0) { if (strcmp(buf, "\r\n") == 0) { break; } if (!strncasecmp(buf, "Host:", 5)) { strncpy(host_hdr, buf, sizeof(host_hdr) - 1); host_hdr[sizeof(host_hdr) - 1] = '\0'; continue; } if (!strncasecmp(buf, "User-Agent:", 11) || !strncasecmp(buf, "Connection:", 11) || !strncasecmp(buf, "Proxy-Connection:", 17)) { continue; } if (strlen(other_hdr) + strlen(buf) < sizeof(other_hdr) - 1) { strcat(other_hdr, buf); } }
if (strlen(host_hdr) == 0) { snprintf(host_hdr, sizeof(host_hdr), "Host: %s\r\n", uri_data->host); }
snprintf(http_header, max_len, "%s%s%s%s%s%s\r\n", request_hdr, host_hdr, "Connection: close\r\n", "Proxy-Connection: close\r\n", user_agent_hdr, other_hdr); }
void sbuf_init(sbuf_t *sp, int n) { sp->buf = Calloc(n, sizeof(int)); sp->n = n; sp->front = sp->rear = 0; Sem_init(&sp->mutex, 0, 1); Sem_init(&sp->slots, 0, n); Sem_init(&sp->items, 0, 0); }
void sbuf_deinit(sbuf_t *sp) { Free(sp->buf); }
void sbuf_insert(sbuf_t *sp, int item) { P(&sp->slots); P(&sp->mutex); sp->buf[(++sp->rear) % (sp->n)] = item; V(&sp->mutex); V(&sp->items); }
int sbuf_remove(sbuf_t *sp) { int item; P(&sp->items); P(&sp->mutex); item = sp->buf[(++sp->front) % (sp->n)]; V(&sp->mutex); V(&sp->slots); return item; }
void cache_init(void) { cache.total_size = 0; cache.global_time = 0; cache.readcnt = 0; Sem_init(&cache.mutex, 0, 1); Sem_init(&cache.w, 0, 1); for (int i = 0; i < CACHE_OBJS_COUNT; i++) { cache.blocks[i].valid = 0; cache.blocks[i].size = 0; cache.blocks[i].lru_time = 0; } }
int cache_find_and_send(const char *uri, int connfd) { int hit = 0; char local_buf[MAX_OBJECT_SIZE]; size_t local_size = 0;
P(&cache.mutex); cache.readcnt++; if (cache.readcnt == 1) { P(&cache.w); } V(&cache.mutex);
for (int i = 0; i < CACHE_OBJS_COUNT; i++) { if (cache.blocks[i].valid && strcmp(cache.blocks[i].uri, uri) == 0) { hit = 1; local_size = cache.blocks[i].size; memcpy(local_buf, cache.blocks[i].object, local_size);
P(&cache.mutex); cache.blocks[i].lru_time = ++cache.global_time; V(&cache.mutex); break; } }
P(&cache.mutex); cache.readcnt--; if (cache.readcnt == 0) { V(&cache.w); } V(&cache.mutex);
if (hit) { Rio_writen(connfd, local_buf, local_size); } return hit; }
void cache_insert(const char *uri, const char *buf, size_t size) { P(&cache.w);
while (cache.total_size + size > MAX_CACHE_SIZE) { int victim = -1; unsigned long min_time = (unsigned long)-1; for (int i = 0; i < CACHE_OBJS_COUNT; i++) { if (cache.blocks[i].valid && cache.blocks[i].lru_time < min_time) { min_time = cache.blocks[i].lru_time; victim = i; } } if (victim == -1) { break; } cache.blocks[victim].valid = 0; cache.total_size -= cache.blocks[victim].size; }
int target = -1; for (int i = 0; i < CACHE_OBJS_COUNT; i++) { if (!cache.blocks[i].valid) { target = i; break; } } if (target == -1) { unsigned long min_time = (unsigned long)-1; for (int i = 0; i < CACHE_OBJS_COUNT; i++) { if (cache.blocks[i].lru_time < min_time) { min_time = cache.blocks[i].lru_time; target = i; } } cache.total_size -= cache.blocks[target].size; }
strncpy(cache.blocks[target].uri, uri, MAXLINE - 1); cache.blocks[target].uri[MAXLINE - 1] = '\0'; memcpy(cache.blocks[target].object, buf, size); cache.blocks[target].size = size; cache.blocks[target].valid = 1; cache.blocks[target].lru_time = ++cache.global_time; cache.total_size += size;
V(&cache.w); }
|