compile function httpSvr_Trace( 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-2021 Revelation Software Inc. All Rights Reserved **
   
   Author   : Wile C Coyote - Super Genius
   Date     : March 2021
   Purpose  : "Trace" function for HTTPSERVER control requests.
   
   
   Comments
   ========
   
   This is a simple function designed to reflect the headers and content received 
   by the HTTPSERVER control back to the client as an HTML page.
   
   Modelled on the venerable "INET_TRACE" function.
   
   Amended  Date        Reason
   =======  ====        ======

*/

   #pragma precomp event_precomp
   
   declare function rti_XMLEncode, exec_Method
   $insert ps_HTTPServer_Equates
   $insert rti_Text_Equates
   $insert logical
   
   varNames =        "CONTENT_LENGTH"     ; varIndexes =        PS_HSVR_REQHDR_CONTENTLEN$
   varNames := @fm : "CONTENT_TYPE"       ; varIndexes := @fm : PS_HSVR_REQHDR_CONTENTTYPE$
   varNames := @fm : "GATEWAY_INTERFACE"  ; varIndexes := @fm : PS_HSVR_REQHDR_GATEWAYINTERFACE$
   varNames := @fm : "HTTPS"              ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPS$
   varNames := @fm : "HTTP_ACCEPT"        ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPACCEPT$
   varNames := @fm : "HTTP_COOKIE"        ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPCOOKIE$
   varNames := @fm : "HTTP_FROM"          ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPFROM$
   varNames := @fm : "HTTP_REFERER"       ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPREFERRER$
   varNames := @fm : "HTTP_USER_AGENT"    ; varIndexes := @fm : PS_HSVR_REQHDR_HTTPUSERAGENT$
   varNames := @fm : "PATH_INFO"          ; varIndexes := @fm : PS_HSVR_REQHDR_PATHINFO$
   varNames := @fm : "PATH_TRANSLATED"    ; varIndexes := @fm : PS_HSVR_REQHDR_PATHTRANSLATED$
   varNames := @fm : "REMOTE_ADDR"        ; varIndexes := @fm : PS_HSVR_REQHDR_REMOTEADDR$
   varNames := @fm : "REMOTE_HOST"        ; varIndexes := @fm : PS_HSVR_REQHDR_REMOTEHOST$
   varNames := @fm : "REMOTE_IDENT"       ; varIndexes := @fm : PS_HSVR_REQHDR_REMOTEIDENT$
   varNames := @fm : "REMOTE_USER"        ; varIndexes := @fm : PS_HSVR_REQHDR_REMOTEUSER$
   varNames := @fm : "REQUEST_METHOD"     ; varIndexes := @fm : PS_HSVR_REQHDR_REQUESTMETHOD$
   varNames := @fm : "SCRIPT_NAME"        ; varIndexes := @fm : PS_HSVR_REQHDR_SCRIPTNAME$
   varNames := @fm : "SERVER_NAME"        ; varIndexes := @fm : PS_HSVR_REQHDR_SERVERNAME$
   varNames := @fm : "SERVER_PORT"        ; varIndexes := @fm : PS_HSVR_REQHDR_SERVERPORT$
   varNames := @fm : "SERVER_PROTOCOL"    ; varIndexes := @fm : PS_HSVR_REQHDR_SERVERPROTOCOL$
   varNames := @fm : "SERVER_SOFTWARE"    ; varIndexes := @fm : PS_HSVR_REQHDR_SERVERSOFTWARE$
   varNames := @fm : "SERVER_URL"         ; varIndexes := @fm : PS_HSVR_REQHDR_SERVERURL$
   
   content     = "<html>"
   content<-1> = "<head>"
   content<-1> = "</head>"
   content<-1> = "<body>"
   
   content<-1> = "<b>Environment Variables</b>"
   content<-1> = "<table border='1' cellpadding='2'>"
   content<-1> = "<tr><th>Name</th><th>Value</th></tr>"
   
   xCount = fieldCount( varNames, @fm )
   for x = 1 to xCount
      row =  "<tr><td>" : varNames<x>                   : "</td>"  
      row :=     "<td>" : requestHeaders<varIndexes<x>> : "</td></tr>"
      
      content<-1> = row
   next
   
   content<-1> = "</table>"
   
   content<-1> = "<br>"
   content<-1> = "<br>"
   
   content<-1> = "<b>Request Headers</b>"
   content<-1> = "<table border='1' cellpadding='2'>"
   content<-1> = "<tr><th>Header Name</th><th>Header Value</th></tr>"
   
   xCount = fieldCount( requestHeaders<PS_HSVR_REQHDR_HEADERNAMES$>, @vm )
   for x = 1 to xCount
      row =  "<tr><td>" : requestHeaders<PS_HSVR_REQHDR_HEADERNAMES$,x>  : "</td>"  
      row :=     "<td>" : requestHeaders<PS_HSVR_REQHDR_HEADERVALUES$,x> : "</td></tr>"
      
      content<-1> = row
   
   next
   content<-1> = "</table>"
   
   content<-1> = "<br>"
   content<-1> = "<br>"
   
   content<-1> = "<b>Query Details</b>"
   content<-1> = "<table border='1' cellpadding='2'>"
   content<-1> = "<tr><th>Query Name</th><th>Query Value</th></tr>"
   
   xCount = fieldCount( requestHeaders<PS_HSVR_REQHDR_QUERYNAMES$>, @vm )
   for x = 1 to xCount
      row =  "<tr><td>" : requestHeaders<PS_HSVR_REQHDR_QUERYNAMES$,x> : "</td>"  
      row :=     "<td>" : requestHeaders<PS_HSVR_REQHDR_QUERYVALUES$,x> : "</td></tr>"
      
      content<-1> = row
   
   next
   content<-1> = "</table>"
   
   content<-1> = "<br>"
   content<-1> = "<br>"
   
   content<-1> = "<b>Cookie Details</b>"
   content<-1> = "<table border='1' cellpadding='2'>"
   content<-1> = "<tr><th>Cookie Name</th><th>Cookie Value</th></tr>"
   
   xCount = fieldCount( requestHeaders<PS_HSVR_REQHDR_COOKIENAMES$>, @vm )
   for x = 1 to xCount
      row =  "<tr><td>" : requestHeaders<PS_HSVR_REQHDR_COOKIENAMES$,x>  : "</td>"  
      row :=     "<td>" : requestHeaders<PS_HSVR_REQHDR_COOKIEVALUES$,x> : "</td></tr>"
      
      content<-1> = row
   
   next
   content<-1> = "</table>"
   
   // Add the request content - encode it to make sure it's safe to embed in 
   // the returned HTML
   content<-1> = "<br/>"
   content<-1> = "<br/>"
   content<-1> = "<b>Request Content</b>"
   content<-1> = "<hr/>"
   
   content<-1> = rti_XMLEncode( @httpServer->GetRequestContent( requestID ) )
   
   content<-1> = "</body>"
   content<-1> = "</html>"
      
   convert @fm to LF$ in content
   
   @httpServer->SetResponseContent( requestID, content )
   @httpServer->SetResponseHeader( requestID, "Content-Type", "text/html" )

return TRUE$
