Http Server C语言.docx

上传人:b****4 文档编号:4019675 上传时间:2023-05-06 格式:DOCX 页数:18 大小:17.30KB
下载 相关 举报
Http Server C语言.docx_第1页
第1页 / 共18页
Http Server C语言.docx_第2页
第2页 / 共18页
Http Server C语言.docx_第3页
第3页 / 共18页
Http Server C语言.docx_第4页
第4页 / 共18页
Http Server C语言.docx_第5页
第5页 / 共18页
Http Server C语言.docx_第6页
第6页 / 共18页
Http Server C语言.docx_第7页
第7页 / 共18页
Http Server C语言.docx_第8页
第8页 / 共18页
Http Server C语言.docx_第9页
第9页 / 共18页
Http Server C语言.docx_第10页
第10页 / 共18页
Http Server C语言.docx_第11页
第11页 / 共18页
Http Server C语言.docx_第12页
第12页 / 共18页
Http Server C语言.docx_第13页
第13页 / 共18页
Http Server C语言.docx_第14页
第14页 / 共18页
Http Server C语言.docx_第15页
第15页 / 共18页
Http Server C语言.docx_第16页
第16页 / 共18页
Http Server C语言.docx_第17页
第17页 / 共18页
Http Server C语言.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Http Server C语言.docx

《Http Server C语言.docx》由会员分享,可在线阅读,更多相关《Http Server C语言.docx(18页珍藏版)》请在冰点文库上搜索。

Http Server C语言.docx

HttpServerC语言

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//建立socket开始侦听接收连接接收客户端数据解析http协议发送文件数据给客户端

#defineHTTP_PORT"2010"

#defineMAX_CONNECTION10

#defineDOCUMENT_ROOT"www"

#defineLOG_PATH"log/access.log"

voidparser(char*s,charres[][255],charhost[][255]);

staticchar*strtoupper(char*s);

staticlongfilesize(constchar*filename);

staticintfile_exists(constchar*filename);

staticvoidmime_content_type(constchar*name,char*ret);

staticintWriteLog(constchar*message);

staticintis_dir(constchar*filename);

staticunsignedshortg_is_log=1;

staticintg_log_fd=0;

intmain(void)

{

intserver_sock;

intclient_sock;

structsockaddr_inserver_addr;

structsockaddr_inclient_addr;

structsockaddr_insin;

structstatfile_stat;

pid_tpid;

charclient_ip[100];

charbuf[20000];

charbuf_all[2000];

charbuf1[2000];

charp[3][255];

charh[3][255];

chartmp[2000];

charcwd[1024];

charfilename[2000];

charfilepath[2000];

intfd,size;

intcurrentConn=0;

DIR*dir;

structdirent*ptr;

chdir("../");

if((pid=fork())<0)

{

perror("fork");

exit

(1);

}

elseif(pid!

=0)

{

exit

(1);

}

if((server_sock=socket(AF_INET,SOCK_STREAM,0))<0)

{

perror("socket");

exit

(1);

}

memset(&server_addr,0,sizeof(server_addr));

server_addr.sin_family=AF_INET;

server_addr.sin_port=htons(atoi(HTTP_PORT));

server_addr.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(server_sock,(structsockaddr*)&server_addr,sizeof(server_addr))<0)

{

perror("bind");

exit

(1);

}

if(listen(server_sock,MAX_CONNECTION)<0)

{

perror("listen");

exit

(1);

}

printf("fasthttpsuccessfulcreated...\n");

while

(1)

{

unsignedintclientlen=sizeof(client_addr);

if((client_sock=accept(server_sock,(structsockaddr*)&client_addr,&clientlen))<0)

{

perror("accept");

exit

(1);

}

if((pid=fork())==0)

{

if(read(client_sock,buf,20000)<0)

{

perror("readdatafromclient");

exit

(1);

}

parser(buf,p,h);

if(strcmp(strtoupper(p[0]),"GET")!

=0

&&strcmp(strtoupper(p[0]),"POST")!

=0

&&strcmp(strtoupper(p[0]),"HEAD")!

=0)

{

memset(&buf,0,sizeof(buf));

sprintf(buf,"HTTP/1.1501NotImplemented\r\nServer:

%s\r\nContent-Type:

text/html\r\nContent-Length:

1489\r\nAccept-Ranges:

bytes\r\nConnection:

close\r\n\r\n","Apache");

write(client_sock,buf,strlen(buf));

memset(&buf,0,sizeof(buf));

sprintf(buf,"

%sMethodNotImplemented

","501");

write(client_sock,buf,strlen(buf));

close(client_sock);

exit(0);

}

if(strcmp(p[1],"/")==0)

{

memset(&tmp,0,sizeof(tmp));

sprintf(tmp,"%s","index.html");

strcat(p[1],tmp);

}

WriteLog(p[1]);

getcwd(filepath,sizeof(filepath));

strcat(filepath,"/");

strcat(filepath,DOCUMENT_ROOT);

strcat(filepath,p[1]);

if(!

file_exists(filepath))

{

memset(&buf,0,sizeof(buf));

sprintf(buf,"HTTP/1.1404NotFound\r\nServer:

%s\r\nContent-Type:

text/html\r\nContent-Length:

257271\r\nConnection:

close\r\n\r\n","Apache");

write(client_sock,buf,strlen(buf));

memset(&buf,0,sizeof(buf));

sprintf(buf,"404NotFound

404NotFound


Poweredby%s
","fasthttp");

write(client_sock,buf,strlen(buf));

close(client_sock);

memset(&buf,0,sizeof(buf));

sprintf(buf,"404NotFound\t%s\n",filepath);

WriteLog(buf);

exit(0);

}

if(access(filepath,R_OK)<0)

{

memset(&buf,0,sizeof(buf));

sprintf(buf,"HTTP/1.1403Forbidden\r\nServer:

%s\r\nContent-Type:

text/html\r\nContent-Length:

25727\r\nConnection:

close\r\n\r\n","Apache");

write(client_sock,buf,strlen(buf));

close(client_sock);

exit(0);

}

/**目录列表**/

if(is_dir(filepath))

{

memset(&tmp,0,sizeof(tmp));

sprintf(tmp,"Indexof%s

Indexof%s

  • ParentDirectory
  • ",filepath,filepath);

    strcat(buf,tmp);

    if((dir=opendir(filepath))!

    =NULL)

    {

    while((ptr=readdir(dir))!

    =NULL)

    {

    if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)

    {

    continue;

    }

    memset(&buf,0,sizeof(buf));

    sprintf(buf,"%s/%s",filepath,ptr->d_name);

    if(is_dir(buf))

    {

    memset(&buf,0,sizeof(buf));

    sprintf(buf,"

  • %s/
  • ",ptr->d_name,ptr->d_name);

    }

    else

    {

    memset(&buf,0,sizeof(buf));

    sprintf(buf,"

  • %s
  • ",ptr->d_name,ptr->d_name);

    }

    strcat(tmp,buf);

    }

    }

    closedir(dir);

    memset(&buf,0,sizeof(buf));

    sprintf(buf,"%s","

");

strcat(tmp,buf);

memset(&buf,0,sizeof(buf));

sprintf(buf,"HTTP/1.1200OK\r\nServer:

fasthttp\r\nContent-Type:

text/html;charset=utf-8\r\nContent-Length:

%d\r\nConnection:

close\r\n\r\n",strlen(tmp));

write(client_sock,buf,strlen(buf));

write(client_sock,tmp,strlen(tmp));

close(client_sock);

}

memset(&tmp,0,sizeof(tmp));

mime_content_type(filepath,tmp);

memset(&buf,0,sizeof(buf));

sprintf(buf,"HTTP/1.1200OK\r\nServer:

%s\r\nContent-Type:

%s\r\nContent-Length:

25727\r\nConnection:

close\r\n\r\n","Apache",tmp);

write(client_sock,buf,strlen(buf));

memset(&buf,0,sizeof(buf));

fd=open(filepath,O_RDONLY);

read(fd,buf,filesize(filepath));

close(fd);

write(client_sock,buf,filesize(filepath));

close(client_sock);

memset(&buf,0,sizeof(buf));

sprintf(buf,"200OK\t%s\t%d\n",filepath,filesize(filepath));

WriteLog(buf);

exit(0);

}

else

{

wait(NULL);

}

close(client_sock);

}

}

voidparser(char*s,charres[][255],charhost[][255])

{

inti,j=0;

intn;

charhosts[255];

for(i=0;s[i]!

='\r';i++)/*obtainthefirstlineinhttpprotocolhead*/

;

s[i]='\0';

n=i++;

for(i=0,j=0;i<3;i++,j++)/*dividetheprotocolheadinblank*/

{

strcpy(res[j],strsep(&s,""));

}

for(i=n;s[i]!

='\r';i++)

{

strcat(hosts,s[i]);

}

for(i=0,j=0;i<3;i++,j++)/*dividetheprotocolheadinblank*/

{

strcpy(host[j],strsep(&hosts,":

"));

}

}

/**

*strtoupper-stringtoupper

*

*/

staticchar*strtoupper(char*s)

{

inti,len=sizeof(s);

for(i=0;i

{

s[i]=(s[i]>='a'&&s[i]<='z'?

s[i]+'A'-'a':

s[i]);

}

return(s);

}

/**

*filesize-getfilesize

*/

staticlongfilesize(constchar*filename)

{

structstatbuf;

if(!

stat(filename,&buf))

{

returnbuf.st_size;

}

return0;

}

/**

*file_exists-checkfileisexist

*/

staticintfile_exists(constchar*filename)

{

structstatbuf;

if(stat(filename,&buf)<0)

{

if(errno==ENOENT)

{

return0;

}

}

return1;

}

/**

*GetMIMEtypeheader

*

*/

staticvoidmime_content_type(constchar*name,char*ret){

char*dot,*buf;

dot=strrchr(name,'.');

/*Text*/

if(strcmp(dot,".txt")==0){

buf="text/plain";

}elseif(strcmp(dot,".css")==0){

buf="text/css";

}elseif(strcmp(dot,".js")==0){

buf="text/javascript";

}elseif(strcmp(dot,".xml")==0||strcmp(dot,".xsl")==0){

buf="text/xml";

}elseif(strcmp(dot,".xhtm")==0||strcmp(dot,".xhtml")==0||strcmp(dot,".xht")==0){

buf="application/xhtml+xml";

}elseif(strcmp(dot,".html")==0||strcmp(dot,".htm")==0||strcmp(dot,".shtml")==0||strcmp(dot,".hts")==0){

buf="text/html";

/*Images*/

}elseif(strcmp(dot,".gif")==0){

buf="image/gif";

}elseif(strcmp(dot,".png")==0){

buf="image/png";

}elseif(strcmp(dot,".bmp")==0){

buf="application/x-MS-bmp";

}elseif(strcmp(dot,".jpg")==0||strcmp(dot,".jpeg")==0||strcmp(dot,".jpe")==0||strcmp(dot,".jpz")==0){

buf="image/jpeg";

/*Audio&Video*/

}elseif(strcmp(dot,".wav")==0){

buf="audio/wav";

}elseif(strcmp(dot,".wma")==0){

buf="audio/x-ms-wma";

}elseif(strcmp(dot,".wmv")==0){

buf="audio/x-ms-wmv";

}elseif(strcmp(dot,".au")==0||strcmp(dot,".snd")==0){

buf="audio/basic";

}elseif(strcmp(dot,".midi")==0||strcmp(dot,".mid")==0){

buf="audio/midi";

}elseif(strcmp(dot,".mp3")==0||strcmp(dot,".mp2")==0){

buf="audio/x-mpeg";

}elseif(strcmp(dot,".rm")==0||strcmp(dot,".rmvb")==0||strcmp(dot,".rmm")==0){

buf="audio/x-pn-realaudio";

}elseif(strcmp(dot,".avi")==0){

buf="video/x-msvideo";

}elseif(strcmp(dot,".3gp")==0){

buf="video/3gpp";

}elseif(strcmp(dot,".mov")==0){

buf="video/quicktime";

}elseif(strcmp(dot,".wmx")==0){

buf="video/x-ms-wmx";

}elseif(strcmp(dot,".asf")==0||strcmp(dot,".asx")==0){

buf="video/x-ms-asf";

}elseif(strcmp(dot,".mp4")==0||strcmp(dot,".mpg4")==0){

buf="video/mp4";

}elseif(strcmp(dot,".mpe")==0||strcmp(dot,".mpeg")==0||strcmp(dot,".mpg")==0||strcmp(dot,".mpga")==0){

buf="video/mpeg";

/*Documents*/

}elseif(strcmp(dot,".pdf")==0){

buf="application/pdf";

}elseif(strcmp(dot,".rtf")==0){

buf="application/rtf";

}elseif(strcmp(dot,".doc")==0||strcmp(dot,".dot")==0){

buf="application/msword";

}elseif(strcmp(dot,".xls")==0||strcmp(dot,".xla")==0){

buf="application/msexcel";

}elseif(strcmp(dot,".hlp")==0||strcmp(dot,".chm")==0){

buf="application/mshelp";

}elseif(strcmp(dot,".swf")==0||strcmp(dot,".swfl")==0||str

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2