Export (0) Print
Expand All

Obtaining Server Variables in ISAPI Applications

IIS 6.0

Server variables provide information about the HTTP server environment. Frequently, the value of a server variable must be determined before event information is processed. Available server variables are listed in IIS Server Variables.

To obtain server variables

  1. Declare an appropriate stack buffer for the result.

  2. Declare a maximum size to accept for the value.

  3. Make the initial call to the GetServerVariable callback function (for extensions) or the GetServerVariable callback function (for filters).

    Check for HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), for which the code should:

    • Verify that the needed size does not exceed the declared maximum.

    • Allocate an appropriate buffer on the heap.

    • Call GetServerVariable again and verify the return code.

  4. To clean up, free the heap if it was used.

The following example code illustrates how to get a server variable in an ISAPI application.

CHAR szBuf[MAX_PATH + 1]; 
CHAR* pszBuf = szBuf; 
DWORD dwBuf = MAX_PATH + 1; 
DWORD dwMaxBufSize = 1024; 
LPSTR szVariableName = "ALL_RAW"; 

if ( !pECB->GetServerVariable(pECB->ConnID, 
                              szVariableName, 
                              pszBuf, 
                              &dwBuf) ) 
{ 
    if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) 
    { 
        // 
        // Not large enough buffer.  Reallocate. 
        // Make sure to not reallocate size blindly (DoS)... 
        // 
        if ( dwBuf > dwMaxBufSize ) 
        { 
            goto Failed; 
        } 

        pszBuf = new CHAR[dwBuf]; 

        if ( pszBuf != NULL ) 
        { 
            // 
            // Try GetServerVariable again 
            // 
            if ( !pECB->GetServerVariable(pECB->ConnID, 
                                          szVariableName, 
                                          pszBuf, 
                                          &dwBuf) ) 
            { 
                // 
                // Unexpected failure.  Fail 
                // 
                goto Failed; 
            } 

            // 
            // Successfully fetched value into heap buffer 
            // 
        } 
        else 
        { 
            // 
            // Failed to allocate memory.  Fail 
            // 
            goto Failed; 
        } 
    } 
    else 
    { 
        // 
        // Deal with GetLastError() however you wish 
        // May optionally decide to fail 
        // 
        // GetLastError() == ERROR_INVALID_INDEX 
        //   szVariableName is not a valid server variable name 
        // 
        goto Failed; 
    } 
} 


// 
// pszBuf points to variable value.  Use it. 
// dwBuf indicates how big the buffer is (NULL included) 
// 

// 
// Rest of code 
// 



// 
// If we've allocated a buffer, cleanup 
// 
Failed: 
if ( pszBuf != NULL && 
     pszBuf != szBuf ) 
{ 
    delete [] pszBuf; 
} 


Show:
© 2014 Microsoft