http server v0.1_http_server.c
/****************************************************************
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的更多相关文章
- http server v0.1_http_reponse.c
#include <string.h> #include <sys/stat.h> #include <sys/mman.h> #include <fcntl ...
- http server v0.1_http_parse.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "mime.h&q ...
- http server v0.1_http_webapp.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- http server v0.1_mime.c
#include <string.h> #include "mime.h" static STR_MIME_MAP mime_map[]= { MIME_MAP(MIM ...
- 用FileZilla Server开FTP
FileZilla(教程)是经典的开源FTP解决方案,包括FileZilla客户端和FileZilla Server.其中,FileZilla Server的功能比起商业软件FTP Serv-U毫不逊 ...
- DNS隧道之DNS2TCP实现——dns2tcpc必须带server IP才可以,此外ssh可以穿过墙的,设置代理上网
我自己的命令: server端: dns2tcpd -F -d 1 -f ./dns2tcpd.conf 输出: 09:08:59 : Debug options.c:97 Add resource ...
- docker 下 安装rancher 笔记
sudo yum update 更新系统环境 curl -sSL https://get.docker.com/ | sh 安装最新docker版本 systemctl start docker.se ...
- 容器基础(七): 使用docker compose部署程序
配置 在上一节的基础上, 增加如下的docker-compose.yml文件, 然后用docker-compose up命令启动容器进行部署: version: " services: s ...
- 容器基础(八): 使用docker swarm部署程序
环境 基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm. docker service部署 ➜ ...
随机推荐
- Rsync同步工具安装文档
(1)下载Rsync源代码进行安装 (2)1' cd rsync 2' ./configure --prefix=/usr/local/rsync 3' make 4' make ...
- 查看LINUX发行商版本与LINUX内核版本
查看LINUX发行商版本:[root@server-mysql ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6.3 (Sant ...
- 非常全面的java基础笔试题
下面是java基础笔试题,当时我去笔试,做了1个小时(80道选择题,后面的简答题就没时间做了),结果很吓人,太挫了,最后被面试官忽悠去培训去了,呵呵.我偷偷把面试题弄了下来,用来学习吧,也希望能对你们 ...
- iOS Runtime 实践(1)
很多时候我们都在看iOS开发中的黑魔法——Runtime.懂很多,但如何实践却少有人提及.本文便是iOS Runtime的实践第一篇. WebView 我们这次的实践主题,是使用针对接口编程的方式,借 ...
- [学习笔记]设计模式之Bridge
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 “魔镜啊魔镜,谁是这个世界上最美丽的人?”月光中,一个低沉的声音回荡在女王的卧室.“是美丽的白雪公主,她正和小霍比特人们幸福快乐地生活 ...
- Win8.1 与 pl2303驱动
在8.1上最新的pl2303驱动都不支持Win8.1了.要使用老的驱动 http://www.drv5.cn/sfinfo/9385.html 这里这个驱动里面有五个文件,通过在设备管理器里面查看串口 ...
- PHP中用PDO方法打开连接关闭mysql数据库
代码如下: <meta http-equiv="content-type" content="text/html" charset="utf-8 ...
- HDU 4196 Remoteland
题意:给定一个n,然后让你从1-n中选出某些数乘起来,使得乘积最大,并且乘积必须是完全平方数. 思路:将1-n种每个数都分解素因子,把他们的素因子的幂加起来,如果是偶数,就说明可以构成完全平方数,乘起 ...
- SharePoint Dialog 使用
SharePoint中弹出模态窗口对体验提高太大了 方法为: 父页面中调用子页面: function showDialog() { var options = { ...
- css3中允许单词内断句word-wrap和怎么处理断句word-break
首先说一下:浏览器的默认行为,在一行中几个单词 排列着,如果最后一个长单词 太长时 首先是移到下一行,如果该单词的长度大于父元素的宽度,会溢出. <!doctype html> <h ...