コード例 #1
0
ファイル: HttpRequestPacket.cs プロジェクト: bestwpw/RestBus
        public static HttpRequestPacket Deserialize(byte[] data)
        {
            if (data == null) throw new ArgumentNullException("data");

            //if (SerializeAsBson)
            //{
            //    return CommonUtils.DeserializeFromBson<HttpRequestPacket>(data);
            //}

            HttpMessageReader reader = new HttpMessageReader(data);

            HttpRequestPacket request = new HttpRequestPacket();

            bool isFirstLine = true;
            string text;
            while ((text = reader.NextLine()) != null)
            {
                if (isFirstLine)
                {
                    isFirstLine = false;
                    string[] components = text.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);

                    if (components.Length < 3)
                    {
                        throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
                    }

                    if (!components[components.Length - 1].StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase) || components[components.Length - 1].Length <= 5 )
                    {
                        throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
                    }

                    request.Version = components[components.Length - 1].Substring(5).Trim();
                    request.Method = components[0].ToUpperInvariant().Trim();

                    string resource = components[1];
                    for (int i = 2; i < components.Length - 1; i++)
                    {
                        //TODO: Should I convert this to a string builder. Is it worth it?
                        resource += (" " + components[i]);
                    }

                    request.Resource = resource;
                }
                else
                {
                    ParseLineIntoHeaders(text, request.Headers);
                }
            }

            if (isFirstLine || !reader.IsContentReady)
            {
                throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
            }

            request.Content = reader.GetContent();

            return request;

        }
コード例 #2
0
        private bool TryGetRequest(SharedQueue<BasicDeliverEventArgs> queue, out HttpRequestPacket request, out IBasicProperties properties)
        {
            object obj;
            BasicDeliverEventArgs evt;
            request = null;
            properties = null;

            obj = queue.DequeueNoWait(null);
            if (obj != null)
            {
                 evt = (BasicDeliverEventArgs)obj;
            }
            else
            {
                return false;
            }

            //Get message properties
            properties = evt.BasicProperties;

            //Deserialize message
            bool wasDeserialized = true;

            try
            {
                request = HttpRequestPacket.Deserialize(evt.Body);

                //Add/Update Content-Length Header
                request.Headers["Content-Length"] = new string[] { (request.Content == null ? 0 : request.Content.Length).ToString() };

                //Add/Update Subscriber-Id header
                request.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { this.subscriberId };

            }
            catch
            {

                wasDeserialized = false;
            }

            //Ack or reject message
            if (evt.ConsumerTag == workConsumer.ConsumerTag)
            {
                if (wasDeserialized)
                {
                    workConsumer.Model.BasicAck(evt.DeliveryTag, false);
                    return true;
                }
                else
                {
                    workConsumer.Model.BasicReject(evt.DeliveryTag, false);
                    return false;
                }
            }
            else if (evt.ConsumerTag == subscriberConsumer.ConsumerTag)
            {
                if (wasDeserialized)
                {
                    subscriberConsumer.Model.BasicAck(evt.DeliveryTag, false);
                    return true;
                }
                else
                {
                    subscriberConsumer.Model.BasicReject(evt.DeliveryTag, false);
                    return false;
                }
            }
            else
            {
                throw new InvalidOperationException("Message was dequeued by an unexpected/unknown consumer");
            }
        }
コード例 #3
0
ファイル: RestBusSubscriber.cs プロジェクト: bestwpw/RestBus
        private bool TryGetRequest(ConcurrentQueueingConsumer consumer, out HttpRequestPacket request, out MessageDispatch dispatch)
        {
            request = null;
            dispatch = null;

            BasicDeliverEventArgs item;
            if (!consumer.TryInstantDequeue(out item, throwIfClosed: false))
            {
                return false;
            }

            //TODO: Pool MessageDispatch
            //Get message 
            dispatch = new MessageDispatch { Consumer = consumer, Delivery = item };

            //Deserialize message
            bool wasDeserialized = true;

            try
            {
                request = HttpRequestPacket.Deserialize(item.Body);
            }
            catch
            {
                wasDeserialized = false;
            }

            if (wasDeserialized)
            {
                //Add/Update Subscriber-Id header
                request.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = this.subscriberIdHeader;

                //Add redelivered header if item was redelivered.
                if (item.Redelivered)
                {
                    request.Headers[Common.Shared.REDELIVERED_HEADER] = TRUE_STRING_ARRAY;
                }
            }
            //Reject message if deserialization failed.
            else if (!wasDeserialized && Settings.AckBehavior != SubscriberAckBehavior.Automatic )
            {
                consumer.Model.BasicReject(item.DeliveryTag, false);
                return false;
            }

            return true;

        }
コード例 #4
0
ファイル: HttpRequestPacket.cs プロジェクト: tuga1975/RestBus
        public static HttpRequestPacket Deserialize(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            //if (SerializeAsBson)
            //{
            //    return CommonUtils.DeserializeFromBson<HttpRequestPacket>(data);
            //}

            HttpMessageReader reader = new HttpMessageReader(data);

            HttpRequestPacket request = new HttpRequestPacket();

            bool   isFirstLine = true;
            string text;

            while ((text = reader.NextLine()) != null)
            {
                if (isFirstLine)
                {
                    isFirstLine = false;
                    string[] components = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (components.Length < 3)
                    {
                        throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
                    }

                    if (!components[components.Length - 1].StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase) || components[components.Length - 1].Length <= 5)
                    {
                        throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
                    }

                    request.Version = components[components.Length - 1].Substring(5).Trim();
                    request.Method  = components[0].ToUpperInvariant().Trim();

                    string resource = components[1];
                    for (int i = 2; i < components.Length - 1; i++)
                    {
                        //TODO: Should I convert this to a string builder. Is it worth it?
                        resource += (" " + components[i]);
                    }

                    request.Resource = resource;
                }
                else
                {
                    ParseLineIntoHeaders(text, request.Headers);
                }
            }

            if (isFirstLine || !reader.IsContentReady)
            {
                throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
            }

            request.Content = reader.GetContent();

            return(request);
        }
コード例 #5
0
ファイル: RestBusHost.cs プロジェクト: bestwpw/RestBus
        private static bool TryGetHttpRequestMessage(HttpRequestPacket packet, string virtualPath, string hostname, out HttpRequestMessage request)
        {
            try
            {
                request = new HttpRequestMessage
                {
                    Content = packet.Content == null ? _emptyByteArrayContent : new ByteArrayContent(packet.Content),
                    Version = packet.Version == "1.1" ? VERSION_1_1 : new Version(packet.Version),
                    Method = new HttpMethod(packet.Method ?? "GET"),
                    RequestUri = packet.BuildUri(virtualPath, hostname)
                };

                packet.PopulateHeaders(request.Content.Headers, request.Headers);
            }
            catch
            {
                request = null;
                return false;
            }

            return true;
        }