Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef __nsFtpState__h_ |
michael@0 | 7 | #define __nsFtpState__h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsBaseContentStream.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsICacheListener.h" |
michael@0 | 12 | #include "nsString.h" |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsIAsyncInputStream.h" |
michael@0 | 15 | #include "nsAutoPtr.h" |
michael@0 | 16 | #include "nsITransport.h" |
michael@0 | 17 | #include "mozilla/net/DNS.h" |
michael@0 | 18 | #include "nsFtpControlConnection.h" |
michael@0 | 19 | #include "nsIProtocolProxyCallback.h" |
michael@0 | 20 | |
michael@0 | 21 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 22 | #include "nsINetworkManager.h" |
michael@0 | 23 | #include "nsProxyRelease.h" |
michael@0 | 24 | #endif |
michael@0 | 25 | |
michael@0 | 26 | // ftp server types |
michael@0 | 27 | #define FTP_GENERIC_TYPE 0 |
michael@0 | 28 | #define FTP_UNIX_TYPE 1 |
michael@0 | 29 | #define FTP_VMS_TYPE 8 |
michael@0 | 30 | #define FTP_NT_TYPE 9 |
michael@0 | 31 | #define FTP_OS2_TYPE 11 |
michael@0 | 32 | |
michael@0 | 33 | // ftp states |
michael@0 | 34 | typedef enum _FTP_STATE { |
michael@0 | 35 | /////////////////////// |
michael@0 | 36 | //// Internal states |
michael@0 | 37 | FTP_INIT, |
michael@0 | 38 | FTP_WAIT_CACHE, |
michael@0 | 39 | FTP_READ_CACHE, |
michael@0 | 40 | FTP_COMMAND_CONNECT, |
michael@0 | 41 | FTP_READ_BUF, |
michael@0 | 42 | FTP_ERROR, |
michael@0 | 43 | FTP_COMPLETE, |
michael@0 | 44 | |
michael@0 | 45 | /////////////////////// |
michael@0 | 46 | //// Command channel connection setup states |
michael@0 | 47 | FTP_S_USER, FTP_R_USER, |
michael@0 | 48 | FTP_S_PASS, FTP_R_PASS, |
michael@0 | 49 | FTP_S_SYST, FTP_R_SYST, |
michael@0 | 50 | FTP_S_ACCT, FTP_R_ACCT, |
michael@0 | 51 | FTP_S_TYPE, FTP_R_TYPE, |
michael@0 | 52 | FTP_S_CWD, FTP_R_CWD, |
michael@0 | 53 | FTP_S_SIZE, FTP_R_SIZE, |
michael@0 | 54 | FTP_S_MDTM, FTP_R_MDTM, |
michael@0 | 55 | FTP_S_REST, FTP_R_REST, |
michael@0 | 56 | FTP_S_RETR, FTP_R_RETR, |
michael@0 | 57 | FTP_S_STOR, FTP_R_STOR, |
michael@0 | 58 | FTP_S_LIST, FTP_R_LIST, |
michael@0 | 59 | FTP_S_PASV, FTP_R_PASV, |
michael@0 | 60 | FTP_S_PWD, FTP_R_PWD, |
michael@0 | 61 | FTP_S_FEAT, FTP_R_FEAT, |
michael@0 | 62 | FTP_S_OPTS, FTP_R_OPTS |
michael@0 | 63 | } FTP_STATE; |
michael@0 | 64 | |
michael@0 | 65 | // higher level ftp actions |
michael@0 | 66 | typedef enum _FTP_ACTION {GET, PUT} FTP_ACTION; |
michael@0 | 67 | |
michael@0 | 68 | class nsFtpChannel; |
michael@0 | 69 | class nsICancelable; |
michael@0 | 70 | class nsICacheEntryDescriptor; |
michael@0 | 71 | class nsIProxyInfo; |
michael@0 | 72 | class nsIStreamListener; |
michael@0 | 73 | |
michael@0 | 74 | // The nsFtpState object is the content stream for the channel. It implements |
michael@0 | 75 | // nsIInputStreamCallback, so it can read data from the control connection. It |
michael@0 | 76 | // implements nsITransportEventSink so it can mix status events from both the |
michael@0 | 77 | // control connection and the data connection. |
michael@0 | 78 | |
michael@0 | 79 | class nsFtpState : public nsBaseContentStream, |
michael@0 | 80 | public nsIInputStreamCallback, |
michael@0 | 81 | public nsITransportEventSink, |
michael@0 | 82 | public nsICacheListener, |
michael@0 | 83 | public nsIRequestObserver, |
michael@0 | 84 | public nsFtpControlConnectionListener, |
michael@0 | 85 | public nsIProtocolProxyCallback |
michael@0 | 86 | { |
michael@0 | 87 | public: |
michael@0 | 88 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 89 | NS_DECL_NSIINPUTSTREAMCALLBACK |
michael@0 | 90 | NS_DECL_NSITRANSPORTEVENTSINK |
michael@0 | 91 | NS_DECL_NSICACHELISTENER |
michael@0 | 92 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 93 | NS_DECL_NSIPROTOCOLPROXYCALLBACK |
michael@0 | 94 | |
michael@0 | 95 | // Override input stream methods: |
michael@0 | 96 | NS_IMETHOD CloseWithStatus(nsresult status); |
michael@0 | 97 | NS_IMETHOD Available(uint64_t *result); |
michael@0 | 98 | NS_IMETHOD ReadSegments(nsWriteSegmentFun fun, void *closure, |
michael@0 | 99 | uint32_t count, uint32_t *result); |
michael@0 | 100 | |
michael@0 | 101 | // nsFtpControlConnectionListener methods: |
michael@0 | 102 | virtual void OnControlDataAvailable(const char *data, uint32_t dataLen); |
michael@0 | 103 | virtual void OnControlError(nsresult status); |
michael@0 | 104 | |
michael@0 | 105 | nsFtpState(); |
michael@0 | 106 | nsresult Init(nsFtpChannel *channel); |
michael@0 | 107 | |
michael@0 | 108 | protected: |
michael@0 | 109 | // Notification from nsBaseContentStream::AsyncWait |
michael@0 | 110 | virtual void OnCallbackPending(); |
michael@0 | 111 | |
michael@0 | 112 | private: |
michael@0 | 113 | virtual ~nsFtpState(); |
michael@0 | 114 | |
michael@0 | 115 | /////////////////////////////////// |
michael@0 | 116 | // BEGIN: STATE METHODS |
michael@0 | 117 | nsresult S_user(); FTP_STATE R_user(); |
michael@0 | 118 | nsresult S_pass(); FTP_STATE R_pass(); |
michael@0 | 119 | nsresult S_syst(); FTP_STATE R_syst(); |
michael@0 | 120 | nsresult S_acct(); FTP_STATE R_acct(); |
michael@0 | 121 | |
michael@0 | 122 | nsresult S_type(); FTP_STATE R_type(); |
michael@0 | 123 | nsresult S_cwd(); FTP_STATE R_cwd(); |
michael@0 | 124 | |
michael@0 | 125 | nsresult S_size(); FTP_STATE R_size(); |
michael@0 | 126 | nsresult S_mdtm(); FTP_STATE R_mdtm(); |
michael@0 | 127 | nsresult S_list(); FTP_STATE R_list(); |
michael@0 | 128 | |
michael@0 | 129 | nsresult S_rest(); FTP_STATE R_rest(); |
michael@0 | 130 | nsresult S_retr(); FTP_STATE R_retr(); |
michael@0 | 131 | nsresult S_stor(); FTP_STATE R_stor(); |
michael@0 | 132 | nsresult S_pasv(); FTP_STATE R_pasv(); |
michael@0 | 133 | nsresult S_pwd(); FTP_STATE R_pwd(); |
michael@0 | 134 | nsresult S_feat(); FTP_STATE R_feat(); |
michael@0 | 135 | nsresult S_opts(); FTP_STATE R_opts(); |
michael@0 | 136 | // END: STATE METHODS |
michael@0 | 137 | /////////////////////////////////// |
michael@0 | 138 | |
michael@0 | 139 | // internal methods |
michael@0 | 140 | void MoveToNextState(FTP_STATE nextState); |
michael@0 | 141 | nsresult Process(); |
michael@0 | 142 | |
michael@0 | 143 | void KillControlConnection(); |
michael@0 | 144 | nsresult StopProcessing(); |
michael@0 | 145 | nsresult EstablishControlConnection(); |
michael@0 | 146 | nsresult SendFTPCommand(const nsCSubstring& command); |
michael@0 | 147 | void ConvertFilespecToVMS(nsCString& fileSpec); |
michael@0 | 148 | void ConvertDirspecToVMS(nsCString& fileSpec); |
michael@0 | 149 | void ConvertDirspecFromVMS(nsCString& fileSpec); |
michael@0 | 150 | nsresult BuildStreamConverter(nsIStreamListener** convertStreamListener); |
michael@0 | 151 | nsresult SetContentType(); |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * This method is called to kick-off the FTP state machine. mState is |
michael@0 | 155 | * reset to FTP_COMMAND_CONNECT, and the FTP state machine progresses from |
michael@0 | 156 | * there. This method is initially called (indirectly) from the channel's |
michael@0 | 157 | * AsyncOpen implementation. |
michael@0 | 158 | */ |
michael@0 | 159 | void Connect(); |
michael@0 | 160 | |
michael@0 | 161 | /** |
michael@0 | 162 | * This method opens a cache entry for reading or writing depending on the |
michael@0 | 163 | * state of the channel and of the system (e.g., opened for reading if we |
michael@0 | 164 | * are offline). This method is responsible for setting mCacheEntry if |
michael@0 | 165 | * there is a cache entry that can be used. It returns true if it ends up |
michael@0 | 166 | * waiting (asynchronously) for access to the cache entry. In that case, |
michael@0 | 167 | * the nsFtpState's OnCacheEntryAvailable method will be called once the |
michael@0 | 168 | * cache entry is available or if an error occurs. |
michael@0 | 169 | */ |
michael@0 | 170 | bool CheckCache(); |
michael@0 | 171 | |
michael@0 | 172 | /** |
michael@0 | 173 | * This method returns true if the data for this URL can be read from the |
michael@0 | 174 | * cache. This method assumes that mCacheEntry is non-null. |
michael@0 | 175 | */ |
michael@0 | 176 | bool CanReadCacheEntry(); |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * This method causes the cache entry to be read. Data from the cache |
michael@0 | 180 | * entry will be fed to the channel's listener. This method returns true |
michael@0 | 181 | * if successfully reading from the cache. This method assumes that |
michael@0 | 182 | * mCacheEntry is non-null and opened with read access. |
michael@0 | 183 | */ |
michael@0 | 184 | bool ReadCacheEntry(); |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * This method configures mDataStream with an asynchronous input stream to |
michael@0 | 188 | * the cache entry. The cache entry is read on a background thread. This |
michael@0 | 189 | * method assumes that mCacheEntry is non-null and opened with read access. |
michael@0 | 190 | */ |
michael@0 | 191 | nsresult OpenCacheDataStream(); |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * This method inserts the cache entry's output stream into the stream |
michael@0 | 195 | * listener chain for the FTP channel. As a result, the cache entry |
michael@0 | 196 | * receives data as data is pushed to the channel's listener. This method |
michael@0 | 197 | * assumes that mCacheEntry is non-null and opened with write access. |
michael@0 | 198 | */ |
michael@0 | 199 | nsresult InstallCacheListener(); |
michael@0 | 200 | |
michael@0 | 201 | /////////////////////////////////// |
michael@0 | 202 | // Private members |
michael@0 | 203 | |
michael@0 | 204 | // ****** state machine vars |
michael@0 | 205 | FTP_STATE mState; // the current state |
michael@0 | 206 | FTP_STATE mNextState; // the next state |
michael@0 | 207 | bool mKeepRunning; // thread event loop boolean |
michael@0 | 208 | int32_t mResponseCode; // the last command response code |
michael@0 | 209 | nsCString mResponseMsg; // the last command response text |
michael@0 | 210 | |
michael@0 | 211 | // ****** channel/transport/stream vars |
michael@0 | 212 | nsRefPtr<nsFtpControlConnection> mControlConnection; // cacheable control connection (owns mCPipe) |
michael@0 | 213 | bool mReceivedControlData; |
michael@0 | 214 | bool mTryingCachedControl; // retrying the password |
michael@0 | 215 | bool mRETRFailed; // Did we already try a RETR and it failed? |
michael@0 | 216 | uint64_t mFileSize; |
michael@0 | 217 | nsCString mModTime; |
michael@0 | 218 | |
michael@0 | 219 | // ****** consumer vars |
michael@0 | 220 | nsRefPtr<nsFtpChannel> mChannel; // our owning FTP channel we pass through our events |
michael@0 | 221 | nsCOMPtr<nsIProxyInfo> mProxyInfo; |
michael@0 | 222 | |
michael@0 | 223 | // ****** connection cache vars |
michael@0 | 224 | int32_t mServerType; // What kind of server are we talking to |
michael@0 | 225 | |
michael@0 | 226 | // ****** protocol interpretation related state vars |
michael@0 | 227 | nsString mUsername; // username |
michael@0 | 228 | nsString mPassword; // password |
michael@0 | 229 | FTP_ACTION mAction; // the higher level action (GET/PUT) |
michael@0 | 230 | bool mAnonymous; // try connecting anonymous (default) |
michael@0 | 231 | bool mRetryPass; // retrying the password |
michael@0 | 232 | bool mStorReplyReceived; // FALSE if waiting for STOR |
michael@0 | 233 | // completion status from server |
michael@0 | 234 | nsresult mInternalError; // represents internal state errors |
michael@0 | 235 | bool mReconnectAndLoginAgain; |
michael@0 | 236 | bool mCacheConnection; |
michael@0 | 237 | |
michael@0 | 238 | // ****** URI vars |
michael@0 | 239 | int32_t mPort; // the port to connect to |
michael@0 | 240 | nsString mFilename; // url filename (if any) |
michael@0 | 241 | nsCString mPath; // the url's path |
michael@0 | 242 | nsCString mPwd; // login Path |
michael@0 | 243 | |
michael@0 | 244 | // ****** other vars |
michael@0 | 245 | nsCOMPtr<nsITransport> mDataTransport; |
michael@0 | 246 | nsCOMPtr<nsIAsyncInputStream> mDataStream; |
michael@0 | 247 | nsCOMPtr<nsIRequest> mUploadRequest; |
michael@0 | 248 | bool mAddressChecked; |
michael@0 | 249 | bool mServerIsIPv6; |
michael@0 | 250 | bool mUseUTF8; |
michael@0 | 251 | |
michael@0 | 252 | static uint32_t mSessionStartTime; |
michael@0 | 253 | |
michael@0 | 254 | mozilla::net::NetAddr mServerAddress; |
michael@0 | 255 | |
michael@0 | 256 | // ***** control read gvars |
michael@0 | 257 | nsresult mControlStatus; |
michael@0 | 258 | nsCString mControlReadCarryOverBuf; |
michael@0 | 259 | |
michael@0 | 260 | nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry; |
michael@0 | 261 | bool mDoomCache; |
michael@0 | 262 | |
michael@0 | 263 | nsCString mSuppliedEntityID; |
michael@0 | 264 | |
michael@0 | 265 | nsCOMPtr<nsICancelable> mProxyRequest; |
michael@0 | 266 | bool mDeferredCallbackPending; |
michael@0 | 267 | |
michael@0 | 268 | // These members are used for network per-app metering (bug 855948) |
michael@0 | 269 | // Currently, they are only available on gonk. |
michael@0 | 270 | uint64_t mCountRecv; |
michael@0 | 271 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 272 | nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork; |
michael@0 | 273 | #endif |
michael@0 | 274 | nsresult SaveNetworkStats(bool); |
michael@0 | 275 | void CountRecvBytes(uint64_t recvBytes) |
michael@0 | 276 | { |
michael@0 | 277 | mCountRecv += recvBytes; |
michael@0 | 278 | SaveNetworkStats(false); |
michael@0 | 279 | } |
michael@0 | 280 | }; |
michael@0 | 281 | |
michael@0 | 282 | #endif //__nsFtpState__h_ |