Ejemplo n.º 1
0
        public static HttpResponsePacket ToHttpResponsePacket(this HttpResponseMessage response)
        {
            var packet = new HttpResponsePacket();

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

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

            packet.Version           = response.Version.ToString();
            packet.StatusCode        = (int)response.StatusCode;
            packet.StatusDescription = response.ReasonPhrase;

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

            return(packet);
        }
Ejemplo n.º 2
0
        ///<summary>Adds a header to the response packet. </summary>
        ///<remarks>NOTE: This method does not fold the headers as expected in WebAPI 2's response stream.</remarks>
        private static void AddHeader(this HttpResponsePacket packet, KeyValuePair <string, IEnumerable <string> > hdr)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }

            if (packet.Headers.ContainsKey(hdr.Key))
            {
                ((List <string>)packet.Headers[hdr.Key]).AddRange(hdr.Value);
            }
            else
            {
                packet.Headers.Add(hdr.Key, new List <string>(hdr.Value));
            }
        }
Ejemplo n.º 3
0
        public static HttpResponsePacket Deserialize(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            HttpMessageReader reader = new HttpMessageReader(data);

            HttpResponsePacket response = new HttpResponsePacket();

            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 < 2)
                    {
                        throw new InvalidOperationException("Unable to deserialize data into HttpPacket");
                    }

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

                    response.Version = components[0].Substring(5).Trim();
                    int statusCode = 0;
                    Int32.TryParse(components[1], out statusCode);
                    response.StatusCode = statusCode;

                    if (components.Length > 2)
                    {
                        string statusDescription = components[2].Trim();

                        for (int i = 3; i < components.Length; i++)
                        {
                            //TODO: Convert to a string builder? Is it worth it?
                            statusDescription += (" " + components[i]);
                        }
                        response.StatusDescription = statusDescription;
                    }
                    else
                    {
                        response.StatusDescription = String.Empty;
                    }
                }
                else
                {
                    ParseLineIntoHeaders(text, response.Headers);
                }
            }

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

            response.Content = reader.GetContent();

            return(response);
        }