Rebol 世界上最小的 Http 服务器:为什么要先等待监听端口?

发布于 2024-08-01 15:53:22 字数 958 浏览 1 评论 0原文

在这段代码中

web-dir: %./www/   ; the path to rebol www subdirectory

listen-port: open/lines tcp://:80  ; port used for web connections
buffer: make string! 1024  ; will auto-expand if needed

forever [
    http-port: first wait listen-port

    while [not empty? client-request: first http-port][
        repend buffer [client-request newline]
    ]
    repend buffer ["Address: " http-port/host newline] 

    parse buffer ["get" ["http" | "/ " | copy file to " "]]

    parse file [thru "." [
            "html" (mime: "text/html") |
            "txt"  (mime: "text/plain")
        ]
    ]

    data: read/binary web-dir/:file

    insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
    write-io http-port data length? data              

    close http-port
]

为什么是first in

http-port: first wait listen-port

而不是just

http-port: wait listen-port

In this code

web-dir: %./www/   ; the path to rebol www subdirectory

listen-port: open/lines tcp://:80  ; port used for web connections
buffer: make string! 1024  ; will auto-expand if needed

forever [
    http-port: first wait listen-port

    while [not empty? client-request: first http-port][
        repend buffer [client-request newline]
    ]
    repend buffer ["Address: " http-port/host newline] 

    parse buffer ["get" ["http" | "/ " | copy file to " "]]

    parse file [thru "." [
            "html" (mime: "text/html") |
            "txt"  (mime: "text/plain")
        ]
    ]

    data: read/binary web-dir/:file

    insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
    write-io http-port data length? data              

    close http-port
]

Why first in

http-port: first wait listen-port

instead of just

http-port: wait listen-port

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-08-08 15:53:22

listen-port 上的 wait 会阻塞,直到有新客户端连接为止。 一旦发生这种情况,它只会返回listen-port。 随后的 first 然后检索与新连接的客户端对应的端口。 之后,您有两个不同的端口:listen-port,这是服务器侦听进一步连接的端口,以及 http-port,这是用于通话的端口到新连接的客户端。

REBOL 中的“创建 TCP 服务器” 部分/Core Users Guide 2.3 版在这些方面仍然是最新的。

The wait on the listen-port blocks until a new client connects. Once that happens, it simply returns listen-port. The subsequent first then retrieves port corresponding to the newly connected client. After this, you have two distinct ports: listen-port, which is the port the server listens on for further connects, and http-port, which is the port for talking to the newly connected client.

The section "Creating TCP Servers" from the REBOL/Core Users Guide for version 2.3 is still perfectly up to date in those regards.

谈下烟灰 2024-08-08 15:53:22

wait 阻塞,直到监听端口有活动,然后返回listen-port

因此,first 获取并返回来自 listen-port 的第一行数据。

文档 进行了解释(但很简短)。

wait blocks until listen-port has activity, then returns listen-port.

Thus, first takes and returns the first line of data from listen-port.

The documentation explains (however briefly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文