最近学习到了《深入理解计算机系统》的第11章网络编程,在最后一节是一个名为Tiny的小型web服务器的实现,源代码书中已经给出,这里就不再复制粘贴了。这篇小博客主要记录一下课后题10的解答。原题目为:
写出CGI adder函数的HTML表单。你的表单应该包括两个文本框,用户将需要相加的两个数字填在这两个文本框中。你的表单应该使用GET方法请求内容。
因为我以前没接触过HTML表单,先百度之,找到了w3school的HTML教材,看了一下表单的部分,写出了一个很简单的小表单,命名为index.html:
1 nbsp;html> 2 3 4 5 <p> 6 please enter two numbers:<br> 7 </p> 8 916 17 18
这是输入localhost:8888之后浏览器显示的效果图。8888是我设置的tiny的端口,至于为什么没有后边的/index.html,是因为我在源代码中将index.html设为了主界面。
我们可以随便输入两个数字,点击Submit按钮,结果肯定是不对的。。
如上图所示,结果显示的是0。注意在该网页的地址栏处,我们看到"localhost:8888/cgi-bin/adder?num1=22&num2=22",看过tiny源码我们就会知道,adder.c所要分析的数据是在&符号两边的纯数字,也就是说,要想正确的被adder.c程序求和,地址栏应该显示"localhost:8888/cgi-bin/adder?22&22"才对。出现上图中的错误的原因就是,adder程序没有取得两个参数的数值大小。解决方法很简单,只需要在adder.c程序里把两个数字取出来就行了,整个adder.c代码如下所示:
1 #include "net.h" 2 3 int main(void) 4 { 5 char *buf, *p; 6 char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE]; 7 char tmp[MAXLINE]; 8 int n1 = 0, n2 = 0; 9 10 if ( (buf = getenv("QUERY_STRING")) != NULL) {11 p = strchr(buf, '&');12 *p = '\0';13 14 strcpy(arg1, buf);15 strcpy(arg2, p+1);16 17 //用来取出两个参数的代码18 p = strchr(arg1, '=');19 strcpy(arg1, p+1);20 p = strchr(arg2, '=');21 strcpy(arg2, p+1);22 23 n1 = atoi(arg1);24 n2 = atoi(arg2);25 }26 27 sprintf(content, "QUERY_STRING = %s", buf);28 sprintf(content, "Welcome to add.com: ");29 //sprintf(content, "arg1=%s, arg2=%s\n", arg1, arg2); 调试输出参数30 sprintf(content, "%sThe Internet addition portal.\r\n<p>", content);31 sprintf(content, "%sThe answer is: %d + %d = %d\r\n</p><p>",32 content, n1, n2, n1 + n2);33 sprintf(content, "%sThanks for visiting!\r\n", content);34 35 //generate the http response36 printf("Connection: close\r\n");37 printf("Content-length: %d\r\n", (int)strlen(content));38 printf("Content-type: text/html\r\n\r\n");39 printf("%s", content);40 fflush(stdout);41 42 exit(0);43 }</p>
重新编译adder.c之后,我们再次在浏览器输入网址:localhost:8888,输入两个数字,结果如图:
至此,我们学习Tiny的第一阶段就算完成了,完成了课后题11.10的要求,能够处理来自浏览器的静态请求和动态请求。但是,由于我们的Tiny一次只能处理一个连接,效率太低了。下一节我们就要对Tiny进行一下改进,使其能够支持并发处理。
The above is the detailed content of A deep dive into tiny servers. For more information, please follow other related articles on the PHP Chinese website!

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.