Condividi tramite


Metodo IHttpResponse::WriteEntityChunkByReference

Inserisce o aggiunge una struttura HTTP_DATA_CHUNK nel corpo della risposta.

Sintassi

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

Parametri

pDataChunk
[IN] Puntatore a una HTTP_DATA_CHUNK struttura.

lInsertPosition
[IN] Valore LONG che specifica se inserire o aggiungere il blocco.

Valore restituito

Oggetto HRESULT. I valori possibili includono, ma non sono limitati a, quelli indicati nella tabella seguente.

Valore Descrizione
S_OK Indica che l'operazione ha avuto esito positivo.
ERROR_INVALID_PARAMETER Indica che il parametro non è valido, ad esempio il HTTP_DATA_CHUNK puntatore è impostato su NULL.
ERROR_NOT_ENOUGH_MEMORY Indica che la memoria non è sufficiente per eseguire l'operazione.
ERROR_ARITHMETIC_OVERFLOW Indica che alla risposta sono stati aggiunti più di 65535 blocchi.

Commenti

Il WriteEntityChunkByReference metodo inserisce o aggiunge una HTTP_DATA_CHUNK struttura nel buffer di risposta a seconda del valore del lInsertPosition parametro.

  • Se lInsertPosition è 0, i dati verranno inseriti prima dei dati di risposta esistenti.

  • Se lInsertPosition è -1, i dati verranno aggiunti dopo l'ultimo blocco di dati di risposta esistenti.

    Il WriteEntityChunkByReference metodo inserisce un riferimento al blocco di dati originale, anziché una copia, nel buffer di risposta. Pertanto, la memoria allocata per pDataChunk->FromMemory.pBuffer deve essere persistente per la durata dell'elaborazione della risposta. L'uso della memoria locale o dello stack comporta un comportamento non definito.

    Un massimo di 65535 (64 KB meno 1) blocchi può essere scritto in una richiesta.

Esempio

Nell'esempio seguente viene illustrato come usare il WriteEntityChunkByReference metodo per inserire i dati nella risposta. Illustra anche come usare il lInsertPosition parametro per inserire o aggiungere blocchi di dati.

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

Requisiti

Tipo Descrizione
Client - IIS 7.0 in Windows Vista
- IIS 7.5 in Windows 7
- IIS 8.0 in Windows 8
- IIS 10.0 in Windows 10
Server - IIS 7.0 in Windows Server 2008
- IIS 7.5 in Windows Server 2008 R2
- IIS 8.0 in Windows Server 2012
- IIS 8.5 in Windows Server 2012 R2
- IIS 10.0 in Windows Server 2016
Prodotto - 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
Intestazione Httpserv.h

Vedere anche

Interfaccia IHttpResponse
Metodo IHttpResponse::WriteEntityChunks