Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / ftp / ftp_network_transaction.h
blob7422f2b3246434d66e901a1037fd9d1be3b0f833
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 StreamSocket;
29 class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction {
30 public:
31 FtpNetworkTransaction(HostResolver* resolver,
32 ClientSocketFactory* socket_factory);
33 ~FtpNetworkTransaction() override;
35 int Stop(int error);
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,
44 int buf_len,
45 const CompletionCallback& callback) override;
46 const FtpResponseInfo* GetResponseInfo() const override;
47 LoadState GetLoadState() const override;
48 uint64 GetUploadProgress() const override;
50 private:
51 FRIEND_TEST_ALL_PREFIXES(FtpNetworkTransactionTest,
52 DownloadTransactionEvilPasvUnsafeHost);
54 enum Command {
55 COMMAND_NONE,
56 COMMAND_USER,
57 COMMAND_PASS,
58 COMMAND_SYST,
59 COMMAND_TYPE,
60 COMMAND_EPSV,
61 COMMAND_PASV,
62 COMMAND_PWD,
63 COMMAND_SIZE,
64 COMMAND_RETR,
65 COMMAND_CWD,
66 COMMAND_LIST,
67 COMMAND_QUIT,
70 // Major categories of remote system types, as returned by SYST command.
71 enum SystemType {
72 SYSTEM_TYPE_UNKNOWN,
73 SYSTEM_TYPE_UNIX,
74 SYSTEM_TYPE_WINDOWS,
75 SYSTEM_TYPE_OS2,
76 SYSTEM_TYPE_VMS,
79 // Data representation type, see RFC 959 section 3.1.1. Data Types.
80 // We only support the two most popular data types.
81 enum DataType {
82 DATA_TYPE_ASCII,
83 DATA_TYPE_IMAGE,
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
88 // it.
89 enum ResourceType {
90 RESOURCE_TYPE_UNKNOWN,
91 RESOURCE_TYPE_FILE,
92 RESOURCE_TYPE_DIRECTORY,
95 enum State {
96 // Control connection states:
97 STATE_CTRL_RESOLVE_HOST,
98 STATE_CTRL_RESOLVE_HOST_COMPLETE,
99 STATE_CTRL_CONNECT,
100 STATE_CTRL_CONNECT_COMPLETE,
101 STATE_CTRL_READ,
102 STATE_CTRL_READ_COMPLETE,
103 STATE_CTRL_WRITE,
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:
118 STATE_DATA_CONNECT,
119 STATE_DATA_CONNECT_COMPLETE,
120 STATE_DATA_READ,
121 STATE_DATA_READ_COMPLETE,
122 STATE_NONE
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,
141 Command cmd);
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);
159 int DoCtrlConnect();
160 int DoCtrlConnectComplete(int result);
161 int DoCtrlRead();
162 int DoCtrlReadComplete(int result);
163 int DoCtrlWrite();
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);
191 int DoDataConnect();
192 int DoDataConnectComplete(int result);
193 int DoDataRead();
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_;
226 int last_error_;
228 SystemType system_type_;
230 // Data type to be used for the TYPE command.
231 DataType data_type_;
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.
238 bool use_epsv_;
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_;
253 State next_state_;
255 // State to switch to after data connection is complete.
256 State state_after_data_connect_complete_;
259 } // namespace net
261 #endif // NET_FTP_FTP_NETWORK_TRANSACTION_H_