2015-12-21 61 views
6

Rebol/Core(278-3-1)一起玩弄一种类似的网络服务器来提供一个静态文本,包含一个重定向链接到一个新的服务位置,显然是一个不可捕捉的错误。调试并避免定期REBOL2错误,try []不会(?)捕获?

错误的具体位置似乎在2006年由Carl Sassenrath自己编写的示例代码中,所以我有点困惑,在这些年后可能会有一个未被发现的错误。

我有三个脚本同时运行,监视三个单独的端口。从本质上讲,脚本的工作原理应该是......当多个浏览器一次(在所有并行脚本中)重复访问时,它看起来似乎相当稳定......但一个接一个地失败。有时候2分钟,有时20分钟后后 - 甚至是60分钟后加入打印语句有时后 - 但他们最终会失败这样的:

** Script Error: Out of range or past end
** Where: forever
** Near: not empty? request: first http-port

我已经试过包装程序循环的几乎每一个部分尝试[] [例外],但错误仍然发生。不幸的是我的搜索功能似乎每年的这个时候都很弱,因为我还没有找到任何可以解释问题的东西。

的代码是卡尔·萨森拉斯的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 "============" 
] 

我很期待看到你伙计们就是这样做的!

回答

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 "============" 
] 
+0

谢谢sqlab。 – fsteff

+0

小心解释您是如何得出原始代码是从关闭连接读取的结论?调试步骤将不胜感激。 – fsteff

+0

结论来自考虑和试验的结合以及大量的经验 – sqlab

1

让我们看看空的文档? 摘要:

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

空?系列 参数:

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

那么空?需要串行,端口或位集或字符串参数。只要端口连接处于打开状态,您的变量(请求)正在获取它们中的任何一个。空?此后可以确定它是否在变量的尾部。 当连接关闭/中断时,变量将不会收到任何内容,但连接到端口时会出现访问错误。错误没有尾巴。空?会因为错误而感到困惑并崩溃。

sqlab已经替换为空?与尝试

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

尝试功能对于频繁情况下的快捷方式:

error? try [block] 

所有他是防范错误和无。 如果没有发生错误,ATTEMPT返回块的结果。如果发生错误,则返回NONE。 也直到

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

他防范这两种。

因此他的代码正在工作。

+0

感谢您提供一个非常好的和非常有教育意义的答案。正是我期望的推理。 – fsteff