Beispiel #1
0
        public static void      fUrlRequestComplete(LaunchTool _oTool)
        {
            ParamHttp _oParam = (ParamHttp)_oTool.oCustom;

            //Console.WriteLine("------------------ Finish :\n"  +_oParam.sURL );

            //Console.WriteLine( _oParam.sResult );
            //Console.WriteLine("" );
            //Console.WriteLine("------------------" );
            if (_oParam.bDownloadFile)
            {
                if (_oParam.nTotalBytes > 0)
                {
                    _oParam.nBytes = _oParam.nTotalBytes;
                }
                else
                {
                    _oParam.nBytes *= 1.5;             //Boost gain
                }
                fUrlProgress(_oParam);                 //100%
                Output.TraceGood("Downloaded: " + _oParam.sToFile);
            }

            ///if(!_oParam.bDownloadFile) {
            _oParam.dComplete(_oParam);
            //}
        }
Beispiel #2
0
        public static void fHttpRequestComplete(Object sender, RunWorkerCompletedEventArgs e)
        {
            ParamHttp _oParam = (ParamHttp)e.Result;

            if (!_oParam.bDownloadFile)
            {
                _oParam.dComplete(_oParam);
            }
        }
Beispiel #3
0
        public static void      fHttpOut(LaunchTool _oTool, string _sOut)
        {
            ParamHttp _oParam = (ParamHttp)_oTool.oCustom;

            _oParam.sResult += _sOut;
            //	Debug.fTrace(": " + _sOut);

            //	_oParam.oHeader = _response.Headers;
        }
Beispiel #4
0
 public static void fGetTagData(ParamHttp _oData)
 {
     if (_oData.nCustom == 0)
     {
         fGetTagDataAPI(_oData);
     }
     else
     {
         Debug.fTrace("---!!!***fGetTagDataALT !! " + _oData.nCustom);
         fGetTagDataALT(_oData);
     }
 }
Beispiel #5
0
        public static void fGetHttp(ParamHttp _oParam)
        {
//if(_oParam.sURL[8] == 'a'){


            Console.WriteLine("------------------------fGetHttp : " + _oParam.sURL);


            //if(!_oParam.bDownloadFile) {
            if (true)
            {
                LaunchTool _oLaunchUrl = new LaunchTool();
                _oLaunchUrl.dOut   = new LaunchTool.dIOut(fHttpOut);
                _oLaunchUrl.dError = new LaunchTool.dIError(fHttpOutInfo);

                _oLaunchUrl.bHidden = true;
                _oLaunchUrl.dExit   = new LaunchTool.dIExit(fUrlRequestComplete);
                _oLaunchUrl.oCustom = (Object)_oParam;

                //_oLaunchUrl.bRunInThread = false;
                //_oLaunchUrl.bWaitEndForOutput = true;
                _oParam.sResult = "";

                //	_oLaunchUrl.fLaunchExe( PathHelper.ToolDir + "curl/curl.exe", "-i  -k -L  -A \"Cwc - app\" \"" + _oParam.sURL + "\"");
                //_oLaunchUrl.fLaunchExe( PathHelper.ToolDir + "curl/curl.exe", "--anyauth -f -i  -k -L  -A \"Cwc - app\" \"" + _oParam.sURL + "\"");
                //_oLaunchUrl.fLaunchExe( PathHelper.ToolDir + "wget/wget.exe", "--no-check-certificate -S -O - \"" + _oParam.sURL + "\"");
                if (!_oParam.bDownloadFile)
                {
                    _oLaunchUrl.fLaunchExe(PathHelper.ToolDir + "wget/wget.exe", "--no-check-certificate -S -O - \"" + _oParam.sURL + "\"");
                }
                else
                {
                    //    _oLaunchUrl.dOut += new LaunchTool.dIOut(fHttpConsole);
                    //  _oLaunchUrl.dError += new LaunchTool.dIError(fHttpConsole);

                    Console.WriteLine("------------------Download File !! ");
                    _oLaunchUrl.fLaunchExe(PathHelper.ToolDir + "wget/wget.exe", "--no-check-certificate -O \"" + _oParam.sToFile + "\" \"" + _oParam.sURL + "\"");
                }
                //
            }
            else
            {
                BackgroundWorker oWorkHttp;
                oWorkHttp = new BackgroundWorker();
                oWorkHttp.WorkerSupportsCancellation = true;
                oWorkHttp.DoWork             += new DoWorkEventHandler(fHttpRequestTagRelease);
                oWorkHttp.RunWorkerCompleted += new RunWorkerCompletedEventHandler(fHttpRequestComplete);
                oWorkHttp.RunWorkerAsync(_oParam);
            }
//}
        }
Beispiel #6
0
        public static DownloadProgressChangedEventHandler fProgress(ParamHttp _oParam)
        {
            Action <object, DownloadProgressChangedEventArgs> action = (sender, e) => {
                try{
                    double bytesIn = e.BytesReceived;
                    //d//ouble totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                    double totalBytes = e.TotalBytesToReceive;
                    //	double totalBytes = -1;

                    double _nApMax = 104857600;

                    _oParam.nBytes      = bytesIn;
                    _oParam.nTotalBytes = totalBytes;


                    if (totalBytes == -1)                                      //Aproximative value
                    {
                        double _nRatio = _oParam.nBytes / 100000000.0;
                        _oParam.nAprox += (_oParam.nBytes - _oParam.nLastBytes) / ((_nRatio * _nRatio) + 1);
                        if (_oParam.nAprox > _nApMax)
                        {
                            _oParam.nAprox = _nApMax;
                        }
                        _oParam.nPercentage = _oParam.nAprox * 97.0 / _nApMax;                                         //Aprox Max was 90%
                    }
                    else
                    {
                        _oParam.nPercentage = bytesIn / totalBytes * 100;
                    }
                    _oParam.nLastBytes = _oParam.nBytes;


                    //		Debug.fTrace("pc" + _oParam.nPercentage + " bytesIn  " + bytesIn + " totalBytes " + totalBytes);

                    _oParam.dProgress(_oParam);
                } catch (Exception ex)    {
                    _oParam.bFail    = true;
                    _oParam.sFailMsg = ex.Message;
                }
            };

            return(new DownloadProgressChangedEventHandler(action));
        }
Beispiel #7
0
/*
 *      public static  void fProgress(Object sender, RunWorkerCompletedEventArgs e) {
 *                      ParamHttp _oParam =   (ParamHttp)e.Result;
 *                      _oParam.dComplete(_oParam);
 *              }*/



        public static void fUrlProgress(ParamHttp _oParam)
        {
            try{
                //	double bytesIn = e.BytesReceived;
                //d//ouble totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                //	double totalBytes = e.TotalBytesToReceive;
                //	double totalBytes = -1;

                double _nApMax = 104857600;

                //	_oParam.nBytes = bytesIn;
                //	_oParam.nTotalBytes= totalBytes;


                if (_oParam.nTotalBytes <= 0)                                          //Aproximative value
                {
                    double _nRatio = _oParam.nBytes / 100000000.0;
                    _oParam.nAprox += (_oParam.nBytes - _oParam.nLastBytes) / ((_nRatio * _nRatio) + 1);
                    if (_oParam.nAprox > _nApMax)
                    {
                        _oParam.nAprox = _nApMax;
                    }
                    _oParam.nPercentage = _oParam.nAprox * 97.0 / _nApMax;                                             //Aprox Max was 90%
                }
                else
                {
                    _oParam.nPercentage = _oParam.nBytes / _oParam.nTotalBytes * 100;
                }
                _oParam.nLastBytes = _oParam.nBytes;


                //		Debug.fTrace("pc" + _oParam.nPercentage + " bytesIn  " + bytesIn + " totalBytes " + totalBytes);

                _oParam.dProgress(_oParam);
            } catch (Exception ex)    {
                _oParam.bFail    = true;
                _oParam.sFailMsg = ex.Message;
            }
        }
Beispiel #8
0
        public void fReadHttpModuleTagsFinish(ParamHttp _oParam)
        {
            //	Debug.fTrace("---------Get-------" + aTags.Count);
            //Tags _aTags = (Tags)_oData.oContainer;
            lock (oLock){
                foreach (Tag _oTag in aTags)
                {
                    new ModuleLink(this, _oTag);
                }
                aLinkList = new List <string>(this.aLink.Keys);
                aLinkList.Sort();
                aLinkList.Reverse();


                //	Debug.fTrace("---------FInish-------" + aLink.Count);
                if (oForm != null)
                {
                    oForm.fDataLoaded();
                }
                nRequestTag--;
            }
        }
Beispiel #9
0
        public static void fGetTagDataAPI(ParamHttp _oData)
        {
            //	Debug.fTrace("sInfo" + _oData.sInfo);
            string _sLimit = "";

            if (_oData.oHeader != null)                        //C# http method
            {
                _sLimit = _oData.oHeader["X-RateLimit-Remaining"];
            }
            else
            {
                _sLimit = fBetween(_oData.sInfo, "X-RateLimit-Remaining:", "\n");
            }

            if (_sLimit != "")
            {
                int _nLimit = -1;
                int.TryParse(_sLimit, out _nLimit);
                if (_nLimit == 0)
                {
                    Debug.fTrace("No API access remain, try alt method... for " + _oData.sURL);
                    if (_oData.sCustom != "")
                    {
                        Debug.fTrace("Try the alternative method with recommend ver :" + _oData.sAltURL + "?after=" + _oData.sCustom);
                        _oData.nCustom = 1;                                 //Try the alternative method with recommend ver
                        Http.fGetHttp(_oData.sAltURL + "?after=" + _oData.sCustom, _oData);
                    }
                    else
                    {
                        Debug.fTrace("Try the alternative method & Just get last version: " + _oData.sAltURL);
                        _oData.nCustom = 4;                                 //Try the alternative method & Just get last version
                        Http.fGetHttp(_oData.sAltURL, _oData);
                    }
                    return;
                }
            }


            /*
             * if(_oData.bFail) {
             *      Debug.fTrace("Fail: " + _oData.sFailMsg + " " + _oData.sURL );
             *      if(_oData.oHeader != null) {
             *              int _nLimit = -1;
             *              int.TryParse( _oData.oHeader["X-RateLimit-Remaining"], out _nLimit);
             *              if(_nLimit == 0) {
             *                      Debug.fTrace("No API access remain, try alt method..." );
             *              }
             *      }
             *      if(_oData.sCustom != "") {
             *              _oData.nCustom = 1; //Try the alternative method with recommend ver
             *              Http.fGetHttp( _oData.sAltURL + "?after=" + _oData.sCustom, _oData);
             *      }else {
             *              _oData.nCustom = 4; //Try the alternative method & Just get last version
             *              Http.fGetHttp( _oData.sAltURL, _oData);
             *      }
             *
             *      return;
             * }*/


            Tags _aTags = (Tags)_oData.oContainer;
            Tags _aNewTags;

            //	Debug.fTrace("fGetTagData " + _oData.sResult);


            //_aNewTags = fExtractTags_API(_oData.sResult) ;
            _aNewTags = fExtractTags_API(fBetween(_oData.sResult, "[", "]", 0, true, true));


            if (_aNewTags != null)
            {
                _aTags.AddRange(_aNewTags);
            }
            else
            {
                Debug.fTrace("No new TAG ");
            }
//Console.WriteLine("assssssssssss");


            //			Debug.fTrace("--Count: " + _aNewTags.Count.ToString());
            //			Debug.fTrace("--last: " + _aNewTags[_aNewTags.Count-1].name);

            //	string _sNext = oPa oHeader
            string _sAllLink = "";

            if (_oData.oHeader != null)                        //C# http method
            {
                _sAllLink = _oData.oHeader["Link"];
            }
            else
            {
                _sAllLink = fBetween(_oData.sInfo, "Link:", "\n");
            }

            //		string _sModified = _oData.oHeader["Last-Modified"];
            //	int _nRemain = _oData.oHeader["X-RateLimit-Limit"];

            string _sNextLink = "";

            if (!FileUtils.IsEmpty(_sAllLink))
            {
                string[] _aLink = _sAllLink.Split(',');
                foreach (string _sLink in _aLink)
                {
                    //Debug.fTrace("Link: " + _sLink);
                    string[] _aLinkData = _sLink.Split(';');
                    if (_aLinkData[1].Trim() == "rel=\"next\"")
                    {
                        _sNextLink = fBetween(_aLinkData[0], "<", ">");
                        if (_sNextLink == "")                                 //If fail
                        {
                            _sNextLink = _aLinkData[0].Trim();
                        }
                    }
                }
            }


            if (_sNextLink != "")
            {
                Http.fGetHttp(_sNextLink, _oData);
            }
            else
            {
                _oData.dCustom(_oData);                         //Finish();
            }


            /*
             * //?after=v4.4.3
             * if(_aNewTags.Count == 10) { ///Have more -> get other
             *      string _sNewURL = _oData.sAltURL + "?after=" +  _aNewTags[_aNewTags.Count-1].name;
             *      Debug.fTrace("_sNewURL" + _sNewURL);
             *      JSON.fGetHttp( _sNewURL, _oData);
             * }else {
             *      _oData.dCustom(_oData); //Finish();
             * }*/
            ///_oData.dCustom(_oData); //Finish();
        }
Beispiel #10
0
        public static void fHttpRequestTagRelease(Object sender, DoWorkEventArgs e)
        {
            ParamHttp _oParam = (ParamHttp)e.Argument;

            e.Result = _oParam;

            //System.Net.ServicePointManager.ServerCertificateValidationCallback +=   (se, cert, chain, sslerror) => {  return true;};
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;



            if (_oParam.bDownloadFile)
            {
                try {
                    WebClient _oClient = new WebClient();
                    _oClient.Proxy = null; //Important remove slow proxy

                    _oClient.DownloadProgressChanged += fProgress(_oParam);
                    _oClient.DownloadFileCompleted   += _oParam.fComplete;

                    _oClient.DownloadFileAsync(new Uri(_oParam.sURL), _oParam.sToFile);
                    //   _oClient.DownloadFile(new Uri(_oParam.sURL), _oParam.sToFile);
                }
                catch (Exception ex)    {
                    _oParam.bFail    = true;
                    _oParam.sFailMsg = ex.Message;
                }
                return;
            }


            try {
                //onsole.WriteLine( _oParam.sURL);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_oParam.sURL);
                request.UserAgent = "Cwc - app";
                request.Proxy     = null;      //Important
                request.Accept    = "*/*";

//request.KeepAlive = false;

                WebResponse _response = null;
                Debug.fTrace("----GET URL ------ " + _oParam.sURL);

                try {
                    _response = request.GetResponse();
                }catch (Exception ex) {
                    //Debug.fTrace("----asss ------ " + _response.Headers);
                    //rogram.fDebug("----exr ------ " + ex.Message);

                    var wex = GetNestedException <WebException>(ex);
                    //rogram.fDebug("----wex ------ " + wex.Message);

                    // If there is no nested WebException, re-throw the exception.
                    if (wex == null)
                    {
                        throw;
                    }

                    // Get the response object.
                    _response     = wex.Response as HttpWebResponse;
                    _oParam.bFail = true;

                    // If it's not an HTTP response or is not error 403, re-throw.
                    if (_response == null)                  // || _response.StatusCode != HttpStatusCode.Forbidden
                    {
                        throw;
                    }
                }

                Stream       _stream = _response.GetResponseStream();
                StreamReader _reader = new StreamReader(_stream);
                _oParam.sResult = _reader.ReadToEnd();
                _oParam.oHeader = _response.Headers;
                //			Debug.fTrace("----_oParam.oHeader ------ " + _oParam.oHeader);

                //	Debug.fTrace("Header:" +  _response.Headers );
            }  catch (Exception ex)  {
                _oParam.bFail    = true;
                _oParam.sFailMsg = ex.Message;
            }
        }
Beispiel #11
0
        public static void      fHttpOutInfo(LaunchTool _oTool, string _sOut)
        {
            ParamHttp _oParam = (ParamHttp)_oTool.oCustom;

            //_oParam.sResult = _sOut;
            _oParam.sInfo += _sOut + "\n";
            //_oParam
            //Debug.fTrace("E: " + _sOut);

            //"Length:"
            if (_oParam.bDownloadFile)
            {
                int _nLimit = -1;
                if (_sOut.Length > 7 && _sOut[0] == 'L' && _sOut[6] == ':')
                {
                    string _sLength = _sOut.Substring(8);
                    //Debug.fTrace("_sLength: " + _sLength);
                    string _sSubLength = _sLength;
                    int    _nEnd       = _sSubLength.IndexOf(' ');
                    if (_nEnd != -1)
                    {
                        _sSubLength = _sSubLength.Substring(0, _nEnd);
                    }

                    int.TryParse(_sSubLength, out _nLimit);
                    //	Debug.fTrace("--Total!! " + _nLimit);
                }
                _oParam.nTotalBytes = _nLimit;
                //	Debug.fTrace("--_sOut[_sOut.Length - 1 ]!! " + _sOut[_sOut.Length - 1 ]);
                if (_sOut[_sOut.Length - 1] == 's')
                {
                    int _nEnd = _sOut.IndexOf('K');
                    if (_nEnd != -1)
                    {
                        string _sBytes = _sOut.Substring(0, _nEnd);
                        int    _nByte  = -1;
                        int.TryParse(_sBytes, out _nByte);
                        _oParam.nBytes = _nByte * 1000;
                        //	Debug.fTrace("--nBytes!! " + _oParam.nBytes);
                        fUrlProgress(_oParam);
                    }
                }

                // if(Data.oGuiConsole != null){ Data.oGuiConsole.Fctb.RemoveLines(aLineToDelete); aLineToDelete.Clear();}
                _sOut = _sOut.Trim();
                if (_sOut != "")
                {
                    if (_sOut.IndexOf("Cannot") >= 0 || _sOut.IndexOf("Bad") >= 0)
                    {
                        Output.TraceError("\rDownload: " + _sOut);
                    }
                    else
                    {
                        Output.TraceAction("\rDownload: " + _sOut);
                    }
                }

                // if(Data.oGuiConsole != null){ aLineToDelete.Add( Data.oGuiConsole.Fctb.LinesCount-1); }

                Debug.fRPrint("Download: " + _sOut + "                                                                                                 ");
            }
            ////	_oParam.oHeader = _response.Headers;
        }
Beispiel #12
0
 /// </summary>
 public static void fGetHttp(string _sURL, ParamHttp _oParam)
 {
     _oParam.sURL = _sURL;
     fGetHttp(_oParam);
 }
Beispiel #13
0
        public static void fDownload(string _sURL, string _sToFile, dHttpComplete _dComplete, dHttpComplete _dProgress, Object _oContainer = null, Object _oObj = null, dHttpComplete _dCustom = null, string _sAltURL = "", string _sCustom = "", int _nCustom = 0)
        {
            ParamHttp _oParam = new ParamHttp(_sURL, _sToFile, _dComplete, _dProgress, _oContainer, _oObj, _dCustom, _sAltURL, _sCustom, _nCustom);

            fGetHttp(_oParam);
        }
Beispiel #14
0
        public static void fGetTagDataALT(ParamHttp _oData)
        {
            Tags _aTags = (Tags)_oData.oContainer;
            Tags _aNewTags;

            _aNewTags = fExtractTags_STD(_oData.sResult);

            switch (_oData.nCustom)
            {
            case 1: {                     //1 = Ver after recommended version -> then get recommended
                string _sPreviousLink = fBetween(_oData.sResult, "\"pagination\"><a href=\"", "\"");
                _oData.nCustom = 2;
                _aTags.AddRange(_aNewTags);

                //Debug.fTrace("!_sPreviousLink!! " + _sPreviousLink);
                Http.fGetHttp(_sPreviousLink, _oData);
            } break;

            case 2: {                    //2 = Ver with recommended version -> then get most recent
                _oData.nCustom = 3;
                //_aTags.InsertRange(0,_aNewTags);

                string _sFistTag = _aTags[0].name;
                int    i         = 0;
                foreach (Tag _oTag in _aNewTags)
                {
                    if (_oTag.name != _sFistTag)
                    {
                        _aTags.Insert(i, _oTag);
                        i++;
                    }
                    else
                    {
                        break;
                    }
                }

                //Debug.fTrace("---Test1: " + _oData.sAltURL );
                //Debug.fTrace("---Test2: " + _oData.sURL );
                if (_oData.sURL != _oData.sAltURL)
                {
                    Http.fGetHttp(_oData.sAltURL, _oData);                             //Get most recent ver
                }
                else
                {
                    //Already got, stop here

                    _oData.dCustom(_oData);                             //Finish();
                }
            } break;

            case 3: {                    //3 = Get most recent date add but don't double it
                                         //_aTags.Insert();

                string _sFistTag = _aTags[0].name;
                int    i         = 0;
                foreach (Tag _oTag in _aNewTags)
                {
                    if (_oTag.name != _sFistTag)
                    {
                        _aTags.Insert(i, _oTag);
                        i++;
                    }
                    else
                    {
                        break;
                    }
                }
                _oData.dCustom(_oData);                         //Finish();
            } break;



            default:
                _aTags.AddRange(_aNewTags);
                _oData.dCustom(_oData);                         //Finish();
                break;
            }
        }