使用 ISAPI 过滤器将标头放入文件中
我试图将整个原始标头放入文件中,但每次尝试写入内容时,我都会得到一个充满 ÌÌÌÌÌÌÌÌÌÌ 的文件。我做错了什么?
DWORD CTryISAPIFilter::OnPreprocHeaders(CHttpFilterContext* httpContext,
PHTTP_FILTER_PREPROC_HEADERS headerInformation)
{
char buffer[4096];
DWORD bufferSize = sizeof(buffer);
BOOL HeaderBoolean = headerInformation->GetHeader(httpContext->m_pFC, "ALL_RAW", buffer, &bufferSize);
char * ptrIn = (char *) buffer;
std::string postData2 = ptrIn;
char * outputString = new char[4096];
int i = 0;
for(i=0;i<4096;i++){
outputString[i] = postData2[i];
}
outputString[i+1] = NULL;
std::ofstream outfile ("D:\\WebSites\\wwwroot\\test.txt",std::ios::app);
outfile << outputString << std::endl;
outfile.close();
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
I am trying to get the entire raw header into a file but everytime I attempt to write the contents I get a file full of ÌÌÌÌÌÌÌÌÌÌÌ. What am I doing wrong?
DWORD CTryISAPIFilter::OnPreprocHeaders(CHttpFilterContext* httpContext,
PHTTP_FILTER_PREPROC_HEADERS headerInformation)
{
char buffer[4096];
DWORD bufferSize = sizeof(buffer);
BOOL HeaderBoolean = headerInformation->GetHeader(httpContext->m_pFC, "ALL_RAW", buffer, &bufferSize);
char * ptrIn = (char *) buffer;
std::string postData2 = ptrIn;
char * outputString = new char[4096];
int i = 0;
for(i=0;i<4096;i++){
outputString[i] = postData2[i];
}
outputString[i+1] = NULL;
std::ofstream outfile ("D:\\WebSites\\wwwroot\\test.txt",std::ios::app);
outfile << outputString << std::endl;
outfile.close();
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
headerInformation->GetHeader()
返回成功吗?如果是这样,它实际写入
buffer
的量是多少(大概它会在bufferSize
中放置的值中告诉您这一点)我怀疑
GetHeader() 失败,并且没有任何内容被写入
buffer
因为:postData2
远远超过通常应该是字符串末尾的位置时(无论如何在大多数情况下),不会引发异常。因此,buffer
中显然没有“\0”终止符(如果成功,GetHeader()
将写入该终止符)。您需要检查此故障并检查
GetLastError()
以获取有关故障原因的更多信息。更新:您的缓冲区可能不够大。请参阅 http://msdn.microsoft.com/en-us/magazine/cc163939。 aspx 了解如何适当调整缓冲区大小。
更新2:我已经有一段时间没有做网络工作了,但是“ALL_RAW”不是一个CGI风格的服务器环境变量而不是一个标头吗?难道您不应该使用
GetServerVariable()
而不是GetHeader()
来检索此内容吗?Is
headerInformation->GetHeader()
returning success?If so, how much is it actually writing into
buffer
(presumably it tells you this in a value it places inbufferSize
)I suspect that
GetHeader()
is failing, and nothing is being written tobuffer
because:postData2
well past what should usually be the end of the string (in most cases anyway). So there's apparently no '\0' terminator inbuffer
(whichGetHeader()
will write if it succeeds).You need to check for this failure and examine
GetLastError()
to get more information on what the failure is.Update: Your buffer might not be large enough. See http://msdn.microsoft.com/en-us/magazine/cc163939.aspx for how to appropriately size the buffer.
Update 2: It's been a while since I've done web stuff, but isn't "ALL_RAW" a CGI-style server environment variable rather than a header? Shouldn't you retrieve this using
GetServerVariable()
instead ofGetHeader()
?