6

Rebol/Core在玩弄(278-3-1) 以制作一种网络服务器来提供静态文本时显然无法捕获的错误,其中包含指向新服务位置的重定向链接。

错误的具体位置似乎在Carl Sassenrath他在 2006 年编写的示例代码中,所以我有点困惑,这么多年后可能会出现未检测到的错误。

我让其中三个脚本同时运行,监控三个单独的端口。本质上,该脚本可以正常工作...当一次使用多个浏览器(在所有并行脚本上)重复访问时,它似乎很稳定...但是一个接一个地失败了。有时在 2 分钟后,有时在 20 分钟后 - 在添加打印语句后有时甚至在 60 分钟后 - 但最终它们会像这样失败:

** 脚本错误:超出范围或结束
** 哪里:永远
** 附近:不是空的?请求:第一个 http 端口

我尝试将程序循环的几乎每个部分都包装在 try[][exception] 中,但错误仍然存​​在。不幸的是,每年的这个时候我的搜索功能似乎很弱,因为我没有找到任何可以解释这个问题的东西。

该代码是 Carl Sassenrath 的Tiny Web Server的精简版,稍作修改以绑定到特定 IP,并发出 HTML 而不是加载文件:

REBOL [title: "TestMovedServer"]
AppName: "Test"
NewSite: "http://test.myserver.org"

listen-port: open/lines tcp://:81   browse http://10.100.44.6?
buffer: make string! 1024  ; will auto-expand if needed

forever [
    http-port: first wait listen-port
    clear buffer

    while [not empty? request: first http-port][
        print request
        repend buffer [request newline]
        print "----------"
    ]
    repend buffer ["Address: " http-port/host newline] 
    print buffer
    Location: ""
    mime: "text/html"
    parse buffer ["get" ["http" | "/ " | copy Location to " "]]

    data: rejoin [{
        <HTML><HEAD><TITLE>Site Relocated</TITLE></HEAD>
        <BODY><CENTER><BR><BR><BR><BR><BR><BR>
        <H1>} AppName { have moved to <A HREF="} NewSite {">} NewSite {</A></H1>
        <BR><BR><BR>Please update the link you came from.
        <BR><BR><BR><BR><BR><A HREF="} NewSite Location {">(Continue directly to the requested page)</A>
        </CENTER></BODY></HTML>
    }]  
    insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
    write-io http-port data length? data
    close http-port
    print "============"
]

我很期待看到你们从中得到什么!

4

2 回答 2

3

尝试从关闭的连接中读取时出现错误。这似乎有效。

n: 0
forever [
   http-port: first wait listen-port
   clear buffer
   if attempt [all [request: first http-port  not empty? request]] [
      until [
        print request
        repend buffer [request newline]
        print "----------"
        any [not request: first http-port empty? request]
      ]
      repend buffer ["Address: " http-port/host newline] 
      print buffer
      Location: ""
      mime: "text/html"
      parse buffer ["get" ["http" | "/ " | copy Location to " "]]

      data: rejoin [{
        <HTML><HEAD><TITLE>Site Relocated</TITLE></HEAD>
        <BODY><CENTER><BR><BR><BR><BR><BR><BR>
        <H1>} AppName n: n + 1 { has moved to <A HREF="} NewSite {">} NewSite {</A></H1>
        <BR><BR><BR>Please update the link you came from.
        <BR><BR><BR><BR><BR><A HREF="} NewSite Location {">(Continue directly to the requested page)</A>
        </CENTER></BODY></HTML>
      }]  
      insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
      write-io http-port data length? data
  ]
  attempt [close http-port]
  print "============"
]
于 2015-12-22T08:33:33.150 回答
1

让我们看看空的文档?概括:

如果系列在其尾部,则返回 TRUE。用法:

空的?系列参数:

series - 系列参数。(必须是:系列端口位集)

这么空?需要系列、端口或位集或字符串参数。只要与端口的连接处于打开状态,您的变量(request )就会得到其中的任何一个。空的?之后可以确定它是否在变量的尾部。当连接关闭/中断时,您的变量什么也没收到,但连接到端口时出现访问错误。错误没有尾巴。空的?感到困惑并因错误而崩溃。

sqlab 已经替换为空了?尝试

if attempt [all [request: first http-port  not empty? request]]

ATTEMPT 函数是以下常见情况的快捷方式:

error? try [block]

他竭尽全力防止错误以及没有错误。如果没有发生错误,ATTEMPT 返回块的结果。如果确实发生了错误,则返回 NONE。也有直到

any [not request: first http-port empty? request]

他正在防范两者。

因此他的代码正在运行。

于 2016-01-17T17:10:10.033 回答