Beispiel #1
0
        ///<summary>Adds a header to the request packet. </summary>
        ///<remarks>NOTE: This method folds the headers as expected in WebAPI 2's Request.Header object.</remarks>
        private static void AddHeader(this HttpRequestPacket packet, KeyValuePair <string, IEnumerable <string> > hdr)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }

            if (packet.Headers.ContainsKey(hdr.Key))
            {
                var headerValue = ((List <string>)packet.Headers[hdr.Key])[0];
                if (String.IsNullOrEmpty(headerValue))
                {
                    headerValue = String.Join(", ", hdr.Value);
                }
                else
                {
                    headerValue = headerValue + ", " + String.Join(", ", hdr.Value);
                }
                ((List <string>)packet.Headers[hdr.Key])[0] = headerValue;
            }
            else
            {
                packet.Headers.Add(hdr.Key, new List <string>()
                {
                    String.Join(", ", hdr.Value)
                });
            }
        }
Beispiel #2
0
        public static HttpRequestPacket ToHttpRequestPacket(this HttpRequestMessage request)
        {
            var packet = new HttpRequestPacket();

            foreach (var hdr in request.Headers)
            {
                packet.AddHeader(hdr);
            }

            if (request.Content != null)
            {
                foreach (var hdr in request.Content.Headers)
                {
                    packet.AddHeader(hdr);
                }
            }

            packet.Method   = request.Method.Method;
            packet.Version  = request.Version.ToString();
            packet.Resource = request.RequestUri.IsAbsoluteUri ? request.RequestUri.PathAndQuery : request.RequestUri.OriginalString;

            if (request.Content != null)
            {
                packet.Content = request.Content.ReadAsByteArrayAsync().Result;
            }

            return(packet);
        }
        public static HttpRequestPacket Deserialize(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("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);
        }