DAY 44 HTML and HTTP Server Interaction Notes

HTML and HTTP Server Interaction Notes

I. Text Type (HTML) Processing

  1. Content-Type Setting

    The server sets Content-Type: text/html;charset=utf-8 for HTML files via the send_head function to ensure proper display of Chinese characters (e.g., "Looking ahead to 2026, China's real estate policies..." in 01.html).

    Code snippet:

    c 复制代码
    case FILE_HTML:  
        http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";  
        break;  
  2. HTML File Features

    • 03.html: Login form page, submits data to the /login path via the form tag, including username (username) and password (userpass) input fields.
    • 01.html: Login success page, containing text, headings, lists, image references (1.jpg, 2.png), hyperlinks, and other HTML elements.
    • 02.html: Redirect page with a red background, accessible via the "go to 2" link in 01.html.
    • 04.html: Login failure page, displaying a "Wrong password" prompt.

II. JPEG Type Processing

  1. Identification and Response

    The server identifies JPEG files by checking the URL suffix (.jpg), sets Content-Type: image/jpeg, and sends the corresponding file (e.g., 1.jpg referenced in 01.html).

    Code snippet:

    c 复制代码
    else if (strlen(url) > 4 && 0 == strcmp(&url[strlen(url) - 4], ".jpg"))  
    {  
        send_file(conn, url + 1, FILE_JPG); // url+1 removes the prefix "/" to directly access the filename  
    }  
  2. Notes

    • Ensure JPEG files (e.g., 1.jpg) are in the same directory as the server program; otherwise, the open function will report an error.
    • The server retrieves the file size via the file_size function and sets content-length in the response header for the client to receive the complete file.

III. Short Dialogue (Single Request-Response)

  1. Interaction Flow

    The client (browser) and server interact in a "short dialogue" mode:

    • The client sends an HTTP request (e.g., GET /login?username=zhangsan&userpass=123 HTTP/1.1).
    • The server parses the URL, verifies parameters, and returns the corresponding file (01.html or 04.html).
    • After responding, the server closes the connection via close(conn) (Connection: keep-closed).
  2. Code Implementation

    The main loop accepts connections via accept, processes the request, and immediately closes the connection:

    c 复制代码
    while (1)  
    {  
        int conn = accept(listfd, (SA)&cli, &len);  
        // Process request (recv, parse URL, send_file)  
        close(conn); // Short dialogue: close connection after one request  
    }  

IV. Long Dialogue (Persistent Connection)

  1. Current Limitations

    The existing code does not support long dialogues because the response header sets Connection: keep-closed, and the connection is actively closed after each request.

  2. Implementation Approach

    To support long dialogues (handling multiple requests on the same connection), modifications are needed:

    • Set the response header to Connection: keep-alive.
    • The server does not immediately close conn but continues to receive requests from the same connection in a loop (until the client disconnects).
      Example pseudo-code:
    c 复制代码
    // Modify Connection in send_head  
    http_cmd[4] = "Connection: keep-alive\r\n";  
    
    // Process multiple requests in a loop  
    while (1) {  
        int ret = recv(conn, buf, sizeof(buf), 0);  
        if (ret <= 0) break;  
        // Parse and process request...  
    }  
    close(conn);  

V. Summary

  • The server distinguishes request types (HTML, JPG, PNG, etc.) via URL parsing and sets the correct Content-Type.
  • Short dialogues are suitable for simple requests, easy to implement but less efficient; long dialogues require persistent connections and are ideal for multiple consecutive requests (e.g., pages with multiple image resources).
  • HTML files form an interaction loop with the server via form elements, hyperlinks, etc. (e.g., login flow).
相关推荐
JS_GGbond26 分钟前
前端大扫除:JS垃圾回收与那些“赖着不走”的内存泄露
前端
老年DBA26 分钟前
PostgreSQL BRIN索引揭秘
数据库·postgresql
葡萄城技术团队30 分钟前
轻量级部署:SpreadJS 包依赖优化与打包体积瘦身秘籍
前端
小光学长31 分钟前
基于微信小程序的评奖评优系统51r12nd0(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·微信小程序·小程序
阿湯哥33 分钟前
Design Token 详解
前端
水天需01034 分钟前
HISTCONTROL 介绍
linux
煎蛋学姐35 分钟前
SSM校园扶助综合服务平台的设计与实现r941j(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·校园扶助平台
ℳ₯㎕ddzོꦿ࿐36 分钟前
企业级 MySQL 8.0 物理备份实践:使用 XtraBackup 实现全量与增量自动备份
数据库·mysql
json{shen:"jing"}37 分钟前
08_组件基础
前端·javascript·vue.js
航Hang*38 分钟前
第二章:综合布线技术 —— 综合布线常用器材和工具
网络·期末·复习