0

I'm trying to reach a web page on an embedded device. I'm using WinHttp on Win32. When trying to read response I get error

ERROR_WINHTTP_INVALID_SERVER_RESPONSE 12152 The server response cannot be parsed.

But when I captured with WireShark I can see that response is coming. So to test I wrote a simple C# program. GetResponse was throwing exception

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

So according to below solution I set useUnsafeHeaderParsing to true. And it worked fine.

HttpWebRequestError: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

Since I can't use C# I need to find a way to set useUnsafeHeaderParsing to true for WinHttp with win32 C++

Many thanks

Community
  • 1
  • 1
AFgone
  • 1,172
  • 4
  • 16
  • 31
  • Talk to the vendor of the web server kindly asking them to stop violating the HTTP protocol. – IInspectable Sep 04 '15 at 13:09
  • the problem is that it shows on the Chrome IE and they say since it shows properly on browsers, they claim it is standard. But obviously NOT. – AFgone Sep 04 '15 at 17:11

2 Answers2

1

I've briefly looked into the option flags of WinHttpSetOption and found the following entry:

WINHTTP_OPTION_UNSAFE_HEADER_BLOCKING This option is reserved for internal use and should not be called.

Since the option looks linke an on/off switch I would try to do the following:

BOOL bResult;
BOOL bOption = FALSE;

bResult = WinHttpSetOption(hInternet, 
                           WINHTTP_OPTION_UNSAFE_HEADER_BLOCKING,
                           &bOption,
                           sizeof(bOption));
if (bResult == FALSE)
{
    /* handle error with GetLastError() */
}

Well but as MSDN says it's reserved for internal use and therefore the function may change in the future (or has already changed in the past). But it's worth a try... Good Luck!

Lukas Thomsen
  • 3,089
  • 2
  • 17
  • 23
  • I'm trying to set this but it says WINHTTP_OPTION_UNSAFE_HEADER_BLOCKING undeclared identifier. What can be the problem? – AFgone Sep 05 '15 at 12:03
  • @AFgone Hmmm... I don't know. Maybe you need a newer version of the Windows SDK? – Lukas Thomsen Sep 06 '15 at 09:48
  • I have 7, 7.1 8 8.1 when I go to winhttp I see it is from 7.0 but in my include directories I couldn't find windows sdk path. How can I change it? – AFgone Sep 06 '15 at 14:16
0

Looks like the name of the option must have changed since then: with the current SDK it's WINHTTP_OPTION_UNSAFE_HEADER_PARSING. Also, I verified (by examining the Assembly code directly) that:

  • the option must be DWORD-sized
  • the value of the option doesn't matter, as long as it's nonzero
  • you can only enable unsafe parsing; trying to disable (by setting the option value to zero) causes an error to be returned

Obviously, since this undocumented, it's subject to change.