📄 main.c
字号:
/*#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/wait.h>#include <signal.h>#include <stdio.h>#include <libgen.h>#include <fcntl.h>#include <ctype.h>#include <time.h>*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/wait.h>#include <signal.h>#include <stdio.h>#include <libgen.h>#include <fcntl.h>#include <ctype.h>#include <time.h>//#include <pthread.h>struct newthread{ char ip[16]; int sfd;};char serverpath[150];int serverport=8888;char rheader[2048];char wheader[2048];char password[20]="password";int contentsize=0;int needauth=0;char defaultcgiext[10]="cgi";//默认程序扩展名为cgichar defaultdoc[100]="index.html";//默认首页char returndata[120];char isauth=0;char cgiconffile[254]="./cgi.conf";//配置文件char request[2048];char requestcmd[2048]="";char requestparam[2048]="";/*******************************Read & write Option function******************************/struct rwoption { char name[512]; char value[512]; struct rwoption *prev; struct rwoption *next;};void delblank(char *lstr)//删除字符串尾空白字符{ int i; char tmpstr[512]; for(i=strlen(lstr)-1;i>=0;i--) if(*(lstr+i)==' ') *(lstr+i)='\0'; else break; for(i=0;i<strlen(lstr);i++) if(*(lstr+i)!=' ') break; memset(tmpstr,0,512); strcpy(tmpstr,(char *)(lstr+i)); memset(lstr,0,strlen(lstr)); strcpy(lstr,tmpstr);}int readoption(FILE *fp,char *paraname,char *value)//从参数文件读出参数{ char o1[512],*o2; char s1[512],s2[512]; fseek(fp,0L,SEEK_SET); while(!feof(fp)) { fscanf(fp,"%[^\n]\n",o1);//读一行// printf("%s\n",o1); o2=strstr(o1,"=")+1; memset(s1,0,512); memset(s2,0,512); strncpy(s1,o1,o2-o1-1); strcpy(s2,o2); delblank(s1); delblank(s2); if(strcasecmp(paraname,s1)==0)//比较参数名 { strcpy(value,s2);//得到参数值 return 1; } } return 0;}int writeoption(char *filename,char *paraname,char *value)//写参数到参数文件{ FILE *fp=NULL; char o1[512],*o2; char s1[512],s2[512]; struct rwoption *s=NULL,*l=NULL,*t=NULL; if((fp=fopen(filename,"r"))==NULL) return 0; fseek(fp,0L,SEEK_SET); while(!feof(fp)) { fscanf(fp,"%[^\n]\n",o1); o2=strstr(o1,"=")+1; memset(s1,0,512); memset(s2,0,512); strncpy(s1,o1,o2-o1-1); if(strcasecmp(paraname,s1)==0) strcpy(s2,value); else strcpy(s2,o2); delblank(s1); delblank(s2); if(s==NULL) { s=(struct rwoption *)malloc(sizeof(struct rwoption)); s->prev=NULL; s->next=NULL; l=s; } else { l->next=(struct rwoption *)malloc(sizeof(struct rwoption)); l->next->prev=l; l=l->next; l->next=NULL; } strcpy(l->name,s1); strcpy(l->value,s2); } fclose(fp); if((fp=fopen(filename,"w"))==NULL) return 0; l=s; t=l; while(l) { fprintf(fp,"%s=%s\n",l->name,l->value); l=t->next; free(t); t=l; } fclose(fp); return 1;}/*******************************Read & write Option function******************************/void log(char *s)//打印日志信息{ time_t timer; char lt[50]; struct tm *tblock; timer = time(NULL); tblock = localtime(&timer); strcpy(lt,asctime(tblock)); lt[strlen(lt)-1]='\0'; printf("%s - %s\n",lt,s);}/***********************************base64 Decode*****************************************/ unsigned char B64[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,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,48,49,50,51,52,53, 54,55,56,57,43,47};int base64decode(void *pinput,void * poutput,int Size)//base64解码{ int i, j, iptr, optr; unsigned char Temp[4]; unsigned char *Input, *Output; int rtvalue=0; Input = (unsigned char *)pinput; Output = (unsigned char *)poutput; iptr = 0; optr = 0; for(i=1;i<Size/4+1;i++) { for(j=0;j<4;j++) { if(Input[iptr]>=65&&Input[iptr]<=90) Temp[j] = Input[iptr] - 'A'; if(Input[iptr]>=97&&Input[iptr]<=122) Temp[j] = Input[iptr] - 'a' + 26; if(Input[iptr]>=48&&Input[iptr]<=57) Temp[j] = Input[iptr] - '0' + 52; if(Input[iptr]==43) Temp[j] = 62; if(Input[iptr]==47) Temp[j] = 63; if(Input[iptr]==61) Temp[j] = 0xFF; iptr++; } Output[optr] = (Temp[0] << 2) | (Temp[1] >> 4); if ((Temp[2]!= 0xFF) && (Temp[3]== 0xFF)) { Output[optr+1] = (Temp[1] << 4) | (Temp[2] >> 2); rtvalue = optr+2; optr++; } else if (Temp[2]!= 0xFF) { Output[optr+1] = (Temp[1] << 4) | (Temp[2] >> 2); Output[optr+2] = (Temp[2] << 6) | Temp[3]; rtvalue = optr+3; optr+=2; }; optr++; } return 1;} /***********************************base64 Decode*****************************************/char * filetype(char *filename){ char a1[128]; char a2[128]; sscanf(filename,"%[^.].%s",a1,a2);//a1得到文件名 a2得到文件扩展名 if(strcasecmp(a2,"jpg")==0 || strcmp(a2,"jpge")==0) { strcpy(returndata,"image/jpeg"); } else if(strcasecmp(a2,"gif")==0) { strcpy(returndata,"image/gif"); } else if(strcasecmp(a2,"ezw")==0) { strcpy(returndata,"ezw"); } else if(strcasecmp(a2,"html")==0 || strcmp(a2,"htm") ==0) { strcpy(returndata,"text/html"); } else { strcpy(returndata,"file/unknown"); } return(returndata);}int sendfile(FILE *fp, int socket){ char filedata[2048]; int rlen=0; int slen; while(1) { rlen=fread(filedata,1,80,fp); if(rlen<=0) break; slen=send(socket, filedata, rlen, 0); if (slen == -1) perror("\nsend from client"); else if (slen<rlen) log("not send all to client!"); } return 0;}void readconfig(void){ FILE *fp; char option1[20],option2[100]; strcpy(serverpath,"./www/"); serverport=1080; needauth=0; getcwd(cgiconffile,254); strcat(cgiconffile,"/cgi.conf"); //printf("%s\n",cgiconffile); strcpy(defaultcgiext,"cgi"); if((fp=fopen("miniweb.conf","r"))==NULL) { log("Error read the miniweb.conf,Use Default Configs!"); } else { while(!feof(fp)) { fscanf(fp,"\n%[^=]=%s\n",option1,option2); //strcpy(option1,(char *)strlwr(option1)); delblank(option1); if(strcasecmp(option1,"port")==0) serverport=atoi(option2); if(strcasecmp(option1,"homedir")==0) strcpy(serverpath,option2); if(strcasecmp(option1,"cgiext")==0) strcpy(defaultcgiext,option2); if(strcasecmp(option1,"needauth")==0) needauth=atoi(option2); if(strcasecmp(option1,"defaultdoc")==0) strcpy(defaultdoc,option2); if(strcasecmp(option1,"password")==0) strcpy(password,option2); if(strcasecmp(option1,"cgiconfig")==0) strcpy(cgiconffile,option2); } } log("Read the config completed!"); //printf("Home dir:%s\nServer Port:%d\nDefault CGI Ext:%s\nPassword:%s\n",serverpath,serverport,defaultcgiext,password); return;}char * readheadervalue(char *name){ char *lsub; lsub=strstr(rheader,name); if(lsub) { strcpy(returndata,lsub); return(returndata); } return NULL;}int readheader(int socket){ int readlen=recv(socket,(void *)rheader,2048,0); if(readlen==-1) perror("Error Read Header request\n"); return readlen;}int writeheader(int socket,int code,char * contenttype){ char codestr[100]; if(code==200) strcpy(codestr,"200 OK"); if(code==404) strcpy(codestr,"404 Not Found"); if(code==400) strcpy(codestr,"400 Bad Request"); if(code==401) strcpy(codestr,"401 Unauthorized\nConnection: close\nWWW-Authenticate: Basic realm=\"Netstorage Setup\"");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -