c++ - HTTP_RECEIVE_REQUEST_FLAG_FLUSH_BODY 不复制 http body

标签 c++ windows http httpserver simplehttpserver

根据MSDN documentation ,传递到 HttpReceiveHttpRequest 函数中的 HTTP_RECEIVE_REQUEST_FLAG_FLUSH_BODY 标志导致实体主体被复制到传入的 HTTP_REQUEST 结构中。

我写了一个简单的 http 服务器(见下文),它调用这个函数并设置了这个标志。然后,我从测试客户端发送了一个 4MB 的 POST 请求,并将调试器附加到 http 服务器,以查看函数返回后有多少字节被复制到 HTTP_REQUEST 结构中;我观察到这没有复制 http 正文,只复制了 header 。

有没有办法让 HttpReceiveHttpRequest 将整个 4MB 主体复制到 HTTP 请求的 pEntityChunks 部分?

注意:我验证了 HttpReceiveRequestEntityBody 函数确实在设置了 HTTP_RECEIVE_REQUEST_ENTITY_BODY_FLAG_FILL_BUFFER 标志时复制了整个主体。

HTTP 服务器

// HttpServer.cpp : Defines the entry point for the console application.
//

#ifndef UNICODE
#define UNICODE
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <http.h>
#include <stdio.h>
#include <string>
#include <memory>

#pragma comment(lib, "httpapi.lib")

int RequestBufferLength = 4194304 + 4096;
int ResponseBufferLength = 4194304;
int SmallBufferLength = 100;

std::unique_ptr<char[]> ResponseBuffer = std::make_unique<char[]>(ResponseBufferLength);
std::unique_ptr<char[]> SmallBuffer = std::make_unique<char[]>(SmallBufferLength);
std::unique_ptr<char[]> RequestBuffer = std::make_unique<char[]>(RequestBufferLength);

DWORD SendHttpResponse(HANDLE hReqQueue, PHTTP_REQUEST pRequest)
{
    HTTP_RESPONSE response;
    HTTP_DATA_CHUNK dataChunk;
    DWORD result;
    DWORD bytesSent;

    // Initialize the HTTP response structure.
    RtlZeroMemory(&response, sizeof(response));
    response.StatusCode = 200;                      
    response.pReason = "OK";                         
    response.ReasonLength = 2;

    // Add known headers.
    std::string contentType = "text/html";
    response.Headers.KnownHeaders[HttpHeaderContentType].pRawValue = contentType.c_str();
    response.Headers.KnownHeaders[HttpHeaderContentType].RawValueLength = (USHORT)contentType.length();

    // Add the body
    if (pRequest->Verb == HttpVerbGET)
    {
        // Send big response
        dataChunk.DataChunkType = HttpDataChunkFromMemory;
        dataChunk.FromMemory.pBuffer = ResponseBuffer.get();
        dataChunk.FromMemory.BufferLength = ResponseBufferLength;
    }
    else
    {
        // Send small response
        dataChunk.DataChunkType = HttpDataChunkFromMemory;
        dataChunk.FromMemory.pBuffer = SmallBuffer.get();
        dataChunk.FromMemory.BufferLength = SmallBufferLength;
    }

    response.EntityChunkCount = 1;
    response.pEntityChunks = &dataChunk;

    // Because the entity body is sent in one call, it is not
    // required to specify the Content-Length.
    result = HttpSendHttpResponse(
        hReqQueue,           // ReqQueueHandle
        pRequest->RequestId, // Request ID
        0,                   // Flags
        &response,           // HTTP response
        NULL,                // HTTP Cache Policy
        &bytesSent,          // bytes sent  (OPTIONAL)
        NULL,                // pReserved2  (must be NULL)
        0,                   // Reserved3   (must be 0)
        NULL,                // LPOVERLAPPED(OPTIONAL)
        NULL                 // pReserved4  (must be NULL)
        );

    if (result != NO_ERROR)
    {
        wprintf(L"HttpSendHttpResponse failed with %lu \n", result);
    }

    return result;
}

DWORD ReceiveRequests(HANDLE hReqQueue)
{
    ULONG result;
    HTTP_REQUEST_ID requestId;
    DWORD bytesRead;
    PHTTP_REQUEST pRequest;

    pRequest = (PHTTP_REQUEST)RequestBuffer.get();

    // Wait for a new request. This is indicated by a NULL 
    // request ID.
    HTTP_SET_NULL_ID(&requestId);

    for (;;)
    {
        result = HttpReceiveHttpRequest(
            hReqQueue,
            requestId,
            HTTP_RECEIVE_REQUEST_FLAG_FLUSH_BODY,
            pRequest,
            RequestBufferLength,
            &bytesRead,
            NULL);

        if (NO_ERROR == result)
        {
            switch (pRequest->Verb)
            {
            case HttpVerbGET:
                result = SendHttpResponse(hReqQueue, pRequest);
                break;
            case HttpVerbPUT:
            case HttpVerbPOST:
                result = HttpReceiveRequestEntityBody(
                    hReqQueue,
                    pRequest->RequestId,
                    HTTP_RECEIVE_REQUEST_ENTITY_BODY_FLAG_FILL_BUFFER,
                    RequestBuffer.get() + bytesRead,
                    RequestBufferLength - bytesRead,
                    &bytesRead,
                    NULL);

                if (NO_ERROR == result)
                {
                    result = SendHttpResponse(hReqQueue, pRequest);
                }
                break;
            default:
                wprintf(L"Got a unknown request for %ws \n", pRequest->CookedUrl.pFullUrl);
                result = E_FAIL;
                break;
            }

            if (result != NO_ERROR)
            {
                break;
            }

            // Reset the Request ID to handle the next request.
            HTTP_SET_NULL_ID(&requestId);
        }
        else
        {
            break;
        }
    }

    return result;
}

int main(int argc, char** argv)
{
    std::wstring url;
    if (argc == 1)
    {
        url = L"http://127.0.0.1:80/NativeBigDataTest";
    }
    else
    {
        std::string serverIp(argv[1]);
        url = L"http://" + std::wstring(serverIp.begin(), serverIp.end()) + L":80/NativeBigDataTest";
    }    

    HTTP_SERVER_SESSION_ID session;
    HTTP_URL_GROUP_ID urlGroup;
    HANDLE hReqQueue = NULL;
    HTTP_BINDING_INFO bindingInfo;
    ULONG retCode;

    // Initialize HTTP Server APIs
    retCode = HttpInitialize(
                    HTTPAPI_VERSION_2,
                    HTTP_INITIALIZE_SERVER,    // Flags
                    NULL                       // Reserved
                    );
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpInitialize failed with %lu \n", retCode);
        return retCode;
    }

    // Create server session    
    retCode = HttpCreateServerSession(HTTPAPI_VERSION_2, &session, 0);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCreateServerSession failed with %lu \n", retCode);
        goto CleanUp5;
    }

    // Create Url Group    
    retCode = HttpCreateUrlGroup(session, &urlGroup, 0);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCreateUrlGroup failed with %lu \n", retCode);
        goto CleanUp4;
    }

    // Add url to group
    retCode = HttpAddUrlToUrlGroup(urlGroup, url.c_str(), HTTP_URL_CONTEXT{}, 0);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpAddUrlToUrlGroup failed with %lu \n", retCode);
        goto CleanUp3;
    }

    // Create a Request Queue Handle    
    retCode = HttpCreateRequestQueue(HTTPAPI_VERSION_2, NULL, NULL, 0, &hReqQueue);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCreateHttpHandle failed with %lu \n", retCode);
        goto CleanUp2;
    }

    // Bind request queue to url group    
    bindingInfo.RequestQueueHandle = hReqQueue;
    bindingInfo.Flags.Present = 1;
    retCode = HttpSetUrlGroupProperty(urlGroup, HttpServerBindingProperty, &bindingInfo, sizeof(bindingInfo));
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpSetUrlGroupProperty failed with %lu \n", retCode);
        goto CleanUp1;
    }

    wprintf(L"Listening on url %s\n", url.c_str());

    // Receive requests
    ReceiveRequests(hReqQueue);

CleanUp1:
    // Close the Request Queue handle.
    retCode = HttpCloseRequestQueue(hReqQueue);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCloseRequestQueue failed with %lu \n", retCode);
    }

CleanUp2:
    // Call HttpRemoveUrlFromUrlGroup for all added URLs.
    retCode = HttpRemoveUrlFromUrlGroup(
        urlGroup,
        url.c_str(),
        0);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpRemoveUrlFromUrlGroup failed with %lu \n", retCode);
    }

CleanUp3:
    // Close Url group
    retCode = HttpCloseUrlGroup(urlGroup);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCloseUrlGroup failed with %lu \n", retCode);
    }

CleanUp4:
    // Close Session
    retCode = HttpCloseServerSession(session);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpCloseServerSession failed with %lu \n", retCode);
    }

CleanUp5:
    // Call HttpTerminate.
    retCode = HttpTerminate(HTTP_INITIALIZE_SERVER, NULL);
    if (retCode != NO_ERROR)
    {
        wprintf(L"HttpTerminate failed with %lu \n", retCode);
    }

    return retCode;
}

最佳答案

在分析了我自己的使用 HttpReceiveHttpRequest 函数的 HTTPServer 中的大量日志后,我发现如果我没有使用 HTTP_RECEIVE_REQUEST_ENTITY_BODY_FLAG_FILL_BUFFER 标志,它总是先接收一个 1 字节的包,然后再接收一个完整的缓冲区. (注意:我在 MS-Windows 8 上发现了这种行为)。如果我确实使用了上述标志,即使是第一次,它也会立即收到一个缓冲区。

我不知道这是否意味着更多或更少的往返,但在使用此标志后,在较大的文件上以毫秒为单位测量的吞吐量几乎下降到 50%。

请同时在其他版本的 MS-Windows 上测试!

关于c++ - HTTP_RECEIVE_REQUEST_FLAG_FLUSH_BODY 不复制 http body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006881/

相关文章:

c++ - 在C++中,如何从1行中获取字符串, float 和整数的输入?

c++ - 来自 std::find 的此错误的优雅解决方案是什么?

javascript - Moment.js 不接受查询字符串作为有效输入

http - Varnish 不 gzip html 页面

php - Cakephp/php Post 数据字段值包含来自 RAW Post 的片段

c++ - 如何在 yacc 中声明 "struct"变量

c++ - 如何保证移动不妨碍RVO?

windows - 我怎样才能拥有一个便携的 Emacs?

c++ - 在 Windows 中部署 Qt 应用程序

windows - 我的 Windows 应用程序真正使用了多少内存?