netwerk/protocol/ftp/nsFtpConnectionThread.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/ftp/nsFtpConnectionThread.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,282 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef __nsFtpState__h_
    1.10 +#define __nsFtpState__h_
    1.11 +
    1.12 +#include "nsBaseContentStream.h"
    1.13 +
    1.14 +#include "nsICacheListener.h"
    1.15 +#include "nsString.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "nsIAsyncInputStream.h"
    1.18 +#include "nsAutoPtr.h"
    1.19 +#include "nsITransport.h"
    1.20 +#include "mozilla/net/DNS.h"
    1.21 +#include "nsFtpControlConnection.h"
    1.22 +#include "nsIProtocolProxyCallback.h"
    1.23 +
    1.24 +#ifdef MOZ_WIDGET_GONK
    1.25 +#include "nsINetworkManager.h"
    1.26 +#include "nsProxyRelease.h"
    1.27 +#endif
    1.28 +
    1.29 +// ftp server types
    1.30 +#define FTP_GENERIC_TYPE     0
    1.31 +#define FTP_UNIX_TYPE        1
    1.32 +#define FTP_VMS_TYPE         8
    1.33 +#define FTP_NT_TYPE          9
    1.34 +#define FTP_OS2_TYPE         11
    1.35 +
    1.36 +// ftp states
    1.37 +typedef enum _FTP_STATE {
    1.38 +///////////////////////
    1.39 +//// Internal states
    1.40 +    FTP_INIT,
    1.41 +    FTP_WAIT_CACHE,
    1.42 +    FTP_READ_CACHE,
    1.43 +    FTP_COMMAND_CONNECT,
    1.44 +    FTP_READ_BUF,
    1.45 +    FTP_ERROR,
    1.46 +    FTP_COMPLETE,
    1.47 +
    1.48 +///////////////////////
    1.49 +//// Command channel connection setup states
    1.50 +    FTP_S_USER, FTP_R_USER,
    1.51 +    FTP_S_PASS, FTP_R_PASS,
    1.52 +    FTP_S_SYST, FTP_R_SYST,
    1.53 +    FTP_S_ACCT, FTP_R_ACCT,
    1.54 +    FTP_S_TYPE, FTP_R_TYPE,
    1.55 +    FTP_S_CWD,  FTP_R_CWD,
    1.56 +    FTP_S_SIZE, FTP_R_SIZE,
    1.57 +    FTP_S_MDTM, FTP_R_MDTM,
    1.58 +    FTP_S_REST, FTP_R_REST,
    1.59 +    FTP_S_RETR, FTP_R_RETR,
    1.60 +    FTP_S_STOR, FTP_R_STOR,
    1.61 +    FTP_S_LIST, FTP_R_LIST,
    1.62 +    FTP_S_PASV, FTP_R_PASV,
    1.63 +    FTP_S_PWD,  FTP_R_PWD,
    1.64 +    FTP_S_FEAT, FTP_R_FEAT,
    1.65 +    FTP_S_OPTS, FTP_R_OPTS
    1.66 +} FTP_STATE;
    1.67 +
    1.68 +// higher level ftp actions
    1.69 +typedef enum _FTP_ACTION {GET, PUT} FTP_ACTION;
    1.70 +
    1.71 +class nsFtpChannel;
    1.72 +class nsICancelable;
    1.73 +class nsICacheEntryDescriptor;
    1.74 +class nsIProxyInfo;
    1.75 +class nsIStreamListener;
    1.76 +
    1.77 +// The nsFtpState object is the content stream for the channel.  It implements
    1.78 +// nsIInputStreamCallback, so it can read data from the control connection.  It
    1.79 +// implements nsITransportEventSink so it can mix status events from both the
    1.80 +// control connection and the data connection.
    1.81 +
    1.82 +class nsFtpState : public nsBaseContentStream,
    1.83 +                   public nsIInputStreamCallback,
    1.84 +                   public nsITransportEventSink,
    1.85 +                   public nsICacheListener,
    1.86 +                   public nsIRequestObserver,
    1.87 +                   public nsFtpControlConnectionListener,
    1.88 +                   public nsIProtocolProxyCallback
    1.89 +{
    1.90 +public:
    1.91 +    NS_DECL_ISUPPORTS_INHERITED
    1.92 +    NS_DECL_NSIINPUTSTREAMCALLBACK
    1.93 +    NS_DECL_NSITRANSPORTEVENTSINK
    1.94 +    NS_DECL_NSICACHELISTENER
    1.95 +    NS_DECL_NSIREQUESTOBSERVER
    1.96 +    NS_DECL_NSIPROTOCOLPROXYCALLBACK
    1.97 +
    1.98 +    // Override input stream methods:
    1.99 +    NS_IMETHOD CloseWithStatus(nsresult status);
   1.100 +    NS_IMETHOD Available(uint64_t *result);
   1.101 +    NS_IMETHOD ReadSegments(nsWriteSegmentFun fun, void *closure,
   1.102 +                            uint32_t count, uint32_t *result);
   1.103 +
   1.104 +    // nsFtpControlConnectionListener methods:
   1.105 +    virtual void OnControlDataAvailable(const char *data, uint32_t dataLen);
   1.106 +    virtual void OnControlError(nsresult status);
   1.107 +
   1.108 +    nsFtpState();
   1.109 +    nsresult Init(nsFtpChannel *channel);
   1.110 +
   1.111 +protected:
   1.112 +    // Notification from nsBaseContentStream::AsyncWait
   1.113 +    virtual void OnCallbackPending();
   1.114 +
   1.115 +private:
   1.116 +    virtual ~nsFtpState();
   1.117 +
   1.118 +    ///////////////////////////////////
   1.119 +    // BEGIN: STATE METHODS
   1.120 +    nsresult        S_user(); FTP_STATE       R_user();
   1.121 +    nsresult        S_pass(); FTP_STATE       R_pass();
   1.122 +    nsresult        S_syst(); FTP_STATE       R_syst();
   1.123 +    nsresult        S_acct(); FTP_STATE       R_acct();
   1.124 +
   1.125 +    nsresult        S_type(); FTP_STATE       R_type();
   1.126 +    nsresult        S_cwd();  FTP_STATE       R_cwd();
   1.127 +
   1.128 +    nsresult        S_size(); FTP_STATE       R_size();
   1.129 +    nsresult        S_mdtm(); FTP_STATE       R_mdtm();
   1.130 +    nsresult        S_list(); FTP_STATE       R_list();
   1.131 +
   1.132 +    nsresult        S_rest(); FTP_STATE       R_rest();
   1.133 +    nsresult        S_retr(); FTP_STATE       R_retr();
   1.134 +    nsresult        S_stor(); FTP_STATE       R_stor();
   1.135 +    nsresult        S_pasv(); FTP_STATE       R_pasv();
   1.136 +    nsresult        S_pwd();  FTP_STATE       R_pwd();
   1.137 +    nsresult        S_feat(); FTP_STATE       R_feat();
   1.138 +    nsresult        S_opts(); FTP_STATE       R_opts();
   1.139 +    // END: STATE METHODS
   1.140 +    ///////////////////////////////////
   1.141 +
   1.142 +    // internal methods
   1.143 +    void        MoveToNextState(FTP_STATE nextState);
   1.144 +    nsresult    Process();
   1.145 +
   1.146 +    void KillControlConnection();
   1.147 +    nsresult StopProcessing();
   1.148 +    nsresult EstablishControlConnection();
   1.149 +    nsresult SendFTPCommand(const nsCSubstring& command);
   1.150 +    void ConvertFilespecToVMS(nsCString& fileSpec);
   1.151 +    void ConvertDirspecToVMS(nsCString& fileSpec);
   1.152 +    void ConvertDirspecFromVMS(nsCString& fileSpec);
   1.153 +    nsresult BuildStreamConverter(nsIStreamListener** convertStreamListener);
   1.154 +    nsresult SetContentType();
   1.155 +
   1.156 +    /**
   1.157 +     * This method is called to kick-off the FTP state machine.  mState is
   1.158 +     * reset to FTP_COMMAND_CONNECT, and the FTP state machine progresses from
   1.159 +     * there.  This method is initially called (indirectly) from the channel's
   1.160 +     * AsyncOpen implementation.
   1.161 +     */
   1.162 +    void Connect();
   1.163 +
   1.164 +    /**
   1.165 +     * This method opens a cache entry for reading or writing depending on the
   1.166 +     * state of the channel and of the system (e.g., opened for reading if we
   1.167 +     * are offline).  This method is responsible for setting mCacheEntry if
   1.168 +     * there is a cache entry that can be used.  It returns true if it ends up
   1.169 +     * waiting (asynchronously) for access to the cache entry.  In that case,
   1.170 +     * the nsFtpState's OnCacheEntryAvailable method will be called once the
   1.171 +     * cache entry is available or if an error occurs.
   1.172 +     */
   1.173 +    bool CheckCache();
   1.174 +
   1.175 +    /**
   1.176 +     * This method returns true if the data for this URL can be read from the
   1.177 +     * cache.  This method assumes that mCacheEntry is non-null.
   1.178 +     */
   1.179 +    bool CanReadCacheEntry();
   1.180 +
   1.181 +    /**
   1.182 +     * This method causes the cache entry to be read.  Data from the cache
   1.183 +     * entry will be fed to the channel's listener.  This method returns true
   1.184 +     * if successfully reading from the cache.  This method assumes that
   1.185 +     * mCacheEntry is non-null and opened with read access.
   1.186 +     */
   1.187 +    bool ReadCacheEntry();
   1.188 +
   1.189 +    /**
   1.190 +     * This method configures mDataStream with an asynchronous input stream to
   1.191 +     * the cache entry.  The cache entry is read on a background thread.  This
   1.192 +     * method assumes that mCacheEntry is non-null and opened with read access.
   1.193 +     */
   1.194 +    nsresult OpenCacheDataStream();
   1.195 +
   1.196 +    /**
   1.197 +     * This method inserts the cache entry's output stream into the stream
   1.198 +     * listener chain for the FTP channel.  As a result, the cache entry
   1.199 +     * receives data as data is pushed to the channel's listener.  This method
   1.200 +     * assumes that mCacheEntry is non-null and opened with write access. 
   1.201 +     */
   1.202 +    nsresult InstallCacheListener();
   1.203 +
   1.204 +    ///////////////////////////////////
   1.205 +    // Private members
   1.206 +
   1.207 +        // ****** state machine vars
   1.208 +    FTP_STATE           mState;             // the current state
   1.209 +    FTP_STATE           mNextState;         // the next state
   1.210 +    bool                mKeepRunning;       // thread event loop boolean
   1.211 +    int32_t             mResponseCode;      // the last command response code
   1.212 +    nsCString           mResponseMsg;       // the last command response text
   1.213 +
   1.214 +        // ****** channel/transport/stream vars 
   1.215 +    nsRefPtr<nsFtpControlConnection> mControlConnection;       // cacheable control connection (owns mCPipe)
   1.216 +    bool                            mReceivedControlData;  
   1.217 +    bool                            mTryingCachedControl;     // retrying the password
   1.218 +    bool                            mRETRFailed;              // Did we already try a RETR and it failed?
   1.219 +    uint64_t                        mFileSize;
   1.220 +    nsCString                       mModTime;
   1.221 +
   1.222 +        // ****** consumer vars
   1.223 +    nsRefPtr<nsFtpChannel>          mChannel;         // our owning FTP channel we pass through our events
   1.224 +    nsCOMPtr<nsIProxyInfo>          mProxyInfo;
   1.225 +
   1.226 +        // ****** connection cache vars
   1.227 +    int32_t             mServerType;    // What kind of server are we talking to
   1.228 +
   1.229 +        // ****** protocol interpretation related state vars
   1.230 +    nsString            mUsername;      // username
   1.231 +    nsString            mPassword;      // password
   1.232 +    FTP_ACTION          mAction;        // the higher level action (GET/PUT)
   1.233 +    bool                mAnonymous;     // try connecting anonymous (default)
   1.234 +    bool                mRetryPass;     // retrying the password
   1.235 +    bool                mStorReplyReceived; // FALSE if waiting for STOR
   1.236 +                                            // completion status from server
   1.237 +    nsresult            mInternalError; // represents internal state errors
   1.238 +    bool                mReconnectAndLoginAgain;
   1.239 +    bool                mCacheConnection;
   1.240 +
   1.241 +        // ****** URI vars
   1.242 +    int32_t                mPort;       // the port to connect to
   1.243 +    nsString               mFilename;   // url filename (if any)
   1.244 +    nsCString              mPath;       // the url's path
   1.245 +    nsCString              mPwd;        // login Path
   1.246 +
   1.247 +        // ****** other vars
   1.248 +    nsCOMPtr<nsITransport>        mDataTransport;
   1.249 +    nsCOMPtr<nsIAsyncInputStream> mDataStream;
   1.250 +    nsCOMPtr<nsIRequest>    mUploadRequest;
   1.251 +    bool                    mAddressChecked;
   1.252 +    bool                    mServerIsIPv6;
   1.253 +    bool                    mUseUTF8;
   1.254 +
   1.255 +    static uint32_t         mSessionStartTime;
   1.256 +
   1.257 +    mozilla::net::NetAddr   mServerAddress;
   1.258 +
   1.259 +    // ***** control read gvars
   1.260 +    nsresult                mControlStatus;
   1.261 +    nsCString               mControlReadCarryOverBuf;
   1.262 +
   1.263 +    nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry;
   1.264 +    bool                    mDoomCache;
   1.265 +
   1.266 +    nsCString mSuppliedEntityID;
   1.267 +
   1.268 +    nsCOMPtr<nsICancelable>  mProxyRequest;
   1.269 +    bool                     mDeferredCallbackPending;
   1.270 +
   1.271 +// These members are used for network per-app metering (bug 855948)
   1.272 +// Currently, they are only available on gonk.
   1.273 +    uint64_t                           mCountRecv;
   1.274 +#ifdef MOZ_WIDGET_GONK
   1.275 +    nsMainThreadPtrHandle<nsINetworkInterface> mActiveNetwork;
   1.276 +#endif
   1.277 +    nsresult                           SaveNetworkStats(bool);
   1.278 +    void                               CountRecvBytes(uint64_t recvBytes)
   1.279 +    {
   1.280 +        mCountRecv += recvBytes;
   1.281 +        SaveNetworkStats(false);
   1.282 +    }
   1.283 +};
   1.284 +
   1.285 +#endif //__nsFtpState__h_

mercurial