/// <summary> /// Parses a HttpResponse string into a structured object. /// If anything goes wrong a HttpProtocolException is thrown /// </summary> public static HttpResponse Parse(String ResponseString) { try { int currentMillis = Environment.TickCount; HttpResponse Response = new HttpResponse(); logger.Info("(" + Response.identifier + ") Parse response: " + ResponseString); if (ResponseString == null || String.IsNullOrWhiteSpace(ResponseString.Trim())) { logger.Debug("(" + Response.identifier + ") Response was null or whitespace"); return Response; } // in the http-protocol lines must end with CR + LF string[] lines = ResponseString.Split(new string[] { "\r\n" }, StringSplitOptions.None); Response.HttpVersion = Response.ParseHttpVersionFromFirstRequestLine(lines[0]); Response.StatusCode = Response.ParseStatusCodeFromFirstRequestLine(lines[0]); Response.headers = Response.ParseHeader(lines); Response.Body = Response.ParseBody(ResponseString); // set the response as valid if everything seems ok Response.CheckAndSetValidity(Response); logger.Debug("(" + Response.identifier + ") Parsing took " + (Environment.TickCount - currentMillis) + " ms"); return Response; } catch (Exception e) { logger.Error("Parse Respone Error", e); throw new HttpProtocolException("Error parsing a http response", e); } }
private void CheckAndSetValidity(HttpResponse r) { bool valid = r.HttpVersion != HttpVersionEnum.None && r.StatusCode != HttpStatusCodeEnum.None; r.valid = valid; logger.Debug("(" + identifier + ") Evaluated validity to " + valid); }