RE: Invoking a request from within an after filter

Hi Yuval,

Sorry I have not seen your email earlier. Maybe you already solved the
problem...
I had similar problem. There are some my emails in wwwlib mailing archives
about it (February - March'99). If I recall correctly the problem was that
afterfilters for the first request were called to service the next one (started
from the first).

That was tricky to fix. I do not remember exactly how I did it since I am not
working on that application since March. But there are some snippets of the code
that I have changed:

////////////////////////////////////////////// main()

int terminate_handler (HTRequest * request, HTResponse * response,
                                   void * param, int status) 
{
    
#ifdef  EAI_DBG
    cerr << "\n******* GLOBAL terminate_handler" << endl;

    
    if (request) {
        if ((status == HT_LOADED) || (status == HT_NOT_MODIFIED)) {

            /* Delete all local after filters and call global after filters if
any */
            HTRequest_deleteAfterAll(request);
            HTNet_executeAfterAll(request, status);            
        }
            
        /* We are done with this request */
        HTRequest_markToDelete(request); 
    }
    fclose (fp);
    
    if (!t) // t is socket listening to the client requests
      EnsureConnectionSocket();

    memset(reqStr, 0, 2048);
    
    // now get the next client request
    strcpy(reqStr, GetRequestStr());

    int result = DispatchRequest(reqStr);
    
    return HT_OK;
}



//////////////////////// HTNet.c


PUBLIC int HTNetCall_executeAfter (HTList * list, HTRequest * request,
                                   int status)
{
    int ret = HT_OK;
    static visit = 0;
    
    if (PROT_TRACE)
      HTTrace("Net After... ENTERING  HTNetCall_executeAfter, visit No = %d\n",
++visit);
    /*olga: if enter the function normally - then reset nalue of newRequest */
    HTRequest_resetIsNew(request);

    if (status != HT_IGNORE) {
        HTParentAnchor * anchor = HTRequest_anchor(request);
        char * url = HTAnchor_physical(anchor);
        char * addr = url ? url : HTAnchor_address((HTAnchor *) anchor);
        HTResponse * response = HTRequest_response(request);

        if (list && request && addr) {
            AfterFilter * pres;
            while ((pres = (AfterFilter *) HTList_nextObject(list))) {
                if ((pres->status == status || pres->status == HT_ALL) &&
                    (!pres->tmplate ||
                     (pres->tmplate && HTStrMatch(pres->tmplate, addr)))) {
                    if (CORE_TRACE)
                        HTTrace("Net After... calling %p (request %p, response
%p, status %d, context %p), visit No = %d\n",
                                pres->after, request, response,
                                status, pres->param, visit);
                    ret = (*pres->after)(request, response, pres->param,
status);
                    
                    /* olga: there is where the request might become a new one
- 
                       if so - break */
                    /*if (HTRequest_isNewRequest(request)) 
                      {
                      ret = HT_OK;
                      break;
                      }
                      */
                    if (HTRequest_shouldDelete(request))
                    {
                        ret = HT_OK;
                        break;
                    }
                    
                    if (ret != HT_OK) break;

                    /*
                    **  Update the address to match against if the filter
changed
                    **  the physical address.
                    */
                    if ((url = HTAnchor_physical(anchor))) addr = url;
                }
            }
        }
        if (!url) HT_FREE(addr);
    }

    if (PROT_TRACE)
      HTTrace("Net After... RETURNING from  HTNetCall_executeAfter, ret = %d,
visit No = %d\n", ret, visit);
    
    return ret;
}


PUBLIC int HTNet_executeAfterAll (HTRequest * request, int status)
{
    int ret;
    BOOL override = NO;
    HTList * afters;

    if (PROT_TRACE) 
      HTTrace("HTNet_executeAfterAll. Calling HTNetCall_executeAfter for
locals\n");
    
    if ((afters = HTRequest_after(request, &override))) {
        if ((ret = HTNetCall_executeAfter(afters, request, status)) != HT_OK)
          return ret;
        
        /*olga: check if new request, if so - reset and return */
        /*if (HTRequest_isNewRequest(request)) 
          {
          HTRequest_resetIsNew(request);  not necessary, but...
          we are not interested in after filters of previous request - exit 
          return HT_OK;
          }
          */
        if (HTRequest_shouldDelete(request)) {
            HTRequest_delete(request);
            return HT_OK;
            
        }    
    }

    if (PROT_TRACE && !override)
      HTTrace("HTNet_executeAfterAll. Calling HTNetCall_executeAfter for global
filters, HTAfter = %d\n", HTAfter);    
    else if (PROT_TRACE)
      HTTrace("HTNet_executeAfterAll. Returning HT_OK\n");
    
    return override ? HT_OK : HTNetCall_executeAfter(HTAfter, request, status);
}

//////////////////////////////////// HTReqMan.c

PUBLIC HTRequest * HTRequest_new (void)
{
    HTRequest * me;
    if ((me = (HTRequest *) HT_CALLOC(1, sizeof(HTRequest))) == NULL)
        HT_OUTOFMEM("HTRequest_new()");
    
   /* Force Reload */
    me->reload = HT_CACHE_OK;

    /* Set the default user profile */
    me->userprofile = HTLib_userProfile();

    /* Format of output */
    me->output_format   = WWW_PRESENT;      /* default it to present to user */
    me->debug_format    = WWW_DEBUG;     /* default format of error messages */

    /* HTTP headers */
    me->GenMask         = DEFAULT_GENERAL_HEADERS;
    me->RequestMask     = DEFAULT_REQUEST_HEADERS;
    me->ResponseMask    = DEFAULT_RESPONSE_HEADERS;
    me->EntityMask      = DEFAULT_ENTITY_HEADERS;

    /* Default retry after value */
    me->priority = HT_PRIORITY_MAX;

    /* Default max forward value */
    me->max_forwards = -1;

    /* Content negotiation */
    me->ContentNegotiation = YES;                      /* Do this by default */

    /*olga - elliminating problems with after filters when new request is
created in
     after filter of the previous request*/
    me->newRequest = YES;
    me->shouldDelete = NO;
    
    if (CORE_TRACE) HTTrace("Request..... Created %p\n", me);

    return me;
}



/****************************************************************

olga: New functions. Elliminating problems with after filters when new request 
is created in after filter of the previous request

*****************************************************************/

PUBLIC BOOL HTRequest_isNewRequest(HTRequest * request)
{
    return request->newRequest;
}

PUBLIC void HTRequest_resetIsNew(HTRequest * request)
{
    request->newRequest = NO;
}

PUBLIC BOOL HTRequest_shouldDelete(HTRequest * request)
{
    return request->shouldDelete;
}

PUBLIC void HTRequest_markToDelete(HTRequest * request)
{
    request->shouldDelete = YES;
}



////////////////////////////////////////////////////////////////

No guarantees... But maybe it will give you some ideas or at least places to
look at. Probably you have it fixed already :-)

Regards,

Olga Antropova.


On 09-Jun-99 Yuval Krymolowski wrote:
> Hello,
> 
>  In the after filter of a request, I create a new request
>  and issue it using HTLoad. 
> 
>  It doesn't work, although the HTLoad returns '1'.
>  The same code worked when I just issued the requests one after the
>  other. It runs on NT.
> 
>  Actually, one requests gets the HEAD (for last access date) and the other
>  gets the HTML data through the HTML parser.
>  Could some one please send a small example of such request nesting, or
>  a simpler way of getting HEAD+parsed HTML ?
> 
>  Thanks,
>    Yuval Krymolowski
>    Bar-Ilan University

-------------------------------------------------
"Have the appropriate amount of fun"

    "Programming Perl", 2nd edition, Last phrase from the first chapter.
     L. Wall, T. Christiansen, R. L. Schwartz.  

Received on Friday, 2 July 1999 11:20:02 UTC