Ejemplo n.º 1
0
        public void op_Contains_IHttpHeader_whenTrue()
        {
            var item = new HttpHeader("name", "value");
            var obj = new HttpHeaderCollection
                          {
                              item
                          };

            Assert.True(obj.Contains(item));
        }
Ejemplo n.º 2
0
        public void op_Contains_IHttpHeaderNull()
        {
            var obj = new HttpHeaderCollection
                          {
                              new HttpHeader("name", "value")
                          };

            Assert.False(obj.Contains(null));
        }
Ejemplo n.º 3
0
        public void op_Contains_IHttpHeader_whenFalse()
        {
            var obj = new HttpHeaderCollection
                          {
                              new HttpHeader("name", "foo")
                          };

            Assert.False(obj.Contains(new HttpHeader("name", "bar")));
        }
Ejemplo n.º 4
0
        public static HttpRequest Parse(string request)
        {
            string[] requestLines = request.Split("\r\n");

            string[] firstLineComponents = requestLines[0].Split(' ');

            if (firstLineComponents.Length != 3)
            {
                throw new ArgumentException("HTTP request line is invalid", nameof(request));
            }

            if (!Enum.TryParse(firstLineComponents[0], out HttpRequestMethod method))
            {
                throw new ArgumentException("HTTP request method is invalid", nameof(request));
            }

            string url = firstLineComponents[1];

            string[] pathComponents = url.Split('?');

            if (pathComponents.Length > 2)
            {
                throw new ArgumentException("HTTP query is invalid", nameof(request));
            }

            string path = pathComponents[0];

            Dictionary <string, object> queryData = null;

            KeyValuePair <string, object> ParseQueryString(string part)
            {
                string[] keyValuePair = part.Split('=');

                if (keyValuePair.Length != 2)
                {
                    throw new ArgumentException("Query string key/value pair does not contain two part", nameof(part));
                }

                return(new KeyValuePair <string, object>(keyValuePair[0], keyValuePair[1]));
            }

            if (pathComponents.Length == 2)
            {
                string query = pathComponents[1];

                if (query.Contains('#'))
                {
                    query = query.Split('#', 1).Single();
                }

                queryData = new Dictionary <string, object>(query.Split('&').Select(ParseQueryString));
            }

            string protocol = firstLineComponents[2];

            if (protocol != Constants.HttpProtocolVersion)
            {
                throw new ArgumentException("HTTP protocol version is invalid", nameof(request));
            }

            HttpHeaderCollection headers = new HttpHeaderCollection();

            int lastLineIndex = 0;

            for (int lineIndex = 1; lineIndex < requestLines.Length; ++lineIndex)
            {
                lastLineIndex = lineIndex;

                string line = requestLines[lineIndex];

                if (line == string.Empty)
                {
                    break;
                }

                string[] keyValuePair = line.Split(": ", 2);

                if (keyValuePair.Length != 2)
                {
                    throw new ArgumentException("A header is invalid", nameof(request));
                }

                headers.Add(keyValuePair[0], keyValuePair[1]);
            }

            if (!headers.Contains("Host"))
            {
                throw new ArgumentException("Host header not found", nameof(request));
            }

            Dictionary <string, object> formData = null;

            {
                int formDataLineIndex = lastLineIndex + 1;

                if (requestLines.Length > formDataLineIndex)
                {
                    string formDataLine = requestLines[formDataLineIndex];

                    if (formDataLine != string.Empty)
                    {
                        formData = new Dictionary <string, object>(formDataLine.Split('&')
                                                                   .Select(ParseQueryString));
                    }
                }
            }

            HttpCookieCollection httpCookieCollection = new HttpCookieCollection();

            if (headers.TryGetHeader("Cookie", out HttpHeader header))
            {
                foreach (string[] parts in header.Value.Split("; ").Select(cookie => cookie.Split('=')))
                {
                    httpCookieCollection.Add(new HttpCookie(parts[0], parts[1]));
                }
            }

            return(new HttpRequest(path, url, formData, queryData, method, headers, httpCookieCollection));
        }