当前位置:网站首页>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

 Insert picture description here

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/

 Insert picture description here

Ahead of time /home/winter/networkProject/epoll_http_server/dir/ Store a write file under the path

case 1: open 1.2.3 file
 Insert picture description here

case 2: open gif file

 Insert picture description here

case 3: open mp3 file

 Insert picture description here

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 ;

原网站

版权声明
本文为[biyezuopinvip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222035102781.html