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_
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"
26 class ClientSocketFactory
;
27 class FtpNetworkSession
;
30 class NET_EXPORT_PRIVATE FtpNetworkTransaction
: public FtpTransaction
{
32 FtpNetworkTransaction(FtpNetworkSession
* session
,
33 ClientSocketFactory
* socket_factory
);
34 ~FtpNetworkTransaction() override
;
38 // FtpTransaction methods:
39 int Start(const FtpRequestInfo
* request_info
,
40 const CompletionCallback
& callback
,
41 const BoundNetLog
& net_log
) override
;
42 int RestartWithAuth(const AuthCredentials
& credentials
,
43 const CompletionCallback
& callback
) override
;
44 int Read(IOBuffer
* buf
,
46 const CompletionCallback
& callback
) override
;
47 const FtpResponseInfo
* GetResponseInfo() const override
;
48 LoadState
GetLoadState() const override
;
49 uint64
GetUploadProgress() const override
;
52 FRIEND_TEST_ALL_PREFIXES(FtpNetworkTransactionTest
,
53 DownloadTransactionEvilPasvUnsafeHost
);
71 // Major categories of remote system types, as returned by SYST command.
80 // Data representation type, see RFC 959 section 3.1.1. Data Types.
81 // We only support the two most popular data types.
87 // In FTP we need to issue different commands depending on whether a resource
88 // is a file or directory. If we don't know that, we're going to autodetect
91 RESOURCE_TYPE_UNKNOWN
,
93 RESOURCE_TYPE_DIRECTORY
,
97 // Control connection states:
98 STATE_CTRL_RESOLVE_HOST
,
99 STATE_CTRL_RESOLVE_HOST_COMPLETE
,
101 STATE_CTRL_CONNECT_COMPLETE
,
103 STATE_CTRL_READ_COMPLETE
,
105 STATE_CTRL_WRITE_COMPLETE
,
106 STATE_CTRL_WRITE_USER
,
107 STATE_CTRL_WRITE_PASS
,
108 STATE_CTRL_WRITE_SYST
,
109 STATE_CTRL_WRITE_TYPE
,
110 STATE_CTRL_WRITE_EPSV
,
111 STATE_CTRL_WRITE_PASV
,
112 STATE_CTRL_WRITE_PWD
,
113 STATE_CTRL_WRITE_RETR
,
114 STATE_CTRL_WRITE_SIZE
,
115 STATE_CTRL_WRITE_CWD
,
116 STATE_CTRL_WRITE_LIST
,
117 STATE_CTRL_WRITE_QUIT
,
118 // Data connection states:
120 STATE_DATA_CONNECT_COMPLETE
,
122 STATE_DATA_READ_COMPLETE
,
126 // Resets the members of the transaction so it can be restarted.
127 void ResetStateForRestart();
129 // Establishes the data connection and switches to |state_after_connect|.
130 // |state_after_connect| should only be RETR or LIST.
131 void EstablishDataConnection(State state_after_connect
);
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
,
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
);
161 int DoCtrlConnectComplete(int result
);
163 int DoCtrlReadComplete(int result
);
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
);
193 int DoDataConnectComplete(int result
);
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_
;
231 SystemType system_type_
;
233 // Data type to be used for the TYPE command.
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.
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_
;
258 // State to switch to after data connection is complete.
259 State state_after_data_connect_complete_
;
264 #endif // NET_FTP_FTP_NETWORK_TRANSACTION_H_