webserver 主程序

//要求:
//监听端口8080
//将浏览器发给服务器的请求的头信息输出
//定义结构体保存请求行信息
//请求路径和发布路径合成绝对路径
//判断文件是否存在如果存在判断文件权限是否可读如果权限允许将文件会送给浏览器如果文加不存在或者文件权限不允许给浏览器报错;
//保存浏览器请求首行信息
//处理客户端请求
//fd和客户端链接描述符
#include"webserv.h"


char postdir[]="./";//发布地址
char ldir[128]={0};//绝对路径
char pictmsg[4000]={0};
char noper[100]="don't have read permission";//没有查看权限
char notfound[100]="file not found!!";//没找到
char flnm[1024]={0};//请求文件为文件夹时文件夹所有文件名
char files[1024]={0};//请求文档文件文件内容
char succode[4]="200";//成功响应码
char nfoundcode[4]="404";
char nopercode[4]="403";
char txtfile[24]="text/html\r\n";//
char pictfile[24]="image/jpg\r\n";
    char pictfile1[24]="image/png\r\n";
int Connect(int port)
{
int fd=socket(AF_INET,SOCK_STREAM,0);
adf.sin_family=AF_INET;
adf.sin_port=htons(port);
//int ip=inet_pton(AF_INET,"176.211.99.81",&(adf.sin_addr.s_addr));
adf.sin_addr.s_addr=htonl(INADDR_ANY);
int b=bind(fd,(comm *)&adf,sizeof(adf));
int l=listen(fd,4);
return fd;
}
void response_c(int fd,char*mseg,char*code,char *filetype)
{
//printf("filetype%s\nmseg%s\n",filetype,mseg);
char str1[128]="HTTP/1.1";
strcat(str1,code);
char str2[128]="Content-Type:";
strcat(str2,filetype);
char str3[128]="Connection: close\r\n\r\n";
write(fd,str1,strlen(str1));
write(fd,str2,strlen(str2));
write(fd,str3,strlen(str3));
write(fd,"<html>",7);
     write(fd,"<head><title>MyWebSever</title>",31);
      write(fd,"</head>",6);
       write(fd,"<body>",6);
       write(fd, mseg,strlen(mseg));
       write(fd,"</body>",6);
        write(fd,"</html>",6);
/* strcat(str4,mseg);
strcat(str4,str5);
write(fd,str1,strlen(str1));
write(fd,str2,strlen(str2));
write(fd,str3,strlen(str3));
write(fd,str4,strlen(str4));*/
return;
}


void parse_f_line(char*buf)
{
//char*p=strstr(buf,"GET");
//strncat(reg->protocol,p+4,4);
//write(1,reg->protocol,4);
sscanf(buf,"%s %s %s",f.method,f.path,f.protocol);
return;
}
void  doit(int fd){
char buf[128]={0};
int r=read(fd,buf,128);       //获取客户端请求信息
write(1,buf,r);  //处理客户端请求信息
//将请求行的首行信息解析到
parse_f_line(buf);
printf("\nmethod:%s\tpath:%s\tprotocol:%s\n",f.method,f.path,f.protocol);
return;
}


int lookup(char*dir)
{
printf("------%s\n",dir);
struct stat buf;
int st=stat(dir,&buf);
if(st==-1)
{
perror("stat");
return -1;
}
if((S_IRWXG&buf.st_mode)<00040)
return 3;

    int len=strlen(dir);
char*l=dir+len-3;
strncpy(l,l,3);
printf("文件后%s\n",l);
if(l=="png"||l=="jpg"){
 int ffd = open(dir,O_RDONLY);
    int rp = 0;
    char bup[128] = {0};
    while(rp =read(ffd,bup,128)){
    strncat(pictmsg,bup,rp);
     }
    if(l=="png")
     return 5;
    return 4;
    }
if(S_ISDIR(buf.st_mode))


{
travdir(dir);
return 2;
}
FILE*RD=fopen(dir,"r");
char*s=(char*)malloc(300);
while(fgets(s,300,RD))
{
strcat(files,"<p>");
strcat(files,s);
strcat(files,"</p>");
}
free (s);
s=NULL;
free (RD);
RD=NULL;
//printf("%s/n",files);
return 1;
}
void travdir(char*dir)
{
struct dirent *tr;
DIR*op=opendir(dir);
strcat(flnm,"<p>This Is Directories</p><ul>");
while(tr=readdir(op))
{
if((strcmp(tr->d_name,".")&&strcmp(tr->d_name,"\0")&&(strcmp(tr->d_name,".."))))
{
strcat(flnm,"<p><li>");
strcat(flnm,tr->d_name);
strcat(flnm,"</li></p>");
}
}
strcat(flnm,"</ol>");
return;
}
void communications(int fd)
{
int m=sizeof(acad);
printf("wait connect......\n");
int a_fd=accept(fd,(comm *)&acad,&m);


pid_t pid;
pid=fork();
if(pid==0){
void*acadc=(void*)malloc(20);
inet_ntop(AF_INET,&acad.sin_addr.s_addr,acadc,20);
doit(a_fd);
printf("访客IP:%s\n",(char*)acadc);
free(acadc);
acadc=NULL;
pid=getpid();
printf("当前进程pid:%d\n",pid);

    strcpy(ldir,postdir);   
        if(strcmp(f.path,"/favicon.ico")) 
    strcat(ldir,f.path);
int con=lookup(ldir);
if(con==-1)
response_c(a_fd,notfound,nfoundcode,txtfile);
if(con==3)
response_c(a_fd,noper,nopercode,txtfile);
if(con==1)
response_c(a_fd,files,succode,txtfile);
if(con==2)
response_c(a_fd,flnm,succode,txtfile);
if(con==4)
{
response_c(a_fd,pictmsg,succode,pictfile);
                         


}
if(con==5)
{
response_c(a_fd,pictmsg,succode,pictfile1);
                         


}
sleep(1);
close(a_fd);
exit(1);
}
if(pid!=0)
{
int o=1;
wait(&o);
printf("退出状态码:%d\n",o);
close(a_fd);
}
return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值