为什么 Rebol Raw Http Head Request 获取远程文件大小与信息相比非常慢?功能

发布于 2024-10-06 11:39:56 字数 467 浏览 0 评论 0原文

回复信息?非常快:

i: info? http://cdimage.ubuntu.com/daily/current/natty-alternate-i386.iso
i/size

使用 http head 请求可能需要 10 倍的时间,为什么?

port: open tcp://cdimage.ubuntu.com:80
insert port "HEAD /daily/current/natty-alternate-i386.iso HTTP/1.1 ^/"
insert port "Host: cdimage.ubuntu.com ^/^/"
out: copy ""
while [data: copy port][append out data]
block: parse out rejoin [": " newline]
select block "Content-Length"

Response with info? is very quick:

i: info? http://cdimage.ubuntu.com/daily/current/natty-alternate-i386.iso
i/size

With http head request it takes maybe 10 times more time why ?

port: open tcp://cdimage.ubuntu.com:80
insert port "HEAD /daily/current/natty-alternate-i386.iso HTTP/1.1 ^/"
insert port "Host: cdimage.ubuntu.com ^/^/"
out: copy ""
while [data: copy port][append out data]
block: parse out rejoin [": " newline]
select block "Content-Length"

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

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

发布评论

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

评论(1

灯角 2024-10-13 11:39:56

在这种情况下,端口模式负责。您在等待模式(默认情况下处于打开状态)中使用缓冲 I/O。

在http中,当您读取所有服务器字节后,客户端负责关闭端口。

由于您基本上直接使用 TCP,使用插入端口,因此您还负责检测请求的结束并在足够的字节到达时关闭端口。这只能在进行低级 tcp 乐趣时在 /lines 或 /no-wait 中完成。

阅读和信息的东西?为你做。

而 [data: copy port][append out data]

直到发生超时(REBOL 中默认为 30 秒)才会终止。

另外,您的请求似乎有错误...

试试这个:

port: open/lines tcp://cdimage.ubuntu.com:80
insert port {HEAD /daily/current/natty-alternate-i386.iso HTTP/1.0
Accept: */*
Connection: close
User-Agent: REBOL View 2.7.7.3.1
Host: cdimage.ubuntu.com
}
out: form copy port
block: parse out none ;rejoin [": ^/"]
probe select block "Content-Length:"

在这里添加 /lines 似乎会阻止等待。它可能与http方案如何处理打开时的线路模式有关。

在文档中查找 REBOL 端口模式,并在网上对其进行了很好的解释。

如果您使用了trace/net,您就会意识到所有数据包都已收到,并且解释器仍在等待。顺便说一句,您的代码实际上在我的测试中返回了错误 400。

the port modes are responsible in this case. you where using buffered I/O with the wait mode (which is on by default).

in http, the client is responsible closing of the port when you've read all the server bytes.

since you are basically using tcp directly, using insert port, you are responsible for also detecting the end of the request and closing the port when sufficient bytes have arrived. this can only be done in /lines or /no-wait when doing low-level tcp fun.

Something that read and info? do for you.

while [data: copy port][append out data]

doesn't terminate until a timeout occurs (which is 30 seconds by default in REBOL).

also, your request seems to be in error...

try this:

port: open/lines tcp://cdimage.ubuntu.com:80
insert port {HEAD /daily/current/natty-alternate-i386.iso HTTP/1.0
Accept: */*
Connection: close
User-Agent: REBOL View 2.7.7.3.1
Host: cdimage.ubuntu.com
}
out: form copy port
block: parse out none ;rejoin [": ^/"]
probe select block "Content-Length:"

here it seems that adding /lines will prevent the wait. its probably related to how the http scheme handles the line mode on open.

look around for REBOL port modes within the documentation and on the net its well explained all over the place.

if you had used trace/net on, you'd realized that all the packets where received and that the interpreter was just still waiting. btw your code actually returned an error 400 in my tests.

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