Пример #1
0
        public void TestThatSecureClientReceivesResponseFromRequestProbeActor()
        {
            var clientConsumer = new TestSecureResponseChannelConsumer();
            var access         = clientConsumer.AfterCompleting(1);
            var unknown        = new UnknownResponseConsumer(access, _output);

            var config =
                Client.Configuration.Secure(
                    _world.Stage,
                    Address.From(Host.Of("webhook.site"), 443, AddressType.None),
                    unknown,
                    false,
                    10,
                    65535,
                    10,
                    65535);

            config.TestInfo(true);

            var requestSender = _world.Stage.ActorFor <IRequestSender>(() => new RequestSenderProbeActor(config, clientConsumer, "1"));

            var get    = "GET /4f0931bb-1c2f-4786-a703-a8b86419c03d HTTP/1.1\nHost: webhook.site\nConnection: close\n\n";
            var buffer = BasicConsumerByteBuffer.Allocate(1, 1000);

            buffer.Put(Encoding.UTF8.GetBytes(get));
            buffer.Flip();

            requestSender.SendRequest(Request.From(buffer));

            Assert.Equal(1, access.ReadFrom <int>("consumeCount"));

            Assert.Contains("HTTP/1.1 404 Not Found", clientConsumer.GetResponses().First());
        }
Пример #2
0
        public void TestThatSecureClientRequestResponse()
        {
            var address = Address.From(Host.Of("www.google.com"), 443, AddressType.None);

            _client = new SecureClientRequestResponseChannel(address, _clientConsumer, _poolSize, 65536, _world.DefaultLogger);

            _clientConsumer.CurrentExpectedResponseLength = 500;
            var access = _clientConsumer.AfterCompleting(1);

            var get    = "GET / HTTP/1.1\nHost: www.google.com\nConnection: close\n\n";
            var buffer = BasicConsumerByteBuffer.Allocate(1, 1000);

            buffer.Put(Encoding.UTF8.GetBytes(get));
            buffer.Flip();
            _client.RequestWith(buffer.ToArray());

            for (int count = 0; count < 100; ++count)
            {
                if (access.TotalWrites > 0)
                {
                    break;
                }
                _client.ProbeChannel();
                Thread.Sleep(100);
            }

            Assert.True(_clientConsumer.GetConsumeCount() > 0);
            Assert.Contains("google.com", _clientConsumer.GetResponses().First());
        }
Пример #3
0
        private void Send(string entity, Headers <ResponseHeader> headers)
        {
            var buffer   = BasicConsumerByteBuffer.Allocate(1, _maxMessageSize);
            var response = Response.Of(Response.ResponseStatus.Ok, Headers, entity);

            _context.RespondWith(response.Into(buffer));
        }
Пример #4
0
        private void SendInitialResponse()
        {
            var response = Response.Of(Response.ResponseStatus.Ok, Headers.Copy());
            var buffer   = BasicConsumerByteBuffer.Allocate(1, _maxMessageSize);

            _context?.RespondWith(response.Into(buffer));
        }
    private IConsumerByteBuffer ConsumerByteBuffer(string message)
    {
        var requestContent = Encoding.UTF8.GetBytes(message);
        var buffer         = BasicConsumerByteBuffer.Allocate(3, requestContent.Length).Put(requestContent);

        buffer.Rewind();
        return(buffer);
    }
Пример #6
0
        public void Send(SseEvent @event)
        {
            var buffer            = BasicConsumerByteBuffer.Allocate(1, _maxMessageSize);
            var entity            = @event.Sendable();
            var withContentLength = Headers.Copy().And(ResponseHeader.WithContentLength(entity));
            var response          = Response.Of(Response.ResponseStatus.Ok, withContentLength, entity);

            _context.RespondWith(response.Into(buffer));
        }
Пример #7
0
 public Context(SocketChannelSelectionProcessorActor parent, Socket clientChannel)
 {
     _parent        = parent;
     _clientChannel = clientChannel;
     _consumer      = parent._provider.RequestChannelConsumer();
     _buffer        = BasicConsumerByteBuffer.Allocate(++_parent._bufferId, _parent._messageBufferSize);
     _id            = $"{++_parent._contextId}";
     _writables     = new Queue <IConsumerByteBuffer>();
 }
Пример #8
0
        public void RespondWith(RequestResponseContext context, object response, bool closeFollowing)
        {
            var textResponse = response.ToString();

            var buffer =
                new BasicConsumerByteBuffer(0, textResponse.Length + 1024)
                .Put(Converters.TextToBytes(textResponse)).Flip();

            RespondWith(context, buffer, closeFollowing);
        }
Пример #9
0
            private IConsumerByteBuffer BufferFor(Response response)
            {
                var size = response.Size;

                if (size < _serverActor._maxMessageSize)
                {
                    return(_serverActor._responseBufferPool.Acquire());
                }

                return(BasicConsumerByteBuffer.Allocate(0, size + 1024));
            }
Пример #10
0
            private IConsumerByteBuffer?BufferFor(Response response)
            {
                var size = response.Size;

                if (size < _serverActor._maxMessageSize)
                {
                    return(_serverActor._responseBufferPool?.Acquire("ServerActor.BasicCompletedBasedResponseCompletes.BufferFor"));
                }

                return(BasicConsumerByteBuffer.Allocate(0, size + 1024));
            }
Пример #11
0
 private void SendInitialResponse()
 {
     try
     {
         var response = Response.Of(ResponseStatus.Ok, Headers.Copy());
         var buffer   = BasicConsumerByteBuffer.Allocate(1, _maxMessageSize);
         _context?.RespondWith(response.Into(buffer));
     }
     catch
     {
         // it's possible that I am being used for an unsubscribe
         // where the client has already disconnected and this
         // attempt will fail; ignore it and return.
     }
 }
        public void Consume <T>(RequestResponseContext <T> context, ReadOnlySequence <byte> buffer)
        {
            var requestPart = buffer.ToArray().BytesToText(0, (int)buffer.Length);

            _requestBuilder.Append(_remaining).Append(requestPart);
            _remaining = string.Empty;
            if (_requestBuilder.Length >= CurrentExpectedRequestLength)
            {
                // assume currentExpectedRequestLength is length of all
                // requests when multiple are received at one time
                var combinedRequests = _requestBuilder.ToString();
                var combinedLength   = combinedRequests.Length;
                _requestBuilder.Clear(); // reuse

                var startIndex   = 0;
                var currentIndex = 0;
                var last         = false;
                while (!last)
                {
                    if (startIndex > combinedRequests.Length)
                    {
                        _remaining = combinedRequests.Substring(currentIndex);
                        return;
                    }

                    var request = combinedRequests.Substring(startIndex, CurrentExpectedRequestLength);
                    currentIndex += CurrentExpectedRequestLength;
                    startIndex    = startIndex + CurrentExpectedRequestLength;

                    Requests.Add(request);

                    var responseBuffer = new BasicConsumerByteBuffer(1, CurrentExpectedRequestLength);
                    context.RespondWith(responseBuffer.Clear().Put(Converters.TextToBytes(request)).Flip()); // echo back

                    last = currentIndex == combinedLength;

                    UntilConsume.WriteUsing("serverConsume", 1);
                }
            }
        }
Пример #13
0
        private void Send(string entity)
        {
            var buffer = BasicConsumerByteBuffer.Allocate(1, _maxMessageSize);

            _context?.RespondWith(buffer.Put(Encoding.UTF8.GetBytes(entity)).Flip());
        }