示例#1
0
        private void DownloadFileProc(object objState)
        {
            try { WinScpExecutor.RunScript(m_strScript); }
            catch (Exception exDl) { m_exDownload = exDl; }

            lock (m_objDownloadSync) { m_bFileDownloaded = true; }
        }
示例#2
0
        public override Stream GetResponseStream()
        {
            if (m_sResponse != null)
            {
                return(m_sResponse);
            }

            if (!m_bJit)
            {
                WinScpExecutor.RunScript(m_strScript);
            }

            if (string.IsNullOrEmpty(m_strDataFile) || !m_bJit)
            {
                byte[] pb = new byte[0];
                return(new MemoryStream(pb, false));
            }

            m_sResponse = new WinScpJitStream(m_strScript, m_strDataFile,
                                              FileMode.Open, FileAccess.Read, FileShare.Read);
            return(m_sResponse);
        }
        // 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);
        }