-
大小: 9KB文件類型: .c金幣: 1下載: 1 次發(fā)布日期: 2021-06-08
- 語言: 其他
- 標(biāo)簽: csapp??proxy??lab??滿分原創(chuàng)??
資源簡介
可以拿到滿分的網(wǎng)絡(luò)代理,采用讀者優(yōu)先的方式避免競爭,避免了pipe broken造成的服務(wù)器中斷。
代碼片段和文件信息
#include?
#include?“csapp.h“
#include?
//the?const?variable?below?are?requested?in?the?reference
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“;
static?const?char?*connect_hdr?=?“Connection:?close\r\n“;
static?const?char?*proxy_hdr?=?“Proxy-Connection:?close\r\n“;
static?const?char?*connection_key?=?“Connection“;
static?const?char?*user_agent_key=?“User-Agent“;
static?const?char?*proxy_connection_key?=?“Proxy-Connection“;
static?const?char?*host_key?=?“Host“;
//the?micro?below?show?some?string?type?and?cache?size
#define?EOF_TYPE?1
#define?HOST_TYPE?2
#define?OTHER_TYPE?3
#define?MAX_CACHE_SIZE?1049000
#define?MAX_object_SIZE?102400
#define?CACHE_NUM?10
int?getType(char?*buf);
void?doit(int?connfd);
void?parseUri(char?uri[]char?hostname[]char?path[]char?port[]);
void?build_http
(char?*server_httpchar?*hostnamechar*pathchar*?portrio_t?*clientrio);
void*?thread(void?*vargp);
void?initCache();
void?PreRead(int?index);
void?afterRead(int?index);
void?preWrite(int?index);
void?afterWrite(int?index);
int?findCache(char?*url);
void?updateLRU(int?index);
int?findSuitCache();
void?writeCacheContent(char?*urlchar*?buf);
//the?struct?of?a?cacheLine?for?a?file
typedef?struct?
{
????char?content[MAX_object_SIZE];
????char?url[MAXLINE];
????int?time;
????int?isEmpty;
????int?readCount;
????int?writeCount;
????sem_t?mutex;
????sem_t?w;
}cacheunit;
//the?cache?struct?consist?of?ten?cacheLine
typedef?struct?
{
????cacheunit?cacheUnit[CACHE_NUM];
}Cache;
Cache?cache;
//record?the?overall?time?to?update?the?cacheline?time
int?allTime=0;
//the?flag?is?used?to?void?compete?of?“allTime“
sem_t?flag;
/*
the?main?function?to?connect?the?client??and?it?is?same?to?
the?code?in?the?page?672?of?textbook
*/
int?main(int?argcchar?**argv)
{
????int?listenfd*connfd;
????char?hostname[MAXLINE]port[MAXLINE];
????socklen_t?clientlen;
????struct??sockaddr_storage?clientaddr;
????pthread_t?tid;
????Signal(SIGPIPE?SIG_IGN);
????Sem_init(&flag01);
????//?the?cmd?parameters?fail
????if?(argc!=2){
????????fprintf(stderr?“usage:?%s?\n“argv[0]);
????????exit(1);
????}
????initCache();
????listenfd=?open_listenfd(argv[1]);
????while?(1)
????{
????????clientlen=sizeof(clientaddr);
????????connfd=malloc(sizeof(int));
????????*connfd=Accept(listenfd(SA*)&clientaddr&clientlen);
????????Getnameinfo((SA*)&clientaddrclientlenhostnameMAXLINE
????????????????????portMAXLINE0);
????????printf(“Accepted?connection?from?(%s??%s)\n“hostnameport);
????????Pthread_create(&tidNULLthread(void*)connfd);
????}
}
//create?a?new?thread?to?run?for?servering?the?client
void?*thread(void?*vargo)
{
????int?connfd?=?*((int?*)vargo);
????Pthread_detach(pthread_self());
????free(vargo);
????doit(connfd);
????Close(connfd);
????return?NULL;
}
//the?main?process?to?get?the?cmdparse?the?cmdfind?the?cache
//build?the?headerconnect?to?the?server?and?return?the?content?
//to?th
評論
共有 條評論