/// <summary> /// Parses a string with a http request into a structured object. /// If something goes wrong it throws an HttpProtocolException /// </summary> public static HttpRequest Parse(string requestString) { try { int currentMillis = Environment.TickCount; HttpRequest request = new HttpRequest(); logger.Info("(" + request.identifier + ") Parse request: " + requestString); if (String.IsNullOrWhiteSpace(requestString)) { logger.Debug("(" + request.identifier + ") Request to parse is null or whitespace"); return request; } // in the http-protocol lines must end with CR + LF string[] lines = requestString.Split(new string[] { "\r\n" }, StringSplitOptions.None); request.Method = request.GetHttpMethodFromFirstRequestLine(lines[0]); request.URL = request.GetURLFromFirstRequestLine(lines[0]); request.HttpVersion = request.GetHttpVersionFromFirstRequestLine(lines[0]); request.headers = request.ParseHeader(lines); request.CheckAndSetValidity(request); logger.Debug("(" + request.identifier + ") Parsing took " + (Environment.TickCount - currentMillis) + " ms"); return request; } catch (Exception e) { throw new HttpProtocolException("Error parsing a http request", e); } }
private void CheckAndSetValidity(HttpRequest r) { bool valid = r.Method != HttpMethodEnum.None && r.HttpVersion != HttpVersionEnum.None && r.URL != null; r.valid = valid; logger.Debug("(" + identifier + ") Evaluated validity to " + valid); }