public void Execute(bool force) { if (RefreshStatus == WebCallRefreshStatus.WaitingForRefreshCallback) { if (!force) { return; } RefreshStatus = WebCallRefreshStatus.NotRefreshing; } if (force) { this.OnUnqueued(this, new WebCallUnqueuedEventArgs()); } SessionDocument sessionDocument = database.GetSessionDocument(swid); if (!force && sessionDocument != null && sessionDocument.ProtocolVersion != 3) { this.OnUnauthorized(this, new WebCallUnauthorizedEventArgs("UNAUTHORIZED_MIX_SESSION")); return; } numAttempts++; bodyBytes = Encoding.UTF8.GetBytes(body); bodyBytes = WebCallEncryptor.Encrypt(bodyBytes); wwwCall = wwwCallFactory.Create(uri, method, bodyBytes, headers, latencyWwwCallTimeout, maxWwwCallTimeout); wwwCall.OnDone += HandleWwwDone; wwwCall.Execute(); logger.Debug(HttpLogBuilder.BuildRequestLog(wwwCall.RequestId, uri, method, headers, body)); }
private void HandleWwwDone(object sender, WwwDoneEventArgs e) { wwwCall.OnDone -= HandleWwwDone; bool flag = false; string error = wwwCall.Error; if (!string.IsNullOrEmpty(error)) { string text = error.ToLower(); flag = text == "couldn't connect to host" || text.Contains("timedout") || text.Contains("timed out"); } if (flag) { string responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody); string text2 = HttpLogBuilder.BuildTimeoutLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, wwwCall.TimeToStartUpload, wwwCall.TimeToFinishUpload, wwwCall.PercentUploaded, wwwCall.TimeToStartDownload, wwwCall.TimeToFinishDownload, wwwCall.PercentDownloaded, wwwCall.TimeoutReason, wwwCall.TimeoutTime); timeoutLogs.Append(text2); timeoutLogs.Append("\n\n"); logger.Error(text2); wwwCall.Dispose(); if (numAttempts > 3) { logger.Critical("Too many timeouts: " + uri.AbsoluteUri + "\nPrevious logs:\n" + timeoutLogs); DispatchError("Too many timeouts"); } else { Execute(); } return; } uint statusCode = wwwCall.StatusCode; if (statusCode >= 500 && statusCode <= 599) { string responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody); string text2 = HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode); wwwCall.Dispose(); if (numAttempts > 1) { logger.Critical("Too many server errors:\n" + text2); DispatchError(statusCode + " Server error: " + wwwCall.Error); } else { logger.Error(text2); Execute(); } return; } bool flag2 = statusCode >= 200 && statusCode <= 299; if (statusCode >= 400 && statusCode <= 499) { string responsePlaintext; try { responsePlaintext = GetResponseText(wwwCall.ResponseBody); } catch (Exception ex) { logger.Warning("Text was not encrypted, error: " + ex); responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody); } if (statusCode == 429) { this.OnError(this, new WebCallErrorEventArgs(statusCode + " Too many requests", "THROTTLED")); return; } string status = GetStatus(responsePlaintext); if (statusCode == 401) { logger.Debug(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode)); wwwCall.Dispose(); this.OnUnauthorized(this, new WebCallUnauthorizedEventArgs(status, responsePlaintext)); } else { logger.Error(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode)); this.OnError(this, new WebCallErrorEventArgs(statusCode + " Client error = " + wwwCall.Error, status)); wwwCall.Dispose(); } } else if (flag2) { string responsePlaintext; try { responsePlaintext = GetResponseText(wwwCall.ResponseBody); } catch (Exception ex) { string responsePlaintext2 = GetResponsePlaintext(wwwCall.ResponseBody); logger.Critical(string.Concat("Error getting response body:\n", ex, "\n", HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext2, statusCode))); DispatchError(statusCode + " Couldn't get response body text"); wwwCall.Dispose(); return; } logger.Debug(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode)); TResponse val = null; try { val = JsonParser.FromJson <TResponse>(responsePlaintext); } catch (Exception ex) { logger.Critical(string.Concat("Error parsing response body: ", ex, "\n", responsePlaintext)); } if (val != null && val.Status == "OK") { this.OnResponse(this, new WebCallEventArgs <TResponse>(val)); } else { DispatchError(statusCode + " Response JSON is invalid or indicates an error: " + responsePlaintext); } wwwCall.Dispose(); } else { bool flag3 = false; if (!string.IsNullOrEmpty(error)) { string text = error.ToLower(); flag3 = text.Contains("connection appears to be offline") || text.Contains("connection was lost") || text.Contains("network is unreachable"); } if (flag3) { logger.Error("Offline error = " + wwwCall.Error); } else { logger.Critical("Other web call error = " + wwwCall.Error); } DispatchError(statusCode + " Other web call error = " + wwwCall.Error); wwwCall.Dispose(); } }
private static string BuildRequestLog(Uri uri, int requestId) { return(HttpLogBuilder.BuildRequestLog(requestId, uri, HttpMethod.GET, emptyHeaders, string.Empty)); }
private static string BuildResponseLog(Uri uri, WwwCallResult result, Dictionary <string, string> headers, uint statusCode, IWwwCall wwwCall) { return((result == WwwCallResult.RetryTimeout) ? HttpLogBuilder.BuildTimeoutLog(wwwCall.RequestId, uri, HttpMethod.GET, headers, string.Empty, wwwCall.TimeToStartUpload, wwwCall.TimeToFinishUpload, wwwCall.PercentUploaded, wwwCall.TimeToStartDownload, wwwCall.TimeToFinishDownload, wwwCall.PercentDownloaded, wwwCall.TimeoutReason, wwwCall.TimeoutTime) : HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, HttpMethod.GET, headers, string.Empty, statusCode)); }