Example #1
0
        bool CreateStream(HttpWebRequest request)
        {
            try {
                NetworkStream serverStream = new NetworkStream(socket, false);

                if (request.Address.Scheme == Uri.UriSchemeHttps)
                {
#if SECURITY_DEP
                    ssl = true;
                    EnsureSSLStreamAvailable(request);
                    if (!reused || nstream == null || tlsStream == null)
                    {
                        byte [] buffer = null;
                        if (sPoint.UseConnect)
                        {
                            bool ok = CreateTunnel(request, sPoint.Address, serverStream, out buffer);
                            if (!ok)
                            {
                                return(false);
                            }
                        }
                        tlsStream = new MonoTlsStream(request, serverStream);
                        nstream   = tlsStream.CreateStream(buffer);
                    }
                    // we also need to set ServicePoint.Certificate
                    // and ServicePoint.ClientCertificate but this can
                    // only be done later (after handshake - which is
                    // done only after a read operation).
#else
                    throw new NotSupportedException();
#endif
                }
                else
                {
                    ssl     = false;
                    nstream = serverStream;
                }
            } catch (Exception ex) {
                if (tlsStream != null)
                {
                    status = tlsStream.ExceptionStatus;
                }
                else if (!request.Aborted)
                {
                    status = WebExceptionStatus.ConnectFailure;
                }
                return(false);
            }

            return(true);
        }
Example #2
0
        async Task <bool> CreateStream(WebOperation operation, bool reused, CancellationToken cancellationToken)
        {
#if MONO_WEB_DEBUG
            var requestID = ++nextRequestID;
#else
            var requestID = 0;
#endif

            try {
                var stream = new NetworkStream(socket, false);

                Debug($"WC CREATE STREAM: Cnc={ID} {requestID} {reused} socket={socket.ID}");

                if (operation.Request.Address.Scheme == Uri.UriSchemeHttps)
                {
                    if (!reused || monoTlsStream == null)
                    {
                        if (ServicePoint.UseConnect)
                        {
                            if (tunnel == null)
                            {
                                tunnel = new WebConnectionTunnel(operation.Request, ServicePoint.Address);
                            }
                            await tunnel.Initialize(stream, cancellationToken).ConfigureAwait(false);

                            if (!tunnel.Success)
                            {
                                return(false);
                            }
                        }
                        monoTlsStream = new MonoTlsStream(operation.Request, stream);
                        networkStream = await monoTlsStream.CreateStream(tunnel, cancellationToken).ConfigureAwait(false);
                    }
                    return(true);
                }

                networkStream = stream;
                return(true);
            } catch (Exception ex) {
                ex = HttpWebRequest.FlattenException(ex);
                Debug($"WC CREATE STREAM EX: Cnc={ID} {requestID} {operation.Aborted} - {ex.Message}");
                if (operation.Aborted || monoTlsStream == null)
                {
                    throw GetException(WebExceptionStatus.ConnectFailure, ex);
                }
                throw GetException(monoTlsStream.ExceptionStatus, ex);
            } finally {
                Debug($"WC CREATE STREAM DONE: Cnc={ID} {requestID}");
            }
        }