1 The Significance of This Sharing
Source code address:
https://github.com/shangguanyongshi/WebFileServer.git
Video explanation:https://www.bilibili.com/video/BV1bGkPYzExW/

After learning C++ and Linux programming, you can use the WebFileServer file server as a practical project. It is more meaningful than the WebServer project as it includes file upload functionality.
This sharing not only explains how to run the WebFileServer project but also helps everyone master how to quickly get familiar with C++ projects that they are not familiar with.

2 Project Features
The Web File Server manages all files in a specified server folder by sending HTTP requests through a browser. The main features include:
-
Returning all files in the folder in HTML page format
-
Allowing local file uploads to the server
-
Enabling download operations on files in the list
-
Allowing deletion of specified files on the server

3 Overall Framework
-
Using the Reactor event handling model, the main thread uses epoll to listen for all events, while worker threads are responsible for executing the logic of the events.
-
Pre-creating a thread pool, when an event occurs, it is added to the work queue of the thread pool, using a random selection algorithm to choose a thread from the pool to handle the event in the work queue.
-
Using the HTTP GET method to retrieve the file list and initiate requests to download or delete files. Using the POST method to upload files to the server.
-
The server uses a finite state machine to parse request messages, executing operations based on the parsing results, and sending pages, files, or redirect messages back to the client.
-
The server uses the sendfile function to implement zero-copy data transmission.
4 Compilation and Running
-
Build the project
sh ./build.sh
The default compilation does not support debugging; you can modify the makefile to add the -g compilation option.

Then recompile to facilitate debugging the code.
-
Start the file server
./main
-
In the browser, enter server IP:port number (the default port number is 8888)
serverip:8888
5 How to Quickly Analyze Code

5.1 Main Function Entry
(gdb) b mainBreakpoint 1 at 0x5555555590ad: file main.cpp, line 3.
5.2 Analyzing the Network Model
By using info threads, you can see that there are five threads in total, with the main thread calling epoll. Combined with the README explanation, it can be inferred that this project uses a single epoll + thread pool model.
* 1 Thread 0x7ffff79a9740 (LWP 339328) "main" 0x00007ffff7c1c68e in epoll_wait (epfd=4, events=0x7fffffffaf14, maxevents=1024, timeout=-1) at ../sysdeps/unix/sysv/linux/epoll_wait.c:30 2 Thread 0x7ffff79a8700 (LWP 339343) "main" futex_abstimed_wait_cancelable ( ) at ../sysdeps/nptl/futex-internal.h:320 3 Thread 0x7ffff71a7700 (LWP 339344) "main" futex_abstimed_wait_cancelable ( ) at ../sysdeps/nptl/futex-internal.h:320 4 Thread 0x7ffff69a6700 (LWP 339345) "main" futex_abstimed_wait_cancelable ( ) at ../sysdeps/nptl/futex-internal.h:320 5 Thread 0x7ffff61a5700 (LWP 339346) "main" futex_abstimed_wait_cancelable ( ) at ../sysdeps/nptl/futex-internal.h:320

5.3 Network Data Transmission
5.3.1 Data Reception
Breakpoint recv
Thread 4 "main" hit Breakpoint 4, __libc_recv (fd=5, buf=0x7ffff69a5600, len=2048, flags=0) at ../sysdeps/unix/sysv/linux/recv.c:2424 ../sysdeps/unix/sysv/linux/recv.c: No such file or directory.(gdb) bt#0 __libc_recv (fd=5, buf=0x7ffff69a5600, len=2048, flags=0) at ../sysdeps/unix/sysv/linux/recv.c:24#1 0x000055555555df4a in HandleRecv::process (this=0x55555558bb90) at event/myevent.cpp:38#2 0x000055555555c114 in ThreadPool::run (this=0x55555558b2b0) at threadpool/threadpool.cpp:114#3 0x000055555555bc5c in ThreadPool::worker (arg=0x55555558b2b0) at threadpool/threadpool.cpp:77#4 0x00007ffff7f8a609 in start_thread (arg=<optimized out>) at pthread_create.c:477#5 0x00007ffff7c1c353 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
It is clear that this project reads socket data and parses it in the threads of the thread pool.
5.3.2 Data Sending
Breakpoint send

This indicates that the data response is also handled in the threads of the thread pool.
6 Expansion Suggestions

It is recommended to test the functionality of this project against the muduo library to analyze performance differences.
It is also suggested to conduct more comparative tests with nginx.
This can continuously optimize the performance of the project and provide more content to discuss during interviews.