Ejemplo n.º 1
0
        public CURLE Perform()
        {
            CURLE result = (CURLE)(-1);

            if (!running)
            {
                running    = true;
                retryCount = maxRetryCount;

                while (true)
                {
                    Prepare();
                    result = Lib.curl_easy_perform(easyPtr);
                    var done = ProcessResponse(result);
                    if (done || --retryCount < 0)
                    {
                        Dump();
                        break;
                    }
                    else
                    {
                        Thread.Sleep(retryInterval);
                    }
                }

                running = false;
            }
            else
            {
                CurlLog.LogError("Can't preform a running handle again!");
            }

            return(result);
        }
Ejemplo n.º 2
0
 void OnPerformCallback(CURLE result, CurlEasy easy)
 {
     if (result == CURLE.OK)
     {
         Debug.Log($"Perform finished: {easy.uri}");
     }
 }
Ejemplo n.º 3
0
 private void OnComplete(CURLE result)
 {
     if (performCallback != null)
     {
         new Task(() =>
         {
             performCallback(result, this);
             performCallback = null;
             running         = false;
         }).Start(taskScheduler);
     }
     else
     {
         running = false;
     }
 }
Ejemplo n.º 4
0
        internal void OnMultiPerform(CURLE result, CurlMulti multi)
        {
            var done = ProcessResponse(result);

            if (done || --retryCount < 0)
            {
                Dump();

                OnComplete(result);
            }
            else
            {
                Thread.Sleep(retryInterval);
                Prepare();
                multi.AddEasy(this);
            }
        }
Ejemplo n.º 5
0
        private bool ProcessResponse(CURLE result)
        {
            var done = false;

            try
            {
                thisHandle.Free();

                if (result == CURLE.OK)
                {
                    responseHeaderStream.Position = 0;
                    var sr = new StreamReader(responseHeaderStream);

                    // Handle first line
                    {
                        var line  = sr.ReadLine();
                        var index = line.IndexOf(' ');
                        httpVersion = line.Substring(0, index);
                        var nextIndex = line.IndexOf(' ', index + 1);
                        if (int.TryParse(line.Substring(index + 1, nextIndex - index), out var _status))
                        {
                            status = _status;
                        }
                        message = line.Substring(nextIndex + 1);
                    }

                    inHeader = new HeaderDict();

                    while (true)
                    {
                        var line = sr.ReadLine();
                        if (!string.IsNullOrEmpty(line))
                        {
                            var index = line.IndexOf(':');
                            var key   = line.Substring(0, index).Trim();
                            var value = line.Substring(index + 1).Trim();
                            inHeader[key] = value;
                        }
                        else
                        {
                            break;
                        }
                    }

                    var ms = responseBodyStream as MemoryStream;
                    if (ms != null)
                    {
                        inData = ms.ToArray();
                    }

                    if (status / 100 == 3)
                    {
                        if (followRedirect && GetInfo(CURLINFO.REDIRECT_URL, out string location) == CURLE.OK)
                        {
                            uri = new Uri(location);
                        }
                        else
                        {
                            done = true;
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
                else
                {
                    CurlLog.LogWarning($"Failed to request: {uri}, reason: {result}");
                }

                CloseStreams();
            }
            catch (Exception e)
            {
                CurlLog.LogError("Unexpected exception: " + e);
            }


            return(done);
        }