private void ParseRequest(string requestText) { var requestLines = CoreParser.Split(requestText, new[] { Environment.NewLine }); if (!requestLines.Any()) { throw new BadRequestException(); } var requestLine = CoreParser.Split(requestLines[0], new char[] { ' ' }); if (requestLine.Length != 3 || requestLine[2].ToLower() != "http/1.1") { throw new BadRequestException(); } this.Method = ParseMethod(requestLine[0]); this.Url = requestLine[1]; this.Path = this.ParsePath(this.Url); this.ParseHeaders(requestLines); this.ParseParameters(); this.ParseFormData(requestLines.Last()); }
private void ParseQuery(string queryString, IDictionary <string, string> dict) { var query = CoreParser.Split(queryString, new char[] { '?' }).Last(); if (!query.Contains('=')) { return; } var queryPairs = CoreParser.Split(query, new char[] { '&' }); foreach (var queryPair in queryPairs) { var queryKvp = CoreParser.Split(queryPair, new char[] { '=' }); if (queryKvp.Length != 2) { continue; } var key = WebUtility.UrlDecode(queryKvp[0]); var value = WebUtility.UrlDecode(queryKvp[1]); dict[key] = value; } }
private void ParseHeaders(string[] requestLines) { var endOfHedears = Array.IndexOf(requestLines, string.Empty); for (int i = 1; i < endOfHedears; i++) { var headerLine = CoreParser.Split(requestLines[i], new char[] { ':' }); if (headerLine.Length != 2) { throw new BadRequestException(); } var key = headerLine[0]; var value = headerLine[1].Trim(); this.Headers.Add(new HttpHeader(key, value)); } if (!this.Headers.ContainsKey("Host")) { throw new BadRequestException(); } }
private string ParsePath(string url) { return(CoreParser.Split(url, new char[] { '?', '#' }).FirstOrDefault()); }