The browser is the new GUI !



Task

Serve our standard text   Goodbye, World!   to   http://localhost:8080/   so that it can be viewed with a web browser.

The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.

Note that starting a web browser or opening a new window with this URL
is not part of the task.

Additionally, it is permissible to serve the provided page as a plain text file (there is no requirement to serve properly formatted HTML here).

The browser will generally do the right thing with simple text like this.




' Define HTTP constants
CONST New$ = CHR$(13) & NL$
CONST Sep$ = CHR$(13) & NL$ & CHR$(13) & NL$
CONST Msg$ = "<html><head>BaCon web greeting</head><body><h2>Goodbye, World!</h2></body></html>"
 
' Get our IP
Ip$ = "localhost"
PRINT "Connect from browser '", Ip$, ":8080'."
 
' Ignore child signals to avoid zombie processes
SIGNAL SIG_IGN, SIGCHLD
 
' Open listening port
OPEN Ip$ & ":8080" FOR SERVER AS mynet
 
' Keep receiving requests
WHILE TRUE
 
    ' Handle for newly incoming connection
    fd = ACCEPT(mynet)
 
    ' Incoming connection -> create background process
    spawn = FORK
 
    ' We are in the child
    IF spawn = 0 THEN
 
        ' Get the request
        REPEAT
            RECEIVE dat$ FROM fd
            PRINT dat$
        UNTIL RIGHT$(dat$, 4) = Sep$
 
        ' Reply that we're OK
        SEND "HTTP/1.1 200 Ok" & New$ & "Content-Length: " & STR$(LEN(Msg$)) & Sep$ & Msg$ TO fd
 
        ' Close connection
        CLOSE SERVER fd
 
        ' End this process
        ENDFORK
    ENDIF
WEND