我试图实现一个简单的代理服务器,到目前为止,我已经设法正确地执行了GET方法,但现在我必须为它实现一个缓存。它背后的想法是当我得到以下命令时:
我第二次要求从谷歌获取信息时应该来自我的缓存系统。我一直在思考,最好的方法是实现一个简单的文件系统,我将收到的信息存储在文件中,然后每当我发现文件存在时就读取它。我遇到的唯一问题是我无法创建http://elf.cs.edu.ro/等文件
这是我的代码,让您了解我正在做的事情。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
void error(char* msg) {
perror(msg);
exit(0);
}
int main(int argc,char* argv[]) {
int count = 0;
FILE *in;
FILE *out;
pid_t pid;
struct sockaddr_in addr_in,cli_addr,serv_addr;
struct hostent* host;
int sockfd,newsockfd;
if(argc<2)
error("./proxy <port_no>");
printf("\n*****WELCOME TO PROXY SERVER*****\n");
bzero((char*)&serv_addr,sizeof(serv_addr));
bzero((char*)&cli_addr, sizeof(cli_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr=INADDR_ANY;
sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sockfd<0)
error("Problem in initializing socket");
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
error("Error on binding");
listen(sockfd,50);
int clilen=sizeof(cli_addr);
startpoint:
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);
if(newsockfd<0)
error("Problem in accepting connection");
pid=fork();
//printf("Pid is: %d\n", pid);
if(pid==0) {
sites saves[10];
int noOfSites = 0;
struct sockaddr_in host_addr;
int flag=0,newsockfd1,n,port=0,i,sockfd1;
char buffer[510],t1[300],t2[300],t3[10], aux[510];
char* temp=NULL;
bzero((char*)buffer,500);
recv(newsockfd,buffer,sizeof(buffer),0);
//printf("%s\n",buffer);
sscanf(buffer,"%s %s %s",t1,t2,t3);
//printf("%s\n",buffer);
if (((strncmp(t1,"GET",3)==0))&&((strncmp(t3,"HTTP/1.0",8)==0)||(strncmp(t3,"HTTP/1.0",8)==0))&&(strncmp(t2,"http://",7)==0)) {
strcpy(t1,t2);
int saved = 0;
flag=0;
for(i=7;i<strlen(t2);i++) {
if(t2[i]==':') {
flag=1;
break;
}
}
// printf("The site to GET is : %s\n",t2);
temp=strtok(t2,"//");
if (flag==0) {
port=80;
temp=strtok(NULL,"/");
}
else {
temp=strtok(NULL,":");
}
sprintf(t2,"%s",temp);
host=gethostbyname(t2);
int it;
for (it = 0; it < noOfSites; it++){
printf("Site saved is: %s\n", saves[it].site);
if (strcmp(saves[it].site,t2) == 0){
saved = 1;
out = fopen(t2, "r");
printf("We have cache!\n");
}
}
if(flag==1) {
temp=strtok(NULL,"/");
port=atoi(temp);
}
strcat(t1,"^]");
temp=strtok(t1,"//");
temp=strtok(NULL,"/");
if(temp!=NULL)
temp=strtok(NULL,"^]");
bzero((char*)&host_addr,sizeof(host_addr));
host_addr.sin_port=htons(port);
host_addr.sin_family=AF_INET;
bcopy((char*)host->h_addr,(char*)&host_addr.sin_addr.s_addr,host->h_length);
sockfd1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
newsockfd1=connect(sockfd1,(struct sockaddr*)&host_addr,sizeof(struct sockaddr));
sprintf(buffer,"\nConnected to %s IP - %s\n",t2,inet_ntoa(host_addr.sin_addr));
if(newsockfd1<0)
error("Error in connecting to remote server");
bzero((char*)buffer,sizeof(buffer));
if(temp!=NULL)
sprintf(buffer,"GET /%s %s\r\nHost: %s\r\nConnection: close\r\n\r\n",temp,t3,t2);
else
sprintf(buffer,"GET / %s\r\nHost: %s\r\nConnection: close\r\n\r\n",t3,t2);
if (saved == 1){
printf("Are we ever here\n");
n = send(sockfd1,buffer,strlen(buffer),0);
char * line = NULL;
size_t len = 0;
ssize_t read;
int count = 0;
if (n<0)
error("Error writing to socket");
else {
while ((read = getline(&line, &len, out)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
if (count > 0)
write(newsockfd,line,read);
if (strcmp(line,"\r\n") == 0)
count++;
}
}
}
if (saved == 0){
//printf("We are only here\n");
strcpy(saves[noOfSites].site,t2);
noOfSites++;
printf("The new site file is: %d\n", noOfSites);
in = fopen(t2,"w");
n=send(sockfd1,buffer,strlen(buffer),0);
printf("I have opened for writing: %s\n\n\n\n\n\n",t2);
if(n<0)
error("Error writing to socket");
else {
do {
memset(buffer, 0, sizeof(buffer));
n=recv(sockfd1,buffer,sizeof(buffer),0);
fwrite(buffer, n, 1, stdout);
printf("\n");
fwrite(buffer, n, 1, in);
if(!(n<=0))
write(newsockfd,buffer,n);
} while(n>0);
}
}
}
else {
send(newsockfd,"400 : BAD REQUEST\nONLY HTTP REQUESTS ALLOWED",18,0);
}
close(sockfd1);
close(newsockfd);
close(sockfd);
fclose(in);
return 0;
}
else {
close(newsockfd);
goto startpoint;
}
return 0;
}
任何人都可以指出我正确的方向吗?我不希望别人为我做这件事,只是想一想你会怎么做,所以我可能有机会。谢谢你的时间!
答案 0 :(得分:0)
您正在寻找通用键/值数据存储。您可以在内存中使用哈希表,使用像gdbm这样的简单数据库,也可以选择像MongoDB这样的更高级的数据库。
答案 1 :(得分:0)
我真的很抱歉,如果我在路上犯了一些错误而且我没有发表好问题,但我已经设法自己解决了这个问题。我遇到的麻烦是,当我尝试创建文件来存储我从get命令中获取信息的文件时,我试图创建名称为http://something.com/的文件,它不会让我/因为/试图从C程序创建文件(或者我已经理解)。我的任务很简单,用其他东西替换/(在我的情况下_),这一切都很顺利。谢谢你的尝试,如果我让你们在试图了解我的意思时失去了时间,我真的很抱歉。
更确切地说,我要做的是当我第二次收到get命令时(同一个get命令使用相同的链接)我需要从我第一次创建的文件中获取信息我收到了命令。它应该是这样的:
再次,对不起,如果我困扰某人。非常感谢你!