从零开始一个http服务器(四)-动态返回
从零开始一个http服务器(四)
代码地址 : https://github.com/flamedancer/cserver
git checkout step4
运行:
make clean && make && ./myserver.out
测试:
浏览器打开 http://127.0.0.1:9734/
response 返回文件 根据request 动态返回 response
- Makefile
- 读取文件内容并作为body返回
- 列出目录下文件
Makefile
看看我们现在的编译运行命令gcc request.h request.c response.h response.c main.c tools/utils.c tools/utils.h && ./a.out
太长了!而且如果以后添加新的文件编译命令还要改。我们用Makefile来优化编译。
objects = main.o request.o response.o \
utils.o
VPATH = tools
myserver.out : $(objects)
cc -o myserver.out $(objects)
main.o :
request.o :
response.o :
utils.o :
.PHONY : clean
clean :
-rm myserver.out $(objects)
第1,2行定义了一个常量objects,因为要经常用到main.o request.o response.o utils.o,定义这个常量可以节省打字量。
VPATH是指除了当前路径外的额外路径,例如当前路径是没有utils.c的,需要告诉它还可以在tools目录找。
第5,6行是一组,第5行:冒号是前置声明,也就是声明如果要生成冒号前的文件,需要冒号后面那些文件存在。注意这只是声明。第6行开头需要有tag键(vim 可以在v模式下打出tab),然后是要执行的命令,注意这个命令不一定非要生成冒号前的目标文件。
第8~11行其实和第5行一样,只是利用了make的自动推导功能:根据 .o文件推导需要同名的.c文件,同时推导命令 cc -o main.o main.c
第14行也是一样的,只不过手动用.PHONY说明了下这里并不是要真正生成clean这个目标文件,只是为了执行后面的命令而已, rm 前的 - 号 是指遇到错误继续执行。
这样我们以后编译只要执行make就好了,会生成目标文件myserver.out,可以执行make clean 来清理中间文件。或者直接执行make clean && make && ./myserver.out来运行我们的程序。
读取文件内容并作为body返回
现在我们发送给浏览器是固定的内容,当需要改变内容时,需要重新编译。这样很不灵活,也不实用。
我们修改逻辑,让服务器收到请求时,都去读取文件,再返回文件内容,这样的话当我们要改变发送内容时,只需要修改文件就好了。
void responeFileContent(char * filePath, struct http_response * response) {
char * error_file = "static/404.html";
FILE * fileptr;
fileptr = fopen(filePath, "rb");
if (NULL == fileptr)
{
fileptr = fopen(error_file, "rb");
}
fseek(fileptr, 0, SEEK_END);
response->body_size = ftell(fileptr);
rewind(fileptr);
response->body = (char *)malloc((response->body_size));
fread(response->body, response->body_size, 1, fileptr);
fclose(fileptr);
return;
}
打开文件和关闭文件的函数 fopen fclose 很简单。
这里主要注意获取文件大小的方法:
fseek 移动文件指针,从文件末尾(SEEK_END代表文件末尾)移动0位置,跳到文件末尾。
ftell 获取文件首到当前文件指针的距离(偏移字节数)。这样就获取了文件大小。
rewind 再把文件指针移动回首。
Content-Length的真实意义是字节数,ftell返回的也是字节数,所以body_size不需要 * sizeof(char)
列出目录下文件
我们想在首页列出可以跳转的链接。为此可以扫描static目录下的所有文件,然后动态构造带标签的html返回。
void show_dir_content(struct http_response * response) {
char * path = "static";
char *html = "<html> <ul> %s </ul> </html>";
char *ui = "<li><a href='/static/%s'>static/%s</a></li>";
char liStr[500];
char * liStrP = liStr;
DIR *d = opendir(path); // open the path
// if (d == NULL)
// return; // if was not able return
struct dirent *dir; // for the directory entries
while ((dir = readdir(d)) != NULL) // if we were able to read somehting from the directory
{
if (dir->d_type != DT_DIR) { // if the type is not directory just print it with blue
printf("%s\n", dir->d_name);
sprintf(liStrP, ui, dir->d_name, dir->d_name);
liStrP = liStr + strlen(liStr);
}
}
closedir(d); // finally close the directory
response->body = (char *)malloc((strlen(liStr) + strlen(html)) * sizeof(char));
sprintf(response->body, html, liStr);
response->body_size = strlen(response->body);
return;
}
这里主要是打开目录,遍历目录和关闭目录 opendir, readdir, closedir 这几个函数。
最后修改doResponse方法:
void doResponse(struct http_request * request, FILE * stream) {
struct http_response responseInstance;
struct http_response * response = &responseInstance;
initHttpResponse(response);
response->version = "HTTP/1.1";
response->code = "200";
response->desc = "OK";
char content_len[25];
char * home_url = "/";
char * static_url = "/static/";
char * action_url = "/action/";
if (strncmp(static_url, request->url, strlen(static_url)) == 0) {
responeFileContent(request->url + 1, response);
}
else if (strncmp(home_url, request->url, strlen(home_url)) == 0) {
show_dir_content(response);
} else {
char *content = "<html><meta charset='utf-8'><a src='/'> >_< 看来你迷路了 </a></html>";
response->body_size = (int)strlen(content);
response->body = (char *)malloc((response->body_size));
strcpy(response->body, content);
}
sprintf(content_len, "%d", response->body_size);
printf("body size is %d\n", response->body_size);
struct Item * item = newItem(
"Content-Length",
content_len
);
struct Map map_instance;
initMap(&map_instance);
response->headers = &map_instance;
mapPush(response->headers, item);
outputToFile(response, stream);
// clean
free(response->body); // If ptr is NULL, no operation is performed.
response->body = NULL;
}
执行 make clean && make && ./myserver.out 看看效果!
从零开始一个http服务器(四)-动态返回的更多相关文章
- 从零开始一个http服务器(三)-返回response 构造
从零开始一个http服务器(三) 代码地址 : https://github.com/flamedancer/cserver git checkout step3 运行: gcc request.h ...
- 从零开始一个http服务器(五)-模拟cgi
从零开始一个http服务器-模拟cgi(五) 代码地址 : https://github.com/flamedancer/cserver git checkout step5 运行: make cle ...
- 从零开始一个http服务器(六)-多路复用和压力测试
从零开始一个http服务器(六)-多路复用和压力测试 代码地址 : https://github.com/flamedancer/cserver git checkout step6 运行: make ...
- 从零开始一个http服务器(二)-请求request解析
从零开始一个http服务器 (二) 代码地址 : https://github.com/flamedancer/cserver git checkout step2 解析http request 观察 ...
- 从零开始一个http服务器(一)-开始
从零开始一个http服务器 (一) 代码地址 : https://github.com/flamedancer/cserver git checkout step1 一个简单的socket serve ...
- 从零开始用 Flask 搭建一个网站(四)
前言 从零开始用 Flask 搭建一个网站(三) 介绍了网页前端与后端.前端与前端之间数据的交流.本节主要介绍一下如何应用 Flask-OAuthlib, 使用 Flask-OAuthlib 就可以轻 ...
- 【重点突破】——使用Express创建一个web服务器
一.引言 在自学node.js的过程中有一个非常重要的框架,那就是Express.它是一个基于NodeJs http模块而编写的高层模块,弥补http模块的繁琐和不方便,能够快速开发http服务器.这 ...
- 用java写一个web服务器
一.超文本传输协议 Web服务器和浏览器通过HTTP协议在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议——客户端发送一个请求,服务器返回该请求的应答.HTTP协议使用可靠的T ...
- Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈
一个用户的Request是如何经过Web服务器(Apache,Nginx,IIS,Light)与后端的动态语言(如PHP等)进行交互并将结果返回给用户的呢? 本文浅谈个人观点,可能有误,欢迎拍砖,共同 ...
随机推荐
- java 中linq 的使用方式 筛选 查找 去重
1.筛选 $.Enumerable.From(value).Where(function(x) {//value 为被操作的内容 return x.name == name;//第一个name为val ...
- 【Leetcode】【Medium】Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 修改容器的hosts文件
修改容器的hosts文件 1.介绍 docker容器中的hosts文件虽然在宿主机磁盘上/var/lib/docker/containers/xxx/hosts中,但是修改该文件是无效的,需要通过其他 ...
- Linux 命令行浏览器
真是没有做不到只有想不到! Linux下竟然有命令行式的浏览器:W3m SPC向下翻页 b向上翻页 J 向下滚动一行 K 向上滚动一行 > 右移一屏 < 左移一屏 TAB 转到下个超链接 ...
- Geekforgeek week1
1. is palindrome solution 1: check to reverse the digit, if they are the same number https://www.gee ...
- mysql 查询大量数据报错
错误:Incorrect key file for table '/tmp/#sql_... 由于一次查询出大量数据,并且使用了临时表,导致产生此错误,主要是存放临时表文件的/tmp目录大小不足导致.
- Linux下XAMPP的部署实战
上传源码文件 rz -be 下载xampp安装包wget http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/5.5.28/xampp- ...
- JavaScript的DOM_操作内容
一.innerText 属性 <script type="text/javascript"> window.onload = function(){ var box = ...
- dba_tables、all_tables、user_tables
本文摘抄自:http://blog.csdn.net/daxiang12092205/article/details/42921063 dba_tables : 系统里所有的表的信息,需要DBA权限才 ...
- Cocos2d-x 3.1.1 学习日志3--C++ 初始化类的常量数据成员、静态数据成员、常量静态数据成员
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011292087/article/details/37598919 有关const成员.stati ...