Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug and avoid periodic REBOL2 error, that try[] does not(?) catch?

Tags:

rebol

rebol2

Apparently un-catchable error while toying around with Rebol/Core (278-3-1) to make a kind-of web-server to serve a static text, containing a redirect link to a new service location.

The specific location of the error appear to be in example code written by Carl Sassenrath himself, back in 2006, so I'm kind of baffled there could be an undetected error after all these years.

I have three of these scripts running simultaneous, monitoring three individual ports. Essentially the script works as it should... when accessed repeatedly with multiple browsers at once (on all parallel scripts) it appear appear to be pretty stable... but one after another they fail. Sometimes after 2 minutes, sometimes after 20 minutes - after adding the print statements sometimes even after 60 minutes - but eventually they will fail like this:

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

I've tried wrapping just about every part of the program loop in a try[][exception], but the error still occurs. Unfortunately my search-fu appear to be weak this time of year, as I haven't found anything that could explain the problem.

The code is a cut down version of Carl Sassenrath's Tiny Web Server, slightly modified to bind to a specific IP, and to emit HTML instead of loading files:

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

I'm looking forward to see what you guys make out of this!

like image 554
fsteff Avatar asked Dec 21 '15 13:12

fsteff


1 Answers

You get an error when trying to read from a closed connection. This seems to work.

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 "============"
]
like image 172
sqlab Avatar answered Sep 29 '22 11:09

sqlab