cwindowsapachewinapiwinhttp

WinHTTP causes Apache web server (localhost) to respond with http status 400 on Windows Vista


I'm trying to send a request to an apache web server using WinHTTP. My code is working from Windows 7 to Windows 11, but on Windows Vista I receive an error 400 from the server after sending the request. My code looks as follows:

if(!(cp->connection = fpWinHttpConnect(cp->session, cp->currentPanel.domain, cp->currentPanel.secure ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT, 0)))
        goto Done;

if (!(cp->request = fpWinHttpOpenRequest(cp->connection, L"POST", cp->currentPanel.gatePath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, cp->currentPanel.secure ? WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH)))
        goto Done;

if (!fpWinHttpSendRequest(cp->request, L"Content-Type: application/x-www-form-urlencoded", 48, postData, StrlA(postData), StrlA(postData), 0))
        goto Done;

if (!fpWinHttpReceiveResponse(cp->request, 0))
        goto Done;

if (!fpWinHttpQueryHeaders(cp->request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &statusCode, &statusCodeSize, WINHTTP_NO_HEADER_INDEX))
        goto Done;

//statusCode == 400

enter image description here

Why does this error occur, and why only on Windows Vista?

Edit: After enabling debug logging in apache, I can see the following error in error.log: [core:debug] [pid 29580:tid 1860] protocol.c(1116): (22)Invalid argument: [client 127.0.0.1:53267] Failed to read request header line Content-Type: application/x-www-form-urlencoded


Solution

  • $ echo -n 'Content-Type: application/x-www-form-urlencoded' | wc -c
    47
    $
    

    Argument length for lpszHeaders (parameter dwHeadersLength) is one too long. It probably sent that null up the socket and Apache was not happy.

    Clearly changing the length to -1 would fix the problem, as stated in comments, as would passing the actual value.