/****************************************************************
filename: http_server.c
author: xxxx
function: Main file of http server
Impliment the response of Get and Post mehtods history:
created by xxxx date: 2014.01.03 *****************************************************************/ #include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h> #include "mime.h"
#include "http_common.h"
#include "http_webapp.h"
// __DEBUG__ is debug tag #define BACK_LOG 50
#define SOCKLEN sizeof(struct sockaddr_in) //
static int open_socket(struct sockaddr_in*);
static int accept_client(int sockfd, struct sockaddr_in* paddr);
static void wait_child(); static void write_response_body(int connfd, char* uri, int filesize);
void process_request(int connfd); int main()
{
// char type[MAX_FILE_TYPE_LEN]="";
// get_mime_type("test.html",type); int sockfd = ;
int connfd = ;
struct sockaddr_in sockaddress;
pid_t chld = ;
//struct sigaction sa; bzero(&sockaddress, sizeof(struct sockaddr_in));
signal(SIGCHLD, wait_child);
//open sock
if((sockfd=open_socket(&sockaddress)) == -)
{
perror("open socket failed");
return -;
} //accept client connection
while()
{
connfd = accept_client(sockfd, &sockaddress); #if __DEBUG__
printf("client accept [%d]\n ", connfd);
#endif if((chld = fork()) > )
{ //root
close(connfd);
continue;
}else if(chld == )
{ //childi
process_request(connfd);
close(sockfd);
exit();
} } return ;
} /*--------------------------------------------------------------- functionname: open_socket
param: NA
return: return sockfd on success, -1 on fail
author: xxxx ----------------------------------------------------------------*/
static int open_socket(struct sockaddr_in* paddr)
{
int sockfd = ;
struct sockaddr_in sockaddress; bzero(&sockaddress, sizeof(sockaddress)); if((sockfd = socket(AF_INET, SOCK_STREAM, )) == -)
return -; sockaddress.sin_family = AF_INET;
sockaddress.sin_port = htons(HTTP_PORT); inet_pton(AF_INET, "10.174.8.163", &(sockaddress.sin_addr)); if(bind(sockfd, (struct sockaddr*)(&sockaddress), sizeof(sockaddress)) == -)
return -; if(listen(sockfd, BACK_LOG) == -)
return -; *paddr = sockaddress;
return sockfd;
} /*--------------------------------------------------------------- functionname: accept_client
param: NA
return: return connfd on success, -1 on fail
author: xxxx ----------------------------------------------------------------*/ static int accept_client(int sockfd, struct sockaddr_in* paddr)
{
socklen_t len = SOCKLEN;
int connfd = ; if(paddr != NULL)
{
connfd = accept(sockfd, (struct sockaddr*)(paddr), &len);
}else
{
connfd = -;
}
return connfd;
} /*---------------------------------------------------------------- functionname: wait_child_exit
param: NA
return: NA
author: xxxx TO KILL CHILD PROCESS, avoid ZOOBIE -----------------------------------------------------------------*/ static void wait_child()
{
int status = ;
while(waitpid(-, &status, WNOHANG) > )
{
#if 0
printf("child process exit\n");
#endif
}
return;
} /*----------------------------------------------------------------- functionname: process_request
param: NA
return: NA
author: xxxx http request process
history:
create by xxxx, 2014.1.08, add simple abilities -----------------------------------------------------------------*/ void process_request(int connfd)
{
char request[MAX_REQUEST_LEN];
STR_REQUEST strreq;
EN_MIME_TYPE type = MIME_ELSE;
EN_REP_STATUS status = HTTP_END;
STR_RESP response;
long filesize = ;
FILE* fstream = NULL; bzero(&strreq, sizeof(STR_REQUEST));
bzero(request, sizeof(request));
bzero(&response, sizeof(STR_RESP)); if(recv(connfd, request, sizeof(request), ) >)
{ if(parse_request(request, &strreq) ==)
{
#if __DEBUG__
printf("request:%s\n", request);
#endif get_mime_type(strreq.URI,&type); // file does not exist 404
if(file_exist(strreq.URI) == -)
{
status = HTTP_NOT_FOUND;
fstream = fopen(HTML_404, "rb");
}else
{
status = HTTP_OK;
fstream = fopen(strreq.URI, "rb");
} filesize=get_file_size(fstream);
#if __DEBUG__
printf("request file size = [%ld]\n", filesize);
#endif
//response.data = (void*)malloc(filesize);
//bzero(response.data, filesize);
get_response_head(status,type,response.head,(int)filesize);
#if __DEBUG__
printf("response head = [%s]", response.head);
#endif
//if(get_response_body(type, response.data, strreq.URI, filesize) == -1)
// perror("fill response body failed"); #if __DEBUG__
//printf("response body = [%s]\n", (char*)(response.data));
#endif
fclose(fstream);
send(connfd, response.head, strlen(response.head), );
write_response_body(connfd, strreq.URI, (int)filesize);
shutdown(connfd, SHUT_RD);
close(connfd);
}
}
} static void write_response_body(int connfd, char* uri, int filesize)
{
int fd =;
void* bodybuf = NULL;
fd = open(uri, O_RDONLY, );
bodybuf = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, );
send(connfd, bodybuf, filesize, );
munmap(bodybuf, filesize);
}

http server v0.1_http_server.c的更多相关文章

  1. http server v0.1_http_reponse.c

    #include <string.h> #include <sys/stat.h> #include <sys/mman.h> #include <fcntl ...

  2. http server v0.1_http_parse.c

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include "mime.h&q ...

  3. http server v0.1_http_webapp.c

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  4. http server v0.1_mime.c

    #include <string.h> #include "mime.h" static STR_MIME_MAP mime_map[]= { MIME_MAP(MIM ...

  5. 用FileZilla Server开FTP

    FileZilla(教程)是经典的开源FTP解决方案,包括FileZilla客户端和FileZilla Server.其中,FileZilla Server的功能比起商业软件FTP Serv-U毫不逊 ...

  6. DNS隧道之DNS2TCP实现——dns2tcpc必须带server IP才可以,此外ssh可以穿过墙的,设置代理上网

    我自己的命令: server端: dns2tcpd -F -d 1 -f ./dns2tcpd.conf 输出: 09:08:59 : Debug options.c:97 Add resource ...

  7. docker 下 安装rancher 笔记

    sudo yum update 更新系统环境 curl -sSL https://get.docker.com/ | sh 安装最新docker版本 systemctl start docker.se ...

  8. 容器基础(七): 使用docker compose部署程序

    配置 在上一节的基础上,  增加如下的docker-compose.yml文件, 然后用docker-compose up命令启动容器进行部署: version: " services: s ...

  9. 容器基础(八): 使用docker swarm部署程序

    环境 基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm. docker service部署 ➜ ...

随机推荐

  1. C#通过FTP账号上传、修改、删除文件 FTPClient

    下面类文件中,主要做的工作是:从ftp服务器上下载文件把本地文件替换.添加.或删除功能,在替换本地文件时会先备份一下本地的文件,若整个操作都完成了就会发出commit命令,表示全部替换成功.若中间操作 ...

  2. GA遗传算法解析

    LinJM  @HQU 谈及遗传算法,我首先想到的就是孟德尔的豌豆实验.当然,遗传算法实质上并不能用豌豆实验说明,豌豆实验探讨了分离定律和自由组合定律,而遗传算法所借鉴的并不是这两个定律.遗传算法,简 ...

  3. Windows环境下tomcat配置日志输出

    在Linux系统中,可以通过tail  -f  catalina.out 来跟踪Tomcat 和相关应用运行的情况. 在windows下,catalina日志与Linux记录的内容有很大区别,大多信息 ...

  4. Solr配置与简单Demo[转]

    Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...

  5. STM32的优先级NVIC_PriorityGroupConfig的理解及其使用

    写作原由:因为之前有对stm32 优先级做过研究,但是没时间把整理的东西发表,最近项目需要2个串口,但是不是两个串口同时使用,只是随机使用其中一个,程序对2个串口的优先级需要配置: 此文思路:“中断优 ...

  6. (转载)equals与==

    引言:从一个朋友的blog转过来的,里面解决了两个困扰我很久的问题.很有久旱逢甘霖的感觉. 概述:        A.==可用于基本类型和引用类型:当用于基本类型时候,是比较值是否相同:当用于引用类型 ...

  7. plupload使用指南(转)

    转自http://www.cnblogs.com/2050/p/3913184.html 现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SWFUpload显然就有点过时了,毕 ...

  8. MKServerBuilder.psd1

    MKServerBuilder.psd1 # # Module manifest for module 'MKServerBuilder' # # Generated by: Edward Guan ...

  9. SQL Server中建立外键的方法

    在SQL中建立外键约束,可以级联查询表中的数据,在C#代码生成器中,也能根据外键关系生成相应的外键表数据模型.外键也可防止删除有外键关系的记录,一定程度上保护了数据的安全性. 步骤: 1.要建立外键关 ...

  10. 【Oracle】INSERT INTO SELECT语句和SELECT INTO FROM语句的区别

    >>>>>>>>>>>>>>>>>>>>>>>>> ...