Example #1
0
        private static void PrepareWebAccess(IOConnectionInfo ioc)
        {
#if !KeePassUAP
            IocProperties p = ((ioc != null) ? ioc.Properties : null);
            if (p == null)
            {
                Debug.Assert(false); p = new IocProperties();
            }

            try
            {
                if (m_bSslCertsAcceptInvalid)
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                        IOConnection.AcceptCertificate;
                }
                else
                {
                    ServicePointManager.ServerCertificateValidationCallback = null;
                }
            }
            catch (Exception) { Debug.Assert(false); }

            try
            {
                SecurityProtocolType spt = (SecurityProtocolType.Ssl3 |
                                            SecurityProtocolType.Tls);

                // The flags Tls11 and Tls12 in SecurityProtocolType have been
                // introduced in .NET 4.5 and must not be set when running under
                // older .NET versions (otherwise an exception is thrown)
                Type     tSpt = typeof(SecurityProtocolType);
                string[] vSpt = Enum.GetNames(tSpt);
                foreach (string strSpt in vSpt)
                {
                    if (strSpt.Equals("Tls11", StrUtil.CaseIgnoreCmp))
                    {
                        spt |= (SecurityProtocolType)Enum.Parse(tSpt, "Tls11", true);
                    }
                    else if (strSpt.Equals("Tls12", StrUtil.CaseIgnoreCmp))
                    {
                        spt |= (SecurityProtocolType)Enum.Parse(tSpt, "Tls12", true);
                    }
                }

                ServicePointManager.SecurityProtocol = spt;
            }
            catch (Exception) { Debug.Assert(false); }

            try
            {
                bool bCurCont = ServicePointManager.Expect100Continue;
                if (!m_obDefaultExpect100Continue.HasValue)
                {
                    Debug.Assert(bCurCont);                     // Default should be true
                    m_obDefaultExpect100Continue = bCurCont;
                }

                bool bNewCont = m_obDefaultExpect100Continue.Value;
                bool?ob       = p.GetBool(IocKnownProperties.Expect100Continue);
                if (ob.HasValue)
                {
                    bNewCont = ob.Value;
                }

                if (bNewCont != bCurCont)
                {
                    ServicePointManager.Expect100Continue = bNewCont;
                }
            }
            catch (Exception) { Debug.Assert(false); }
#endif
        }
Example #2
0
        internal static void ConfigureWebRequest(WebRequest request,
                                                 IOConnectionInfo ioc)
        {
            if (request == null)
            {
                Debug.Assert(false); return;
            }                                                                // No throw

            IocProperties p = ((ioc != null) ? ioc.Properties : null);

            if (p == null)
            {
                Debug.Assert(false); p = new IocProperties();
            }

            IHasIocProperties ihpReq = (request as IHasIocProperties);

            if (ihpReq != null)
            {
                IocProperties pEx = ihpReq.IOConnectionProperties;
                if (pEx != null)
                {
                    p.CopyTo(pEx);
                }
                else
                {
                    ihpReq.IOConnectionProperties = p.CloneDeep();
                }
            }

            if (IsHttpWebRequest(request))
            {
                // WebDAV support
#if !KeePassUAP
                request.PreAuthenticate = true;                 // Also auth GET
#endif
                if (string.Equals(request.Method, WebRequestMethods.Http.Post,
                                  StrUtil.CaseIgnoreCmp))
                {
                    request.Method = WebRequestMethods.Http.Put;
                }

#if !KeePassUAP
                HttpWebRequest hwr = (request as HttpWebRequest);
                if (hwr != null)
                {
                    string strUA = p.Get(IocKnownProperties.UserAgent);
                    if (!string.IsNullOrEmpty(strUA))
                    {
                        hwr.UserAgent = strUA;
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
#endif
            }
#if !KeePassUAP
            else if (IsFtpWebRequest(request))
            {
                FtpWebRequest fwr = (request as FtpWebRequest);
                if (fwr != null)
                {
                    bool?obPassive = p.GetBool(IocKnownProperties.Passive);
                    if (obPassive.HasValue)
                    {
                        fwr.UsePassive = obPassive.Value;
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }
#endif

#if !KeePassUAP
            // Not implemented and ignored in Mono < 2.10
            try
            {
                request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            }
            catch (NotImplementedException) { }
            catch (Exception) { Debug.Assert(false); }
#endif

            try
            {
                IWebProxy prx;
                if (GetWebProxy(out prx))
                {
                    request.Proxy = prx;
                }
            }
            catch (Exception) { Debug.Assert(false); }

#if !KeePassUAP
            long?olTimeout = p.GetLong(IocKnownProperties.Timeout);
            if (olTimeout.HasValue && (olTimeout.Value >= 0))
            {
                request.Timeout = (int)Math.Min(olTimeout.Value, (long)int.MaxValue);
            }

            bool?ob = p.GetBool(IocKnownProperties.PreAuth);
            if (ob.HasValue)
            {
                request.PreAuthenticate = ob.Value;
            }
#endif

            if (IOConnection.IOWebRequestPre != null)
            {
                IOWebRequestEventArgs e = new IOWebRequestEventArgs(request,
                                                                    ((ioc != null) ? ioc.CloneDeep() : null));
                IOConnection.IOWebRequestPre(null, e);
            }
        }