Compartir a través de


IHttpResponse::WriteEntityChunkByReference (Método)

Inserta o anexa una estructura de HTTP_DATA_CHUNK en el cuerpo de la respuesta.

Sintaxis

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

Parámetros

pDataChunk
[IN] Puntero a una HTTP_DATA_CHUNK estructura.

lInsertPosition
[IN] Valor LONG que especifica si se va a insertar o anexar el fragmento.

Valor devuelto

Una clase HRESULT. Entre los valores posibles se incluyen los que se indican en la tabla siguiente, entre otros.

Valor Descripción
S_OK Indica que la operación se realizó correctamente.
ERROR_INVALID_PARAMETER Indica que el parámetro no es válido (por ejemplo, el HTTP_DATA_CHUNK puntero se establece en NULL).
ERROR_NOT_ENOUGH_MEMORY Indica que no hay memoria suficiente para realizar la operación.
ERROR_ARITHMETIC_OVERFLOW Indica que se han agregado más de 65535 fragmentos a la respuesta.

Comentarios

El WriteEntityChunkByReference método inserta o anexa una HTTP_DATA_CHUNK estructura en el búfer de respuesta según el valor del lInsertPosition parámetro.

  • Si lInsertPosition es 0, los datos se insertarán antes de los datos de respuesta existentes.

  • Si lInsertPosition es -1, los datos se anexarán después del último fragmento de datos de respuesta existentes.

    El WriteEntityChunkByReference método inserta una referencia al fragmento de datos original, en lugar de una copia, en el búfer de respuesta. Por lo tanto, la memoria asignada para pDataChunk->FromMemory.pBuffer debe conservarse durante el procesamiento de la respuesta. El uso de la memoria local o de pila da como resultado un comportamiento indefinido.

    Un máximo de 65535 fragmentos (64 KB menos 1) se puede escribir en una solicitud.

Ejemplo

En el ejemplo siguiente se muestra cómo usar el WriteEntityChunkByReference método para insertar datos en la respuesta. También muestra cómo usar el lInsertPosition parámetro para insertar o anexar fragmentos de datos.

//  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;       
}

Requisitos

Tipo Descripción
Remoto - IIS 7.0 en Windows Vista
- IIS 7.5 en Windows 7
- IIS 8.0 en Windows 8
- IIS 10.0 en Windows 10
Servidor - IIS 7.0 en Windows Server 2008
- IIS 7.5 en Windows Server 2008 R2
- IIS 8.0 en Windows Server 2012
- IIS 8.5 en Windows Server 2012 R2
- IIS 10.0 en Windows Server 2016
Producto - 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
Encabezado Httpserv.h

Consulte también

IHttpResponse (Interfaz)
IHttpResponse::WriteEntityChunks (Método)