Web 浏览器如何从 http 服务器访问内容(html)?

How web browser access content(html) from http server?

目前,我正在研究lighttpd,这是一个小型网络服务器。

我试图通过使用 strace 来弄清楚服务器是如何工作的,但我发现了一些奇怪的东西,在 strace 运行 lighttpd 之后:

$ strace ./lighttpd -D -f lighttpd.conf

...

# Web browser access localhost:3000/

epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [{EPOLLIN, {u32=3206347936, u64=94861854047392}}], 1025, 1000) = 1
accept4(3, {sa_family=AF_INET, sin_port=htons(40532), sin_addr=inet_addr("127.0.0.1")}, [112->16], SOCK_CLOEXEC|SOCK_NONBLOCK) = 5
getsockopt(5, SOL_TCP, TCP_NODELAY, [1], [4]) = 0
brk(0x5646bf20f000)                     = 0x5646bf20f000
read(5, 0x5646bf20c600, 8191)           = -1 EAGAIN (Resource temporarily unavailable)
epoll_ctl(4, EPOLL_CTL_ADD, 5, {EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=3206595920, u64=94861854295376}}) = 0
accept4(3, 0x7ffcb0801b60, [112], SOCK_CLOEXEC|SOCK_NONBLOCK) = -1 EAGAIN (Resource temporarily unavailable)
epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [], 1025, 1000)           = 0
epoll_wait(4, [], 1025, 1000)           = 0

# Web browser access localhost:3000/ second times

brk(0x56083a818000)                     = 0x56083a818000
stat("/.../test/", {st_mode=S_IFDIR|0775, st_size=4096, ...}) = 0
stat("/..../test/index.html", {st_mode=S_IFREG|0664, st_size=15608, ...}) = 0
openat(AT_FDCWD, "/.../test/index.html", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC) = 7
fstat(7, {st_mode=S_IFREG|0664, st_size=15608, ...}) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x4), ...}) = 0
writev(5, [{iov_base="HTTP/1.1 304 Not Modified\r\nConte"..., iov_len=217}], 1) = 217
read(5, 0x56083a7f4600, 8191)           = -1 EAGAIN (Resource temporarily unavailable)
epoll_wait(4, [], 1025, 1000)           = 0

为什么浏览器请求后没有任何IO(或send)?如果不是,浏览器如何知道要显示什么内容?

以下是上述情况下 writev 中缓冲区的内容(第二个 http 响应):

HTTP/1.1 304 Not Modified
Content-Type: text/html
ETag: "2940132131"
Last-Modified: Mon, 08 Mar 2021 14:20:42 GMT
Content-Length: 15608
Accept-Ranges: bytes
Date: Mon, 08 Mar 2021 23:35:28 GMT
Server: lighttpd/1.4.60-devel-lighttpd-1.4.59-40-g8c7dbf1a

@Steven:你的问题省略了你不理解的strace的重要部分,但出于某种原因仍然选择省略。

在继续研究 lighttpd 之前,您可能希望仔细阅读 strace 手册页。

然后试试这个:strace -s 4096 ./lighttpd -D -f lighttpd.conf