We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / ftp / ftp_network_transaction.h
blobd51bd771e05be63b754d2bbf391cf236bffc07da
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/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"
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 int Stop(int error);
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,
45 int buf_len,
46 const CompletionCallback& callback) override;
47 const FtpResponseInfo* GetResponseInfo() const override;
48 LoadState GetLoadState() const override;
49 uint64 GetUploadProgress() const override;
51 private:
52 FRIEND_TEST_ALL_PREFIXES(FtpNetworkTransactionTest,
53 DownloadTransactionEvilPasvUnsafeHost);
55 enum Command {
56 COMMAND_NONE,
57 COMMAND_USER,
58 COMMAND_PASS,
59 COMMAND_SYST,
60 COMMAND_TYPE,
61 COMMAND_EPSV,
62 COMMAND_PASV,
63 COMMAND_PWD,
64 COMMAND_SIZE,
65 COMMAND_RETR,
66 COMMAND_CWD,
67 COMMAND_LIST,
68 COMMAND_QUIT,
71 // Major categories of remote system types, as returned by SYST command.
72 enum SystemType {
73 SYSTEM_TYPE_UNKNOWN,
74 SYSTEM_TYPE_UNIX,
75 SYSTEM_TYPE_WINDOWS,
76 SYSTEM_TYPE_OS2,
77 SYSTEM_TYPE_VMS,
80 // Data representation type, see RFC 959 section 3.1.1. Data Types.
81 // We only support the two most popular data types.
82 enum DataType {
83 DATA_TYPE_ASCII,
84 DATA_TYPE_IMAGE,
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
89 // it.
90 enum ResourceType {
91 RESOURCE_TYPE_UNKNOWN,
92 RESOURCE_TYPE_FILE,
93 RESOURCE_TYPE_DIRECTORY,
96 enum State {
97 // Control connection states:
98 STATE_CTRL_RESOLVE_HOST,
99 STATE_CTRL_RESOLVE_HOST_COMPLETE,
100 STATE_CTRL_CONNECT,
101 STATE_CTRL_CONNECT_COMPLETE,
102 STATE_CTRL_READ,
103 STATE_CTRL_READ_COMPLETE,
104 STATE_CTRL_WRITE,
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:
119 STATE_DATA_CONNECT,
120 STATE_DATA_CONNECT_COMPLETE,
121 STATE_DATA_READ,
122 STATE_DATA_READ_COMPLETE,
123 STATE_NONE
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,
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_