다음을 통해 공유


IHttpResponse::WriteEntityChunkByReference 메서드

응답 본문에 HTTP_DATA_CHUNK 구조를 삽입하거나 추가합니다.

구문

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

매개 변수

pDataChunk
[IN] 구조체에 대한 포인터입니다 HTTP_DATA_CHUNK .

lInsertPosition
[IN] 청크를 LONG 삽입할지 추가할지 여부를 지정하는 값입니다.

반환 값

HRESULT입니다. 가능한 값에는 다음 표에 있는 값이 포함되지만, 이에 국한되는 것은 아닙니다.

설명
S_OK 작업이 성공했음을 나타냅니다.
ERROR_INVALID_PARAMETER 매개 변수가 유효하지 않음을 나타냅니다(예: 포인터가 HTTP_DATA_CHUNK NULL로 설정됨).
ERROR_NOT_ENOUGH_MEMORY 작업을 수행할 메모리가 부족했음을 나타냅니다.
ERROR_ARITHMETIC_OVERFLOW 응답에 65535개 이상의 청크가 추가되었음을 나타냅니다.

설명

메서드는 WriteEntityChunkByReference 매개 변수의 값 lInsertPosition 에 따라 응답 버퍼에 구조를 삽입하거나 추가 HTTP_DATA_CHUNK 합니다.

  • 가 0이면 lInsertPosition 기존 응답 데이터 앞에 데이터가 삽입됩니다.

  • 가 -1이면 lInsertPosition 기존 응답 데이터의 마지막 청크 다음에 데이터가 추가됩니다.

    메서드는 WriteEntityChunkByReference 복사 대신 원래 데이터 청크에 대한 참조를 응답 버퍼에 삽입합니다. 따라서 에 pDataChunk->FromMemory.pBuffer 할당된 메모리는 응답 처리 기간 동안 유지되어야 합니다. 로컬 또는 스택 메모리를 사용하면 정의되지 않은 동작이 발생합니다.

    요청에 최대 65535(64KB -1) 청크를 쓸 수 있습니다.

예제

다음 예제에서는 메서드를 사용하여 응답에 WriteEntityChunkByReference 데이터를 삽입하는 방법을 보여 줍니다. 또한 매개 변수를 사용하여 lInsertPosition 데이터 청크를 삽입하거나 추가하는 방법을 보여 줍니다.

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

요구 사항

형식 Description
클라이언트 - Windows Vista의 IIS 7.0
- Windows 7의 IIS 7.5
- Windows 8의 IIS 8.0
- WINDOWS 10 IIS 10.0
서버 - Windows Server 2008의 IIS 7.0
- Windows Server 2008 R2의 IIS 7.5
- Windows Server 2012의 IIS 8.0
- Windows Server 2012 R2의 IIS 8.5
- WINDOWS SERVER 2016 IIS 10.0
제품 - 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
헤더 Httpserv.h

참고 항목

IHttpResponse 인터페이스
IHttpResponse::WriteEntityChunks 메서드