cfv_http

cfv_http.c


#ifndef CFV_HTTP_H

#define CFV_HTTP_H
#include"cfv_http.h"
#endif


#ifndef CFV_SEARCH_H
#define CFV_SEARCH_H
#include"cfv_search.h"
#endif


#ifndef GET_CONFIGURE_H
#define GET_CONFIGURE_H
#include"get_configure.h"
#endif


int main(int argc,char **argv)
{
pid_t pid;
int listenfd,connfd,i;
socklen_t client_address_length;
static struct sockaddr_in server_address,client_address;




/*create deamon process*/
while((pid = fork())== -1);
if( pid > 0) exit(0); /*parent process exit*/
setsid(); /*runs a program in new session*/
        const char *ServerRoot = get_ServerRoot();/* get the cfv_http configure,ServerRoot*/
  chdir(ServerRoot);/*change the current work directory*/
umask(0); /*set the right of using file*/
signal(SIGCHLD,SIG_IGN);/*stop zombie happenning*/
close(0); /*close the open file*/
for(i=2;i<MAX_FILE;i++)
close(i); /*close the open file*/
operation_option(argc,argv);/* operation option such as start|stop|restart*/
close(1); /*close the open file*/
  chdir(ServerRoot);/*change the current work directory*/
        int SERVER_PORT = get_port();/* get the cfv_http configure,port*/


listenfd = socket(AF_INET,SOCK_STREAM,0); /*create a new socket*/


server_address.sin_family = AF_INET; /*IPv4*/
server_address.sin_addr.s_addr = htonl(INADDR_ANY);/*Any IP local host*/
server_address.sin_port = htons(SERVER_PORT);/*set the PORT*/

while(bind(listenfd,(struct sockaddr *)&server_address,sizeof(server_address)) == -1);/*bind the IP and port*/
while(listen(listenfd,20) == -1); /* listening and allow 20 client connected*/
client_address_length = sizeof(client_address);
while(1)
{
connfd = accept(listenfd,(struct sockaddr *)&client_address,&client_address_length);/*accept the connection and put the information in client_address*/
while((pid = fork())==-1);/*create a child process*/
if(pid == 0){
close(listenfd);
search(connfd);
} else close(connfd);
}
}




/* operation option such as start|stop|restart*/
void operation_option(int argc,char **argv)
{
if(argc < 3)
{
        printf("usage:%s -k|-help start|stop|restart\n",argv[0]);
exit(1);
}
        else if (!strcmp(argv[1],"-k") && !strcmp(argv[2],"start"))
{
if(is_running() == 1) exit(1);
else printf("start sucessfully !!!!!!!!!!!!\n");
}
        else if (!strcmp(argv[1],"-k") && !strcmp(argv[2],"restart"))
{
if(is_running() == 1) 
{
int fd;
if ((fd = open(PID_FILE,O_RDWR | O_CREAT,0777)) < 0)
{
printf("ERROR:failt to open file:%s\n",PID_FILE);
exit(1);
}
if (fcntl(fd,F_SETLK,file_lock(F_UNLCK,SEEK_SET)) < 0)
{
printf("ERROR:%s\n",strerror(errno));
exit(1);
}
char *buf = (char*)malloc(10);
read(fd,buf,10);
pid_t pid = atoi(buf);
if(kill(pid,SIGKILL) == 0) 
printf("cfv_http stop sucessfully!!!!!!!!!\n");
else 
printf("cfv_http stop failed!!!!!!!!!!!!!\n");
sleep(1);/* sleep 1 second*/
chdir("./bin");/*change the current work directory*/
if(execl("./cfv_http","./cfv_http","-k","start",NULL) == -1)
printf("restart failed!!!!!!!!\n");
exit(0);
}
else 
{
printf("cfv_http is not running!!\n");
exit(1);
}
}
        else if (!strcmp(argv[1],"-k") && !strcmp(argv[2],"stop"))
{
if(is_running() == 1) 
{
int fd;
if ((fd = open(PID_FILE,O_RDWR | O_CREAT,0777)) < 0)
{
printf("ERROR:failt to open file:%s\n",PID_FILE);
exit(1);
}
if (fcntl(fd,F_SETLK,file_lock(F_UNLCK,SEEK_SET)) < 0)
{
printf("ERROR:%s\n",strerror(errno));
exit(1);
}
char *buf = (char*)malloc(10);
read(fd,buf,10);
pid_t pid = atoi(buf);
if(kill(pid,SIGKILL) == 0) 
printf("cfv_http stop sucessfully!!!!!!!!!\n");
else 
printf("cfv_http stop failed\n");
exit(0);
}
else 
{
printf("cfv_http is not running!!\n");
exit(1);
}
}
        else 
{
printf("usage:%s -k start|stop|restart\n",argv[0]);
exit(1);
}
}


/*initialize struct flock*/
struct flock * file_lock(short type,short whence)
{
static struct flock fl;
fl.l_type = type;
fl.l_start = 0;
fl.l_whence = whence;
fl.l_len = 0;
return &fl;
}


/*whether the cfv_http is running*/
int is_running()
{
int fd;
if ((fd = open(PID_FILE,O_WRONLY | O_CREAT,0777)) < 0)
{
printf("ERROR:failt to open file:%s\n",PID_FILE);
exit(1);
}
if (fcntl(fd,F_SETLK,file_lock(F_WRLCK,SEEK_SET)) < 0)
{
if(errno == EACCES || errno == EAGAIN)
{
printf("cfv_http already running!\n");
return 1;
}
else 
{
printf("ERROR:%s\n",strerror(errno));
exit(1);
}
}
char *buf = (char*)malloc(10);
sprintf(buf,"%d",getpid());
write(fd,buf,strlen(buf));
return 0;

}


cfv_http.h

#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<signal.h>


#define MAX_FILE 65535      /*the number of open files*/
#define PID_FILE "./cfv_http.pid" /*the file ,which stores the cfv_http pid*/


/* operation option such as start|stop|restart*/
void operation_option(int argc,char **argv);


/*initialize struct flock*/
struct flock * file_lock(short type,short whence);


/*whether the cfv_http is running*/
int is_running();



get_configure.c


#ifndef CFV_CONFIGURE_H
#define CFV_CONFIGURE_H 
#include"get_configure.h"
#endif
/*get ServerRoot directory*/
const char *get_ServerRoot()
{
int i,j,k,fd;
if ((fd = open("../cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ../cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *SR = "ServerRoot ";
for(i=0;i<strlen(buf)-strlen(SR);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'ServerRoot'*/
if(!strncmp((buf + i),SR,strlen(SR)))
{
for(j=i+strlen(SR);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of ServerRoot*/
for(k=i+strlen(SR);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


/*get DocumentRoot directory*/
const char *get_DocumentRoot()
{
int i,j,k,fd;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *DR = "DocumentRoot ";
for(i=0;i<strlen(buf)-strlen(DR);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'DocumentRoot'*/
if(!strncmp((buf + i),DR,strlen(DR)))
{
for(j=i+strlen(DR);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of ServerRoot*/
for(k=i+strlen(DR);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


/*get the port of cfv_http server*/
int get_port()
{
int i,j,k,fd,p;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *port = "Listen ";
for(i=0;i<strlen(buf)-strlen(port);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'Listen'*/
if(!strncmp((buf + i),port,strlen(port)))
{
for(j=i+strlen(port);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find port*/
for(k=i+strlen(port);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (atoi((buf + i)));
}


/*get the CGI directory*/
const char *get_CGI()
{
int i,j,k,fd,p;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *CGI= "CGI ";
for(i=0;i<strlen(buf)-strlen(CGI);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'CGI'*/
if(!strncmp((buf + i),CGI,strlen(CGI)))
{
for(j=i+strlen(CGI);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of ServerRoot*/
for(k=i+strlen(CGI);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


/*get ServerAdmin*/
const char *get_ServerAdmin()
{
int i,j,k,fd,p;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *SA= "ServerAdmin ";
for(i=0;i<strlen(buf)-strlen(SA);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'ServerAdmin'*/
if(!strncmp((buf + i),SA,strlen(SA)))
{
for(j=i+strlen(SA);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of ServerAdmin*/
for(k=i+strlen(SA);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


/*get the ServerName directory*/
const char *get_ServerName()
{
int i,j,k,fd,p;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *SN= "ServerName";
for(i=0;i<strlen(buf)-strlen(SN);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'SN'*/
if(!strncmp((buf + i),SN,strlen(SN)))
{
for(j=i+strlen(SN);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of ServerName*/
for(k=i+strlen(SN);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


/*get the cfv_http.log*/
const char *get_Log()
{
int i,j,k,fd,p;
if ((fd = open("./cfv_http.conf",O_RDONLY)) < 0)
{
printf("failted to open ./cfv_httpd.conf\n");
exit(1);
}
char *buf = (char*)malloc(FILE_SIZE);
read(fd,buf,FILE_SIZE);
close(fd);
char *Log= "Log";
for(i=0;i<strlen(buf)-strlen(Log);i++)
{
/*ignore the comment*/
if(*(buf + i) == '#')
{
for(j=i;j<strlen(buf);j++)
{
if(*(buf + j) == '\n')
{
i = j;
break;
}
}
}
/*find 'Log'*/
if(!strncmp((buf + i),Log,strlen(Log)))
{
for(j=i+strlen(Log);j<strlen(buf);j++) if(*(buf + j) == '\n') goto l1;
}
}
l1:
/*find content of Log*/
for(k=i+strlen(Log);k<j;k++){if(*(buf + k) !=' '){i=k;break;}}
for(k=k+1;k<j;k++) if(*(buf + k) == ' ') break;
*(buf + k)= '\0';
return (buf + i);
}


get_configure.h

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>


#ifndef FILE_SIZE
#define FILE_SIZE 1048576 /*1M=1048576*/
#endif


/*get ServerRoot directory*/
const char *get_ServerRoot();


/*get DocumentRoot directory*/
const char *get_DocumentRoot();


/*get the port of cfv_http server*/
int get_port();


/*get the CGI directory*/
const char *get_CGI();


/*get ServerAdmin*/
const char *get_ServerAdmin();


/*get the ServerName directory*/
const char *get_ServerName();


/*get the Log path*/
const char *get_Log();


cfv_log.c


#ifndef CFV_LOG_H
#define CFV_LOG_H
#include"cfv_log.h"
#endif


/*record in cfv_http.log*/
void Log(int type,const char *message,int connfd,const char *path)
{
int fd;
char logbuffer[BUFFER_SIZE];
time_t timep;
time(&timep);
switch(type) 
{
case ERROR: sprintf(logbuffer,"ERROR:%s\t%s",message,asctime(gmtime(&timep)));break;
case SORRY:
sprintf(logbuffer,"HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n");
write(connfd,logbuffer,strlen(logbuffer));
sprintf(logbuffer,"<html><body><h1>cfv_httpd SORRY:%s</h1><body></html>\r\n",message);
write(connfd,logbuffer,strlen(logbuffer));
sprintf(logbuffer,"SORRY:%s\t%s",message,asctime(gmtime(&timep)));
break;
case LOG:sprintf(logbuffer,"INFO:%s\t%s",message,asctime(gmtime(&timep)));break;
}
        if((fd = open(path,O_CREAT| O_WRONLY | O_APPEND,0222)) >= 0) 
        {
        write(fd,logbuffer,strlen(logbuffer)); 
        close(fd);
        }
if(type == ERROR || type == SORRY) exit(1);
}



cfv_log.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<time.h>


#ifndef BUFFER_SIZE
#define BUFFER_SIZE 8192 
#endif
#ifndef _TYPE_
#define _TYPE_
#define SORRY 100
#define ERROR 101
#define LOG   102
#endif
void Log(int,const char*,int,const char*);



cfv_search.c

#ifndef CFV_SEARCH_H
#define CFV_SEARCH_H
#include"cfv_search.h"
#endif


#ifndef CFV_LOG_H
#define CFV_LOG_H
#include"cfv_log.h"
#endif


#ifndef GET_CONFIGURE_H
#define GET_CONFIGURE_H
#include"get_configure.h"
#endif


struct {
        char *extension;
        char *content_type; 
} content_types[] = {
        {"gif","image/gif"},
        {"jpg", "image/jpg"},
        {"jpeg","image/jpeg"},
        {"png", "image/png" },
        {"htm", "text/html" },  
        {"html","text/html" },  
        {"cgi","text/html" },  
        {"php","text/html" },  
        {0,0} };




/*request in get way*/
void get(int connfd,char* buffer)
{

int fd,i,j,number_byte;
const char *path = get_Log();/*get the cfv_http.log path*/


/* '/r' or '\n' insteaded by '*'*/
for(i=0;i<strlen(buffer);i++)
if(buffer[i] == '\r' || buffer[i] == '\n') buffer[i] = '*';


Log(LOG,strcat(buffer,"\t:request"),connfd,path);/*record */


        /*set the REQUEST_STRING VALUES*/
/*************************************/
        for(i=4;i<strlen(buffer);i++) {
        if(buffer[i] == ' ') {
        buffer[i] = 0;
        break;
        }
        }
char *REQUEST_STRING = (char *)malloc(strlen(buffer)+strlen("REQUEST_STRING=")+1);
        strcat(REQUEST_STRING,"REQUEST_STRING="); 
        strcat(REQUEST_STRING,buffer); 
for(i=0;i<strlen(REQUEST_STRING);i++)
{
if (*(REQUEST_STRING + i) == 'R')
{
for(j=0;j<strlen(REQUEST_STRING);j++,i++)
*(REQUEST_STRING + j) = *(REQUEST_STRING + i);
}
}
        /*************************************/


/*find the file type */
        /*************************************/
for(i=4;i<strlen(buffer);i++) {
if(buffer[i] == '?'|| buffer[i] == ' ') {
buffer[i] = 0;
break;
}
}


if( !strncmp(&buffer[0],"GET /\0",6) || !strncmp(&buffer[0],"get /\0",6)) /*conver no filename to index file*/
strcpy(buffer,"GET /index.html");


int buflen = strlen(buffer);
char *ext = (char *)0;
char *ct = (char *)0;
for(i = 0;content_types[i].extension != 0;i++) {
int len = strlen(content_types[i].extension);
if( !strncmp(&buffer[buflen-len],content_types[i].extension,len)) {
ct = content_types[i].content_type;
ext = content_types[i].extension;
break;
}
}
        /*************************************/

if(ext == 0) Log(SORRY,strcat(buffer,"\t:file extensiontype not supported"),connfd,path);/*no extension supported*/
/* execute CGI*/
/********************************/
else if(!strcmp(ext,"cgi")) 
{
const char *path_cgi = get_CGI();/*get path of cgi_bin*/
chdir(path_cgi); /*change the current work directory to path_cgi*/
dup2(connfd,1);/*redirect 1 to connfd*/
/*get the name of CGI*/
char *envp[] = {REQUEST_STRING,NULL}; 
execle(&buffer[5],&buffer[5],NULL,envp);
free(REQUEST_STRING);
close(connfd);
exit(0);/*exit*/
}
/********************************/


/*execute php like *//*has not been finished*/
/********************************/
else if(!strcmp(ext,"php"))
{
exit(0);/*exit*/

/********************************/


/*execute html*/
/********************************/
else 
{
const char *DocumentRoot = get_DocumentRoot();/*get path of htdocs*/
  chdir(DocumentRoot);
if((fd = open(&buffer[5],O_RDONLY)) == -1) /*open the file for reading*/
Log(SORRY,strcat(&buffer[5],"\t:failed to open file"),connfd,path);
Log(LOG,strcat(&buffer[5],"\t:SEND"),connfd,path);


sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n\r\n",ct);/*send HTTP/1.1 200 OK\r\nContent-Type: %s\r\n\r\n*/
write(connfd,buffer,strlen(buffer));
/*send file in 8KB block - last block maybe smaller*/
while(( number_byte = read(fd,buffer,BUFFER_SIZE)) >0)
{
write(connfd,buffer,number_byte);
}
close(fd);
exit(0);/*exit*/
}
/********************************/
}


/*request in post way*//*HAS NOT BEEN FINISHED*/
void post(int connfd,char *buffer){
}


/*open the request page*/
void search(int connfd)
{
static char buffer[BUFFER_SIZE];
int i,number_byte;
number_byte = read(connfd,buffer,BUFFER_SIZE); /*read web request in buffer*/


const char *path = get_Log();/*get the cfv_http.log path*/


if (number_byte == 0 || number_byte == -1)  /*fail to read stop now*/
Log(SORRY,"failed to read brower request",connfd,path);
else if (number_byte > 0 && number_byte < BUFFER_SIZE) /*web request is valid chars*/
buffer[number_byte] = 0; /*terminate the buffer*/
else Log(SORRY,"failture:length of request is too long",connfd,path);/*record and exit*/


if(!(strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4))) get(connfd,buffer); /*request in get way*/
else if( !(strncmp(buffer,"POST ",5) && strncmp(buffer,"post ",5))) post(connfd,buffer);  /*request in post way*//*HAS NOT BEEN FINISHED*/
else  Log(SORRY,strcat(buffer,":Only simple GET/POST operation supported"),connfd,path);/*request in other  way*/
}


cfv_search.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>


#define BUFFER_SIZE 8192    /*8k*/
#define MAX_FILE 65535      /*the number of open files*/

/*request in get way*/
void get(int connfd,char* buffer);


/*request in post way*/
void post(int connfd,char *buffer);


/*answer the request page*/
void search(int connfd);



file_operation.c

#ifndef FILE_OPERATION_H
#define FILE_OPERATION_H
#include "file_operation.h"
#endif
int main(int argc,int argv){
int i,j;/*declare two variables*/
printf("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
        printf("<html>\n\t<head></head>\n");
char *request_string=getenv("REQUEST_STRING"); /*get the REQUEST_STRING pass from cfv_http*/


/*delete the characters before '?' (include ?)*/
/*******************************************/
for(i=0;i<strlen(request_string);i++)
if(*(request_string + i) == '?') 
for(j=0;j<strlen(request_string-i);j++,i++)
*(request_string + j) = *(request_string + i+1);
/*******************************************/


/*if the value of key_word is NULL,locate http://localhost*/
/**********************************************************/
        if(strcmp(request_string,"key_word=")==0){
printf("\t\t<body οnlοad=parent.location='http://localhost'>\n");
printf("\t\t<body>\n</html>\n");
        exit(0);
        }
/**********************************************************/


printf("\t\t<body>\n");
char *key_word=get_key_word(request_string);/*get the key_word from request_string*/
char *path = "/usr/local/cfv_http";/*the path where the files at*/
        find_by_title(path,key_word);/* find the relactive file by title*/
printf("\t\t<body>\n</html>\n");
return 0;
}


/*find the file by title*/
void find_by_title(char *path,char *title)
{
DIR *dirp;
struct dirent *direntp;
char childpath[512];
memset(childpath,0,sizeof(childpath));
        if((dirp=opendir(path))==NULL)/*open the directory,point by the path*/
{
printf("Open Directory %s Error:%s\n",path,strerror(errno));
exit(1);
}
while((direntp=readdir(dirp))!=NULL) /*read the dirp*/
{
                if(direntp->d_type & DT_DIR) /*if is a directory*/
                {
                if(strcmp(direntp->d_name,".")==0 || strcmp(direntp->d_name,"..")==0) continue;
                sprintf(childpath,"%s/%s",path,direntp->d_name);
                find_by_title(childpath,title);
                }
        if(!( direntp->d_name[0] == '.')) /* ignore the hidden file*/
{
                if(strstr(direntp->d_name,title)!=NULL)
          print_file_attribute(path,direntp->d_name);
}
}
closedir(dirp);
}


/*get the key word from the request_string*//*just for the key_word,which not including special character,such as space*/
char *get_key_word(char *request_string)
{
char equal_str='=';
static char key_word[100];
int temp,temp1=0;
for(temp=0;temp<strlen(request_string);temp++)
{
if(*(request_string + temp)==equal_str)break;
}
for(temp=temp+1;temp<strlen(request_string);temp++,temp1++)
{
*(key_word+temp1) = *(request_string+temp);
}
*(key_word+temp1) = '\0';
return key_word;
}


/*print the attribute of file,such as location,size and modify time*/
int  print_file_attribute(char *path,const char *filename){
        struct stat statbuf;
char *temp_path = (char*)malloc(strlen(path)+strlen(filename)+1);
sprintf(temp_path,"%s/%s",path,filename);
        if(stat(temp_path,&statbuf)==-1){
        printf("Get stat on %s Error:%s<br>\n",filename,strerror(errno));
exit(1);
        }
FILE *fp;
        printf("\t\t\t<font color='blue'><a href='http://localhost'>%s</a><br>\n\t\t\t</font>\n",filename);//print the file name
        char read_buff[BUFF_SIZE+20];
        if((fp=fopen(temp_path,"r"))==NULL){
printf("Open %s Error:%s<br>\n",temp_path,strerror(errno));
        exit(1);
}
        size_t byte_size=fread(read_buff,1,BUFF_SIZE+20,fp);
/*print file content*/
int temp_index;
        printf("\t\t\t<font color='black'>");
for(temp_index=0;temp_index<strlen(read_buff);temp_index++){
       if(read_buff[1]=='E' && read_buff[2]=='L' && read_buff[3]=='F'){ /*Is it a excutable file(or obj)?*/
goto flag;
}
/*escape some sign*/
       else if(read_buff[temp_index]=='<'){
printf("%s","&#60;");
}
       else if(read_buff[temp_index]=='>'){
printf("%s","&#62;");
}
       else if(read_buff[temp_index]=='\n'){
printf("%s","<br>");
}
       else if(read_buff[temp_index]==' '){
printf("%s","&#160;");
}
       else if(read_buff[temp_index]==' '){
printf("%s","&nbsp;");
}
       else if(read_buff[temp_index]=='"'){
printf("%s","&#34;");
}
       else if(!isascii(read_buff[temp_index])){
break;
}
else printf("%c",read_buff[temp_index]);
}
printf("%s","<br>\n\t\t\t</font>\n");
flag: printf("\t\t\t<font color='green'>%s&nbsp;&nbsp;Size:%ld&nbsp;&nbsp;Modified:%s<br>\n\t\t\t</font>\n",temp_path,statbuf.st_size,ctime(&statbuf.st_mtime));/*print the file path,size and modified time*/ 
        fclose(fp);
printf("<br><br>\n");
return 0;
}



file_operation.h


#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<time.h>


#ifndef _BUFF_SIZE_
#define _BUFF_SIZE_
#define BUFF_SIZE 256 
#endif


/*find the file by title*/
void find_by_title(char *path,char *title);
/*get the key word from the request_string*/
char *get_key_word(char *request_string);
/*print the attribute of file,such as location,size and modify time*/
int print_file_attribute(char *path,const char *filename);



cfv_log.conf

# ServerRoot: The top of the directory tree under which the server's
# configuration,and log files are kept.
ServerRoot /usr/local/cfv_http




# Listen: Allows you to bind Apache to specific 
# ports, instead of the default. 
Listen 80




# ServerAdmin: Your address, where problems with the server should be
# e-mailed.
ServerAdmin chenfenva@163.com




# ServerName gives the name and port that the server uses to identify itself.
ServerName http://192.168.1.100:80




# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory
DocumentRoot /usr/local/cfv_http/htdocs


# CGI
CGI  /usr/local/cfv_http/cgi_bin


# Log: The location of the log file.
Log  /usr/local/cfv_http/cfv_http.log




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值