sitemule / ILEastic

Embedded application server for ILE on IBM i
Apache License 2.0
58 stars 29 forks source link

Wrong route id set on request #110

Closed m1h43l closed 3 months ago

m1h43l commented 1 year ago

From time to time the wrong route id is set on in the request data structure on a request.

**FREE

///
// Route Id Example
//
// This example shows how to add a route id to a route and how to query the route
// id in a plugin.
//
// Start it:
// ADDLIBLE ILEASTIC
// SBMJOB CMD(CALL PGM(ROUTEID)) JOB(ILEASTIC1) JOBQ(QSYSNOMAX) ALWMLTTHD(*YES)        
// 
// The web service can be tested with the browser by entering the following URL:
// http://my_ibm_i:44000
// 
// @info: It requires your RPG code to be reentrant and compiled for 
//        multithreading. Each client request is handled by a seperate thread.
///

ctl-opt copyright('Sitemule.com  (C), 2018');
ctl-opt decEdit('0,') datEdit(*YMD.) main(main);
ctl-opt debug(*yes) bndDir('ILEASTIC');
ctl-opt thread(*CONCURRENT);

/include /home/mihael/include/ileastic/ileastic.rpgle

// -----------------------------------------------------------------------------
// Program Entry Points-------------------------     
dcl-proc main;

    dcl-ds config likeds(il_config);

    config.port = 44000; 
    config.host = '*ANY';

    il_addPlugin(config : %paddr(logRouteId) : IL_PREREQUEST);

    il_addRoute(config : %paddr(sayHello) : IL_GET : '/api/hello' : *omit : 'hello');
    il_addRoute(config : %paddr(ping) : IL_GET : '/ping');
    il_addRoute(config : %paddr(sayBye) : IL_GET : '/bye');

    // Starts the server.
    il_listen(config);
end-proc;

// -----------------------------------------------------------------------------
// Servlet callback implementation
// -----------------------------------------------------------------------------     
dcl-proc ping;
    dcl-pi *n;
        request  likeds(IL_REQUEST);
        response likeds(IL_RESPONSE);
    end-pi;

    response.contentType = 'text/plain';
    il_responseWrite(response : 'Ping: ' + %char(%timestamp()));
end-proc;

dcl-proc sayHello export;
    dcl-pi *n;
        request  likeds(IL_REQUEST);
        response likeds(IL_RESPONSE);
    end-pi;

    response.contentType = 'text/plain';
    il_responseWrite(response : 'Hello');
end-proc;

dcl-proc sayBye export;
    dcl-pi *n;
        request  likeds(IL_REQUEST);
        response likeds(IL_RESPONSE);
    end-pi;

    response.contentType = 'text/plain';
    il_responseWrite(response : 'bye');
end-proc;

dcl-proc logRouteId export;
    dcl-pi *n ind;
        request  likeds(IL_REQUEST);
        response likeds(IL_RESPONSE);
    end-pi;

    if (request.routeId = *blank);
        il_joblog('No route id.');
    else;
        il_joblog('Route id: %s' : request.routeId);
    endif;

    return *on;
end-proc;
m1h43l commented 1 year ago

The problem seems to be on the il_addRoute function in api.c, line 389.

If I add routes without the route id then everything is fine. If I add all routes with a route id then also everything is fine. But when I use a mix of routes with and without a route id then it seems to mess things up.

  il_addRoute(config : %paddr(routeFirst) : IL_ANY : REGEX_START + '/first$' : *omit : 'first');
  il_addRoute(config : %paddr(routeSecond) : IL_ANY : REGEX_START + '/second$');
  il_addRoute(config : %paddr(routeThird) : IL_ANY : REGEX_START + '/third$');

In line 406 the function checks if a route id parameter has been passed. It checks on the number of passed parameters. In this case on the first call there are 6 parameters and on the second and third call only 4. But when being debugged it will always go into the if branch as if there always have been passed 6 parameters, see attached screenshot.

debugging api c

NielsLiisberg commented 1 year ago

I have looked at it - perhaps this is one for IBM? What do I miss?

On Thu, Sep 22, 2022 at 3:56 PM m1h43l @.***> wrote:

The problem seems to be on the il_addRoute function in api.c, line 389.

If I add routes without the route id then everything is fine. If I add all routes with a route id then also everything is fine. But when I use a mix of routes with and without a route id then it seems to mess things up.

il_addRoute(config : %paddr(routeFirst) : IL_ANY : REGEX_START + '/first$' : *omit : 'first'); il_addRoute(config : %paddr(routeSecond) : IL_ANY : REGEX_START + '/second$'); il_addRoute(config : %paddr(routeThird) : IL_ANY : REGEX_START + '/third$');

In line 406 the function checks if a route id parameter has been passed. It checks on the number of passed parameters. In this case on the first call there are 6 parameters and on the second and third call only 4. But when being debugged it will always go into the if branch as if there always have been passed 6 parameters, see attached screenshot.

[image: debugging api c] https://user-images.githubusercontent.com/10236415/191766376-16b1bb7f-bad4-41ed-a627-ed5ad78add58.png

— Reply to this email directly, view it on GitHub https://github.com/sitemule/ILEastic/issues/110#issuecomment-1255062444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAVIPHU5WRLXZS7S2SVLZ5LV7RQRRANCNFSM6AAAAAAQS7GQEE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

m1h43l commented 1 year ago

I will try to create a simple example program so that we (or IBM) can analyze the problem better.

m1h43l commented 1 year ago

I added a unit test but could not reproduced the problem with this test.

@NielsLiisberg perhaps you could check if the test is correct (as it also contains some C code) : commit 1818615

NielsLiisberg commented 1 year ago

Perhaps the ParmNum is overwritten in the invocations stack. I can try theo create a temporary variable as the first thing .. This is my best guess.

On Thu, Sep 29, 2022 at 8:44 AM m1h43l @.***> wrote:

I added a unit test but could not reproduced the problem with this test.

@NielsLiisberg https://github.com/NielsLiisberg perhaps you could check if the test is correct (as it also contains some C code) : commit 1818615 https://github.com/sitemule/ILEastic/commit/1818615fd0e959881bd481dc41d62dd1d7bd1e94

— Reply to this email directly, view it on GitHub https://github.com/sitemule/ILEastic/issues/110#issuecomment-1261837880, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAVIPHXDYQROWGXGZH5D6TDWAU3EDANCNFSM6AAAAAAQS7GQEE . You are receiving this because you were mentioned.Message ID: @.***>

m1h43l commented 1 year ago

Perhaps this is a connected to #114 .

NielsLiisberg commented 1 year ago

Remember that I had to change it to be thread safe !! The routing was ( unfortunately) put in to the "global" part of the config - That is changed

On Fri, Jan 27, 2023 at 3:02 PM m1h43l @.***> wrote:

Perhaps this is a connected to #114 https://github.com/sitemule/ILEastic/issues/114 .

— Reply to this email directly, view it on GitHub https://github.com/sitemule/ILEastic/issues/110#issuecomment-1406547731, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAVIPHRZOQNQ7VVM5LH2F2LWUPIO5ANCNFSM6AAAAAAQS7GQEE . You are receiving this because you were mentioned.Message ID: @.***>

m1h43l commented 3 months ago

I haven't seen this problem for some time now. Closing this issue.