public string ToUnityDebugString() { if (this.webRequest == null) { return("Request failed prior to being sent.\n" + this.errorMessage); } else { return(DebugUtilities.GetResponseInfo(this.webRequest)); } }
private static void DownloadModBinary_Internal(ModfileIdPair idPair, string downloadURL) { FileDownloadInfo downloadInfo = modfileDownloadMap[idPair]; downloadInfo.request = UnityWebRequest.Get(downloadURL); string tempFilePath = downloadInfo.target + ".download"; DataStorage.WriteFile(tempFilePath, new byte[0], (p, success) => { if (success) { downloadInfo.request.downloadHandler = new DownloadHandlerFile(tempFilePath); #if PLATFORM_PS4 // NOTE(@jackson): This workaround addresses an issue in UnityWebRequests on the // PS4 whereby redirects fail in specific cases. Special thanks to @Eamon of // Spiderling Studios (http://spiderlinggames.co.uk/) downloadInfo.request.redirectLimit = 0; #endif var operation = downloadInfo.request.SendWebRequest(); #if DEBUG DebugUtilities.DebugDownload(operation, LocalUser.instance, tempFilePath); #endif operation.completed += (o) => DownloadClient.OnModBinaryRequestCompleted(idPair); DownloadClient.StartMonitoringSpeed(); // notify download started if (DownloadClient.modfileDownloadStarted != null) { DownloadClient.modfileDownloadStarted(idPair, downloadInfo); } } else if (DownloadClient.modfileDownloadFailed != null) { string warningInfo = ("Failed to create download file on disk." + "\nSource: " + downloadURL + "\nDestination: " + tempFilePath + "\n\n"); modfileDownloadFailed(idPair, WebRequestError.GenerateLocal(warningInfo)); } }); }
public static ImageRequest DownloadImage(string imageURL) { ImageRequest request = new ImageRequest(); request.isDone = false; UnityWebRequest webRequest = UnityWebRequest.Get(imageURL); webRequest.downloadHandler = new DownloadHandlerTexture(true); var operation = webRequest.SendWebRequest(); operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request); #if DEBUG DebugUtilities.DebugDownload(operation, LocalUser.instance, null); #endif return(request); }
/// <summary>Tracks and logs a download upon it completing.</summary> public static void DebugDownload(UnityWebRequestAsyncOperation operation, LocalUser userData, string downloadLocation, int timeSent = -1) { #if DEBUG Debug.Assert(operation != null); UnityWebRequest webRequest = operation.webRequest; string userIdString = DebugUtilities.GenerateUserIdString(userData.profile); if (timeSent < 0) { timeSent = ServerTimeStamp.Now; } if (PluginSettings.REQUEST_LOGGING.logOnSend) { var logString = new System.Text.StringBuilder(); logString.AppendLine("[mod.io] Web Request Sent"); logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(downloadLocation); } if (timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(timeSent).ToString()); logString.Append(" ["); logString.Append(timeSent.ToString()); logString.AppendLine("]"); } logString.AppendLine(); string requestString = DebugUtilities.GetRequestInfo(webRequest, userIdString); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); Debug.Log(logString.ToString()); } if (PluginSettings.REQUEST_LOGGING.logAllResponses || PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { RequestDebugData debugData = new RequestDebugData() { userIdString = userIdString, timeSent = timeSent, downloadLocation = downloadLocation, }; DebugUtilities.webRequestDebugData.Add(webRequest, debugData); // handle completion if (operation.isDone) { DebugUtilities.OnOperationCompleted(operation); } else { operation.completed += DebugUtilities.OnOperationCompleted; } } #endif // DEBUG }
/// <summary>Generates a debug-friendly string of a web request.</summary> public static string GetRequestInfo(UnityWebRequest webRequest, string userIdString) { if (webRequest == null) { return("NULL_WEB_REQUEST"); } // check user string if (userIdString == null) { userIdString = "[NOT RECORDED]"; } // build string var requestString = new System.Text.StringBuilder(); requestString.Append("URL: "); requestString.Append(webRequest.url); requestString.Append(" ("); requestString.Append(webRequest.method.ToUpper()); requestString.AppendLine(")"); requestString.Append("User: "******"Headers:"); foreach (string headerKey in APIClient.MODIO_REQUEST_HEADER_KEYS) { string headerValue = webRequest.GetRequestHeader(headerKey); if (headerValue != null) { requestString.Append(" "); requestString.Append(headerKey); requestString.Append('='); if (headerKey.ToUpper() == "AUTHORIZATION") { if (headerValue != null && headerValue.StartsWith("Bearer ") && headerValue.Length > 8) { requestString.Append("Bearer [OAUTH_TOKEN]"); } else // NULL { requestString.Append(headerValue); } } else { requestString.Append(headerValue); } requestString.AppendLine(); } } // add uploaded data var uploadHandler = webRequest.uploadHandler; if (uploadHandler != null) { List <API.StringValueParameter> stringFields = null; List <API.BinaryDataParameter> binaryFields = null; string contentType = webRequest.GetRequestHeader("content-type"); if (contentType.ToLower() == "application/x-www-form-urlencoded") { DebugUtilities.ParseURLEncodedFormData(uploadHandler.data, out stringFields); } else if (contentType.Contains("multipart/form-data")) { DebugUtilities.ParseMultipartFormData(uploadHandler.data, out stringFields, out binaryFields); } else { Debug.Log("[mod.io] Unable to parse upload data for content-type \'" + contentType + "\'"); } // add string fields if (stringFields != null) { requestString.AppendLine("String Fields:"); int countInsertIndex = requestString.Length - 1; int count = 0; foreach (var svp in stringFields) { requestString.Append(" "); requestString.Append(svp.key); requestString.Append('='); requestString.Append(svp.value); requestString.AppendLine(); ++count; } requestString.Insert(countInsertIndex, " [" + count.ToString() + "]"); } // add binary fields if (binaryFields != null) { requestString.AppendLine("Binary Fields:"); int countInsertIndex = requestString.Length - 1; int count = 0; foreach (var bdp in binaryFields) { requestString.Append(" "); requestString.Append(bdp.key); requestString.Append('='); requestString.Append(bdp.fileName); requestString.Append(" ("); requestString.Append(bdp.contents == null ? "NULL_DATA" : ValueFormatting.ByteCount(bdp.contents.Length, null)); requestString.Append(")"); requestString.AppendLine(); ++count; } requestString.Insert(countInsertIndex, " [" + count.ToString() + "]"); } } return(requestString.ToString()); }
/// <summary>Callback upon request operation completion.</summary> private static void OnOperationCompleted(AsyncOperation operation) { if (operation == null) { return; } // get vars UnityWebRequestAsyncOperation o = operation as UnityWebRequestAsyncOperation; UnityWebRequest webRequest = o.webRequest; var now = ServerTimeStamp.Now; bool isError = (webRequest.isNetworkError || webRequest.isHttpError); // should we log? if (PluginSettings.REQUEST_LOGGING.logAllResponses || isError) { RequestDebugData debugData; if (!DebugUtilities.webRequestDebugData.TryGetValue(webRequest, out debugData)) { debugData = new RequestDebugData() { userIdString = "NONE_RECORDED", timeSent = -1, downloadLocation = null, }; } // generate strings string requestString = DebugUtilities.GetRequestInfo(webRequest, debugData.userIdString); string responseString = DebugUtilities.GetResponseInfo(webRequest); // generate log string var logString = new System.Text.StringBuilder(); if (!isError) { logString.AppendLine("[mod.io] Web Request Succeeded"); } else { logString.AppendLine("[mod.io] Web Request Failed"); } logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(debugData.downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(debugData.downloadLocation); } if (debugData.timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(debugData.timeSent).ToString()); logString.Append(" ["); logString.Append(debugData.timeSent.ToString()); logString.AppendLine("]"); } logString.Append("Completed: "); logString.Append(ServerTimeStamp.ToLocalDateTime(now).ToString()); logString.Append(" ["); logString.Append(now.ToString()); logString.AppendLine("]"); logString.AppendLine(); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); logString.AppendLine("------[ Response ]------"); logString.AppendLine(responseString); // log if (isError && PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { Debug.LogWarning(logString.ToString()); } else { Debug.Log(logString.ToString()); } } DebugUtilities.webRequestDebugData.Remove(webRequest); }