问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Web服务器编程实验:使用C++和Boost库实现基本功能

创作时间:
作者:
@小白创作中心

Web服务器编程实验:使用C++和Boost库实现基本功能

引用
CSDN
1.
https://blog.csdn.net/lijj0304/article/details/137605114

本文将带你完成一个简单的Web服务器编程实验。通过这个实验,你将学习如何处理HTTP请求、解析请求内容、从服务器文件系统中获取文件,并创建HTTP响应信息。实验将使用C++语言和Boost库来实现这些功能。

实验目的

  1. 处理一个HTTP请求
  2. 接收并解析HTTP请求
  3. 从服务器文件系统中获得被请求的文件
  4. 创建一个包括被请求文件的HTTP响应信息
  5. 直接发送该信息到客户端

具体内容

  1. 使用C++程序实现Web服务器功能
  2. 用HTML语言编写两个HTML文件,并制作两个网页,以验证Web服务器能否成功运行
  3. 验证处理HTTP请求和应对错误请求显示错误信息两种情况

实验过程

用HTML语言编写制作三个简易网页:

  1. 主页,包括欢迎信息和一个跳转链接
  2. 跳转页,包含一个图片和提示信息
  3. 404错误处理页,当跳转到无法访问的地址时显示

编写C++代码,使用Boost.Asio库处理TCP连接和数据读写。使用Boost.Filesystem获取文件的扩展名和检查文件是否存在。监听8888端口的访问,并实现一些获取返回信息和跳转页面的逻辑。然后编译链接运行。

在Linux虚拟机环境中运行,先通过ifconfig获取局域网内的IP(例如192.168.146.138),然后在物理机上运行浏览器,在地址栏中输入192.168.146.138:8888进入主页,依次测试跳转和输入错误地址的情况。

关键代码讲解

主要处理逻辑代码如下:

void handle_request(tcp::socket& socket) {
    try {
        boost::asio::streambuf request;
        boost::asio::read_until(socket, request, "\r\n");
        std::string method, path, protocol;
        std::istream request_stream(&request);
        request_stream >> method >> path >> protocol;
        if (path == "/") {
            path = "/index.html";
        }
        std::string full_path = root_dir + path;
        std::ifstream file(full_path, std::ios::binary);
        boost::asio::streambuf response;
        std::ostream response_stream(&response);
        if (!file) {
            // Open the 404.html file
            std::ifstream file_404(root_dir + "/404.html", std::ios::binary);
            if (!file_404) {
                response_stream << "HTTP/1.0 500 Internal Server Error\r\n";
                response_stream << "Connection: close\r\n\r\n";
                std::cout << "Response: 500 Internal Server Error" << std::endl;
            } else {
                response_stream << "HTTP/1.0 404 Not Found\r\n";
                response_stream << "Content-Type: text/html\r\n";
                response_stream << "Connection: close\r\n\r\n";
                response_stream << file_404.rdbuf();
                std::cout << "Response: 404 Not Found" << std::endl;
            }
        } 
        else {
            response_stream << "HTTP/1.0 200 OK\r\n";
            response_stream << "Content-Type: " << get_content_type(full_path) << "\r\n";
            response_stream << "Connection: close\r\n\r\n";
            response_stream << file.rdbuf();
            std::cout << "Response: 200 OK, Content-Type: " << get_content_type(full_path) << std::endl;
        }
        boost::asio::write(socket, response);
        socket.shutdown(tcp::socket::shutdown_both);
    } catch (boost::system::system_error& e) {
        if (e.code() != boost::asio::error::eof) {
            throw; // Rethrow if it's not the expected exception.
        }
        // Handle EOF exception here if necessary.
        std::cout << "Connection closed by client." << std::endl;
    }
}  

这段代码首先读取请求行(方法、路径和协议),然后根据路径找到相应的文件。如果文件不存在,它会返回一个404错误页面;如果文件存在,它会返回文件的内容。主函数中只需创建一个TCP接受器,然后进入一个无限循环,接受新的连接并处理请求。

运行示例

  1. 当输入IP:8888实现访问主页
  2. 当点击“Next Page”,会转到下一个界面,展示预设好的内容
  3. 当输入一个错误的网址时,例如aaa.html,将会显示404界面

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号