使用 ISAPI 过滤器将标头放入文件中

发布于 2024-10-07 05:29:20 字数 838 浏览 0 评论 0原文

我试图将整个原始标头放入文件中,但每次尝试写入内容时,我都会得到一个充满 ÌÌÌÌÌÌÌÌÌÌ 的文件。我做错了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-10-14 05:29:20

headerInformation->GetHeader() 返回成功吗?

如果是这样,它实际写入 buffer 的量是多少(大概它会在 bufferSize 中放置的值中告诉您这一点)

我怀疑 GetHeader() 失败,并且没有任何内容被写入 buffer 因为:

  • 你得到了所有“ÌÌÌÌÌÌÌÌÌÌÌ”字符(这是 VC 的调试版本将设置未初始化内存的内容),并且
  • 你当您索引 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 in bufferSize)

I suspect that GetHeader() is failing, and nothing is being written to buffer because:

  • you're getting all "ÌÌÌÌÌÌÌÌÌÌÌ" characters (which is what the debug builds of VC will set uninitialized memory to), and
  • you're not getting an exception thrown when you index postData2 well past what should usually be the end of the string (in most cases anyway). So there's apparently no '\0' terminator in buffer (which GetHeader() 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 of GetHeader()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文