标签:style blog http color os io 使用 ar 文件
xinetd:负责http底层的传输
service micro_httpd
{
socket_type = stream
protocol = tcp
wait = no
user = liuwei
server = /home/liuwei/http_protect/micro_httpd
server_args = /home/liuwei/www
disable = no
flags = IPv4
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
void sys_err( char * str)
{
printf( "%s\n" , str);
exit(-1);
}
void show_dir(char * pathname , char * p)
{
struct dirent * ptr;
struct stat buf;
DIR * dir = opendir(pathname);
if( dir == NULL)
sys_err("open dir error!");
printf("<style>a{color:black;}.dir{color:red;font-size:25px;text-decoration:none}span{color:purple}td{width:25%%;text-align:left;}tr{height:35px}</style><table>");
while( (ptr = readdir(dir)) != NULL )
{
if(ptr->d_name[0] == ‘.‘)
continue;
char tmp[1024] , css[10] = "dir";
strcpy(tmp , pathname);
strcat(tmp , "/");
strcat(tmp , ptr->d_name);
stat(tmp, &buf);
if(strcmp( p , "/") == 0)
p[0] = ‘\0‘;
if(S_ISDIR(buf.st_mode) == 0)
css[0] = ‘\0‘;
printf("<tr><td><a class=‘%s‘ href=‘%s/%s‘>%s</a></td><td>%s</td><td>%d</td></tr>",css,p,ptr->d_name,ptr->d_name,ctime(&buf.st_atime),(int)buf.st_size );
}
printf("</table><br/><hr/><span>apache_httpd</span>");
fflush(stdout);
exit(0);
}
void show_file( char * buf)
{
FILE *fp;
char * p = NULL , ch;
char pathname[50] = "/home/liuwei/www";
if((p = strtok( buf , " ")) && (p = strtok(NULL , " ")))
{
struct stat buf;
if( strcmp( p , "/") == 0)
show_dir(pathname , p);
strcat(pathname , p);
stat(pathname, &buf);
if(S_ISDIR(buf.st_mode))
show_dir(pathname , p);
fp = fopen( pathname , "r");
if(fp == NULL)
printf("404 not found\n");
else
{
//printf("HTTP/1.1 200 OK\r\n");
//printf("Content-Language:zh-ch\r\n");
//printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
while(fread(&ch, sizeof(ch),1,fp) != 0 )
fwrite(&ch,sizeof(ch),1,stdout);
}
}
fflush(stdout);
fclose( fp );
}
void write_log( void )
{
char buf[8192];
time_t t ;
int i = 1;
FILE * fp = fopen("log.txt","a");
if( fp == NULL )
sys_err("fopen log.txt error");
time(&t);
fputs( ctime(&t) , fp);
fputs( "-----------------------\n" , fp);
while(( fgets(buf,sizeof(buf),stdin) != NULL ) && strlen(buf) != 2)
{
fputs( buf , fp );
if(i++ == 1)
show_file( buf );
}
fprintf( fp , "\n");
fclose( fp );
}
int main( int argc , char * argv[])
{
if(chdir("/home/liuwei/http-project/"))
sys_err("change dir error");
write_log();
return 0;
}
all:clean httpd
@echo "123456"
sudo /etc/init.d/xinetd restart
httpd:
gcc liuwei_httpd.c -o liuwei_httpd
clean:
rm -rf liuwei_httpd log.txt
标签:style blog http color os io 使用 ar 文件
原文地址:http://www.cnblogs.com/l6241425/p/3955023.html