深入理解计算机系统 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文件生成的地址中读取变量的.

相关推荐
小安运维日记1 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
CoolTiger、4 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
学习3人组5 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神5 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0666 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen016 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦6 小时前
linux 解压缩
linux·运维·服务器
牧小七7 小时前
Linux命令---查看端口是否被占用
linux
鸡鸭扣8 小时前
虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
linux·运维·ubuntu
友友马9 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http