src/network/kernel/qnetworkproxy_win.cpp
changeset 33 3e2da88830cd
parent 18 2f34d5167611
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    97 #define WINHTTP_ACCESS_TYPE_NO_PROXY                    1
    97 #define WINHTTP_ACCESS_TYPE_NO_PROXY                    1
    98 #define WINHTTP_ACCESS_TYPE_NAMED_PROXY                 3
    98 #define WINHTTP_ACCESS_TYPE_NAMED_PROXY                 3
    99 
    99 
   100 #define WINHTTP_NO_PROXY_NAME     NULL
   100 #define WINHTTP_NO_PROXY_NAME     NULL
   101 #define WINHTTP_NO_PROXY_BYPASS   NULL
   101 #define WINHTTP_NO_PROXY_BYPASS   NULL
       
   102 
       
   103 #define WINHTTP_ERROR_BASE                      12000
       
   104 #define ERROR_WINHTTP_LOGIN_FAILURE             (WINHTTP_ERROR_BASE + 15)
       
   105 #define ERROR_WINHTTP_AUTODETECTION_FAILED      (WINHTTP_ERROR_BASE + 180)
   102 
   106 
   103 QT_BEGIN_NAMESPACE
   107 QT_BEGIN_NAMESPACE
   104 
   108 
   105 typedef BOOL (WINAPI * PtrWinHttpGetProxyForUrl)(HINTERNET, LPCWSTR, WINHTTP_AUTOPROXY_OPTIONS*, WINHTTP_PROXY_INFO*);
   109 typedef BOOL (WINAPI * PtrWinHttpGetProxyForUrl)(HINTERNET, LPCWSTR, WINHTTP_AUTOPROXY_OPTIONS*, WINHTTP_PROXY_INFO*);
   106 typedef HINTERNET (WINAPI * PtrWinHttpOpen)(LPCWSTR, DWORD, LPCWSTR, LPCWSTR,DWORD);
   110 typedef HINTERNET (WINAPI * PtrWinHttpOpen)(LPCWSTR, DWORD, LPCWSTR, LPCWSTR,DWORD);
   318         if (!hHttpSession)
   322         if (!hHttpSession)
   319             return;
   323             return;
   320 
   324 
   321         isAutoConfig = true;
   325         isAutoConfig = true;
   322         memset(&autoProxyOptions, 0, sizeof autoProxyOptions);
   326         memset(&autoProxyOptions, 0, sizeof autoProxyOptions);
   323         autoProxyOptions.fAutoLogonIfChallenged = true;
   327         autoProxyOptions.fAutoLogonIfChallenged = false;
   324         if (ieProxyConfig.fAutoDetect) {
   328         if (ieProxyConfig.fAutoDetect) {
   325             autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
   329             autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
   326             autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP |
   330             autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP |
   327                                                  WINHTTP_AUTO_DETECT_TYPE_DNS_A;
   331                                                  WINHTTP_AUTO_DETECT_TYPE_DNS_A;
   328         } else {
   332         } else {
   375             return sp->defaultResult;
   379             return sp->defaultResult;
   376         if (query.queryType() != QNetworkProxyQuery::UrlRequest) {
   380         if (query.queryType() != QNetworkProxyQuery::UrlRequest) {
   377             // change the scheme to https, maybe it'll work
   381             // change the scheme to https, maybe it'll work
   378             url.setScheme(QLatin1String("https"));
   382             url.setScheme(QLatin1String("https"));
   379         }
   383         }
   380         if (ptrWinHttpGetProxyForUrl(sp->hHttpSession,
   384 
   381                                      (LPCWSTR)url.toString().utf16(),
   385         bool getProxySucceeded = ptrWinHttpGetProxyForUrl(sp->hHttpSession,
   382                                      &sp->autoProxyOptions,
   386                                                 (LPCWSTR)url.toString().utf16(),
   383                                      &proxyInfo)) {
   387                                                 &sp->autoProxyOptions,
       
   388                                                 &proxyInfo);
       
   389         DWORD getProxyError = GetLastError();
       
   390 
       
   391         if (!getProxySucceeded
       
   392             && (ERROR_WINHTTP_LOGIN_FAILURE == getProxyError)) {
       
   393             // We first tried without AutoLogon, because this might prevent caching the result.
       
   394             // But now we've to enable it (http://msdn.microsoft.com/en-us/library/aa383153%28v=VS.85%29.aspx)
       
   395             sp->autoProxyOptions.fAutoLogonIfChallenged = TRUE;
       
   396             getProxySucceeded = ptrWinHttpGetProxyForUrl(sp->hHttpSession,
       
   397                                                (LPCWSTR)url.toString().utf16(),
       
   398                                                 &sp->autoProxyOptions,
       
   399                                                 &proxyInfo);
       
   400             getProxyError = GetLastError();
       
   401         }
       
   402 
       
   403         if (getProxySucceeded) {
   384             // yes, we got a config for this URL
   404             // yes, we got a config for this URL
   385             QString proxyBypass = QString::fromWCharArray(proxyInfo.lpszProxyBypass);
   405             QString proxyBypass = QString::fromWCharArray(proxyInfo.lpszProxyBypass);
   386             QStringList proxyServerList = splitSpaceSemicolon(QString::fromWCharArray(proxyInfo.lpszProxy));
   406             QStringList proxyServerList = splitSpaceSemicolon(QString::fromWCharArray(proxyInfo.lpszProxy));
   387             if (proxyInfo.lpszProxy)
   407             if (proxyInfo.lpszProxy)
   388                 GlobalFree(proxyInfo.lpszProxy);
   408                 GlobalFree(proxyInfo.lpszProxy);
   393                 return sp->defaultResult;
   413                 return sp->defaultResult;
   394             return parseServerList(query, proxyServerList);
   414             return parseServerList(query, proxyServerList);
   395         }
   415         }
   396 
   416 
   397         // GetProxyForUrl failed
   417         // GetProxyForUrl failed
       
   418 
       
   419         if (ERROR_WINHTTP_AUTODETECTION_FAILED == getProxyError) {
       
   420             //No config file could be retrieved on the network.
       
   421             //Don't search for it next time again.
       
   422             sp->isAutoConfig = false;
       
   423         }
       
   424 
   398         return sp->defaultResult;
   425         return sp->defaultResult;
   399     }
   426     }
   400 
   427 
   401     // static configuration
   428     // static configuration
   402     if (isBypassed(query.peerHostName(), sp->proxyBypass))
   429     if (isBypassed(query.peerHostName(), sp->proxyBypass))