diff --git a/runtime/shims/winhttp_offline.cpp b/runtime/shims/winhttp_offline.cpp index e8467f06c0..82b004110b 100644 --- a/runtime/shims/winhttp_offline.cpp +++ b/runtime/shims/winhttp_offline.cpp @@ -203,8 +203,10 @@ HINTERNET WINAPI open_connection(HINTERNET session, LPCWSTR server, INTERNET_POR return nullptr; } if (!server || !*server) { + // As the host's winhttp.dll answers (measured on Windows 11): no name is ERROR_INVALID_PARAMETER, an empty + // one ERROR_WINHTTP_INVALID_URL. release(s); - SetLastError(ERROR_INVALID_PARAMETER); + SetLastError(server ? ERROR_WINHTTP_INVALID_URL : ERROR_INVALID_PARAMETER); return nullptr; } return make_handle(Kind::Connect, s, s->async, server, port); // keeps the reference on the session @@ -342,7 +344,7 @@ BOOL WINAPI set_option(HINTERNET handle, DWORD option, LPVOID buffer, DWORD leng release(h); return fail(ERROR_INVALID_PARAMETER); } - if (length < sizeof(DWORD_PTR)) { + if (length != sizeof(DWORD_PTR)) { // the host takes exactly a DWORD_PTR: 4 and 16 bytes both fail (measured) release(h); return fail(ERROR_INSUFFICIENT_BUFFER); } diff --git a/runtime/test/winhttp_offline_test.cpp b/runtime/test/winhttp_offline_test.cpp index 00ba5326d8..7753fd3e90 100644 --- a/runtime/test/winhttp_offline_test.cpp +++ b/runtime/test/winhttp_offline_test.cpp @@ -342,6 +342,18 @@ int main() { check(set_option(request, WINHTTP_OPTION_CONTEXT_VALUE, &short_ctx, sizeof(short_ctx)) == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "a 4-byte WINHTTP_OPTION_CONTEXT_VALUE fails with ERROR_INSUFFICIENT_BUFFER"); + const DWORD_PTR long_ctx[2] = {0x5678, 0x9abc}; + SetLastError(0); + check(set_option(request, WINHTTP_OPTION_CONTEXT_VALUE, const_cast(long_ctx), sizeof(long_ctx)) == + FALSE && + GetLastError() == ERROR_INSUFFICIENT_BUFFER, + "a 16-byte WINHTTP_OPTION_CONTEXT_VALUE fails with ERROR_INSUFFICIENT_BUFFER too, as on the host"); + { + DWORD_PTR kept = 0; + DWORD kept_size = sizeof(kept); + check(query_option(request, WINHTTP_OPTION_CONTEXT_VALUE, &kept, &kept_size) != FALSE && kept == 0x1234, + "and the context set before stays"); + } SetLastError(0); check(set_option(nullptr, WINHTTP_OPTION_CONTEXT_VALUE, &value, sizeof(value)) == FALSE && GetLastError() == ERROR_WINHTTP_INCORRECT_HANDLE_TYPE, @@ -364,6 +376,9 @@ int main() { SetLastError(0); check(connect(session, nullptr, 80, 0) == nullptr && GetLastError() == ERROR_INVALID_PARAMETER, "a connection needs a server name"); + SetLastError(0); + check(connect(session, L"", 80, 0) == nullptr && GetLastError() == ERROR_WINHTTP_INVALID_URL, + "an empty server name fails with ERROR_WINHTTP_INVALID_URL, as on the host"); check(close(request) != FALSE && close(connection) != FALSE && close(session) != FALSE, "the handles close, children first");