Esempio n. 1
0
        private bool Receive()
        {
            SupportedProtocols protocol = (base.CurrentRequest.ProtocolHandler != 0) ? base.CurrentRequest.ProtocolHandler : HTTPProtocolFactory.GetProtocolFromUri(base.CurrentRequest.CurrentUri);

            base.CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, base.CurrentRequest, Stream, base.CurrentRequest.UseStreaming, isFromCache: false);
            if (!base.CurrentRequest.Response.Receive())
            {
                base.CurrentRequest.Response = null;
                return(false);
            }
            if (base.CurrentRequest.Response.StatusCode == 304)
            {
                if (base.CurrentRequest.IsRedirected)
                {
                    if (!LoadFromCache(base.CurrentRequest.RedirectUri))
                    {
                        LoadFromCache(base.CurrentRequest.Uri);
                    }
                }
                else
                {
                    LoadFromCache(base.CurrentRequest.Uri);
                }
            }
            return(true);
        }
Esempio n. 2
0
        private void Connect()
        {
            Uri uri = (!base.CurrentRequest.HasProxy) ? base.CurrentRequest.CurrentUri : base.CurrentRequest.Proxy.Address;

            if (Client == null)
            {
                Client = new TcpClient();
            }
            if (!Client.Connected)
            {
                Client.ConnectTimeout = base.CurrentRequest.ConnectTimeout;
                Client.Connect(uri.Host, uri.Port);
                if (HTTPManager.Logger.Level <= Loglevels.Information)
                {
                    HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host + ":" + uri.Port.ToString());
                }
            }
            else if (HTTPManager.Logger.Level <= Loglevels.Information)
            {
                HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host + ":" + uri.Port.ToString());
            }
            lock (HTTPManager.Locker)
            {
                base.StartTime = DateTime.UtcNow;
            }
            if (Stream == null)
            {
                bool flag = HTTPProtocolFactory.IsSecureProtocol(base.CurrentRequest.CurrentUri);
                if (base.HasProxy && (!base.Proxy.IsTransparent || (flag && base.Proxy.NonTransparentForHTTPS)))
                {
                    Stream = Client.GetStream();
                    BinaryWriter binaryWriter = new BinaryWriter(Stream);
                    bool         flag2;
                    do
                    {
                        flag2 = false;
                        binaryWriter.SendAsASCII($"CONNECT {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port} HTTP/1.1");
                        binaryWriter.Write(HTTPRequest.EOL);
                        binaryWriter.SendAsASCII("Proxy-Connection: Keep-Alive");
                        binaryWriter.Write(HTTPRequest.EOL);
                        binaryWriter.SendAsASCII("Connection: Keep-Alive");
                        binaryWriter.Write(HTTPRequest.EOL);
                        binaryWriter.SendAsASCII($"Host: {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port}");
                        binaryWriter.Write(HTTPRequest.EOL);
                        if (base.HasProxy && base.Proxy.Credentials != null)
                        {
                            switch (base.Proxy.Credentials.Type)
                            {
                            case AuthenticationTypes.Basic:
                                binaryWriter.Write(string.Format("Proxy-Authorization: {0}", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(base.Proxy.Credentials.UserName + ":" + base.Proxy.Credentials.Password))).GetASCIIBytes());
                                binaryWriter.Write(HTTPRequest.EOL);
                                break;

                            case AuthenticationTypes.Unknown:
                            case AuthenticationTypes.Digest:
                            {
                                Digest digest = DigestStore.Get(base.Proxy.Address);
                                if (digest != null)
                                {
                                    string text = digest.GenerateResponseHeader(base.CurrentRequest, base.Proxy.Credentials);
                                    if (!string.IsNullOrEmpty(text))
                                    {
                                        binaryWriter.Write($"Proxy-Authorization: {text}".GetASCIIBytes());
                                        binaryWriter.Write(HTTPRequest.EOL);
                                    }
                                }
                                break;
                            }
                            }
                        }
                        binaryWriter.Write(HTTPRequest.EOL);
                        binaryWriter.Flush();
                        base.CurrentRequest.ProxyResponse = new HTTPResponse(base.CurrentRequest, Stream, isStreamed: false, isFromCache: false);
                        if (!base.CurrentRequest.ProxyResponse.Receive())
                        {
                            throw new Exception("Connection to the Proxy Server failed!");
                        }
                        if (HTTPManager.Logger.Level <= Loglevels.Information)
                        {
                            HTTPManager.Logger.Information("HTTPConnection", "Proxy returned - status code: " + base.CurrentRequest.ProxyResponse.StatusCode + " message: " + base.CurrentRequest.ProxyResponse.Message);
                        }
                        int statusCode = base.CurrentRequest.ProxyResponse.StatusCode;
                        if (statusCode == 407)
                        {
                            string text2 = DigestStore.FindBest(base.CurrentRequest.ProxyResponse.GetHeaderValues("proxy-authenticate"));
                            if (!string.IsNullOrEmpty(text2))
                            {
                                Digest orCreate = DigestStore.GetOrCreate(base.Proxy.Address);
                                orCreate.ParseChallange(text2);
                                if (base.Proxy.Credentials != null && orCreate.IsUriProtected(base.Proxy.Address) && (!base.CurrentRequest.HasHeader("Proxy-Authorization") || orCreate.Stale))
                                {
                                    flag2 = true;
                                }
                            }
                        }
                        else if (!base.CurrentRequest.ProxyResponse.IsSuccess)
                        {
                            throw new Exception($"Proxy returned Status Code: \"{base.CurrentRequest.ProxyResponse.StatusCode}\", Message: \"{base.CurrentRequest.ProxyResponse.Message}\" and Response: {base.CurrentRequest.ProxyResponse.DataAsText}");
                        }
                    }while (flag2);
                }
                if (flag)
                {
                    if (base.CurrentRequest.UseAlternateSSL)
                    {
                        TlsClientProtocol tlsClientProtocol = new TlsClientProtocol(Client.GetStream(), new SecureRandom());
                        List <string>     list = new List <string>(1);
                        list.Add(base.CurrentRequest.CurrentUri.Host);
                        TlsClientProtocol tlsClientProtocol2 = tlsClientProtocol;
                        Uri    currentUri = base.CurrentRequest.CurrentUri;
                        object verifyer;
                        if (base.CurrentRequest.CustomCertificateVerifyer == null)
                        {
                            ICertificateVerifyer certificateVerifyer = new AlwaysValidVerifyer();
                            verifyer = certificateVerifyer;
                        }
                        else
                        {
                            verifyer = base.CurrentRequest.CustomCertificateVerifyer;
                        }
                        tlsClientProtocol2.Connect(new LegacyTlsClient(currentUri, (ICertificateVerifyer)verifyer, base.CurrentRequest.CustomClientCredentialsProvider, list));
                        Stream = tlsClientProtocol.Stream;
                    }
                    else
                    {
                        SslStream sslStream = new SslStream(Client.GetStream(), leaveInnerStreamOpen: false, (object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) => base.CurrentRequest.CallCustomCertificationValidator(cert, chain));
                        if (!sslStream.IsAuthenticated)
                        {
                            sslStream.AuthenticateAsClient(base.CurrentRequest.CurrentUri.Host);
                        }
                        Stream = sslStream;
                    }
                }
                else
                {
                    Stream = Client.GetStream();
                }
            }
        }