Fix crash after Launcher drag/drop (take 3 - no unit test)
[chromium-blink-merge.git] / net / ftp / ftp_network_transaction.h
blobe350e16ffb3755b1a5381902cb3df5945d89c947
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_FTP_FTP_NETWORK_TRANSACTION_H_
6 #define NET_FTP_FTP_NETWORK_TRANSACTION_H_
8 #include <string>
9 #include <utility>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/auth.h"
17 #include "net/base/net_log.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/dns/single_request_host_resolver.h"
20 #include "net/ftp/ftp_ctrl_response_buffer.h"
21 #include "net/ftp/ftp_response_info.h"
22 #include "net/ftp/ftp_transaction.h"
24 namespace net {
26 class ClientSocketFactory;
27 class FtpNetworkSession;
28 class StreamSocket;
30 class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction {
31 public:
32 FtpNetworkTransaction(FtpNetworkSession* session,
33 ClientSocketFactory* socket_factory);
34 ~FtpNetworkTransaction() override;
36 virtual int Stop(int error);
37 virtual int RestartIgnoringLastError(const CompletionCallback& callback);
39 // FtpTransaction methods:
40 int Start(const FtpRequestInfo* request_info,
41 const CompletionCallback& callback,
42 const BoundNetLog& net_log) override;
43 int RestartWithAuth(const AuthCredentials& credentials,
44 const CompletionCallback& callback) override;
45 int Read(IOBuffer* buf,
46 int buf_len,
47 const CompletionCallback& callback) override;
48 const FtpResponseInfo* GetResponseInfo() const override;
49 LoadState GetLoadState() const override;
50 uint64 GetUploadProgress() const override;
52 private:
53 FRIEND_TEST_ALL_PREFIXES(FtpNetworkTransactionTest,
54 DownloadTransactionEvilPasvUnsafeHost);
56 enum Command {
57 COMMAND_NONE,
58 COMMAND_USER,
59 COMMAND_PASS,
60 COMMAND_SYST,
61 COMMAND_TYPE,
62 COMMAND_EPSV,
63 COMMAND_PASV,
64 COMMAND_PWD,
65 COMMAND_SIZE,
66 COMMAND_RETR,
67 COMMAND_CWD,
68 COMMAND_LIST,
69 COMMAND_QUIT,
72 // Major categories of remote system types, as returned by SYST command.
73 enum SystemType {
74 SYSTEM_TYPE_UNKNOWN,
75 SYSTEM_TYPE_UNIX,
76 SYSTEM_TYPE_WINDOWS,
77 SYSTEM_TYPE_OS2,
78 SYSTEM_TYPE_VMS,
81 // Data representation type, see RFC 959 section 3.1.1. Data Types.
82 // We only support the two most popular data types.
83 enum DataType {
84 DATA_TYPE_ASCII,
85 DATA_TYPE_IMAGE,
88 // In FTP we need to issue different commands depending on whether a resource
89 // is a file or directory. If we don't know that, we're going to autodetect
90 // it.
91 enum ResourceType {
92 RESOURCE_TYPE_UNKNOWN,
93 RESOURCE_TYPE_FILE,
94 RESOURCE_TYPE_DIRECTORY,
97 enum State {
98 // Control connection states:
99 STATE_CTRL_RESOLVE_HOST,
100 STATE_CTRL_RESOLVE_HOST_COMPLETE,
101 STATE_CTRL_CONNECT,
102 STATE_CTRL_CONNECT_COMPLETE,
103 STATE_CTRL_READ,
104 STATE_CTRL_READ_COMPLETE,
105 STATE_CTRL_WRITE,
106 STATE_CTRL_WRITE_COMPLETE,
107 STATE_CTRL_WRITE_USER,
108 STATE_CTRL_WRITE_PASS,
109 STATE_CTRL_WRITE_SYST,
110 STATE_CTRL_WRITE_TYPE,
111 STATE_CTRL_WRITE_EPSV,
112 STATE_CTRL_WRITE_PASV,
113 STATE_CTRL_WRITE_PWD,
114 STATE_CTRL_WRITE_RETR,
115 STATE_CTRL_WRITE_SIZE,
116 STATE_CTRL_WRITE_CWD,
117 STATE_CTRL_WRITE_LIST,
118 STATE_CTRL_WRITE_QUIT,
119 // Data connection states:
120 STATE_DATA_CONNECT,
121 STATE_DATA_CONNECT_COMPLETE,
122 STATE_DATA_READ,
123 STATE_DATA_READ_COMPLETE,
124 STATE_NONE
127 // Resets the members of the transaction so it can be restarted.
128 void ResetStateForRestart();
130 // Resets the data connection after an error and switches to |next_state|.
131 void ResetDataConnectionAfterError(State next_state);
133 void DoCallback(int result);
134 void OnIOComplete(int result);
136 // Executes correct ProcessResponse + command_name function based on last
137 // issued command. Returns error code.
138 int ProcessCtrlResponse();
140 int SendFtpCommand(const std::string& command,
141 const std::string& command_for_log,
142 Command cmd);
144 // Returns request path suitable to be included in an FTP command. If the path
145 // will be used as a directory, |is_directory| should be true.
146 std::string GetRequestPathForFtpCommand(bool is_directory) const;
148 // See if the request URL contains a typecode and make us respect it.
149 void DetectTypecode();
151 // Runs the state transition loop.
152 int DoLoop(int result);
154 // Each of these methods corresponds to a State value. Those with an input
155 // argument receive the result from the previous state. If a method returns
156 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
157 // next state method as the result arg.
158 int DoCtrlResolveHost();
159 int DoCtrlResolveHostComplete(int result);
160 int DoCtrlConnect();
161 int DoCtrlConnectComplete(int result);
162 int DoCtrlRead();
163 int DoCtrlReadComplete(int result);
164 int DoCtrlWrite();
165 int DoCtrlWriteComplete(int result);
166 int DoCtrlWriteUSER();
167 int ProcessResponseUSER(const FtpCtrlResponse& response);
168 int DoCtrlWritePASS();
169 int ProcessResponsePASS(const FtpCtrlResponse& response);
170 int DoCtrlWriteSYST();
171 int ProcessResponseSYST(const FtpCtrlResponse& response);
172 int DoCtrlWritePWD();
173 int ProcessResponsePWD(const FtpCtrlResponse& response);
174 int DoCtrlWriteTYPE();
175 int ProcessResponseTYPE(const FtpCtrlResponse& response);
176 int DoCtrlWriteEPSV();
177 int ProcessResponseEPSV(const FtpCtrlResponse& response);
178 int DoCtrlWritePASV();
179 int ProcessResponsePASV(const FtpCtrlResponse& response);
180 int DoCtrlWriteRETR();
181 int ProcessResponseRETR(const FtpCtrlResponse& response);
182 int DoCtrlWriteSIZE();
183 int ProcessResponseSIZE(const FtpCtrlResponse& response);
184 int DoCtrlWriteCWD();
185 int ProcessResponseCWD(const FtpCtrlResponse& response);
186 int ProcessResponseCWDNotADirectory();
187 int DoCtrlWriteLIST();
188 int ProcessResponseLIST(const FtpCtrlResponse& response);
189 int DoCtrlWriteQUIT();
190 int ProcessResponseQUIT(const FtpCtrlResponse& response);
192 int DoDataConnect();
193 int DoDataConnectComplete(int result);
194 int DoDataRead();
195 int DoDataReadComplete(int result);
197 void RecordDataConnectionError(int result);
199 Command command_sent_;
201 CompletionCallback io_callback_;
202 CompletionCallback user_callback_;
204 scoped_refptr<FtpNetworkSession> session_;
206 BoundNetLog net_log_;
207 const FtpRequestInfo* request_;
208 FtpResponseInfo response_;
210 // Cancels the outstanding request on destruction.
211 SingleRequestHostResolver resolver_;
212 AddressList addresses_;
214 // User buffer passed to the Read method for control socket.
215 scoped_refptr<IOBuffer> read_ctrl_buf_;
217 scoped_ptr<FtpCtrlResponseBuffer> ctrl_response_buffer_;
219 scoped_refptr<IOBuffer> read_data_buf_;
220 int read_data_buf_len_;
222 // Buffer holding the command line to be written to the control socket.
223 scoped_refptr<IOBufferWithSize> write_command_buf_;
225 // Buffer passed to the Write method of control socket. It actually writes
226 // to the write_command_buf_ at correct offset.
227 scoped_refptr<DrainableIOBuffer> write_buf_;
229 int last_error_;
231 SystemType system_type_;
233 // Data type to be used for the TYPE command.
234 DataType data_type_;
236 // Detected resource type (file or directory).
237 ResourceType resource_type_;
239 // Initially we favour EPSV over PASV for transfers but should any
240 // EPSV fail, we fall back to PASV for the duration of connection.
241 bool use_epsv_;
243 AuthCredentials credentials_;
245 // Current directory on the remote server, as returned by last PWD command,
246 // with any trailing slash removed.
247 std::string current_remote_directory_;
249 uint16 data_connection_port_;
251 ClientSocketFactory* socket_factory_;
253 scoped_ptr<StreamSocket> ctrl_socket_;
254 scoped_ptr<StreamSocket> data_socket_;
256 State next_state_;
258 // State to switch to after data connection is complete.
259 State state_after_data_connect_complete_;
262 } // namespace net
264 #endif // NET_FTP_FTP_NETWORK_TRANSACTION_H_