2015-12-21 64 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