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
| #define MAX_CACHE_SIZE 1049000 #define MAX_OBJECT_SIZE 102400
char BUF[MAX_CACHE_SIZE]; char TMP[MAX_OBJECT_SIZE]; int LRU[10]; int cnt = 0, clk = 0; char REC[10][2500]; sem_t mutex_cnt, write_lock, mutex_tmp;
void sigpipe_handler(int sig) { return; }
void init_sem() { sem_init(&mutex_cnt, 0, 1); sem_init(&mutex_tmp, 0, 1); sem_init(&write_lock, 0, 1); }
int read_cache(int fd, char *fn) { int sign = 0, id; P(&mutex_cnt); if (cnt == 0) P(&write_lock); cnt++; V(&mutex_cnt);
for (int i = 0; i < 10; i++) { if (LRU[i] != -1 && strcmp(fn, REC[i]) == 0) { Rio_writen(fd, &BUF[MAX_OBJECT_SIZE * i], MAX_OBJECT_SIZE); sign = 1; id = i; break; } }
P(&mutex_cnt); cnt--; if (sign) { LRU[id] = clk++; } if (cnt == 0) { V(&write_lock); } V(&mutex_cnt); return sign; }
void write_cache(char *fn, char *buf) { P(&write_lock); int cid, cv = INT_MAX; for (int i = 0; i < 10; i++) { if (LRU[i] < cv) { cv = LRU[i]; cid = i; } } LRU[cid] = clk++; strcpy(&BUF[cid * MAX_OBJECT_SIZE], buf); strcpy(REC[cid], fn); V(&write_lock); }
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";
void parse_cmd(char *url, char *host, char *port, char *method, char *uri);
void *exe_cmd(void *vargp) { Pthread_detach(pthread_self()); int client_fd, server_fd, stat; client_fd = *((int*)vargp); Free(vargp); char buf[MAXLINE], host[MAXLINE], port[MAXLINE], method[MAXLINE], uri[MAXLINE], header[MAXLINE]; char PAK[MAX_OBJECT_SIZE + 1], SIGN[MAXLINE]; char version[MAXLINE] = "HTTP/1.0"; rio_t rio_c, rio_s;
Rio_readinitb(&rio_c, client_fd); if (!Rio_readlineb(&rio_c, buf, MAXLINE)) return NULL; printf("%s", buf); parse_cmd(buf, host, port, method, uri); sprintf(SIGN, "%s %s %s %s", host, port, method, uri); printf("%s\n", SIGN);
int in_cache = read_cache(client_fd, SIGN); if (in_cache) { printf("IN!\n"); Close(client_fd); return NULL; } server_fd = Open_clientfd(host, port); Rio_readinitb(&rio_s, server_fd); sprintf(header, "%s %s %s\r\n", method, uri, version); sprintf(header, "%sConnection: close\r\n", header); sprintf(header, "%sProxy-Connection: close\r\n", header); sprintf(header, "%s%s\r\n", header, user_agent_hdr); printf("%s", header); Rio_writen(server_fd, header, sizeof(header));
int count = 0, sign = 0; while ((stat = Rio_readnb(&rio_s, PAK, sizeof(PAK))) > 0) { if (count == 0 && stat <= sizeof(PAK) - 1) { printf("GET!\n"); sign = 1; write_cache(SIGN, PAK); printf("%s", PAK); Rio_writen(client_fd, PAK, stat); } else { printf("LARGE!\n"); Rio_writen(client_fd, PAK, sizeof(PAK)); count++; } } Close(client_fd); Close(server_fd); return NULL; }
int main(int argc, char**argv) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; pthread_t tid; init_sem(); Signal(SIGPIPE, sigpipe_handler);
printf("start\n"); if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); }
listenfd = Open_listenfd(argv[1]); while (1) { clientlen = sizeof(clientaddr); int *tr = (int*)malloc(sizeof(int)); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); *tr = connfd; Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); pthread_create(&tid, NULL, exe_cmd, tr); }
return 0; }
void parse_cmd(char *url, char *host, char *port, char *method, char *uri) { char *ptr;
ptr = strchr(url, ' '); *ptr = '\0'; strcpy(method, url); ptr++; while (*ptr == ' ') ptr++; url = ptr; ptr = strstr(url, "http://"); if (ptr != NULL) url = ptr + 7;
ptr = strchr(url, ' '); if (ptr != NULL) *ptr = '\0';
ptr = strchr(url, '/'); if (ptr != NULL) { strcpy(uri, ptr); *ptr = '\0'; } else { strcpy(uri, "/"); }
ptr = strchr(url, ':'); if (ptr == NULL) { strcpy(port, "80"); } else { strcpy(port, ptr + 1); *ptr = '\0'; }
strcpy(host, url); }
|