当前位置:网站首页>A web server where browser users access server files
A web server where browser users access server files
2022-07-23 07:33:00 【biyezuopinvip】
epoll_http_server
0 Folder introduction
- Ideas .txt It's my summary , Easy to understand , Just for your reference ;
- version 1.0 yes C Language version of the project
- version 1.1 yes CPP Items written by version
- epoll_http_server.assets yes readme.md Pictures of the
1 Project objectives
Write a browser user to access server files WEB The server
2 Demand analysis
(1) be based on epoll Multiple channels of IO Reuse technology realizes multi-user access ;
(2) You can access different types of files ;
(3) You can access the directory ;
3 Module introduction
3.1 tool The header file
The folder tool.h Three functions are declared in :16 Binary conversion 10 Base number 、 code 、 decode .
The encoding here refers to the processing of Chinese characters into unicode code , Decoding is the reverse process .
16 Base to zero 10 Base number ,hexit function
int hexit(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
}
Coding function ,encode_str function
void encode_str(char* to, int tosize, const char* from){
int tolen;
for (tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from) {
if (isalnum(*from) || strchr("/_.-~", *from) != (char*)0) {
*to = *from;
++to;
++tolen;
} else {
sprintf(to, "%%%02x", (int) *from & 0xff);
to += 3;
tolen += 3;
}
}
*to = '\0';
}
Decoding function ,decode_str function
void decode_str(char* to, char* from) {
for ( ; *from != '\0'; ++to, ++from ) {
if (from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2])) {
*to = hexit(from[1])*16 + hexit(from[2]);
from += 2;
} else {
*to = *from;
}
}
*to = '\0';
}
3.2 server file
1、epoll_run function
function :
(1) establish epoll Monitor the red black tree ;
(2) call init_listen_fd function ;
(3) Only read events are processed , Is a connection request , call do_accept Function to process connection requests ; Read data request , call do_read Function processing .
Read data request .
2、init_listen_fd function
function :
(1) Create listening file descriptor lfd;
(2) Set up port multiplexing 、 Binding address structure 、 Set listening upper limit 、 establish epoll Monitor red black tree and other operations ;
(3) Add the listener file descriptor to epoll On the red and black trees .
3、do_accept function
function :
(1) Blocking listener file descriptor lfd, Get the communication file descriptor cfd;
(2) Set the communication file descriptor to non blocking mode , To add to epoll On the red and black trees , Set the red black tree to edge mode ( The default is non blocking
The way ).
4、do_read function
function :
(1) Read a line from the browser http agreement , call http_request Handle get request , And transfer the communication file descriptor from epoll On the red and black trees
del fall .
5、get_line function
function :
(1) Get a row \r\n Data at the end
6、http_request function
function :
(2) Split http Agreement request line , utilize sscanf And regular expressions are divided into request methods (GET)、 route (path)、 agreement
(protocol)
(2) call decode_str Function decodes the path into unicode code %23 %34 %5f
(3) Determine file type ;
(4) If it's a regular document , call send_respond send out http Reply protocol header , And call get_file_type Determine file type , call
send_file Send file content ;
(5) If it's a catalog file , call send_respond send out http Reply protocol header , And call get_file_type Determine file type , call
send_dir Send file content ;
7、send_respond function
function :
(1) Send a header response to the browser , Include status lines 、 The message header 、 Blank line
8、get_file_type function
function :
(1) Determine file type , Determine how to open the file
9、send_file function
function :
(1) Open the server local file , Send the server local file to the browser ; Open the failure , send out 404 Page to browser .
10、send_dir function
function :
(1) Open directory file , Show files in current directory ( Directory or file ); Open the failure , send out 404 Page to browser .
11、send_error function
function :
(1) Send error page .
4 Function relation

This figure is just for the convenience of showing the process , Not standard .
5 test
compile
gcc tool.c server.c main.c -o server
function
./server 9527 /home/winter/networkProject/epoll_http_server/dir/

Ahead of time /home/winter/networkProject/epoll_http_server/dir/ Store a write file under the path
case 1: open 1.2.3 file 
case 2: open gif file

case 3: open mp3 file

6 summary
(1)、 Create a monitoring red black tree , Create listening file descriptor lfd, Hang the monitor file descriptor on the red black tree ;
(2)、 Use the red black tree to listen for file descriptors , If listening for file descriptors lfd Read event , Then handle the connection event , The resulting communication file descriptor cfd Hang it on the red black tree to monitor , If the communication file descriptor cfd Read event , Then deal with communication events ;
(3)、 A communication event indicates that the browser has http request , What we use here is get agreement , analysis get agreement , Get the content requested by the browser , Determine whether it is a file request or a directory request ;
(4)、 If it is a file request , Then the server reads the local file , And in accordance with the http Send response header in response format 、 Request content to browser ;
(5)、 If it is a directory request , Then the browser needs to traverse the directory , And in accordance with the http Send response header in response format 、 File directory to browser ;
(6)、(4)(5) You also need to judge the file / Whether the directory exists , If it doesn't exist , You need to send 404 page ;
(7)、 Because there are many kinds of documents , You need to judge and parse different file types ;
(8)、 because http Each line of the agreement is in /r/n ending , all (3) Chinese analysis http Special judgment is needed in the agreement ;
(9)、 Attention should be paid to the problem of Chinese garbled code , Write encoding and decoding functions ;
边栏推荐
猜你喜欢

Digital collections start the 100 billion level market

【技术科普】联盟链Layer2-论一种新的可能性

iQOO 10系列来袭 OriginOS原系统强化手机体验

小程序毕设作品之微信校园二手书交易小程序毕业设计成品(7)中期检查报告

【JDBC】报错Exception in thread “main”com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communica

园区招商难在“哪”?产业园区招商引资困点难点问题盘点

上采样方式(反卷积、插值、反池化)

你的 NFT 会消失吗?DFINITY 提供 NFT 存储最佳方案

基于网络防御知识图谱的0day攻击路径预测方法

基于API调用管理的SDN应用层DDoS攻击防御机制
随机推荐
自定义View:悬浮球与加速球
Interface Fiddler introduction and installation
C语言 文件操作(含所有知识点,相关函数及代码详解)
聪明人的游戏提高篇:第三章第三课:找数游戏(find)
启牛老师说给开的vip账户安全吗?
Educational Codeforces Round 132 A - D
基于以太坊状态数据库的攻击与防御方案
【无标题】分享一个基于Abp Vnext开发的API网关项目
Inside the hard core of LAN SDN technology - 15 MPLS implementation of user roaming in the three from thing to person Park
直播预告 | openGauss的自治运维平台DBMind实践分享
uniapp切换tab栏显示不同页面并记住页面位置和上拉获取新数据
J9数字论:什么是 Web3.0?Web3.0 有哪些特征?
How much is the CPU temperature? Is it normal that the CPU will burn at 100 ℃ for a long time
小程序毕设作品之微信校园二手书交易小程序毕业设计成品(4)开题报告
聪明人的游戏提高篇:第三章第三课例题:素数的秘密(prime)
妙用cURL
W25q128fv translation (II)
基于证据理论物联网安全态势感知方法研究
Analysis of cache read and write strategy
编写一个具有搜索提示的搜索框