Project

General

Profile

How to pass some params to spawned Wt server process with FCgi

Added by Paweł Grodowski about 4 years ago

Hello for all,

In my last post, https://redmine.webtoolkit.eu/boards/2/topics/16847

I got information and help on starting the WT C WebToolkit server application in the Linux environment, (httpd) Apache2, TLS postifix together with dovecot on the example of Auth2 from "Wt" examples.

Now I configuring, Nginx with the proxy to the Wt application and everything works very well, here is the configuration:

server {

        listen       443 ssl;
        server_name  wt.onion.lan;

        #ssl_certificate      cert.pem;
        #ssl_certificate_key  cert.key;
        ssl_certificate      www.onion.lan.crt;
        ssl_certificate_key  www.onion.lan.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location /helptext/ {
          root /home/vacterm/projects/workdirs;
          index index.wt;
          proxy_pass http://localhost:80/;
        }

    }

Next a I will start reading dokumentation of OAuth2.0 with server.

This is another example from the library about authorization, I'm talking about Auth2 from Wt examples.

Proxy (server Wt Http C ToolKit process), started with:

sudo ./index.wt --docroot=".;/resources,/wt_config.xml,/css" --http-address 127.0.0.1 --port=80

The working directory looks like the following:

drwxr-xr-x  2 vacterm ngex  4096 mar 12 23:30 css/
-rw-r--r--  1 vacterm ngex     8 sty 26 21:36 helptext.cflags
-rw-r--r--  1 vacterm ngex    86 sty 26 21:36 helptext.config
-rw-r--r--  1 vacterm ngex    10 sty 26 21:36 helptext.creator
-rw-r--r--  1 vacterm ngex 15008 mar 14 21:07 helptext.creator.user
-rw-r--r--  1 vacterm ngex    10 sty 26 21:36 helptext.cxxflags
-rw-r--r--  1 vacterm ngex 95931 mar 13 00:13 helptext.files
-rw-r--r--  1 vacterm ngex 10990 mar 13 00:13 helptext.includes
lrwxrwxrwx  1 vacterm ngex    26 mar  4 18:33 index.wt -> root/debug/bin/helptext.wt*
-rw-r--r--  1 vacterm ngex     0 sty 16 21:55 Readme.md
drwxr-xr-x  5 vacterm ngex  4096 mar 12 23:25 resources/
drwxr-xr-x 10 vacterm ngex  4096 mar 15 10:46 root/
-rw-r--r--  1 vacterm ngex   128 mar 13 00:11 strings.xml
-rw-r--r--  1 vacterm ngex  1661 mar 13 00:11 templates.xml
-rw-r--r--  1 vacterm ngex  1533 lut 17 18:46 wt_config.xml

Then I wanted to test the configuration based on FCgi. I started with base configuration from:

https://redmine.webtoolkit.eu/projects/wt/wiki/Fastcgi_on_nginx

There is a configuration of Nginx.

server {

        listen       443 ssl;
        server_name  wt-cgi.onion.lan;

        #ssl_certificate      cert.pem;
        #ssl_certificate_key  cert.key;
        ssl_certificate      www.onion.lan.crt;
        ssl_certificate_key  www.onion.lan.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location /helptext/ {

          root /home/vacterm/projects/workdirs;

          index index.wt;

          fastcgi_pass   127.0.0.1:9091;

                fastcgi_param  QUERY_STRING       $query_string;
                fastcgi_param  REQUEST_METHOD     $request_method;
                fastcgi_param  CONTENT_TYPE       $content_type;
                fastcgi_param  CONTENT_LENGTH     $content_length;

                if ($document_uri ~ "^/index.wt/(.*)") {
                        set $apache_path_info /$1;
                }

                fastcgi_param  SCRIPT_NAME        /index.wt;
                fastcgi_param  PATH_INFO          $apache_path_info;
                fastcgi_param  REQUEST_URI        $request_uri;
                fastcgi_param  DOCUMENT_URI       $document_uri;
                fastcgi_param  DOCUMENT_ROOT      $document_root;
                fastcgi_param  SERVER_PROTOCOL    $server_protocol;

                fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

                fastcgi_param  REMOTE_ADDR        $remote_addr;
                fastcgi_param  REMOTE_PORT        $remote_port;
                fastcgi_param  SERVER_ADDR        $server_addr;
                fastcgi_param  SERVER_PORT        $server_port;
                fastcgi_param  SERVER_NAME        $server_name;
        }

    }

and there is a command line of: spawn-fcgi with environment:

source /etc/rc.d/init.d/functions

FCGI_DAEMON="/usr/bin/spawn-fcgi"
FCGI_WTPATH="/home/vacterm/projects/workdirs/helptext/root/debug/bin"
FCGI_PROGRAM="index.wt"
FCGI_WT_RPATH='/home/vacterm/projects/workdirs/helptext' ;
FCGI_SOCKET="/var/run/$(basename $FCGI_WTPATH)-fastcgi.sock"
FCGI_PIDFILE="/var/run/spawn-$(basename $FCGI_WTPATH)-fcgi.pid"
declare -i FCGI_PORT="9091"
FCGIHOST_ADDRESS='127.0.0.1'

prog="${FCGI_PROGRAM%.wt}.cgi"


start() {
        echo -n $"Starting $prog: " 
        $FCGI_DAEMON -a $FCGIHOST_ADDRESS \
         -p $FCGI_PORT \
         -d $FCGI_WT_RPATH \
         -P $FCGI_PIDFILE -- $FCGI_PROGRAM --docroot=".;/resources,/wt_config.xml,/css" ;
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}

The page is served properly but without /resources and /css.

My question is, Is it possible to send parameters for eg directory eg /resources, /css

It is about:

--docroot=".;/resources,/wt_config.xml,/css"

Or can I make such configurations from the main() function code: "C/C "?

Two examples of different main () functions from the Wt C Http toolKit example library:

Example 1

With lambda param to Run().

int main(int argc, char **argv)
{
  /*
   * Your main method may set up some shared resources, but should then
   * start the server application (FastCGI or httpd) that starts listening
   * for requests, and handles all of the application life cycles.
   *
   * The last argument to WRun specifies the function that will instantiate
   * new application objects. That function is executed when a new user surfs
   * to the Wt application, and after the library has negotiated browser
   * support. The function should return a newly instantiated application
   * object.
   */
  return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
    /*
     * You could read information from the environment to decide whether
     * the user has permission to start a new application
     */
    return Wt::cpp14::make_unique<HelloApplication>(env);
  });
}

Example 2

Server instance of WHttpd:

int main(int argc, char **argv)
{
  try {
    WServer server(argc, argv, WTHTTP_CONFIGURATION);

    server.addEntryPoint(EntryPointType::Application, createApplication);

    Session::configureAuth();

    server.run();
  } catch (WServer::Exception& e) {
    std::cerr << e.what() << std::endl;
  } catch (std::exception &e) {
    std::cerr << "exception: " << e.what() << std::endl;
  }
}

In Example 1, is a information about:

Your main method may set up some shared resources, but should then

start the server application (FastCGI or httpd) that starts listening

for requests, and handles all of the application life cycles.

The last argument to WRun specifies the function that will instantiate

new application objects. That function is executed when a new user surfs

to the Wt application, and after the library has negotiated browser

support. The function should return a newly instantiated application

object.

Can the "end user" set parameters for any access paths with shared resources of directories like /rsource from the main function for FCgi or Wt Httpd?

In the future I plan to use the Nginx server with the Wt and FCgi server,

Thank You in advance for all your help to the whole team.


Replies (4)

RE: How to pass some params to spawned Wt server process with FCgi - Added by Roel Standaert about 4 years ago

wtfcgi does not serve files in your --docroot, only wthttp does that. wtfcgi does not look at the --docroot argument. Static files should be handled by the web server. You will have to set up nginx to serve your resources and CSS.

As for wt_config.xml: you can set the WT_CONFIG_XML environment variable (with FASTCGI_PARAM) to point to your wt_config.xml.

RE: How to pass some params to spawned Wt server process with FCgi - Added by Paweł Grodowski about 4 years ago

After changing the configuration settings (Your suggestions), which I thank you a hundred times, everything works fine.

I still have a small problem with the URL that is sent via email:

http://wt-cgi.onion.lan/index.wt/auth/mail/9Y0NOMawyXnjAsU0yQ7l8ij27CKNFbDv

This link contains the part described in the variable:

if ($document_uri ~ "^/index.wt/(.*)") {
  set $apache_path_info /$1;
}

and:

fastcgi_param  PATH_INFO          $apache_path_info;

If I try to use the link and the request is sent, the application starts but does not go to the parsing part,

where you can read part of the token from:

/auth/mail/9Y0NOMawyXnjAsU0yQ7l8ij27CKNFbDv

as a consequence of this, the token has not been extracted and the user cannot be verified.

My next question is:

How to correctly move (which supports this process, which will change or settings) and that the function:

AuthWidget::processEnvironment()

next:

if (registrationEnabled_)
    if (handleRegistrationPath(env.internalPath()))

correctly returns the content.

Thank You very much for further help.

RE: How to pass some params to spawned Wt server process with FCgi - Added by Paweł Grodowski about 4 years ago

I add:

std::string emailToken
    = model_->baseAuth()->parseEmailToken(env.internalPath());

RE: How to pass some params to spawned Wt server process with FCgi - Added by Paweł Grodowski about 4 years ago

I dealt with it and thank You again for Your help.

I pass to:

https://redmine.webtoolkit.eu/boards/2/topics/16900

Please read because it describes another related issue there.

Thank You once more.

    (1-4/4)