深入理解计算机系统 CSAPP 家庭作业11.10

A:

复制代码
//home.html
<form action="/cgi-bin/adder" method="GET">
  <ul>
    <li>
      <label for="n1">n1:</label>
      <input type="text" id="n1" name="n1" /> //name的值决定页面提交后,生成的地址。
    </li>
    <li>
      <label for="n2">n2:</label>
      <input type="text" id="n2" name="n2" /> //name的值决定页面提交后,生成的地址。
    </li>
	<li class="button">
	<button type="submit">Send</button>
	</li>

  </ul>
</form>

home.html放在根目录.那样访问127.0.0.1:XX的时候会直接跳转到这个提交数据的页面.

html文件中的name决定用户点击Send按钮后跳转的地址后的参数名.

复制代码
//adder.c
#include "csapp.h"

int main(void) {
    char *buf, *p;
    char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
    int n1=0, n2=0;

    /* Extract the two arguments */
    if ((buf = getenv("QUERY_STRING")) != NULL) {
	p = strchr(buf, '&');
	*p = '\0';
	sscanf(buf,"n1=%d",&n1); 	//此处决定读取地址的格式
	sscanf(p+1,"n2=%d",&n2);    //此处决定读取地址的格式
    }

    /* Make the response body */
    sprintf(content, "Welcome to add.com: ");
    sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
    sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>", 
	    content, n1, n2, n1 + n2);
    sprintf(content, "%sThanks for visiting!\r\n", content);
  
    /* Generate the HTTP response */
    printf("Connection: close\r\n");
    printf("Content-length: %d\r\n", (int)strlen(content));
    printf("Content-type: text/html\r\n\r\n");
    printf("%s", content);
    fflush(stdout);

    exit(0);
}

adder.c文件中的sscanf中的按格式输入是"n1=%d",这里是和html文件对应的. .adder程序是从html文件生成的地址中读取变量的.

相关推荐
鸠摩智首席音效师9 分钟前
linux 系统中 Shutting Down, Restarting, Halting 有什么区别 ?
linux·运维·服务器
CIb0la10 分钟前
Linux 将继续不支持 HDMI 2.1 实现
linux·运维·服务器
德生coding1 小时前
wifi驱动编译出来的驱动文件怎么做strip
linux
鹿鸣天涯1 小时前
Kali Linux 2025.4 发布:桌面环境增强,新增 3 款安全工具
linux·运维·安全
学习&笔记2 小时前
MTK(系统篇)user版本无法使用setenforce 0命令关闭selinux权限
linux·运维·服务器
Bdygsl2 小时前
Linux(8)—— 进程优先级与环境变量
linux·运维·服务器
another heaven2 小时前
【软考 磁盘磁道访问时间】总容量等相关案例题型
linux·网络·算法·磁盘·磁道
杨云龙UP4 小时前
MySQL 8.0.x InnoDB 写入链路优化:Redo Log 与 Buffer Pool 扩容与缓冲区调优实战记录-20251029
linux·运维·数据库·sql·mysql
txzz88885 小时前
CentOS-Stream-10 系统安装之网络设置
linux·运维·服务器·网络·计算机网络·centos
qq_401700415 小时前
嵌入式Linux网口MAC地址修改
linux·运维·macos