/**************************************************************************/ private void ProcessResponseHttpHeaders(HttpWebRequest req, HttpWebResponse res) { foreach (string HttpHeaderName in res.Headers) { if (HttpHeaderName.ToLower().Equals("date")) { string DateString = res.GetResponseHeader(HttpHeaderName); this.DateServer = MacroscopeDateTools.ParseHttpDate(HeaderField: HttpHeaderName, DateString: DateString); } if (HttpHeaderName.ToLower().Equals("last-modified")) { string DateString = res.GetResponseHeader(HttpHeaderName); this.DateModified = MacroscopeDateTools.ParseHttpDate(HeaderField: HttpHeaderName, DateString: DateString); } } if (this.DateServer.Date == new DateTime().Date) { this.DateServer = DateTime.UtcNow; } if (this.DateModified.Date == new DateTime().Date) { this.DateModified = this.DateServer; } }
/**************************************************************************/ private void ProcessResponseHttpHeaders(MacroscopeHttpTwoClientResponse Response) { HttpResponseMessage ResponseMessage = Response.GetResponse(); HttpResponseHeaders ResponseHeaders = ResponseMessage.Headers; HttpContentHeaders ContentHeaders = ResponseMessage.Content.Headers; /** Date HTTP Header ------------------------------------------------- **/ try { DateTimeOffset?HeaderValue = ResponseHeaders.Date; if (HeaderValue != null) { this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); this.DateServer = new DateTime(); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "date", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "date", Callback: Callback); } } this.DebugMsg(string.Format("this.DateServer: {0}", this.DateServer)); /** Last-Modified HTTP Header ---------------------------------------- **/ try { DateTimeOffset?HeaderValue = ContentHeaders.LastModified; if (HeaderValue != null) { this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); this.DateModified = new DateTime(); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "last-modified", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "last-modified", Callback: Callback); } } this.DebugMsg(string.Format("this.DateModified: {0}", this.DateModified)); return; }
/**************************************************************************/ private void ProcessResponseHttpHeaders(MacroscopeHttpTwoClientResponse Response) { HttpResponseMessage ResponseMessage = Response.GetResponse(); HttpResponseHeaders ResponseHeaders = ResponseMessage.Headers; HttpContentHeaders ContentHeaders = ResponseMessage.Content.Headers; /** Status Code ------------------------------------------------------ **/ this.SetStatusCode(ResponseMessage.StatusCode); this.SetErrorCondition(ResponseMessage.ReasonPhrase); try { switch (this.GetStatusCode()) { // 200 Range case HttpStatusCode.OK: this.SetIsNotRedirect(); break; // 300 Range case HttpStatusCode.Moved: this.SetErrorCondition(HttpStatusCode.Moved.ToString()); this.SetIsRedirect(); break; case HttpStatusCode.SeeOther: this.SetErrorCondition(HttpStatusCode.SeeOther.ToString()); this.SetIsRedirect(); break; case HttpStatusCode.Found: this.SetErrorCondition(HttpStatusCode.Redirect.ToString()); this.SetIsRedirect(); break; // 400 Range case HttpStatusCode.BadRequest: this.SetErrorCondition(HttpStatusCode.BadRequest.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.Unauthorized: this.SetErrorCondition(HttpStatusCode.Unauthorized.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.PaymentRequired: this.SetErrorCondition(HttpStatusCode.PaymentRequired.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.Forbidden: this.SetErrorCondition(HttpStatusCode.Forbidden.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.NotFound: this.SetErrorCondition(HttpStatusCode.NotFound.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.MethodNotAllowed: this.SetErrorCondition(HttpStatusCode.MethodNotAllowed.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.Gone: this.SetErrorCondition(HttpStatusCode.Gone.ToString()); this.SetIsNotRedirect(); break; case HttpStatusCode.RequestUriTooLong: this.SetErrorCondition(HttpStatusCode.RequestUriTooLong.ToString()); this.SetIsNotRedirect(); break; // Unhandled default: throw new MacroscopeDocumentException("Unhandled HttpStatusCode Type"); } } catch (MacroscopeDocumentException ex) { this.DebugMsg(string.Format("MacroscopeDocumentException: {0}", ex.Message)); } /** Raw HTTP Headers ------------------------------------------------- **/ this.SetHttpResponseStatusLine(Response: Response); this.SetHttpResponseHeaders(Response: Response); /** Server Information ----------------------------------------------- **/ /*{ * this.ServerName = ResponseHeaders.Server.First().ToString(); * }*/ /** PROBE HTTP HEADERS ----------------------------------------------- **/ /** Server HTTP Header ----------------------------------------------- **/ try { HttpHeaderValueCollection <ProductInfoHeaderValue> HeaderValue = ResponseHeaders.Server; if (HeaderValue != null) { if (HeaderValue.FirstOrDefault() != null) { this.SetServerName(HeaderValue.FirstOrDefault().ToString()); } } } catch (Exception ex) { this.DebugMsg(ex.Message); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.SetServerName(HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "server", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "server", Callback: Callback); } } this.DebugMsg(string.Format("this.ServerName: {0}", this.ServerName)); /** Content-Type HTTP Header ----------------------------------------- **/ try { MediaTypeHeaderValue HeaderValue = ContentHeaders.ContentType; if (HeaderValue != null) { this.DebugMsg(string.Format("HeaderValue: {0}", HeaderValue)); this.MimeType = HeaderValue.MediaType; if (HeaderValue.CharSet != null) { this.SetCharacterSet(HeaderValue.CharSet); // TODO: Implement character set probing this.SetCharacterEncoding(NewEncoding: new UTF8Encoding()); } } } catch (Exception ex) { this.DebugMsg(string.Format("MediaType Exception: {0}", ex.Message)); this.MimeType = MacroscopeConstants.DefaultMimeType; } this.DebugMsg(string.Format("this.MimeType: {0}", this.MimeType)); /** Content-Length HTTP Header --------------------------------------- **/ try { long?HeaderValue = null; if (ContentHeaders.Contains("Content-Length")) { HeaderValue = ContentHeaders.ContentLength; } if (HeaderValue != null) { this.ContentLength = HeaderValue; } else { this.ContentLength = 0; } } catch (Exception ex) { this.DebugMsg(ex.Message); this.SetContentLength(Length: 0); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.SetContentLength(Length: long.Parse(HeaderValues.FirstOrDefault())); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "content-length", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "content-length", Callback: Callback); } } this.DebugMsg(string.Format("this.GetContentLength(): {0}", this.GetContentLength())); /** Content-Encoding HTTP Header ------------------------------------- **/ try { ICollection <string> HeaderValue = ContentHeaders.ContentEncoding; if (HeaderValue != null) { this.ContentEncoding = HeaderValue.FirstOrDefault(); } } catch (Exception ex) { this.DebugMsg(ex.Message); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.ContentEncoding = HeaderValues.FirstOrDefault(); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "content-encoding", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "content-encoding", Callback: Callback); } } if (string.IsNullOrEmpty(this.CompressionMethod) && (!string.IsNullOrEmpty(this.ContentEncoding))) { this.IsCompressed = true; this.CompressionMethod = this.ContentEncoding; } this.DebugMsg(string.Format("this.ContentEncoding: {0}", this.ContentEncoding)); this.DebugMsg(string.Format("this.CompressionMethod: {0}", this.CompressionMethod)); /** Date HTTP Header ------------------------------------------------- **/ try { DateTimeOffset?HeaderValue = ResponseHeaders.Date; if (HeaderValue != null) { this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); this.DateServer = new DateTime(); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.DateServer = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "date", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "date", Callback: Callback); } } this.DebugMsg(string.Format("this.DateServer: {0}", this.DateServer)); /** Last-Modified HTTP Header ---------------------------------------- **/ try { DateTimeOffset?HeaderValue = ContentHeaders.LastModified; if (HeaderValue != null) { this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); this.DateModified = new DateTime(); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.DateModified = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "last-modified", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "last-modified", Callback: Callback); } } this.DebugMsg(string.Format("this.DateModified: {0}", this.DateModified)); /** Expires HTTP Header ---------------------------------------------- **/ try { DateTimeOffset?HeaderValue = ContentHeaders.Expires; if (HeaderValue != null) { this.DateExpires = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); this.DateExpires = new DateTime(); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.DateExpires = MacroscopeDateTools.ParseHttpDate(DateString: HeaderValues.First().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "expires", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "expires", Callback: Callback); } } this.DebugMsg(string.Format("this.DateExpires: {0}", this.DateExpires)); /** HTST Policy HTTP Header ------------------------------------------ **/ // https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet // Strict-Transport-Security: max-age=31536000; includeSubDomains; preload { FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.HypertextStrictTransportPolicy = true; return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "strict-transport-security", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "strict-transport-security", Callback: Callback); } } this.DebugMsg(string.Format("this.HypertextStrictTransportPolicy: {0}", this.HypertextStrictTransportPolicy)); /** Location (Redirect) HTTP Header ---------------------------------- **/ try { Uri HeaderValue = ResponseHeaders.Location; if (HeaderValue != null) { this.SetUrlRedirectTo(Url: HeaderValue.ToString()); } } catch (Exception ex) { this.DebugMsg(ex.Message); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { this.SetUrlRedirectTo(Url: HeaderValues.FirstOrDefault().ToString()); return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "location", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "location", Callback: Callback); } } this.DebugMsg(string.Format("this.GetIsRedirect(): {0}", this.GetIsRedirect())); this.DebugMsg(string.Format("this.GetUrlRedirectTo(): {0}", this.GetUrlRedirectTo())); /** Link HTTP Headers ------------------------------------------------ **/ { FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { foreach (string HeaderValue in HeaderValues) { this.DebugMsg(string.Format("HeaderValue: {0}", HeaderValue)); this.ProcessHttpLinkHeader(HttpLinkHeader: HeaderValue); } return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "link", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "link", Callback: Callback); } } /** ETag HTTP Header ------------------------------------------------- **/ try { EntityTagHeaderValue HeaderValue = ResponseHeaders.ETag; if (HeaderValue != null) { string ETagValue = HeaderValue.Tag; if (!string.IsNullOrEmpty(ETagValue)) { this.SetEtag(HeaderValue.Tag); } } } catch (Exception ex) { this.DebugMsg(ex.Message); FindHttpResponseHeaderCallback Callback = delegate(IEnumerable <string> HeaderValues) { string HeaderValue = HeaderValues.FirstOrDefault(); if (HeaderValue != null) { if (!string.IsNullOrEmpty(HeaderValue)) { this.SetEtag(HeaderValue); } } return(true); }; if (!this.FindHttpResponseHeader(ResponseHeaders: ResponseHeaders, HeaderName: "etag", Callback: Callback)) { this.FindHttpContentHeader(ContentHeaders: ContentHeaders, HeaderName: "etag", Callback: Callback); } } this.DebugMsg(string.Format("this.Etag: {0}", this.Etag)); /** WWW-AUTHENTICATE HTTP Header ------------------------------------- **/ // Reference: http://httpbin.org/basic-auth/user/passwd try { HttpHeaderValueCollection <AuthenticationHeaderValue> HeaderValue = ResponseHeaders.WwwAuthenticate; if (HeaderValue != null) { string Scheme = null; string Realm = null; foreach (AuthenticationHeaderValue AuthenticationValue in HeaderValue) { Scheme = AuthenticationValue.Scheme; string Parameter = AuthenticationValue.Parameter; Match Matched = Regex.Match(Parameter, "^[^\"]+\"([^\"]+)\""); if (Matched.Success) { Realm = Matched.Groups[1].Value; } } if (!string.IsNullOrEmpty(Scheme) && !string.IsNullOrEmpty(Realm)) { if (Scheme.ToLower() == "basic") { this.SetAuthenticationType(MacroscopeConstants.AuthenticationType.BASIC); this.SetAuthenticationRealm(Realm); } else { this.SetAuthenticationType(MacroscopeConstants.AuthenticationType.UNSUPPORTED); } } } } catch (Exception ex) { this.DebugMsg(ex.Message); } this.DebugMsg(string.Format("WwwAuthenticate: \"{0}\", Realm: \"{1}\"", this.GetAuthenticationType(), this.GetAuthenticationRealm())); /** Process Dates ---------------------------------------------------- **/ { if (this.DateServer.Date == new DateTime().Date) { this.DateServer = DateTime.UtcNow; } if (this.DateModified.Date == new DateTime().Date) { this.DateModified = this.DateServer; } } /** Process MIME Type ------------------------------------------------ **/ { Regex reIsHtml = new Regex(@"^(text/html|application/xhtml+xml)", RegexOptions.IgnoreCase); Regex reIsCss = new Regex(@"^text/css", RegexOptions.IgnoreCase); Regex reIsJavascript = new Regex(@"^(application/javascript|text/javascript)", RegexOptions.IgnoreCase); Regex reIsImage = new Regex(@"^image/(gif|png|jpeg|bmp|webp|vnd.microsoft.icon|x-icon)", RegexOptions.IgnoreCase); Regex reIsPdf = new Regex(@"^application/pdf", RegexOptions.IgnoreCase); Regex reIsAudio = new Regex(@"^audio/[a-z0-9]+", RegexOptions.IgnoreCase); Regex reIsVideo = new Regex(@"^video/[a-z0-9]+", RegexOptions.IgnoreCase); Regex reIsXml = new Regex(@"^(application|text)/(atom\+xml|xml)", RegexOptions.IgnoreCase); Regex reIsText = new Regex(@"^(text)/(plain)", RegexOptions.IgnoreCase); if (reIsHtml.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.HTML); } else if (reIsCss.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.CSS); } else if (reIsJavascript.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.JAVASCRIPT); } else if (reIsImage.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.IMAGE); } else if (reIsPdf.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.PDF); } else if (reIsAudio.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.AUDIO); } else if (reIsVideo.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.VIDEO); } else if (reIsXml.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.XML); } else if (reIsText.IsMatch(this.MimeType)) { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.TEXT); } else { this.SetDocumentType(Type: MacroscopeConstants.DocumentType.BINARY); } } /** Process Cookies -------------------------------------------------- **/ // https://stackoverflow.com/questions/29224734/how-to-read-cookies-from-httpresponsemessage { try { CookieContainer CookieMonster = MacroscopeHttpTwoClient.GetCookieMonster(); CookieCollection Biscuits = CookieMonster.GetCookies(uri: this.GetUri()); this.AddCookies(Cookies: Biscuits); this.DebugMsg("cookies"); // CookieContainer CookieTin = MacroscopeHttpTwoClient.GetCookieMonster(); // string LimpBizkit = tin.GetCookieHeader( uri: Request.RequestUri ); } catch (Exception ex) { this.DebugMsg(ex.Message); } } return; }