Example #1
0
        public static IEnumerator GetResponse(WWW www, CancellationToken cancellationToken, string expectedHash = null, Action<float> reportProgress = null)
        {
            var monitor = new WWWConnectionMonitor(www);
            var progress = 0.0f;

            while (!www.isDone && !cancellationToken.IsCanceled)
            {
            monitor.AssertLiveliness();

            if (reportProgress != null && www.progress != progress)
            {
                progress = www.progress;
                reportProgress(progress);
            }
            yield return null;
            }

            if (reportProgress != null)
            {
            reportProgress(www.progress);
            }

            if (cancellationToken.IsCanceled) yield break;

            if (!string.IsNullOrEmpty(www.error))
            {
            throw new WwwErrorException(www);
            }

            if (expectedHash != null)
            {
            using (var md5 = MD5.Create())
            {
                if (expectedHash != FileHelper.GetFingerprint(md5.ComputeHash(www.bytes)))
                {
                    throw new WwwErrorException(HttpStatusCode.ExpectationFailed);
                }
            }
            }
        }
Example #2
0
        private IEnumerator DoRequest(WWW www, CancellationToken cancellationToken, bool verifyResponse, Action<Dictionary<string, object>> responseCallback)
        {
            using (www)
            {
            var monitor = new WWWConnectionMonitor(www);

            while (!www.isDone && !cancellationToken.IsCanceled)
            {
                monitor.AssertLiveliness();
                yield return null;
            }

            if (cancellationToken.IsCanceled)
            {
                yield break;
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                throw new WwwErrorException(www);
            }

            PSDebug.Log("Request returned: {0}, text: {1}", www.url, www.text);
            var response = ResponseAsDictionary(www);
            if (verifyResponse)
            {
                var password = JSONDict.Wrap(response).Get<string>("meta", "password");
                var responseDigest = ResponseDigest(www.text, password);
                var sigHeader = www.responseHeaders.Get(X_SIGNATURE.ToUpper());
                if (sigHeader != responseDigest)
                {
                    throw new Exception(
                        string.Format("Singature mismatch in {0}. {1} vs {2}", www.url, sigHeader, responseDigest));
                }
            }

            if (www.responseHeaders.ContainsKey("SET-COOKIE"))
            {
                SetCookie(www.responseHeaders["SET-COOKIE"]);
            }

            responseCallback(response);
            }
        }