IHttpResponse::WriteEntityChunkByReference Method

Inserts or appends an HTTP_DATA_CHUNK structure into the response body.

Syntax

HRESULT WriteEntityChunkByReference(  
   IN HTTP_DATA_CHUNK* pDataChunk,  
   IN LONG lInsertPosition = -1  
)  

Parameters

pDataChunk
[IN] A pointer to an HTTP_DATA_CHUNK structure.

lInsertPosition
[IN] A LONG value that specifies whether to insert or append the chunk.

Return Value

An HRESULT. Possible values include, but are not limited to, those in the following table.

Value Description
S_OK Indicates that the operation was successful.
ERROR_INVALID_PARAMETER Indicates that the parameter is not valid (for example, the HTTP_DATA_CHUNK pointer is set to NULL).
ERROR_NOT_ENOUGH_MEMORY Indicates that there is insufficient memory to perform the operation.
ERROR_ARITHMETIC_OVERFLOW Indicates that more than 65535 chunks have been added to the response.

Remarks

The WriteEntityChunkByReference method inserts or appends an HTTP_DATA_CHUNK structure into the response buffer depending on the value of the lInsertPosition parameter.

  • If lInsertPosition is 0, the data will be inserted before the existing response data.

  • If lInsertPosition is -1, the data will be appended after the last chunk of existing response data.

    The WriteEntityChunkByReference method inserts a reference to the original data chunk, instead of a copy, into the response buffer. Therefore, the memory allocated for pDataChunk->FromMemory.pBuffer must persist for the duration of your response processing. Using local or stack memory results in undefined behavior.

    A maximum of 65535 (64 KB minus 1) chunks can be written to a request.

Example

The following example demonstrates how to use the WriteEntityChunkByReference method to insert data into the response. It also demonstrates how to use the lInsertPosition parameter to insert or append data chunks.

//  Insert data from ostringstream into the response
//  On error, Provider error status set here
//  ostringstream  buffer cleared for next call 

HRESULT  WECbyRefChunk( std::ostringstream  &os, IHttpContext *pHttpContext, 
                       IHttpEventProvider *pProvider, LONG InsertPosition= -1)
{

    HRESULT hr = S_OK;

    // create convenience string from ostringstream  
    std::string str(os.str());

    HTTP_DATA_CHUNK dc;
    dc.DataChunkType = HttpDataChunkFromMemory;
    dc.FromMemory.BufferLength = static_cast<DWORD>(str.size());
    dc.FromMemory.pBuffer = pHttpContext->AllocateRequestMemory( static_cast<DWORD>( str.size()+1) );

    if(!dc.FromMemory.pBuffer){
        hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
        LOG_ERR_HR(hr,"AllocateRequestMemory");
        pProvider->SetErrorStatus(hr);
        return hr;
    }

    //  use char pointer p for convenience
    char *p = static_cast<char *>(dc.FromMemory.pBuffer);
    strcpy_s(p, str.size()+1, str.c_str());

    hr = pHttpContext->GetResponse()->WriteEntityChunkByReference( &dc, InsertPosition );

    if (FAILED(hr)){
        LOG_ERR_HR(hr,"AllocateRequestMemory");
        pProvider->SetErrorStatus( hr );
    }

    os.str("");                // clear the ostringstream for next call

    return hr;
}  
REQUEST_NOTIFICATION_STATUS
CMyHttpModule::OnBeginRequest(
                              IHttpContext*       pHttpContext,
                              IHttpEventProvider* pProvider
                              )
{
    HRESULT hr;

    static long cnt;               
    InterlockedIncrement (&cnt);  // keep track of how many times we are called
    cnt++;

    IHttpRequest *pRequest = pHttpContext->GetRequest();

    PCWSTR url = pRequest->GetRawHttpRequest()->CookedUrl.pAbsPath;
    OutputDebugStringW( url  );

    // return unless requesting a HTML file

    if( !wcsstr(url, L".htm"))
        return RQ_NOTIFICATION_CONTINUE;

    IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

    // Return most times so we can still view content
    if( (cnt%5) || pHttpResponse == NULL)
        return RQ_NOTIFICATION_CONTINUE;

    TRC_MSG_FULL("HTML  cnt = " << cnt  );

    static int insertPosCnt;
    int insertPos = ++insertPosCnt%2 -1;    // toggle between 0 and -1

    // Use ostringstream to create some dynamic content
    std::ostringstream os; 

    os << "<p /> first chunk  callback count = " << cnt 
        << " insertPos = " << insertPos << "<br />";

    // 
    // WECbyRefChunk does all the work of inserting data into the response
    //

    hr = WECbyRefChunk( os, pHttpContext, pProvider, insertPos);
    if (FAILED(hr))
        return RQ_NOTIFICATION_FINISH_REQUEST;       

    os << "<br /> <b> Adding 2nd chunk in Bold </b> File insertPos = " << insertPos ;
    hr = WECbyRefChunk( os, pHttpContext, pProvider,insertPos);
    if (FAILED(hr))
        return RQ_NOTIFICATION_FINISH_REQUEST;       

    os << " <p /> Last (3rd) Chunk added with default append chunk  GetCurrentThreadId = " 
        << GetCurrentThreadId();
    
    // any errors will be logged/handled in  WECbyRefChunk
    WECbyRefChunk( os, pHttpContext, pProvider);

    // End additional processing, not because of error, but so another request
    // doesn't wipe out our WriteEntityChunkByReference

    return RQ_NOTIFICATION_FINISH_REQUEST;       
}

Requirements

Type Description
Client - IIS 7.0 on Windows Vista
- IIS 7.5 on Windows 7
- IIS 8.0 on Windows 8
- IIS 10.0 on Windows 10
Server - IIS 7.0 on Windows Server 2008
- IIS 7.5 on Windows Server 2008 R2
- IIS 8.0 on Windows Server 2012
- IIS 8.5 on Windows Server 2012 R2
- IIS 10.0 on Windows Server 2016
Product - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0
- IIS Express 7.5, IIS Express 8.0, IIS Express 10.0
Header Httpserv.h

See Also

IHttpResponse Interface
IHttpResponse::WriteEntityChunks Method