private static HttpRequestMessage CreateRequest(UriInfo uriInfo, Uri uri) { return(new HttpRequestMessage { Method = uriInfo.UseHead ? HttpMethod.Head : HttpMethod.Get, RequestUri = uri }); }
public UriInfo(UriInfo copyFrom) { _followRedirects = copyFrom._followRedirects; _ignoreSslErrors = copyFrom._ignoreSslErrors; _useHead = copyFrom._useHead; _uri = new Uri(copyFrom._uri.OriginalString); _enabled = copyFrom.Enabled; }
private async Task <HttpStatusCode> GetResponseCode(UriInfo uriInfo) { int redirectCount; bool done; HttpRequestMessage request; HttpStatusCode statusCode; redirectCount = 0; done = false; request = DownDetectorClient.CreateRequest(uriInfo, uriInfo.Uri); do { HttpResponseMessage response; response = await _httpClient.SendAsync(request).ConfigureAwait(false); statusCode = response.StatusCode; if (DownDetectorClient.IsRedirectResponse(statusCode) && uriInfo.FollowRedirects) { redirectCount++; if (redirectCount > _settings.MaximumRedirects) { done = true; } else { Uri redirectUri; redirectUri = response.Headers.Location; if (!redirectUri.IsAbsoluteUri) { redirectUri = new Uri(request.RequestUri, redirectUri); } request = DownDetectorClient.CreateRequest(uriInfo, redirectUri); } } else { done = true; } } while (!done); return(statusCode); }
public async Task CheckUri(UriInfo uriInfo) { Uri uri; UriEventArgs args; UriStatus newStatus; Exception error; HttpStatusCode httpStatus; uri = uriInfo.Uri; if (!_settings.Statuses.TryGetValue(uri, out UriStatusInfo status)) { status = new UriStatusInfo { LastChange = DateTimeOffset.UtcNow, Status = UriStatus.Unknown, Uri = uri }; _settings.Statuses.Add(status); } args = new UriEventArgs(uri); this.OnUriChecking(args); (newStatus, httpStatus, error) = await this.GetUriStatus(uriInfo).ConfigureAwait(false); if (error != null) { this.OnUriException(new UriExceptionEventArgs(uri, error)); } this.UpdateStatus(status, httpStatus, newStatus); this.OnUriChecked(args); }
private async Task <Tuple <UriStatus, HttpStatusCode, Exception> > GetUriStatus(UriInfo uriInfo) { UriStatus newStatus; Exception error; HttpStatusCode httpStatus; _sslPolicyErrors = SslPolicyErrors.None; try { httpStatus = await this.GetResponseCode(uriInfo).ConfigureAwait(false); newStatus = httpStatus == HttpStatusCode.OK ? UriStatus.Online : UriStatus.Unstable; if (newStatus == UriStatus.Online && _sslPolicyErrors != SslPolicyErrors.None) { newStatus = uriInfo.IgnoreSslErrors ? UriStatus.InvalidCertificate : UriStatus.Unstable; } error = null; } catch (Exception ex) { newStatus = UriStatus.Unstable; httpStatus = 0; error = ex; } return(Tuple.Create(newStatus, httpStatus, error)); }
public bool TryGetValue(UriInfo uriInfo, out UriStatusInfo value) { return(this.TryGetValue(uriInfo.Uri, out value)); }