首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调试并避免周期性的REBOL2错误,try[]没有(?)接住?

调试并避免周期性的REBOL2错误,try[]没有(?)接住?
EN

Stack Overflow用户
提问于 2015-12-21 13:09:01
回答 2查看 168关注 0票数 6

显然,在玩Rebol/Core (278-3-1)使一种web服务器为静态文本提供服务时,出现了不可捕捉的错误,其中包含指向新服务位置的重定向链接。

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

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

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

我尝试过在一次尝试中包装程序循环的每个部分,但是错误仍然发生。不幸的是,这个时候我的搜索似乎很薄弱,因为我没有找到任何可以解释这个问题的东西。

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

代码语言:javascript
复制
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 "============"
]

我很期待能看到你们对此的看法!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-22 08:33:33

尝试从关闭的连接读取时会出现错误。这似乎很管用。

代码语言:javascript
复制
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 "============"
]
票数 3
EN

Stack Overflow用户

发布于 2016-01-17 17:10:10

让我们看看文件是空的?摘要:

如果序列位于其尾部,则返回TRUE。用法:

空的?系列参数:

系列-系列论证。(必须是:系列端口位集)

这么空?需要序列、端口或位集或字符串参数。只要打开到端口的连接,您的变量(request)就会得到其中的任何一个。空的?然后才能确定它是否在变量的尾部。当连接关闭/中断时,您的变量只会收到连接到端口的访问错误。错误没有尾巴。空的?会因错误而混乱和崩溃。

已经取代空房了吗?用尝试

代码语言:javascript
复制
if attempt [all [request: first http-port  not empty? request]]

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

代码语言:javascript
复制
error? try [block]

有了all,他既不会犯错,也不会犯错。如果没有发生错误,尝试返回块的结果。如果确实发生错误,则返回“无”。也可以使用,直到

代码语言:javascript
复制
any [not request: first http-port empty? request]

他在防范这两种情况。

所以他的代码起作用了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34396474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档