Example #1
0
        public void Open_should_not_complete_the_second_call_until_the_first_is_completed(
            [Values(false, true)]
            bool async1,
            [Values(false, true)]
            bool async2)
        {
            var task1IsBlocked   = false;
            var completionSource = new TaskCompletionSource <Stream>();

            _streamFactory.CreateStream(null, CancellationToken.None)
            .ReturnsForAnyArgs(_ => { task1IsBlocked = true; return(completionSource.Task.GetAwaiter().GetResult()); });
            _streamFactory.CreateStreamAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(_ => { task1IsBlocked = true; return(completionSource.Task); });

            Task openTask1;

            if (async1)
            {
                openTask1 = _subject.OpenAsync(CancellationToken.None);
            }
            else
            {
                openTask1 = Task.Run(() => _subject.Open(CancellationToken.None));
            }
            SpinWait.SpinUntil(() => task1IsBlocked);

            Task openTask2;

            if (async2)
            {
                openTask2 = _subject.OpenAsync(CancellationToken.None);
            }
            else
            {
                openTask2 = Task.Run(() => _subject.Open(CancellationToken.None));
            }

            openTask1.IsCompleted.Should().BeFalse();
            openTask2.IsCompleted.Should().BeFalse();
            _subject.Description.Should().BeNull();

            completionSource.SetResult(Substitute.For <Stream>());
            SpinWait.SpinUntil(() => openTask1.IsCompleted);
            SpinWait.SpinUntil(() => openTask2.IsCompleted);
            _subject.Description.Should().NotBeNull();

            _capturedEvents.Next().Should().BeOfType <ConnectionOpeningEvent>();
            _capturedEvents.Next().Should().BeOfType <ConnectionOpenedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
        public async Task Invoke(IDictionary <string, object> environment)
        {
            var request = new OwinRequest(environment);
            Uri uri     = request.Uri;

            // Create a stream for the host and port so we can send the request
            Stream stream = await _streamFactory.CreateStream(uri).ConfigureAwait(continueOnCapturedContext: false);

            var requestWriter = new StreamWriter(stream);

            // Request line
            requestWriter.WriteHttpLine("{0} {1} {2}", request.Method, uri.PathAndQuery, request.Protocol);

            // Write headers
            foreach (var header in request.Headers)
            {
                requestWriter.WriteHttpLine("{0}: {1}", header.Key, request.GetHeader(header.Key));
            }

            // End headers
            requestWriter.WriteHttpLine();

            if (request.Body == null)
            {
                // End request
                requestWriter.WriteHttpLine();
            }

            // Flush buffered content to the stream async
            await requestWriter.FlushAsync().ConfigureAwait(continueOnCapturedContext: false);

            if (request.Body != null)
            {
                // Copy the body to the request
                await request.Body.CopyToAsync(stream).ConfigureAwait(continueOnCapturedContext: false);
            }

            var response = new OwinResponse(environment);

            // Parse the response
            HttpParser.ParseResponse(stream, (protocol, statusCode, reasonPhrase) =>
            {
                response.Protocol     = protocol;
                response.StatusCode   = statusCode;
                response.ReasonPhrase = reasonPhrase;
            },
                                     (key, value) => response.SetHeader(key, value));

            // Set the body to the rest of the stream
            response.Body = stream;
        }
Example #3
0
        /// <summary>
        /// Creates a new non intialized Indirect object
        /// </summary>
        public PDFIndirectObject(IStreamFactory factory)
        {
            if (null == factory)
            {
                throw new ArgumentNullException("factory");
            }

            this._gen     = -1;
            this._number  = -1;
            this._offset  = -1L;
            this._factory = factory;
            this._objdata = factory.CreateStream(null, this);
            this._written = false;
        }
Example #4
0
        // public methods
        public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToken)
        {
            var stream = _wrapped.CreateStream(endPoint, cancellationToken);

            try
            {
                var sslStream          = CreateSslStream(stream);
                var targetHost         = GetTargetHost(endPoint);
                var clientCertificates = new X509CertificateCollection(_settings.ClientCertificates.ToArray());
                sslStream.AuthenticateAsClientAsync(targetHost, clientCertificates, _settings.EnabledSslProtocols, _settings.CheckCertificateRevocation);
                return(sslStream);
            }
            catch
            {
                DisposeStreamIgnoringExceptions(stream);
                throw;
            }
        }
        // public methods
        public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToken)
        {
            var stream = _wrapped.CreateStream(endPoint, cancellationToken);

            try
            {
                var sslStream          = CreateSslStream(stream);
                var targetHost         = GetTargetHost(endPoint);
                var clientCertificates = new X509CertificateCollection(_settings.ClientCertificates.ToArray());
#if NETCORE50 || NETSTANDARD1_5 || NETSTANDARD1_6
                sslStream.AuthenticateAsClientAsync(targetHost, clientCertificates, _settings.EnabledSslProtocols, _settings.CheckCertificateRevocation).GetAwaiter().GetResult();
#else
                sslStream.AuthenticateAsClient(targetHost, clientCertificates, _settings.EnabledSslProtocols, _settings.CheckCertificateRevocation);
#endif
                return(sslStream);
            }
            catch
            {
                DisposeStreamIgnoringExceptions(stream);
                throw;
            }
        }
Example #6
0
 public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToken)
 {
     return(IsUnixEndPoint(endPoint, out var unixEndPoint)
         ? CreateStream(unixEndPoint, cancellationToken)
         : _innerFactory.CreateStream(endPoint, cancellationToken));
 }