Beispiel #1
0
        private void EnsureStream()
        {
            if (m_s != null)
            {
                return;
            }

            StatusStateInfo st = StatusUtil.Begin("Downloading file...");

            try
            {
                m_bFileDownloaded = false;
                m_exDownload      = null;

                WaitCallback wc = new WaitCallback(DownloadFileProc);
                ThreadPool.QueueUserWorkItem(wc);

                bool bFinished = false;
                while (!bFinished)
                {
                    lock (m_objDownloadSync) { bFinished = m_bFileDownloaded; }

                    Thread.Sleep(100);
                    Application.DoEvents();
                }

                if (m_exDownload != null)
                {
                    throw m_exDownload;
                }

                m_s = new FileStream(m_strTempFile, m_fm, m_fa, m_fs);
            }
            finally { StatusUtil.End(st); }
        }
        // private static string GetHostKey(string strError)
        // {
        //	strError = strError.Replace("\r\n", "\n");
        //	strError = strError.Replace("\r", "\n");
        //	string[] vLines = strError.Split(new char[] { '\n' },
        //		StringSplitOptions.RemoveEmptyEntries);
        //	foreach(string strLineIt in vLines)
        //	{
        //		if(strLineIt.StartsWith("ssh-rsa")) return strLineIt;
        //		if(strLineIt.StartsWith("ssh-dss")) return strLineIt;
        //	}
        //	return null;
        // }

        private WebResponse RunOp(string strTempFile, string strSessionUrl,
                                  string strRemotePath)
        {
            WswrOp wOp;

            if (m_strMethod == IOConnection.WrmDeleteFile)
            {
                wOp = WswrOp.Delete;
            }
            else if (m_strMethod == IOConnection.WrmMoveFile)
            {
                wOp = WswrOp.Move;
            }
            else if (m_lRequestData.Count > 0)
            {
                wOp = WswrOp.Upload;
            }
            else
            {
                wOp = WswrOp.Download;
            }

            if (wOp == WswrOp.Upload)
            {
                File.WriteAllBytes(strTempFile, m_lRequestData.ToArray());
            }

            // StringBuilder sbHostKeyDetect = InitScript();
            // sbHostKeyDetect.AppendLine(AddCommonOpenOptions(
            //	"open " + strSessionUrl, strSessionUrl, string.Empty));
            // sbHostKeyDetect.AppendLine("exit");
            // string strHostKey = null;
            // try { WinScpExecutor.RunScript(sbHostKeyDetect.ToString()); }
            // catch(Exception exProbe)
            // {
            //	strHostKey = GetHostKey(exProbe.Message);
            //	if(string.IsNullOrEmpty(strHostKey)) throw;
            // }

            string strScript;

            if (wOp == WswrOp.Download)            // Test file exists
            {
                strScript = BuildScript(WswrOp.Download, true, strTempFile,
                                        strSessionUrl, strRemotePath);
                string strResult = WinScpExecutor.RunScript(strScript);

                string strRemoteFile = UrlUtil.GetFileName(strRemotePath);

                strResult = strResult.Replace("\r\n", "\n");
                strResult = strResult.Replace("\r", "\n");
                string[] vLines = strResult.Split(new char[] { '\n' },
                                                  StringSplitOptions.RemoveEmptyEntries);
                bool bFound = false;
                foreach (string strLine in vLines)
                {
                    if ((strLine == strRemoteFile) ||
                        strLine.EndsWith(" " + strRemoteFile) ||
                        strLine.EndsWith("\t" + strRemoteFile) ||
                        strLine.EndsWith("/" + strRemoteFile))
                    {
                        bFound = true;
                        break;
                    }
                }
                if (!bFound)
                {
                    throw new FileNotFoundException("Remote file not found!");
                }
            }

            strScript = BuildScript(wOp, false, strTempFile, strSessionUrl,
                                    strRemotePath);

            StatusStateInfo st = null;

            if (wOp == WswrOp.Upload)
            {
                st = StatusUtil.Begin("Uploading file...");
            }

            WebResponse wr;

            try
            {
                if (wOp == WswrOp.Upload)
                {
                    object[] vState = new object[4];
                    vState[0] = m_uri;
                    vState[1] = strScript;
                    vState[2] = strTempFile;
                    vState[3] = m_whcHeaders;

                    m_bUploadResponseReady = false;
                    m_exUpload             = null;

                    object objState = (object)vState;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(
                                                     CreateUploadResponse), objState);

                    bool bReady = false;
                    while (!bReady)
                    {
                        lock (objState) { bReady = m_bUploadResponseReady; }

                        Thread.Sleep(100);
                        Application.DoEvents();
                    }

                    wr = m_wswrUpload;
                    if (m_exUpload != null)
                    {
                        throw m_exUpload;
                    }
                }
                else
                {
                    wr = new WinScpWebResponse(m_uri, strScript, strTempFile,
                                               (wOp == WswrOp.Download), m_whcHeaders);
                }
            }
            finally
            {
                if (wOp == WswrOp.Upload)
                {
                    StatusUtil.End(st);
                }
            }

            return(wr);
        }