Back to index

lightning-sunbird  0.9+nobinonly
nsFtpConnectionThread.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Bradley Baetz <bbaetz@student.usyd.edu.au>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef __nsFtpState__h_
00040 #define __nsFtpState__h_
00041 
00042 #include "ftpCore.h"
00043 #include "nsInt64.h"
00044 #include "nsIThread.h"
00045 #include "nsIRunnable.h"
00046 #include "nsISocketTransportService.h"
00047 #include "nsISocketTransport.h"
00048 #include "nsIServiceManager.h"
00049 #include "nsIStreamListener.h"
00050 #include "nsIURI.h"
00051 #include "prtime.h"
00052 #include "nsString.h"
00053 #include "nsIFTPChannel.h"
00054 #include "nsIProtocolHandler.h"
00055 #include "nsCOMPtr.h"
00056 #include "nsIInputStream.h"
00057 #include "nsIOutputStream.h"
00058 #include "nsAutoLock.h"
00059 #include "nsAutoPtr.h"
00060 #include "nsIEventQueueService.h"
00061 #include "nsIPrompt.h"
00062 #include "nsIAuthPrompt.h"
00063 #include "nsITransport.h"
00064 #include "nsIProxyInfo.h"
00065 
00066 #include "nsFtpControlConnection.h"
00067 
00068 #include "nsICacheEntryDescriptor.h"
00069 
00070 // ftp server types
00071 #define FTP_GENERIC_TYPE     0
00072 #define FTP_UNIX_TYPE        1
00073 #define FTP_VMS_TYPE         8
00074 #define FTP_NT_TYPE          9
00075 #define FTP_OS2_TYPE         11
00076 
00077 // ftp states
00078 typedef enum _FTP_STATE {
00081     FTP_COMMAND_CONNECT,
00082     FTP_READ_BUF,
00083     FTP_ERROR,
00084     FTP_COMPLETE,
00085 
00088     FTP_S_USER, FTP_R_USER,
00089     FTP_S_PASS, FTP_R_PASS,
00090     FTP_S_SYST, FTP_R_SYST,
00091     FTP_S_ACCT, FTP_R_ACCT,
00092     FTP_S_TYPE, FTP_R_TYPE,
00093     FTP_S_CWD,  FTP_R_CWD,
00094     FTP_S_SIZE, FTP_R_SIZE,
00095     FTP_S_MDTM, FTP_R_MDTM,
00096     FTP_S_REST, FTP_R_REST,
00097     FTP_S_RETR, FTP_R_RETR,
00098     FTP_S_STOR, FTP_R_STOR,
00099     FTP_S_LIST, FTP_R_LIST,
00100     FTP_S_PASV, FTP_R_PASV,
00101     FTP_S_PWD,  FTP_R_PWD
00102 } FTP_STATE;
00103 
00104 // higher level ftp actions
00105 typedef enum _FTP_ACTION {GET, PUT} FTP_ACTION;
00106 
00107 class DataRequestForwarder;
00108 class nsFTPChannel;
00109 
00110 class nsFtpState : public nsIStreamListener,
00111                    public nsIRequest {
00112 public:
00113     NS_DECL_ISUPPORTS
00114     NS_DECL_NSISTREAMLISTENER
00115     NS_DECL_NSIREQUESTOBSERVER
00116     NS_DECL_NSIREQUEST
00117 
00118     nsFtpState();
00119     virtual ~nsFtpState();
00120 
00121     nsresult Init(nsFTPChannel *aChannel, 
00122                   nsICacheEntryDescriptor* cacheEntry,
00123                   nsIProxyInfo* proxyInfo,
00124                   PRUint64 startPos,
00125                   const nsACString& entity);
00126 
00127     // use this to provide a stream to be written to the server.
00128     nsresult SetWriteStream(nsIInputStream* aInStream);
00129 
00130     nsresult GetEntityID(nsACString& aEntityID);
00131 
00132     nsresult Connect();
00133 
00134     // lets the data forwarder tell us when the the data pipe has been created
00135     // and when the data pipe has finished.
00136     void DataConnectionEstablished();    
00137     void DataConnectionComplete();
00138 private:
00140     // BEGIN: STATE METHODS
00141     nsresult        S_user(); FTP_STATE       R_user();
00142     nsresult        S_pass(); FTP_STATE       R_pass();
00143     nsresult        S_syst(); FTP_STATE       R_syst();
00144     nsresult        S_acct(); FTP_STATE       R_acct();
00145 
00146     nsresult        S_type(); FTP_STATE       R_type();
00147     nsresult        S_cwd();  FTP_STATE       R_cwd();
00148 
00149     nsresult        S_size(); FTP_STATE       R_size();
00150     nsresult        S_mdtm(); FTP_STATE       R_mdtm();
00151     nsresult        S_list(); FTP_STATE       R_list();
00152 
00153     nsresult        S_rest(); FTP_STATE       R_rest();
00154     nsresult        S_retr(); FTP_STATE       R_retr();
00155     nsresult        S_stor(); FTP_STATE       R_stor();
00156     nsresult        S_pasv(); FTP_STATE       R_pasv();
00157     nsresult        S_pwd();  FTP_STATE       R_pwd();
00158     // END: STATE METHODS
00160 
00161     // internal methods
00162     void        MoveToNextState(FTP_STATE nextState);
00163     nsresult    Process();
00164 
00165     void KillControlConnection();
00166     nsresult StopProcessing();
00167     nsresult EstablishControlConnection();
00168     nsresult SendFTPCommand(nsCString& command);
00169     void ConvertFilespecToVMS(nsCString& fileSpec);
00170     void ConvertDirspecToVMS(nsCString& fileSpec);
00171     void ConvertDirspecFromVMS(nsCString& fileSpec);
00172     nsresult BuildStreamConverter(nsIStreamListener** convertStreamListener);
00173     nsresult SetContentType();
00174     PRBool CanReadEntry();
00175 
00177     // Private members
00178 
00179         // ****** state machine vars
00180     FTP_STATE           mState;             // the current state
00181     FTP_STATE           mNextState;         // the next state
00182     PRPackedBool        mKeepRunning;       // thread event loop boolean
00183     PRInt32             mResponseCode;      // the last command response code
00184     nsCAutoString       mResponseMsg;       // the last command response text
00185 
00186         // ****** channel/transport/stream vars 
00187     nsFtpControlConnection*         mControlConnection;       // cacheable control connection (owns mCPipe)
00188     PRPackedBool                    mReceivedControlData;  
00189     PRPackedBool                    mTryingCachedControl;     // retrying the password
00190     PRPackedBool                    mWaitingForDConn;         // Are we wait for a data connection
00191     PRPackedBool                    mRETRFailed;              // Did we already try a RETR and it failed?
00192     nsCOMPtr<nsISocketTransport>    mDPipe;                   // the data transport
00193     nsCOMPtr<nsIRequest>            mDPipeRequest;
00194     DataRequestForwarder*           mDRequestForwarder;
00195     PRUint64                        mFileSize;
00196     nsCString                       mModTime;
00197 
00198         // ****** consumer vars
00199     nsRefPtr<nsFTPChannel>          mChannel;         // our owning FTP channel we pass through our events
00200     nsCOMPtr<nsIProxyInfo>          mProxyInfo;
00201 
00202         // ****** connection cache vars
00203     PRInt32             mServerType;    // What kind of server are we talking to
00204     PRPackedBool        mList;          // Use LIST instead of NLST
00205 
00206         // ****** protocol interpretation related state vars
00207     nsString            mUsername;      // username
00208     nsString            mPassword;      // password
00209     FTP_ACTION          mAction;        // the higher level action (GET/PUT)
00210     PRPackedBool        mAnonymous;     // try connecting anonymous (default)
00211     PRPackedBool        mRetryPass;     // retrying the password
00212     nsresult            mInternalError; // represents internal state errors
00213 
00214         // ****** URI vars
00215     nsCOMPtr<nsIURI>       mURL;        // the uri we're connecting to
00216     PRInt32                mPort;       // the port to connect to
00217     nsString               mFilename;   // url filename (if any)
00218     nsCString              mPath;       // the url's path
00219     nsCString              mPwd;        // login Path
00220 
00221         // ****** other vars
00222     PRUint8                mSuspendCount;// number of times we've been suspended.
00223     PRUint32               mBufferSegmentSize;
00224     PRUint32               mBufferMaxSize;
00225     PRLock                 *mLock;
00226     nsCOMPtr<nsIInputStream> mWriteStream; // This stream is written to the server.
00227     PRUint32                 mWriteCount;
00228     PRPackedBool            mAddressChecked;
00229     PRPackedBool            mServerIsIPv6;
00230     
00231     static PRUint32         mSessionStartTime;
00232 
00233     char                    mServerAddress[64];
00234 
00235     // ***** control read gvars
00236     nsresult                mControlStatus;
00237     nsCAutoString           mControlReadCarryOverBuf;
00238 
00239     nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry;
00240     
00241     nsUint64 mStartPos;
00242     nsCString mSuppliedEntityID;
00243     nsCString mEntityID;
00244 };
00245 
00246 
00247 #endif //__nsFtpState__h_