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.




      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets
 
      maxSess% = 8
      DIM sock%(maxSess%-1), rcvd$(maxSess%-1), Buffer% 255
 
      ON ERROR PRINT REPORT$ : PROC_exitsockets : END
      ON CLOSE PROC_exitsockets : QUIT
 
      port$ = "8080"
      host$ = FN_gethostname
      PRINT "Host name is " host$
 
      listen% = FN_tcplisten(host$, port$)
      PRINT "Listening on port ";port$
 
      REPEAT
        socket% = FN_check_connection(listen%)
        IF socket% THEN
          FOR i% = 0 TO maxSess%-1
            IF sock%(i%) = 0 THEN
              sock%(i%) = socket%
              rcvd$(i%) = ""
              PRINT "Connection on socket "; sock%(i%) " opened"
              EXIT FOR
            ENDIF
          NEXT i%
          listen% = FN_tcplisten(host$, port$)
        ENDIF
 
        FOR i% = 0 TO maxSess%-1
          IF sock%(i%) THEN
            res% = FN_readsocket(sock%(i%), Buffer%, 256)
            IF res% >= 0 THEN
              Buffer%?res% = 0
              rcvd$(i%) += $$Buffer%
              IF LEFT$(rcvd$(i%),4) = "GET " AND ( \
              \ RIGHT$(rcvd$(i%),4) = CHR$13+CHR$10+CHR$13+CHR$10 OR \
              \ RIGHT$(rcvd$(i%),4) = CHR$10+CHR$13+CHR$10+CHR$13 OR \
              \ RIGHT$(rcvd$(i%),2) = CHR$10+CHR$10 ) THEN
                rcvd$(i%) = ""
                IF FN_writelinesocket(sock%(i%), "HTTP/1.0 200 OK")
                IF FN_writelinesocket(sock%(i%), "Content-type: text/html")
                IF FN_writelinesocket(sock%(i%), "")
                IF FN_writelinesocket(sock%(i%), "<html><head><title>Hello World!</title></head>")
                IF FN_writelinesocket(sock%(i%), "<body><h1>Hello World!</h1>")
                IF FN_writelinesocket(sock%(i%), "</body></html>")
                PROC_closesocket(sock%(i%))
                PRINT "Connection on socket " ; sock%(i%) " closed (local)"
                sock%(i%) = 0
              ENDIF
            ELSE
              PROC_closesocket(sock%(i%))
              PRINT "Connection on socket " ; sock%(i%) " closed (remote)"
              sock%(i%) = 0
            ENDIF
          ENDIF
        NEXT i%
 
        WAIT 0
      UNTIL FALSE
      END