Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用ISAPI扩展从IIS发送回客户端的零字节_C++_Iis_Isapi_Isapi Extension - Fatal编程技术网

C++ 使用ISAPI扩展从IIS发送回客户端的零字节

C++ 使用ISAPI扩展从IIS发送回客户端的零字节,c++,iis,isapi,isapi-extension,C++,Iis,Isapi,Isapi Extension,为了更好地理解IIS,我正在尝试编写一个形式上的ISAPI扩展。我有以下EXEXION代码: #include <httpext.h> #include <cstring> #include <cstdio> #include <fstream> #include <iostream> #include <windows.h> BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO

为了更好地理解IIS,我正在尝试编写一个形式上的ISAPI扩展。我有以下EXEXION代码:

#include <httpext.h>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <windows.h>

BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO * versionInfo)
{
    versionInfo->dwExtensionVersion = HSE_VERSION;
    strcpy_s( versionInfo->lpszExtensionDesc, "ISAPIExtension.dll");
    return TRUE;
}

DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK* ecb)
{
    char * msg = "<html><head/><body>Hello world</body></html>";
    DWORD msg_length = strlen(msg) * sizeof(char);
    DWORD sent_length = msg_length;
    std::cout << sent_length;
    auto result = ecb->WriteClient(ecb->ConnID, msg, &sent_length, 0);
    if (!result || sent_length != msg_length)
    {
        std::fstream output("errors.txt");
        output << "Could not write to client. Last error was " << GetLastError();
        output.flush();
        output.close();
        return HSE_STATUS_ERROR;
    }
    std::cout << sent_length;

    return HSE_STATUS_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
#包括
BOOL WINAPI GetExtensionVersion(HSE\u版本\u信息*版本信息)
{
versionInfo->dwExtensionVersion=HSE\U版本;
strcpy_s(versionInfo->lpszExtensionDesc,“ISAPIExtension.dll”);
返回TRUE;
}
DWORD WINAPI HttpExtensionProc(扩展控制块*ecb)
{
char*msg=“你好,世界”;
DWORD msg_length=strlen(msg)*sizeof(char);
DWORD发送长度=消息长度;
std::cout WriteClient(ecb->ConnID,msg,&sent_length,0);
如果(!result | | sent_length!=msg_length)
{
std::fstream输出(“errors.txt”);

输出添加内容类型标题可修复此问题:

DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK* ecb)
{
    char msg [] = "<html><head/><body>Hello world</body></html>";
    DWORD msg_length = strlen(msg);
    DWORD sent_length = msg_length;

    HSE_SEND_HEADER_EX_INFO HeaderExInfo;

    HeaderExInfo.pszStatus = "200 OK";
    HeaderExInfo.pszHeader = "Content-type: text/html\r\n\r\n";
    HeaderExInfo.cchStatus = strlen(HeaderExInfo.pszStatus);
    HeaderExInfo.cchHeader = strlen(HeaderExInfo.pszHeader);
    HeaderExInfo.fKeepConn = FALSE;

    ecb->ServerSupportFunction(ecb->ConnID, HSE_REQ_SEND_RESPONSE_HEADER_EX, &HeaderExInfo, NULL, NULL);
    auto result = ecb->WriteClient(ecb->ConnID, msg, &sent_length, 0);
    if (!result || sent_length != msg_length)
    {
        return HSE_STATUS_ERROR;
    }
    return HSE_STATUS_SUCCESS;
}
DWORD WINAPI HttpExtensionProc(扩展控制块*ecb)
{
char msg[]=“你好,世界”;
DWORD msg_length=strlen(msg);
DWORD发送长度=消息长度;
HSE发送头信息发送头信息;
HeaderExInfo.pszStatus=“200正常”;
HeaderExInfo.pszHeader=“内容类型:text/html\r\n\r\n”;
HeaderExInfo.cchStatus=strlen(HeaderExInfo.pszStatus);
HeaderExInfo.cchHeader=strlen(HeaderExInfo.pszHeader);
HeaderExInfo.fKeepConn=FALSE;
ecb->ServerSupportFunction(ecb->ConnID、HSE\U REQ\U SEND\U RESPONSE\U HEADER\U EX和HeaderExInfo,NULL,NULL);
自动结果=ecb->WriteClient(ecb->ConnID、msg和sent_长度,0);
如果(!result | | sent_length!=msg_length)
{
返回HSE_状态_错误;
}
返回HSE_状态_成功;
}
Microsoft在其GitHub帐户上发布了一个类似的ISAPI扩展示例:

DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK* ecb)
{
    char msg [] = "<html><head/><body>Hello world</body></html>";
    DWORD msg_length = strlen(msg);
    DWORD sent_length = msg_length;

    HSE_SEND_HEADER_EX_INFO HeaderExInfo;

    HeaderExInfo.pszStatus = "200 OK";
    HeaderExInfo.pszHeader = "Content-type: text/html\r\n\r\n";
    HeaderExInfo.cchStatus = strlen(HeaderExInfo.pszStatus);
    HeaderExInfo.cchHeader = strlen(HeaderExInfo.pszHeader);
    HeaderExInfo.fKeepConn = FALSE;

    ecb->ServerSupportFunction(ecb->ConnID, HSE_REQ_SEND_RESPONSE_HEADER_EX, &HeaderExInfo, NULL, NULL);
    auto result = ecb->WriteClient(ecb->ConnID, msg, &sent_length, 0);
    if (!result || sent_length != msg_length)
    {
        return HSE_STATUS_ERROR;
    }
    return HSE_STATUS_SUCCESS;
}