IHttpResponse::WriteEntityChunkByReference 方法
将 HTTP_DATA_CHUNK 结构插入或追加到响应正文中。
语法
HRESULT WriteEntityChunkByReference(
IN HTTP_DATA_CHUNK* pDataChunk,
IN LONG lInsertPosition = -1
)
parameters
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
根据 参数的值将结构插入或追加 HTTP_DATA_CHUNK
到响应缓冲区中 lInsertPosition
。
如果
lInsertPosition
为 0,则将在现有响应数据之前插入数据。如果
lInsertPosition
为 -1,则数据将追加到现有响应数据的最后一个区块之后。方法
WriteEntityChunkByReference
将原始数据区块的引用(而不是副本)插入响应缓冲区。 因此,为pDataChunk->FromMemory.pBuffer
分配的内存必须在响应处理期间保留。 使用本地或堆栈内存会导致未定义的行为。最多可将 65535 个 (64 KB 减去 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;
}
要求
类型 | 说明 |
---|---|
客户端 | - 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 |
Header | Httpserv.h |