Example #1
0
        public Request Parse(string requestData)
        {
            var lines       = requestData.Split(Environment.NewLine);
            var requestLine = lines.First();
            var match       = RequestLineRegex.Match(requestLine);

            if (!match.Success)
            {
                throw new ApplicationException("Unable to process request");
            }
            var method = ParseHelper.ParseMethod(match.Groups[1].Value);

            if (method == Method.None)
            {
                throw new ApplicationException($"Unable to match {match.Groups[1].Value} to an available method");
            }

            var path            = match.Groups[2].Value;
            var query           = match.Groups[3].Value;
            var queryDictionary = new Dictionary <string, string>();

            var qmatch = QueryRegex.Matches(query);

            foreach (var qm in qmatch)
            {
                //var KeyVauePairmatch = qm.ToString();
                var kvp = qm.ToString();

                var KVPParsed = QueryKeyValuePair.Match(qm.ToString());

                var key   = KVPParsed.Groups[1].ToString();
                var value = KVPParsed.Groups[2].ToString();
                queryDictionary.Add(key, value);
            }

            ServerInterfaces.QueryCollection queryColl = new ServerInterfaces.QueryCollection(queryDictionary);
            // to-do: map headers, querystring, body, etc...

            var headerLines = lines.Skip(1).TakeWhile(line => !string.IsNullOrEmpty(line));
            var headerDict  = new Dictionary <string, string>();

            foreach (var line in headerLines)
            {
                var hmatch = HeaderRegex.Match(line);
                if (!hmatch.Success)
                {
                    throw new ApplicationException($"Unable to process header line {line}");
                }
                var headerName  = hmatch.Groups[1].Value;
                var headerValue = hmatch.Groups[2].Value;
                headerDict.Add(headerName, headerValue);
            }

            HeaderCollection headers = new HeaderCollection(headerDict);

            var bodyLines = lines.SkipWhile(line => !string.IsNullOrEmpty(line)).Skip(1);
            var body      = string.Join(Environment.NewLine, bodyLines);

            return(new Request
            {
                Method = method,
                Path = path,
                Query = queryColl.ToString(),
                Headers = headers,
                Body = body
            });
        }
Example #2
0
        public Request Parse(string requestData)
        {
            var lines       = requestData.Split(Environment.NewLine);
            var requestLine = lines.First();
            var match       = RequestLineRegex.Match(requestLine);

            if (!match.Success)
            {
                throw new ApplicationException("Unable to process request");
            }
            var method = ParseHelper.ParseMethod(match.Groups[1].Value);

            if (method == Method.None)
            {
                throw new ApplicationException($"Unable to match {match.Groups[1].Value} to an available method");
            }

            //PATH
            var path = match.Groups[2].Value;

            //QUERY with Tuple

            var queryLine  = match.Groups[3].Value;
            var queryRegex = @"([^?=&]+)(=([^&]*))?";

            var             queryTuple = new List <Tuple <string, string> >();
            MatchCollection matches    = Regex.Matches(queryLine, queryRegex);

            foreach (Match queryMatch in matches)
            {
                var queryKey   = queryMatch.Groups[1].Value;
                var queryValue = queryMatch.Groups[2].Value;

                queryTuple.Add(new Tuple <string, string>(queryKey, queryValue));
            }
            QueryCollection queries = new QueryCollection(queryTuple);

            //QUERY with Dictionary

            //var query = match.Groups[3].Value;

            //var result = Regex.Matches(query, "([^?=&]+)(=([^&]*))?")
            //    .Cast<Match>()
            //    .ToDictionary(x => x.Groups[1].Value, x => x.Groups[3].Value);

            //QueryCollection queries = new QueryCollection(result);

            //HEADER
            var headerLines = lines.Skip(1).TakeWhile(line => !string.IsNullOrEmpty(line));
            var headerDict  = new Dictionary <string, string>();

            foreach (var line in headerLines)
            {
                var hmatch = HeaderRegex.Match(line);
                if (!hmatch.Success)
                {
                    throw new ApplicationException($"Unable to process header line {line}");
                }
                var headerName  = hmatch.Groups[1].Value;
                var headerValue = hmatch.Groups[2].Value;
                headerDict.Add(headerName, headerValue);
            }

            HeaderCollection headers = new HeaderCollection(headerDict);

            var bodyLines = lines.SkipWhile(line => !string.IsNullOrEmpty(line)).Skip(1);
            var body      = string.Join(Environment.NewLine, bodyLines);

            return(new Request
            {
                Method = method,
                Path = path,
                Queries = queries,
                Headers = headers,
                Body = body
            });
        }
        public Request Parse(string requestData)
        {
            var lines       = requestData.Split(Environment.NewLine);
            var requestLine = lines.First();
            var match       = RequestLineRegex.Match(requestLine);

            if (!match.Success)
            {
                throw new ApplicationException("Unable to process request");
            }
            var method = ParseHelper.ParseMethod(match.Groups[1].Value);

            if (method == Method.None)
            {
                throw new ApplicationException($"Unable to match {match.Groups[1].Value} to an available method");
            }

            var path  = match.Groups[2].Value;
            var query = match.Groups[3].Value;
            // to-do: map headers, querystring, body, etc...

            var headerLines = lines.Skip(1).TakeWhile(line => !string.IsNullOrEmpty(line));
            var headerDict  = new Dictionary <string, string>();

            foreach (var line in headerLines)
            {
                var hmatch = HeaderRegex.Match(line);
                if (!hmatch.Success)
                {
                    throw new ApplicationException($"Unable to process header line {line}");
                }
                var headerName  = hmatch.Groups[1].Value;
                var headerValue = hmatch.Groups[2].Value;
                headerDict.Add(headerName, headerValue);
            }

            HeaderCollection headers = new HeaderCollection(headerDict);

            var queryKvp = new List <KeyValuePair <string, string> >();

            if (!string.IsNullOrEmpty(query))
            {
                var queries = query.Split('&');
                foreach (var q in queries)
                {
                    var key   = q.Split('=')[0];
                    var value = q.Split('=')[1];
                    KeyValuePair <string, string> kvp = new KeyValuePair <string, string>(key, value);
                    queryKvp.Add(kvp);
                }
            }
            QueryCollection queryCol = new QueryCollection(queryKvp);

            var bodyLines = lines.SkipWhile(line => !string.IsNullOrEmpty(line)).Skip(1);
            var body      = string.Join(Environment.NewLine, bodyLines);

            return(new Request
            {
                Method = method,
                Path = path,
                Query = queryCol,
                Headers = headers,
                Body = body
            });
        }
Example #4
0
        public Request Parse(string requestData)
        {
            var lines       = requestData.Split(Environment.NewLine);
            var requestLine = lines.First();
            var match       = RequestLineRegex.Match(requestLine);

            if (!match.Success)
            {
                throw new ApplicationException("Unable to process request");
            }
            var method = ParseHelper.ParseMethod(match.Groups[1].Value);

            if (method == Method.None)
            {
                throw new ApplicationException($"Unable to match {match.Groups[1].Value} to an available method");
            }

            var path = match.Groups[2].Value;

            var queryLine = lines.First();
            var queryPair = new List <KeyValuePair <string, string> >();

            string          patternQuery = @"(\?|\&)([^=]+)\=([^&|\s]+)";
            MatchCollection matches      = Regex.Matches(queryLine, patternQuery);

            foreach (Match qmatch in matches)
            {
                var queryName  = qmatch.Groups[2].Value;
                var queryValue = qmatch.Groups[3].Value;
                queryPair.Add(new KeyValuePair <string, string>(queryName, queryValue));
            }

            QueryCollection query = new QueryCollection(queryPair);

            var headerLines = lines.Skip(1).TakeWhile(line => !string.IsNullOrEmpty(line));
            var headerDict  = new Dictionary <string, string>();

            foreach (var line in headerLines)
            {
                var hmatch = HeaderRegex.Match(line);
                if (!hmatch.Success)
                {
                    throw new ApplicationException($"Unable to process header line {line}");
                }
                var headerName  = hmatch.Groups[1].Value;
                var headerValue = hmatch.Groups[2].Value;
                headerDict.Add(headerName, headerValue);
            }

            HeaderCollection headers = new HeaderCollection(headerDict);

            var bodyLines = lines.SkipWhile(line => !string.IsNullOrEmpty(line)).Skip(1);
            var body      = string.Join(Environment.NewLine, bodyLines);

            return(new Request
            {
                Method = method,
                Path = path,
                Query = query,
                Headers = headers,
                Body = body
            });
        }