Obtaining Server Variables

Server variables provide information about the HTTP server environment. Frequently an ISAPI filter will need to determine the value of a server variable before it can process event information. The following code would return the value of the SERVER_PORT_SECURE server variable.

DWORD WINAPI HttpFilterProc(
  PHTTP_FILTER_CONTEXT pfc, 
  DWORD NotificationType, 
  LPVOID pvNotification
)
{
  HTTP_FILTER_PREPROC_HEADERS *pHeaders = pvNotification;
  char szServerName[1024], szUrl[1024], szSecure[2], 
  szLocationHeader[1024], szRequest[1024];
  DWORD dwBuffSize;
  dwBuffSize = sizeof(szRequest);

  // Fetch URL server variable.
  pfc->GetServerVariable(
       pfc, 
       "URL", 
       szRequest, 
       &dwBuffSize
       );

  // Fetch secure/unsecure port status.
  dwBuffSize = 2;
  pfc->GetServerVariable(
       pfc,
       "SERVER_PORT_SECURE", 
       szSecure, 
       &dwBuffSize
       );

  // If request is on secure port, end handling for this
  // notification type, for this request.
  if (szSecure[0] == '1')   
    return SF_STATUS_REQ_NEXT_NOTIFICATION;

  // Fetch additional server variables.
  dwBuffSize = sizeof( szServerName );
  pfc->GetServerVariable(
       pfc, 
       "SERVER_NAME",
       szServerName, 
       &dwBuffSize
       );
  pfc->GetServerVariable(
       pfc, 
       "HTTP_HOST",
       szServerName, 
       &dwBuffSize
       );

  // Fetch URL header from request.
  dwBuffSize = sizeof( szUrl );
  pHeaders->GetHeader(
            pfc, 
            "url", 
            szUrl, 
            &dwBuffSize
            );

  // Create concatenated URL string.
  wsprintf(szLocationHeader, "Location:
           https://%s%s\r\n\r\n", szServerName, szUrl);
  
  pfc->AddResponseHeaders(
       pfc, 
       szLocationHeader, 
       0
       );

  // Send redirection header.
  pfc->ServerSupportFunction(
       pfc,
       SF_REQ_SEND_RESPONSE_HEADER, 
       "302 Object Moved",
       (DWORD)"Please resubmit the request using a secure
                   port.", 
       0
       );
   
  return SF_STATUS_REQ_FINISHED;
}