dynamic_http_server.anubis 5.99 KB


                               The Anubis Project. 
                               
               A 'generic' web server automatically loading web sites. 
               
read tools/basis.anubis               
read web/multihost_http_server.anubis
read web/making_a_web_site.anubis               
               
   Since Anubis 1.13, .adm files can be loaded dynamically (see 'predefined.anubis', section 7.8).
   The web server defined here automatically loads web sites whose .adm files (secondary modules)
   are in a given directory. If such a secondary adm is updated, it is automatically reloaded by
   the server within 5 seconds. 
   
   Each secondary adm must be of type 'Web_Site_Description' (defined in 'multihost_http_server.anubis'), 
   hence constructed as follows (using the tools in 'making_a_web_site.anubis'): 
   
   global define Web_Site_Description 
      site_name
        =
      ...
      
   The program 'multihost_http_server.anubis' has been enhanced so that if a requested web site is
   not loaded, a default web site is served. This default web site needs just have the name "default"
   among its common names. A generic default site is provided in 'default_web_site.anubis'. 
      
   The web server itself is the primary module defined by: 
   
global define One
   dynamic_http_server
     (
       List(String) args
     ).   
      
   You can pass parameters (in any order) on the command line:
   
     parameter             meaning                                      default value
   --------------------------------------------------------------------------------------------------------
     -a:nnn.nnn.nnn.nnn    the IP address on which the server listens   0.0.0.0   (listen on all interfaces)
     -p:nnnn               the port number for HTTP                     80
    -sp:nnnn               the port number for HTTPS                    443
     -c:name               SSL certificate common name                  georges
     -d:directory          directory for adm files                      my_anubis/web_site_modules
      
      
   You can also modify the following definitions if you like: 
   
define One 
   warn_admin
     (
       String message
     ) =
   print(message+"\n"). 
      
   This function is called each time the web server has a message for the administrator. 
   
   
      
      
   --- That's all for the public part ! ---------------------------------------------------------------------


   Handling command line arguments.    
   
define $T
   get_parameter   
     (
       List(String)          args,
       String -> Maybe($T)   read,
       $T                    default
     ) =
   if args is 
   {
     [ ] then default, 
     [h . t] then if read(h) is 
     {
       failure     then get_parameter(t,read,default),
       success(p)  then p
     }
   }.


define Maybe(String)
   extract_String
    (
      String   arg,
      String   prefix
    ) =
  with   la = length(arg),
         lp = length(prefix), 
  if sub_string(arg,0,lp) is 
  {
    failure then failure, 
    success(p) then 
      if p = prefix
      then sub_string(arg,lp,la-lp) 
      else failure
  }.

       
define Maybe(Word32)
  extract_Word32
    (
      String   arg,
      String   prefix
    ) =
  if extract_String(arg,prefix) is 
  {
    failure then failure, 
    success(s) then if decimal_scan(s) is 
    {
      failure then failure, 
      success(n) then success(truncate_to_Word32(n))
    }
  }. 
   

   
global define One
   dynamic_http_server
     (
       List(String) args
     ) =
   with 
   /* get command line arguments values (or default values) */ 
        ip_addr = get_parameter(args,
                                (String s) |-> if extract_String(s,"-a:") is 
                                               {
                                                 failure then failure, 
                                                 success(a) then ip_address(a)
                                               },
                                0), 
      http_port = get_parameter(args,
                                (String s) |-> extract_Word32(s,"-p:"),
                                 80), 
     https_port = get_parameter(args,
                                (String s) |-> extract_Word32(s,"-sp:"),
                                 443), 
       ssl_cert = get_parameter(args,
                                (String s) |-> extract_String(s,"-c:"),
                                 "georges"), 
        adm_dir = get_parameter(args,
                                (String s) |-> extract_String(s,"-d:"),
                                 my_anubis_directory/"web_site_modules"/"/"), 
                                 
    /* start the site loader */ 
    site_loader = make_site_loader(http_port,https_port,adm_dir,var(false),warn_admin), 

    warn_admin("Trying to listen on ports "+http_port+"/"+https_port+".\n");     
    warn_admin("Web site modules to be searched for in: "+adm_dir+"\n"); 
    
    /* start the http and https servers */ 
    if start_http_server(ip_addr,http_port,site_loader,load_denial_of_service_info) is 
    {
      cannot_create_the_socket     then warn_admin("Cannot create the http socket for listening")
      cannot_bind_to_port          then warn_admin("Cannot bind to port "+to_decimal(http_port))
      cannot_listen_on_port        then warn_admin("Cannot listen on port "+to_decimal(http_port))
      ok(http_s)  then 
        if start_https_server(ip_addr,https_port,ssl_cert,site_loader,load_denial_of_service_info) is 
        {
          cannot_create_the_socket     then warn_admin("Cannot create the https socket for listening")
          cannot_bind_to_port          then warn_admin("Cannot bind to port "+to_decimal(https_port))
          cannot_listen_on_port        then warn_admin("Cannot listen on port "+to_decimal(https_port))
          ok(https_s)  then 
            warn_admin("Servers started on "+ip_addr_to_string(ip_addr)+":"+to_decimal(http_port)+"/"+to_decimal(https_port))
        }
    }.