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/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/address_list.h"
15 #include "net/base/auth.h"
16 #include "net/base/net_log.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/dns/single_request_host_resolver.h"
19 #include "net/ftp/ftp_ctrl_response_buffer.h"
20 #include "net/ftp/ftp_response_info.h"
21 #include "net/ftp/ftp_transaction.h"
25 class ClientSocketFactory
;
26 class FtpNetworkSession
;
29 class NET_EXPORT_PRIVATE FtpNetworkTransaction
: public FtpTransaction
{
31 FtpNetworkTransaction(FtpNetworkSession
* session
,
32 ClientSocketFactory
* socket_factory
);
33 ~FtpNetworkTransaction() override
;
35 virtual int Stop(int error
);
36 virtual int RestartIgnoringLastError(const CompletionCallback
& callback
);
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 // Resets the data connection after an error and switches to |next_state|.
130 void ResetDataConnectionAfterError(State next_state
);
132 void DoCallback(int result
);
133 void OnIOComplete(int result
);
135 // Executes correct ProcessResponse + command_name function based on last
136 // issued command. Returns error code.
137 int ProcessCtrlResponse();
139 int SendFtpCommand(const std::string
& command
,
140 const std::string
& command_for_log
,
143 // Returns request path suitable to be included in an FTP command. If the path
144 // will be used as a directory, |is_directory| should be true.
145 std::string
GetRequestPathForFtpCommand(bool is_directory
) const;
147 // See if the request URL contains a typecode and make us respect it.
148 void DetectTypecode();
150 // Runs the state transition loop.
151 int DoLoop(int result
);
153 // Each of these methods corresponds to a State value. Those with an input
154 // argument receive the result from the previous state. If a method returns
155 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
156 // next state method as the result arg.
157 int DoCtrlResolveHost();
158 int DoCtrlResolveHostComplete(int result
);
160 int DoCtrlConnectComplete(int result
);
162 int DoCtrlReadComplete(int result
);
164 int DoCtrlWriteComplete(int result
);
165 int DoCtrlWriteUSER();
166 int ProcessResponseUSER(const FtpCtrlResponse
& response
);
167 int DoCtrlWritePASS();
168 int ProcessResponsePASS(const FtpCtrlResponse
& response
);
169 int DoCtrlWriteSYST();
170 int ProcessResponseSYST(const FtpCtrlResponse
& response
);
171 int DoCtrlWritePWD();
172 int ProcessResponsePWD(const FtpCtrlResponse
& response
);
173 int DoCtrlWriteTYPE();
174 int ProcessResponseTYPE(const FtpCtrlResponse
& response
);
175 int DoCtrlWriteEPSV();
176 int ProcessResponseEPSV(const FtpCtrlResponse
& response
);
177 int DoCtrlWritePASV();
178 int ProcessResponsePASV(const FtpCtrlResponse
& response
);
179 int DoCtrlWriteRETR();
180 int ProcessResponseRETR(const FtpCtrlResponse
& response
);
181 int DoCtrlWriteSIZE();
182 int ProcessResponseSIZE(const FtpCtrlResponse
& response
);
183 int DoCtrlWriteCWD();
184 int ProcessResponseCWD(const FtpCtrlResponse
& response
);
185 int ProcessResponseCWDNotADirectory();
186 int DoCtrlWriteLIST();
187 int ProcessResponseLIST(const FtpCtrlResponse
& response
);
188 int DoCtrlWriteQUIT();
189 int ProcessResponseQUIT(const FtpCtrlResponse
& response
);
192 int DoDataConnectComplete(int result
);
194 int DoDataReadComplete(int result
);
196 void RecordDataConnectionError(int result
);
198 Command command_sent_
;
200 CompletionCallback io_callback_
;
201 CompletionCallback user_callback_
;
203 scoped_refptr
<FtpNetworkSession
> session_
;
205 BoundNetLog net_log_
;
206 const FtpRequestInfo
* request_
;
207 FtpResponseInfo response_
;
209 // Cancels the outstanding request on destruction.
210 SingleRequestHostResolver resolver_
;
211 AddressList addresses_
;
213 // User buffer passed to the Read method for control socket.
214 scoped_refptr
<IOBuffer
> read_ctrl_buf_
;
216 scoped_ptr
<FtpCtrlResponseBuffer
> ctrl_response_buffer_
;
218 scoped_refptr
<IOBuffer
> read_data_buf_
;
219 int read_data_buf_len_
;
221 // Buffer holding the command line to be written to the control socket.
222 scoped_refptr
<IOBufferWithSize
> write_command_buf_
;
224 // Buffer passed to the Write method of control socket. It actually writes
225 // to the write_command_buf_ at correct offset.
226 scoped_refptr
<DrainableIOBuffer
> write_buf_
;
230 SystemType system_type_
;
232 // Data type to be used for the TYPE command.
235 // Detected resource type (file or directory).
236 ResourceType resource_type_
;
238 // Initially we favour EPSV over PASV for transfers but should any
239 // EPSV fail, we fall back to PASV for the duration of connection.
242 AuthCredentials credentials_
;
244 // Current directory on the remote server, as returned by last PWD command,
245 // with any trailing slash removed.
246 std::string current_remote_directory_
;
248 int data_connection_port_
;
250 ClientSocketFactory
* socket_factory_
;
252 scoped_ptr
<StreamSocket
> ctrl_socket_
;
253 scoped_ptr
<StreamSocket
> data_socket_
;
257 // State to switch to after data connection is complete.
258 State state_after_data_connect_complete_
;
263 #endif // NET_FTP_FTP_NETWORK_TRANSACTION_H_