From 66e49d5b0868a9832f339056ddfd25cf4aee01ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
Date: Sat, 19 Mar 2016 03:53:51 +0100
Subject: wininet: Set default HTTP port correctly when passing
 INTERNET_FLAG_SECURE to InternetConnect.

---
 dlls/wininet/http.c       |  8 +++++--
 dlls/wininet/tests/http.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/dlls/wininet/http.c b/dlls/wininet/http.c
index 143dc75..2f8fba4 100644
--- a/dlls/wininet/http.c
+++ b/dlls/wininet/http.c
@@ -3335,7 +3335,7 @@ static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
 {
     appinfo_t *hIC = session->appInfo;
     http_request_t *request;
-    DWORD len;
+    DWORD port, len;
 
     TRACE("-->\n");
 
@@ -3364,7 +3364,11 @@ static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
     request->session = session;
     list_add_head( &session->hdr.children, &request->hdr.entry );
 
-    request->server = get_server(session->hostName, session->hostPort, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
+    port = session->hostPort;
+    if (port == INTERNET_INVALID_PORT_NUMBER)
+        port = (session->hdr.dwFlags & INTERNET_FLAG_SECURE) ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
+
+    request->server = get_server(session->hostName, port, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
     if(!request->server) {
         WININET_Release(&request->hdr);
         return ERROR_OUTOFMEMORY;
diff --git a/dlls/wininet/tests/http.c b/dlls/wininet/tests/http.c
index 82fd10b..4e5b7fe 100644
--- a/dlls/wininet/tests/http.c
+++ b/dlls/wininet/tests/http.c
@@ -5733,7 +5733,8 @@ static void test_connection_failure(void)
 static void test_default_service_port(void)
 {
     HINTERNET session, connect, request;
-    DWORD error;
+    DWORD size, count, error;
+    char buffer[128];
     BOOL ret;
 
     session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
@@ -5753,6 +5754,63 @@ static void test_default_service_port(void)
     ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
        "got %u\n", error);
 
+    size = sizeof(buffer) - 1;
+    count = 0;
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
+    ok(ret, "HttpQueryInfo succeeded\n");
+    ok(!strcmp(buffer, "test.winehq.org:80"), "Expected test.winehg.org:80, got %s\n", buffer);
+
+    InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+    InternetCloseHandle(session);
+
+    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(session != NULL, "InternetOpen failed\n");
+
+    connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
+                              INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+
+    SetLastError(0xdeadbeef);
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
+
+    size = sizeof(buffer) - 1;
+    count = 0;
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
+    ok(ret, "HttpQueryInfo succeeded\n");
+    ok(!strcmp(buffer, "test.winehq.org"), "Expected test.winehg.org, got %s\n", buffer);
+
+    InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+    InternetCloseHandle(session);
+
+    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(session != NULL, "InternetOpen failed\n");
+
+    connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
+                              INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+
+    SetLastError(0xdeadbeef);
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
+
+    size = sizeof(buffer) - 1;
+    count = 0;
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
+    ok(ret, "HttpQueryInfo succeeded\n");
+    ok(!strcmp(buffer, "test.winehq.org:443"), "Expected test.winehg.org:443, got %s\n", buffer);
+
     InternetCloseHandle(request);
     InternetCloseHandle(connect);
     InternetCloseHandle(session);
-- 
2.7.1

