#!/usr/local/bin/tops -s /usr/local/tops/sys -u /opt/mytops/usr/
{  File tops_https
   March 2011

   Copyright (C) 2011  Dale R. Williamson

   Running the program as a daemon, this script listens on port 443 for 
   an HTTPS request, forwards it to Apache web server running with these
   directives (directives are made in file /etc/apache/httpd.conf):
      BindAddress 127.0.0.1
      Port 9880
   and then sends the HTTP response to the requestor.

   Mar 18 13:36:22 PDT 2011.  If flag APACHE is equal to no, daemon
   tops_wbsvr is running instead.
}
\-----------------------------------------------------------------------

   CATMSG push no catmsg

   no "APACHE" book

   SSL not 
   IF " tops_https: OpenSSL functions are missing" . nl HALT THEN

\  Logging.

   keys? not
   IF
    \ A log file is defined for SYSOUT using word set_sysout:
      "HOME" env "tops_https.log" catpath "LOG" book LOG set_sysout
      LOG "memlog" "LOG" bank

    \ Log memory usage at 10 minutes past every hour:
      "LOGMEM" everyhour "memlog" ptr 10 LOGMEM
   
    \ Display some starting lines in the log file:
      "-" 72 cats nl dot nl "PID " getpid intstr + spaced date + dot nl
   THEN

\  Make sure a web server is listening; this connection is on loop back
\  (127.0.0.1) and is not encrypted:
   APACHE
   IF 9880
   ELSE \ running tops_wbsvr:
      "WBSVRport" exists? \ defined in mytops/usr/uboot.v
      IF WBSVRport ELSE 9880 THEN
   THEN "WPORT" book
   WPORT port_listening not
   IF " tops_https: port" . WPORT .i " not listening; halting" . nl HALT
   THEN

\-----------------------------------------------------------------------

\  Words.

   "tops_http.v" filefound IF (qFile) source THEN

\  The following line defines this server as an HTTP server:
   0 "SERVE_F" "TYPE" bank \ HTTP type

\  Bank the ptr to the word that services HTTP requests into word
\  SERVE_F, the word the program runs on every request to SERVER:
   "REQUESTrun" exists?
   IF "REQUESTrun"
   ELSE "HTTPput" \ HTTPput is the default to service HTTP requests
   THEN ptr (n)
   (n) "SERVE_F" "SERVICE" bank \ ptr to word that services requests

\  In word HTTPput, define the HTTP provider and port (set these
\  even if REQUESTrun is used for SERVE_F.SERVICE):
   ontheweb (f)
   IF "127.0.0.1" "HTTPput" "PROVIDER" bank
      WPORT "HTTPput" "PORT" bank

   ELSE host "plunger" =
      IF \ tops_wbsvr on 127.0.0.1:WBSVRport:
         "127.0.0.1" "HTTPput" "PROVIDER" bank
         WPORT "HTTPput" "PORT" bank

      ELSE "WSERVER" ptr "HTTPput" "PROVIDER" bank
         UDEF "HTTPput" "PORT" bank
      THEN
   THEN

\  Use word APP_CLIENT_ALLOW and its default file, usrpath/ACCESS, to
\  define hosts that are not allowed to connect:
   "APP_CLIENT_ALLOW" missing
   IF " tops_https: APP_CLIENT_ALLOW not present, halting" . nl HALT
   THEN

\  To hook word APP_CLIENT_ALLOW into the program, bank its ptr into
\  word CLIENT_ALLOW, the word the program runs on every connect:
   "APP_CLIENT_ALLOW" ptr "CLIENT_ALLOW" "APP_CLIENT_ALLOW" bank

\-----------------------------------------------------------------------

   pull catmsg

   "HTTPSport" exists? IF HTTPSport ELSE 443 THEN "PORT" book

 \ Thu Mar 17 16:36:14 PDT 2011.  Set flag for tops_http.v:
   yes "PORTSSL" book \ listening port is secure
{
 \ Thu Mar 17 15:51:10 PDT 2011.  Do not run SERVER-CYCLE until this 
 \ has been tested for a few months.

   ontheweb (f)
   IF PORT "SERVER-CYCLE" "PORT" bank
      1 20 / "SERVER-CYCLE" PLAY 
   THEN
}
   4096 netrate

   host "plunger" = IF 5 "NIST_DELTA" ELSE 5 "NIST_SYNC" THEN ALARM           
\  Start the server:
   "*" PORT (qIPaddr nPort) keys?
   IF
    \ This portion runs during interactive debugging when this file
    \ is sourced at the ready prompt:
      (qIPaddr nPort) SERVER_SSL
   ELSE
      (qIPaddr nPort) DSERVER_SSL
   THEN

   private halt
