示例#1
0
            public void publishes_partition_eof_on_each_stream_eof()
            {
                var messages =
                    HandledMessages.Where(
                        v =>
                        v is EventReaderSubscriptionMessage.CommittedEventReceived ||
                        v is EventReaderSubscriptionMessage.PartitionEofReached).ToList();

                Assert.AreEqual(9, messages.Count);
                Assert.IsAssignableFrom <EventReaderSubscriptionMessage.PartitionEofReached>(messages[2]);
                Assert.AreEqual("test-stream", ((EventReaderSubscriptionMessage.PartitionEofReached)messages[2]).Partition);
                Assert.IsAssignableFrom <EventReaderSubscriptionMessage.PartitionEofReached>(messages[5]);
                Assert.AreEqual("test-stream2", ((EventReaderSubscriptionMessage.PartitionEofReached)messages[5]).Partition);
                Assert.IsAssignableFrom <EventReaderSubscriptionMessage.PartitionEofReached>(messages[8]);
                Assert.AreEqual("test-stream3", ((EventReaderSubscriptionMessage.PartitionEofReached)messages[8]).Partition);
            }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        private async void HandleRequest(TcpClient client)
        {
            try
            {
                using (client)
                    using (NetworkStream clientNs = client.GetStream())
                        using (MemoryStream clienMs = new MemoryStream())
                        {
                            var buffer = new byte[BufferSize];
                            int bytesRead;
                            Thread.Sleep(5);
                            if (clientNs.CanRead)
                            {
                                if (clientNs.DataAvailable)
                                {
                                    do
                                    {
                                        bytesRead = clientNs.Read(buffer, 0, buffer.Length);
                                        clienMs.Write(buffer, 0, bytesRead);
                                    } while (clientNs.DataAvailable);
                                }
                            }
                            try
                            {
                                HttpRequest request = HttpRequest.Parse(clienMs.GetBuffer());
                                dispatcher.Invoke(() => AddToLog(request));
                                HttpResponse response = null;
                                if (!AccessAuthentication || Authenticated(request))
                                {
                                    FilterUser(request);
                                    if (FilterContent && request.AsksForImage())
                                    {
                                        response = HttpResponse.GetPlaceholderImageResponse(PlaceholderImage);
                                        clientNs.Write(response.ToBytes, 0, response.ToBytes.Length);
                                    }
                                    if (ProxyCache.Where(x => x.RequestLine == request.RequestLine && x.Valid(CacheTimeOutSec)).Count() > 0 && (CheckChangeContent || HandledMessages.Where(x => x.FirstLine == request.FirstLine).Count() > FrequentUseCount))
                                    {
                                        response = ProxyCache.FirstOrDefault(x => x.RequestLine == request.RequestLine && x.Valid(CacheTimeOutSec)).ToResponse();
                                        clientNs.Write(response.ToBytes, 0, response.ToBytes.Length);
                                    }
                                    if (response == null)
                                    {
                                        try
                                        {
                                            using (TcpClient serverClient = new TcpClient(request.GetRequestedHost(), 80))
                                                using (NetworkStream serverStream = serverClient.GetStream())
                                                {
                                                    serverClient.SendTimeout    = 500;
                                                    serverClient.ReceiveTimeout = 1000;
                                                    var serverBuffer = new byte[BufferSize];
                                                    using (MemoryStream ms = new MemoryStream())
                                                    {
                                                        await serverStream.WriteAsync(request.ToBytes, 0, request.ToBytes.Length);

                                                        int numBytesRead;
                                                        try
                                                        {
                                                            while ((numBytesRead = serverStream.Read(serverBuffer, 0, serverBuffer.Length)) > 0)
                                                            {
                                                                clientNs.Write(serverBuffer, 0, numBytesRead);
                                                                ms.Write(serverBuffer, 0, numBytesRead);
                                                            }
                                                        }
                                                        catch { }
                                                        try
                                                        {
                                                            response = HttpResponse.Parse(ms.GetBuffer());
                                                            if (IsCacheable(request) && (CheckChangeContent || HandledMessages.Where(x => x.FirstLine == request.FirstLine).Count() > FrequentUseCount))
                                                            {
                                                                ProxyCache.Add(new CacheItem(request, response));
                                                            }
                                                        }
                                                        catch
                                                        {
                                                            Debug.WriteLine("Response not parsable");
                                                        }
                                                    }
                                                }
                                        }
                                        catch (SocketException)
                                        {
                                            clientNs.Write(HttpResponse.Get404Response().ToBytes, 0, HttpResponse.Get400Response().ToBytes.Length);
                                        }
                                    }
                                }
                                else
                                {
                                    response = HttpResponse.Get407Response();
                                    clientNs.Write(response.ToBytes, 0, response.ToBytes.Length);
                                }
                                dispatcher.Invoke(() => AddToLog(response));
                            }
                            catch (FormatException)
                            {
                                clientNs.Write(HttpResponse.Get400Response().ToBytes, 0, HttpResponse.Get400Response().ToBytes.Length);
                            }
                            catch
                            {
                                clientNs.Write(HttpResponse.Get500Response().ToBytes, 0, HttpResponse.Get400Response().ToBytes.Length);
                            }
                        }
            }
            catch { Debug.WriteLine("Error while handling incomming request"); }
        }