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 #include "net/url_request/url_request_ftp_job.h"
7 #include "base/compiler_specific.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "net/base/auth.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/ftp/ftp_auth_cache.h"
16 #include "net/ftp/ftp_response_info.h"
17 #include "net/ftp/ftp_transaction_factory.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/http/http_transaction_factory.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_error_job.h"
26 URLRequestFtpJob::URLRequestFtpJob(
28 NetworkDelegate
* network_delegate
,
29 FtpTransactionFactory
* ftp_transaction_factory
,
30 FtpAuthCache
* ftp_auth_cache
)
31 : URLRequestJob(request
, network_delegate
),
32 priority_(DEFAULT_PRIORITY
),
33 proxy_service_(request_
->context()->proxy_service()),
35 http_response_info_(NULL
),
36 read_in_progress_(false),
38 ftp_transaction_factory_(ftp_transaction_factory
),
39 ftp_auth_cache_(ftp_auth_cache
) {
40 DCHECK(proxy_service_
);
41 DCHECK(ftp_transaction_factory
);
42 DCHECK(ftp_auth_cache
);
45 URLRequestFtpJob::~URLRequestFtpJob() {
47 proxy_service_
->CancelPacRequest(pac_request_
);
50 bool URLRequestFtpJob::IsSafeRedirect(const GURL
& location
) {
51 // Disallow all redirects.
55 bool URLRequestFtpJob::GetMimeType(std::string
* mime_type
) const {
56 if (proxy_info_
.is_direct()) {
57 if (ftp_transaction_
->GetResponseInfo()->is_directory_listing
) {
58 *mime_type
= "text/vnd.chromium.ftp-dir";
62 // No special handling of MIME type is needed. As opposed to direct FTP
63 // transaction, we do not get a raw directory listing to parse.
64 return http_transaction_
->GetResponseInfo()->
65 headers
->GetMimeType(mime_type
);
70 void URLRequestFtpJob::GetResponseInfo(HttpResponseInfo
* info
) {
71 if (http_response_info_
)
72 *info
= *http_response_info_
;
75 HostPortPair
URLRequestFtpJob::GetSocketAddress() const {
76 if (proxy_info_
.is_direct()) {
77 if (!ftp_transaction_
)
78 return HostPortPair();
79 return ftp_transaction_
->GetResponseInfo()->socket_address
;
81 if (!http_transaction_
)
82 return HostPortPair();
83 return http_transaction_
->GetResponseInfo()->socket_address
;
87 void URLRequestFtpJob::SetPriority(RequestPriority priority
) {
89 if (http_transaction_
)
90 http_transaction_
->SetPriority(priority
);
93 void URLRequestFtpJob::Start() {
94 DCHECK(!pac_request_
);
95 DCHECK(!ftp_transaction_
);
96 DCHECK(!http_transaction_
);
99 if (request_
->load_flags() & LOAD_BYPASS_PROXY
) {
100 proxy_info_
.UseDirect();
102 DCHECK_EQ(request_
->context()->proxy_service(), proxy_service_
);
103 rv
= proxy_service_
->ResolveProxy(
106 base::Bind(&URLRequestFtpJob::OnResolveProxyComplete
,
107 base::Unretained(this)),
109 request_
->net_log());
111 if (rv
== ERR_IO_PENDING
)
114 OnResolveProxyComplete(rv
);
117 void URLRequestFtpJob::Kill() {
118 if (ftp_transaction_
)
119 ftp_transaction_
.reset();
120 if (http_transaction_
)
121 http_transaction_
.reset();
122 URLRequestJob::Kill();
123 weak_factory_
.InvalidateWeakPtrs();
126 void URLRequestFtpJob::OnResolveProxyComplete(int result
) {
130 OnStartCompletedAsync(result
);
134 // Remove unsupported proxies from the list.
135 proxy_info_
.RemoveProxiesWithoutScheme(
136 ProxyServer::SCHEME_DIRECT
|
137 ProxyServer::SCHEME_HTTP
|
138 ProxyServer::SCHEME_HTTPS
);
140 // TODO(phajdan.jr): Implement proxy fallback, http://crbug.com/171495 .
141 if (proxy_info_
.is_direct())
142 StartFtpTransaction();
143 else if (proxy_info_
.is_http() || proxy_info_
.is_https())
144 StartHttpTransaction();
146 OnStartCompletedAsync(ERR_NO_SUPPORTED_PROXIES
);
149 void URLRequestFtpJob::StartFtpTransaction() {
150 // Create a transaction.
151 DCHECK(!ftp_transaction_
);
153 ftp_request_info_
.url
= request_
->url();
154 ftp_transaction_
.reset(ftp_transaction_factory_
->CreateTransaction());
156 // No matter what, we want to report our status as IO pending since we will
157 // be notifying our consumer asynchronously via OnStartCompleted.
158 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
160 if (ftp_transaction_
) {
161 rv
= ftp_transaction_
->Start(
163 base::Bind(&URLRequestFtpJob::OnStartCompleted
,
164 base::Unretained(this)),
165 request_
->net_log());
166 if (rv
== ERR_IO_PENDING
)
171 // The transaction started synchronously, but we need to notify the
172 // URLRequest delegate via the message loop.
173 OnStartCompletedAsync(rv
);
176 void URLRequestFtpJob::StartHttpTransaction() {
177 // Create a transaction.
178 DCHECK(!http_transaction_
);
180 // Do not cache FTP responses sent through HTTP proxy.
181 request_
->SetLoadFlags(request_
->load_flags() |
183 LOAD_DO_NOT_SAVE_COOKIES
|
184 LOAD_DO_NOT_SEND_COOKIES
);
186 http_request_info_
.url
= request_
->url();
187 http_request_info_
.method
= request_
->method();
188 http_request_info_
.load_flags
= request_
->load_flags();
190 int rv
= request_
->context()->http_transaction_factory()->CreateTransaction(
191 priority_
, &http_transaction_
);
193 rv
= http_transaction_
->Start(
195 base::Bind(&URLRequestFtpJob::OnStartCompleted
,
196 base::Unretained(this)),
197 request_
->net_log());
198 if (rv
== ERR_IO_PENDING
)
201 // The transaction started synchronously, but we need to notify the
202 // URLRequest delegate via the message loop.
203 OnStartCompletedAsync(rv
);
206 void URLRequestFtpJob::OnStartCompleted(int result
) {
207 // Clear the IO_PENDING status
208 SetStatus(URLRequestStatus());
210 // Note that ftp_transaction_ may be NULL due to a creation failure.
211 if (ftp_transaction_
) {
212 // FTP obviously doesn't have HTTP Content-Length header. We have to pass
213 // the content size information manually.
214 set_expected_content_size(
215 ftp_transaction_
->GetResponseInfo()->expected_content_size
);
219 if (http_transaction_
) {
220 http_response_info_
= http_transaction_
->GetResponseInfo();
222 if (http_response_info_
->headers
->response_code() == 401 ||
223 http_response_info_
->headers
->response_code() == 407) {
224 HandleAuthNeededResponse();
228 NotifyHeadersComplete();
229 } else if (ftp_transaction_
&&
230 ftp_transaction_
->GetResponseInfo()->needs_auth
) {
231 HandleAuthNeededResponse();
234 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, result
));
238 void URLRequestFtpJob::OnStartCompletedAsync(int result
) {
239 base::MessageLoop::current()->PostTask(
241 base::Bind(&URLRequestFtpJob::OnStartCompleted
,
242 weak_factory_
.GetWeakPtr(), result
));
245 void URLRequestFtpJob::OnReadCompleted(int result
) {
246 read_in_progress_
= false;
248 NotifyDone(URLRequestStatus());
249 } else if (result
< 0) {
250 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, result
));
252 // Clear the IO_PENDING status
253 SetStatus(URLRequestStatus());
255 NotifyReadComplete(result
);
258 void URLRequestFtpJob::RestartTransactionWithAuth() {
259 DCHECK(auth_data_
.get() && auth_data_
->state
== AUTH_STATE_HAVE_AUTH
);
261 // No matter what, we want to report our status as IO pending since we will
262 // be notifying our consumer asynchronously via OnStartCompleted.
263 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
266 if (proxy_info_
.is_direct()) {
267 rv
= ftp_transaction_
->RestartWithAuth(
268 auth_data_
->credentials
,
269 base::Bind(&URLRequestFtpJob::OnStartCompleted
,
270 base::Unretained(this)));
272 rv
= http_transaction_
->RestartWithAuth(
273 auth_data_
->credentials
,
274 base::Bind(&URLRequestFtpJob::OnStartCompleted
,
275 base::Unretained(this)));
277 if (rv
== ERR_IO_PENDING
)
280 OnStartCompletedAsync(rv
);
283 LoadState
URLRequestFtpJob::GetLoadState() const {
284 if (proxy_info_
.is_direct()) {
285 return ftp_transaction_
?
286 ftp_transaction_
->GetLoadState() : LOAD_STATE_IDLE
;
288 return http_transaction_
?
289 http_transaction_
->GetLoadState() : LOAD_STATE_IDLE
;
293 bool URLRequestFtpJob::NeedsAuth() {
294 return auth_data_
.get() && auth_data_
->state
== AUTH_STATE_NEED_AUTH
;
297 void URLRequestFtpJob::GetAuthChallengeInfo(
298 scoped_refptr
<AuthChallengeInfo
>* result
) {
301 if (http_response_info_
) {
302 *result
= http_response_info_
->auth_challenge
;
306 scoped_refptr
<AuthChallengeInfo
> auth_info(new AuthChallengeInfo
);
307 auth_info
->is_proxy
= false;
308 auth_info
->challenger
= HostPortPair::FromURL(request_
->url());
309 // scheme and realm are kept empty.
310 DCHECK(auth_info
->scheme
.empty());
311 DCHECK(auth_info
->realm
.empty());
312 result
->swap(auth_info
);
315 void URLRequestFtpJob::SetAuth(const AuthCredentials
& credentials
) {
316 DCHECK(ftp_transaction_
|| http_transaction_
);
319 auth_data_
->state
= AUTH_STATE_HAVE_AUTH
;
320 auth_data_
->credentials
= credentials
;
322 if (ftp_transaction_
) {
323 ftp_auth_cache_
->Add(request_
->url().GetOrigin(),
324 auth_data_
->credentials
);
327 RestartTransactionWithAuth();
330 void URLRequestFtpJob::CancelAuth() {
331 DCHECK(ftp_transaction_
|| http_transaction_
);
334 auth_data_
->state
= AUTH_STATE_CANCELED
;
336 // Once the auth is cancelled, we proceed with the request as though
337 // there were no auth. Schedule this for later so that we don't cause
338 // any recursing into the caller as a result of this call.
339 OnStartCompletedAsync(OK
);
342 UploadProgress
URLRequestFtpJob::GetUploadProgress() const {
343 return UploadProgress();
346 bool URLRequestFtpJob::ReadRawData(IOBuffer
* buf
,
349 DCHECK_NE(buf_size
, 0);
351 DCHECK(!read_in_progress_
);
354 if (proxy_info_
.is_direct()) {
355 rv
= ftp_transaction_
->Read(buf
, buf_size
,
356 base::Bind(&URLRequestFtpJob::OnReadCompleted
,
357 base::Unretained(this)));
359 rv
= http_transaction_
->Read(buf
, buf_size
,
360 base::Bind(&URLRequestFtpJob::OnReadCompleted
,
361 base::Unretained(this)));
369 if (rv
== ERR_IO_PENDING
) {
370 read_in_progress_
= true;
371 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
373 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
378 void URLRequestFtpJob::HandleAuthNeededResponse() {
379 GURL origin
= request_
->url().GetOrigin();
381 if (auth_data_
.get()) {
382 if (auth_data_
->state
== AUTH_STATE_CANCELED
) {
383 NotifyHeadersComplete();
387 if (ftp_transaction_
&& auth_data_
->state
== AUTH_STATE_HAVE_AUTH
)
388 ftp_auth_cache_
->Remove(origin
, auth_data_
->credentials
);
390 auth_data_
= new AuthData
;
392 auth_data_
->state
= AUTH_STATE_NEED_AUTH
;
394 FtpAuthCache::Entry
* cached_auth
= NULL
;
395 if (ftp_transaction_
&& ftp_transaction_
->GetResponseInfo()->needs_auth
)
396 cached_auth
= ftp_auth_cache_
->Lookup(origin
);
398 // Retry using cached auth data.
399 SetAuth(cached_auth
->credentials
);
401 // Prompt for a username/password.
402 NotifyHeadersComplete();