Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
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);
        }