Example #1
0
 protected void ProcessResponse(UnityWebRequest www, long wwwTime, string url, Action <RESTResponse> listener)
 {
     try
     {
         if (!www.isNetworkError || !www.isHttpError)
         {
             string decodedString = null;
             bool   flag          = ResponseBodyTester.TestUTF8(www.downloadHandler.data, out decodedString);
             Dictionary <string, string> dictionary = new Dictionary <string, string>();
             string text = null;
             if (www.GetResponseHeaders() != null)
             {
                 Dictionary <string, string> .Enumerator enumerator = www.GetResponseHeaders().GetEnumerator();
                 while (enumerator.MoveNext())
                 {
                     KeyValuePair <string, string> current = enumerator.Current;
                     dictionary.Add(current.Key.ToUpper(), current.Value);
                 }
                 if (dictionary.ContainsKey("CONTENT-ENCODING"))
                 {
                     text = dictionary["CONTENT-ENCODING"];
                 }
             }
             if (flag)
             {
                 AddMetrics(url, wwwTime, error: false);
                 listener(new RESTResponse(decodedString, dictionary));
             }
             else
             {
                 AddMetrics(url, wwwTime, error: true);
                 listener(new RESTResponse(decodedString, dictionary));
             }
         }
         else
         {
             AddMetrics(url, wwwTime, error: true);
             listener(new RESTResponse(www.responseCode));
         }
     }
     catch (Exception message)
     {
         SwrveLog.LogError(message);
     }
 }
        protected void ProcessResponse(UnityWebRequest www, long wwwTime, string url, Action <RESTResponse> listener)
        {
#if SWRVE_SUPPORTED_PLATFORM
            try {
                if (!www.isNetworkError)
                {
                    // - made it there and it was ok
                    string responseBody = null;
                    bool   success      = ResponseBodyTester.TestUTF8(www.downloadHandler.data, out responseBody);
                    Dictionary <string, string> headers = new Dictionary <string, string> ();

                    if (www.GetResponseHeaders() != null)
                    {
                        Dictionary <string, string> .Enumerator headersEnum = www.GetResponseHeaders().GetEnumerator();
                        while (headersEnum.MoveNext())
                        {
                            KeyValuePair <string, string> header = headersEnum.Current;
                            headers.Add(header.Key.ToUpper(), header.Value);
                        }
                    }

                    if (success)
                    {
                        AddMetrics(url, wwwTime, false);
                        listener.Invoke(new RESTResponse(error: UnityWwwHelper.DeduceWwwError(www), responseCode: www.responseCode, responseBody: responseBody, headers: headers));
                    }
                    else
                    {
                        AddMetrics(url, wwwTime, true);
                        listener.Invoke(new RESTResponse(error: WwwDeducedError.ApplicationErrorBody, responseCode: www.responseCode, responseBody: responseBody, headers: headers));
                    }
                }
                else
                {
                    AddMetrics(url, wwwTime, true);
                    listener.Invoke(new RESTResponse(error: UnityWwwHelper.DeduceWwwError(www)));
                }
            } catch (Exception exp) {
                SwrveLog.LogError(exp);
            }
#endif
        }
Example #3
0
        protected void ProcessResponse(WWW www, long wwwTime, string url, Action <RESTResponse> listener)
        {
#if SWRVE_SUPPORTED_PLATFORM
            try {
                WwwDeducedError deducedError = UnityWwwHelper.DeduceWwwError(www);
                if (deducedError == WwwDeducedError.NoError)
                {
                    // - made it there and it was ok
                    string responseBody = null;
                    bool   success      = ResponseBodyTester.TestUTF8(www.bytes, out responseBody);
                    Dictionary <string, string> headers = new Dictionary <string, string> ();

                    string contentEncodingHeader = null;
                    if (www.responseHeaders != null)
                    {
                        Dictionary <string, string> .Enumerator headersEnum = www.responseHeaders.GetEnumerator();
                        while (headersEnum.MoveNext())
                        {
                            KeyValuePair <string, string> header = headersEnum.Current;
                            headers.Add(header.Key.ToUpper(), header.Value);
                        }
                        // Get the content encoding header, if present
                        if (headers.ContainsKey(CONTENT_ENCODING_HEADER_KEY))
                        {
                            contentEncodingHeader = headers[CONTENT_ENCODING_HEADER_KEY];
                        }
                    }

                    // BitConverter.ToInt32 needs at least 4 bytes
                    if (www.bytes != null && www.bytes.Length > 4 && contentEncodingHeader != null && string.Equals(contentEncodingHeader, "gzip", StringComparison.OrdinalIgnoreCase))
                    {
                        // Check if the response is gzipped or json (eg. iOS automatically unzips it already)
                        if (responseBody != null && !((responseBody.StartsWith("{") && responseBody.EndsWith("}")) || (responseBody.StartsWith("[") && responseBody.EndsWith("]"))))
                        {
                            int dataLength = BitConverter.ToInt32(www.bytes, 0);
                            if (dataLength > 0)
                            {
                                var buffer = new byte[dataLength];

                                using (var ms = new MemoryStream(www.bytes)) {
                                    using (var gs = new GZipInputStream(ms)) {
                                        gs.Read(buffer, 0, buffer.Length);
                                        gs.Close();
                                    }

                                    success = ResponseBodyTester.TestUTF8(buffer, out responseBody);
                                    ms.Close();
                                }
                            }
                        }
                    }

                    if (success)
                    {
                        AddMetrics(url, wwwTime, false);
                        listener.Invoke(new RESTResponse(responseBody, headers));
                    }
                    else
                    {
                        AddMetrics(url, wwwTime, true);
                        listener.Invoke(new RESTResponse(WwwDeducedError.ApplicationErrorBody));
                    }
                }
                else
                {
                    AddMetrics(url, wwwTime, true);
                    listener.Invoke(new RESTResponse(deducedError));
                }
            } catch (Exception exp) {
                SwrveLog.LogError(exp);
            }
#endif
        }