在回應本文中插入或附加 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 至回應緩衝區。
如果
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;
}
規格需求
| 類型 | 描述 |
|---|---|
| Client | - 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 |