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/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"
22 #include "net/log/net_log.h"
26 class ClientSocketFactory
;
29 class NET_EXPORT_PRIVATE FtpNetworkTransaction
: public FtpTransaction
{
31 FtpNetworkTransaction(HostResolver
* resolver
,
32 ClientSocketFactory
* socket_factory
);
33 ~FtpNetworkTransaction() override
;
37 // FtpTransaction methods:
38 int Start(const FtpRequestInfo
* request_info
,
39 const CompletionCallback
& callback
,
40 const BoundNetLog
& net_log
) override
;
41 int RestartWithAuth(const AuthCredentials
& credentials
,
42 const CompletionCallback
& callback
) override
;
43 int Read(IOBuffer
* buf
,
45 const CompletionCallback
& callback
) override
;
46 const FtpResponseInfo
* GetResponseInfo() const override
;
47 LoadState
GetLoadState() const override
;
48 uint64
GetUploadProgress() const override
;
51 FRIEND_TEST_ALL_PREFIXES(FtpNetworkTransactionTest
,
52 DownloadTransactionEvilPasvUnsafeHost
);
70 // Major categories of remote system types, as returned by SYST command.
79 // Data representation type, see RFC 959 section 3.1.1. Data Types.
80 // We only support the two most popular data types.
86 // In FTP we need to issue different commands depending on whether a resource
87 // is a file or directory. If we don't know that, we're going to autodetect
90 RESOURCE_TYPE_UNKNOWN
,
92 RESOURCE_TYPE_DIRECTORY
,
96 // Control connection states:
97 STATE_CTRL_RESOLVE_HOST
,
98 STATE_CTRL_RESOLVE_HOST_COMPLETE
,
100 STATE_CTRL_CONNECT_COMPLETE
,
102 STATE_CTRL_READ_COMPLETE
,
104 STATE_CTRL_WRITE_COMPLETE
,
105 STATE_CTRL_WRITE_USER
,
106 STATE_CTRL_WRITE_PASS
,
107 STATE_CTRL_WRITE_SYST
,
108 STATE_CTRL_WRITE_TYPE
,
109 STATE_CTRL_WRITE_EPSV
,
110 STATE_CTRL_WRITE_PASV
,
111 STATE_CTRL_WRITE_PWD
,
112 STATE_CTRL_WRITE_RETR
,
113 STATE_CTRL_WRITE_SIZE
,
114 STATE_CTRL_WRITE_CWD
,
115 STATE_CTRL_WRITE_LIST
,
116 STATE_CTRL_WRITE_QUIT
,
117 // Data connection states:
119 STATE_DATA_CONNECT_COMPLETE
,
121 STATE_DATA_READ_COMPLETE
,
125 // Resets the members of the transaction so it can be restarted.
126 void ResetStateForRestart();
128 // Establishes the data connection and switches to |state_after_connect|.
129 // |state_after_connect| should only be RETR or LIST.
130 void EstablishDataConnection(State state_after_connect
);
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 BoundNetLog net_log_
;
204 const FtpRequestInfo
* request_
;
205 FtpResponseInfo response_
;
207 // Cancels the outstanding request on destruction.
208 SingleRequestHostResolver resolver_
;
209 AddressList addresses_
;
211 // User buffer passed to the Read method for control socket.
212 scoped_refptr
<IOBuffer
> read_ctrl_buf_
;
214 scoped_ptr
<FtpCtrlResponseBuffer
> ctrl_response_buffer_
;
216 scoped_refptr
<IOBuffer
> read_data_buf_
;
217 int read_data_buf_len_
;
219 // Buffer holding the command line to be written to the control socket.
220 scoped_refptr
<IOBufferWithSize
> write_command_buf_
;
222 // Buffer passed to the Write method of control socket. It actually writes
223 // to the write_command_buf_ at correct offset.
224 scoped_refptr
<DrainableIOBuffer
> write_buf_
;
228 SystemType system_type_
;
230 // Data type to be used for the TYPE command.
233 // Detected resource type (file or directory).
234 ResourceType resource_type_
;
236 // Initially we favour EPSV over PASV for transfers but should any
237 // EPSV fail, we fall back to PASV for the duration of connection.
240 AuthCredentials credentials_
;
242 // Current directory on the remote server, as returned by last PWD command,
243 // with any trailing slash removed.
244 std::string current_remote_directory_
;
246 uint16 data_connection_port_
;
248 ClientSocketFactory
* socket_factory_
;
250 scoped_ptr
<StreamSocket
> ctrl_socket_
;
251 scoped_ptr
<StreamSocket
> data_socket_
;
255 // State to switch to after data connection is complete.
256 State state_after_data_connect_complete_
;
261 #endif // NET_FTP_FTP_NETWORK_TRANSACTION_H_