compile function rti_Run_HTTPServer_Request( httpServer, requestID, requestHeaders )
/*
   ****************************************************************************
   ** IF YOU WANT TO MODIFY THIS FOR YOUR OWN APPLICATIONS PLEASE USE A COPY **
   ** DO NOT CHANGE THIS PROGRAM AS IT MAY BE OVERWRITTEN BY FUTURE UPDATES! **
   ****************************************************************************  
   
   ** Copyright (C) 2012-2022 Revelation Software Inc. All Rights Reserved **

   Author   : Mr C
   Date     : March 2021 - Lockdown 3 (still)
   Purpose  : Core handler for running HTTPSERVER requests

   Comments
   ========
   
   This is basically an analog of RUN_OECGI_REQUEST and is intended to be 
   called from the HTTPREQUEST event of an HTTPSERVER control.
   
   Assumptions:
   
      1) This proc is always in EventContext from an HTTPREQUEST event, and
      2) This proc is called as a quick event
      
   This has error-handling implications due to the fact that:
   
      1) The promoted handler forwards the event to a QE handler and then  
         checks EventStatus() to see if it should continue.  
        
      2) If so then it executes a SENDRESPONSE method to return content to the 
         client.
   
   We don't want to stop that last step otherwise the client will time out, so 
   _this_ proc will _not_ set the EventStatus() if it encounters an error - 
   instead it will raise an HTTPERROR event on the server and ensure that 
   the SP Status is also cleared.
   
   If the handler proc wants to issue it's own send and set the EventStatus then
   it is free to do so.
   
   
   Amended  Date        Reason
   =======  ====        ======
*/

   #pragma precomp event_precomp
   
   declare function rti_Convert, rti_Verify_Proc, rti_UC, rti_ErrorText
   $insert ps_HTTPServer_Equates
   $insert rti_SSP_Equates
   $insert rti_Text_Equates
   $insert logical
   
   equ HTTPSVR_PREFIX$ to "HTTPSVR_"
   
   errStat = FALSE$ 
   errInfo = ""
   
   retVal  = TRUE$
   
   procID = rti_UC( requestHeaders<PS_HSVR_REQHDR_PATHINFO$> )[-1, "B/"]
   if ( procID[1,8] != HTTPSVR_PREFIX$ ) then
      procID = HTTPSVR_PREFIX$ : procID
   end
   
   call set_Status( SETSTAT_OK$ )
   if rti_Verify_Proc( procID, FALSE$, 3, "" ) else
      // Not a valid HTTPSVR_ proc ...
      call get_status( errInfo ) ; goSub setHTTPError
      
      @httpServer->SetResponseStatus( requestID, 404 ) ; // HTTP 404 - not found
      
      return FALSE$
      
   end
   
   call @procID( httpServer, requestID, requestHeaders )
   if get_Status( errInfo ) then
      // Something got away from the handler? Make a note and then let it 
      // through.
      call set_Status( SETSTAT_OK$ )
      goSub setHTTPError
      return FALSE$
   end
   
return TRUE$

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// setHTTPError subroutine
//
// This subroutine raises an HTTPERROR event for each error string contained
// in the errInfo var.
//
// ----------------------------------------------------------------------------
// [i]   errInfo : @fm'd list of errors to report
// ----------------------------------------------------------------------------
setHTTPError:

   errInfo = rti_ErrorText( "SP", errInfo ) 
   pos = 1
   loop
      tmp = errInfo[pos,@fm,TRUE$]; pos += bCol2()+1
      if bLen( tmp ) then
         @httpServer->postEvent( "HTTPERROR", requestID, errInfo )
      end
   while ( pos < bLen( errInfo ) )
   repeat
   
return

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