Http server api issue

Viewed 25

I am trying to use this server API: https://perpetualprogrammers.wordpress.com/2016/05/22/the-http-server-api/

It works fine if I use xampp and do this locally on the same PC from PHP:

$ch = curl_init("http://localhost:9000/api/cpp/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);

But, if I upload this code to a website, it doesn't even connect with my HTTP server API:

ULONG result = 0;
HTTPAPI_VERSION version = HTTPAPI_VERSION_2;
result = HttpInitialize(version, HTTP_INITIALIZE_SERVER, 0);
HTTP_SERVER_SESSION_ID serverSessionId;
result = HttpCreateServerSession(version, &serverSessionId, 0);
HTTP_URL_GROUP_ID groupId;
result = HttpCreateUrlGroup(serverSessionId, &groupId, 0);
HANDLE requestQueueHandle;
result = HttpCreateRequestQueue(version, NULL, NULL, 0, &requestQueueHandle);
HTTP_BINDING_INFO info;
info.Flags.Present = 1;
info.RequestQueueHandle = requestQueueHandle;
result = HttpSetUrlGroupProperty(groupId, HttpServerBindingProperty, &info, sizeof(info));
PCWSTR url = (argc == 2) ? argv[1] : L"http://localhost:9000/api/cpp/";
result = HttpAddUrlToUrlGroup(groupId, url, 0, 0);
wprintf(L"Listening. Please submit requests to: %s\n", url);
 
for (;;)
{
    HTTP_REQUEST_ID requestId = 0;
    HTTP_SET_NULL_ID(&requestId);
    int bufferSize = 4096;
    int requestSize = sizeof(HTTP_REQUEST) + bufferSize;
    BYTE *buffer = new BYTE[requestSize];
    PHTTP_REQUEST pRequest = (PHTTP_REQUEST)buffer;
    RtlZeroMemory(buffer, requestSize);
    ULONG bytesReturned;
    result = HttpReceiveHttpRequest(
        requestQueueHandle,
        requestId,
        HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
        pRequest,
        requestSize,
        &bytesReturned,
        NULL
    );
    wprintf(L"Full URL: %ws\n", pRequest->CookedUrl.pFullUrl);
    wprintf(L"    Path: %ws\n", pRequest->CookedUrl.pAbsPath);
 
    if (pRequest->Verb == HttpVerbDELETE)
    {
        wprintf(L"Asked to stop.\n");
        break;
    }
 
    // Respond to the request.
    HTTP_RESPONSE response;
    RtlZeroMemory(&response, sizeof(response));
    response.StatusCode = 200;
    response.pReason = "OK";
    response.ReasonLength = (USHORT)strlen(response.pReason);
    response.Headers.KnownHeaders[HttpHeaderContentType].pRawValue = "text/html";
    response.Headers.KnownHeaders[HttpHeaderContentType].RawValueLength = (USHORT)strlen(response.Headers.KnownHeaders[HttpHeaderContentType].pRawValue);
    PSTR pEntityString = "Hello from C++";
    HTTP_DATA_CHUNK dataChunk;
    dataChunk.DataChunkType = HttpDataChunkFromMemory;
    dataChunk.FromMemory.pBuffer = pEntityString;
    dataChunk.FromMemory.BufferLength = (ULONG)strlen(pEntityString);
    response.EntityChunkCount = 1;
    response.pEntityChunks = &dataChunk;
 
    result = HttpSendHttpResponse(
        requestQueueHandle,
        pRequest->RequestId,
        0,
        &response,
        NULL,
        NULL,   // &bytesSent (optional)
        NULL,
        0,
        NULL,
        NULL
    );
 
    delete buffer;
}
 
result = HttpRemoveUrlFromUrlGroup(groupId, url, 0);
info.Flags.Present = 0;
info.RequestQueueHandle = NULL;
result = HttpSetUrlGroupProperty(groupId, HttpServerBindingProperty, &info, sizeof(info));
result = HttpShutdownRequestQueue(requestQueueHandle);
result = HttpTerminate(HTTP_INITIALIZE_SERVER, NULL);

I have looked for a fix, but I couldn't find one, so I ask here hopefully someone can fix it.

0 Answers
Related