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_URL_REQUEST_URL_FETCHER_H_
6 #define NET_URL_REQUEST_URL_FETCHER_H_
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/supports_user_data.h"
15 #include "net/base/net_export.h"
16 #include "net/url_request/url_request.h"
22 class MessageLoopProxy
;
23 class SequencedTaskRunner
;
30 class HttpRequestHeaders
;
31 class HttpResponseHeaders
;
32 class URLFetcherDelegate
;
33 class URLFetcherResponseWriter
;
34 class URLRequestContextGetter
;
35 class URLRequestStatus
;
36 typedef std::vector
<std::string
> ResponseCookies
;
38 // To use this class, create an instance with the desired URL and a pointer to
39 // the object to be notified when the URL has been loaded:
40 // scoped_ptr<URLFetcher> fetcher =
41 // URLFetcher::Create("http://www.google.com",
45 // You must also set a request context getter:
47 // fetcher->SetRequestContext(&my_request_context_getter);
49 // Then, optionally set properties on this object, like the request context or
51 // fetcher->set_extra_request_headers("X-Foo: bar");
53 // Finally, start the request:
56 // You may cancel the request by destroying the URLFetcher:
59 // The object you supply as a delegate must inherit from
60 // URLFetcherDelegate; when the fetch is completed,
61 // OnURLFetchComplete() will be called with a pointer to the URLFetcher. From
62 // that point until the original URLFetcher instance is destroyed, you may use
63 // accessor methods to see the result of the fetch. You should copy these
64 // objects if you need them to live longer than the URLFetcher instance. If the
65 // URLFetcher instance is destroyed before the callback happens, the fetch will
66 // be canceled and no callback will occur.
68 // You may create the URLFetcher instance on any thread; OnURLFetchComplete()
69 // will be called back on the same thread you use to create the instance.
72 // NOTE: By default URLFetcher requests are NOT intercepted, except when
73 // interception is explicitly enabled in tests.
74 class NET_EXPORT URLFetcher
{
76 // Imposible http response code. Used to signal that no http response code
79 RESPONSE_CODE_INVALID
= -1
86 DELETE_REQUEST
, // DELETE is already taken on Windows.
87 // <winnt.h> defines a DELETE macro.
92 // Used by SetURLRequestUserData. The callback should make a fresh
93 // base::SupportsUserData::Data object every time it's called.
94 typedef base::Callback
<base::SupportsUserData::Data
*()> CreateDataCallback
;
96 // Used by SetUploadStreamFactory. The callback should assign a fresh upload
97 // data stream every time it's called.
98 typedef base::Callback
<scoped_ptr
<UploadDataStream
>()>
99 CreateUploadStreamCallback
;
101 virtual ~URLFetcher();
103 // |url| is the URL to send the request to.
104 // |request_type| is the type of request to make.
105 // |d| the object that will receive the callback on fetch completion.
106 static scoped_ptr
<URLFetcher
> Create(const GURL
& url
,
107 URLFetcher::RequestType request_type
,
108 URLFetcherDelegate
* d
);
110 // Like above, but if there's a URLFetcherFactory registered with the
111 // implementation it will be used. |id| may be used during testing to identify
112 // who is creating the URLFetcher.
113 static scoped_ptr
<URLFetcher
> Create(int id
,
115 URLFetcher::RequestType request_type
,
116 URLFetcherDelegate
* d
);
118 // Cancels all existing URLFetchers. Will notify the URLFetcherDelegates.
119 // Note that any new URLFetchers created while this is running will not be
120 // cancelled. Typically, one would call this in the CleanUp() method of an IO
121 // thread, so that no new URLRequests would be able to start on the IO thread
122 // anyway. This doesn't prevent new URLFetchers from trying to post to the IO
123 // thread though, even though the task won't ever run.
124 static void CancelAll();
126 // Normally, URLFetcher will abort loads that request SSL client certificate
127 // authentication, but this method may be used to cause URLFetchers to ignore
128 // requests for client certificates and continue anonymously. Because such
129 // behaviour affects the URLRequestContext's shared network state and socket
130 // pools, it should only be used for testing.
131 static void SetIgnoreCertificateRequests(bool ignored
);
133 // Sets data only needed by POSTs. All callers making POST requests should
134 // call one of the SetUpload* methods before the request is started.
135 // |upload_content_type| is the MIME type of the content, while
136 // |upload_content| is the data to be sent (the Content-Length header value
137 // will be set to the length of this data).
138 virtual void SetUploadData(const std::string
& upload_content_type
,
139 const std::string
& upload_content
) = 0;
141 // Sets data only needed by POSTs. All callers making POST requests should
142 // call one of the SetUpload* methods before the request is started.
143 // |upload_content_type| is the MIME type of the content, while
144 // |file_path| is the path to the file containing the data to be sent (the
145 // Content-Length header value will be set to the length of this file).
146 // |range_offset| and |range_length| specify the range of the part
147 // to be uploaded. To upload the whole file, (0, kuint64max) can be used.
148 // |file_task_runner| will be used for all file operations.
149 virtual void SetUploadFilePath(
150 const std::string
& upload_content_type
,
151 const base::FilePath
& file_path
,
154 scoped_refptr
<base::TaskRunner
> file_task_runner
) = 0;
156 // Sets data only needed by POSTs. All callers making POST requests should
157 // call one of the SetUpload* methods before the request is started.
158 // |upload_content_type| is the MIME type of the content, while |callback| is
159 // the callback to create the upload data stream (the Content-Length header
160 // value will be set to the length of this data). |callback| may be called
161 // mutliple times if the request is retried.
162 virtual void SetUploadStreamFactory(
163 const std::string
& upload_content_type
,
164 const CreateUploadStreamCallback
& callback
) = 0;
166 // Indicates that the POST data is sent via chunked transfer encoding.
167 // This may only be called before calling Start().
168 // Use AppendChunkToUpload() to give the data chunks after calling Start().
169 virtual void SetChunkedUpload(const std::string
& upload_content_type
) = 0;
171 // Adds the given bytes to a request's POST data transmitted using chunked
172 // transfer encoding.
173 // This method should be called ONLY after calling Start().
174 virtual void AppendChunkToUpload(const std::string
& data
,
175 bool is_last_chunk
) = 0;
177 // Set one or more load flags as defined in net/base/load_flags.h. Must be
178 // called before the request is started.
179 virtual void SetLoadFlags(int load_flags
) = 0;
181 // Returns the current load flags.
182 virtual int GetLoadFlags() const = 0;
184 // The referrer URL for the request. Must be called before the request is
186 virtual void SetReferrer(const std::string
& referrer
) = 0;
188 // The referrer policy to apply when updating the referrer during redirects.
189 // The referrer policy may only be changed before Start() is called.
190 virtual void SetReferrerPolicy(
191 URLRequest::ReferrerPolicy referrer_policy
) = 0;
193 // Set extra headers on the request. Must be called before the request
195 // This replaces the entire extra request headers.
196 virtual void SetExtraRequestHeaders(
197 const std::string
& extra_request_headers
) = 0;
199 // Add header (with format field-name ":" [ field-value ]) to the request
200 // headers. Must be called before the request is started.
201 // This appends the header to the current extra request headers.
202 virtual void AddExtraRequestHeader(const std::string
& header_line
) = 0;
204 // Set the URLRequestContext on the request. Must be called before the
205 // request is started.
206 virtual void SetRequestContext(
207 URLRequestContextGetter
* request_context_getter
) = 0;
209 // Set the URL that should be consulted for the third-party cookie
211 virtual void SetFirstPartyForCookies(
212 const GURL
& first_party_for_cookies
) = 0;
214 // Set the key and data callback that is used when setting the user
215 // data on any URLRequest objects this object creates.
216 virtual void SetURLRequestUserData(
218 const CreateDataCallback
& create_data_callback
) = 0;
220 // If |stop_on_redirect| is true, 3xx responses will cause the fetch to halt
221 // immediately rather than continue through the redirect. OnURLFetchComplete
222 // will be called, with the URLFetcher's URL set to the redirect destination,
223 // its status set to CANCELED, and its response code set to the relevant 3xx
224 // server response code.
225 virtual void SetStopOnRedirect(bool stop_on_redirect
) = 0;
227 // If |retry| is false, 5xx responses will be propagated to the observer. If
228 // it is true URLFetcher will automatically re-execute the request, after
229 // backoff_delay() elapses, up to the maximum number of retries allowed by
230 // SetMaxRetriesOn5xx. Defaults to true.
231 virtual void SetAutomaticallyRetryOn5xx(bool retry
) = 0;
233 // |max_retries| is the maximum number of times URLFetcher will retry a
234 // request that receives a 5XX response. Depends on
235 // SetAutomaticallyRetryOn5xx. Defaults to 0.
236 virtual void SetMaxRetriesOn5xx(int max_retries
) = 0;
237 virtual int GetMaxRetriesOn5xx() const = 0;
239 // Returns the back-off delay before the request will be retried,
240 // when a 5xx response was received.
241 virtual base::TimeDelta
GetBackoffDelay() const = 0;
243 // Retries up to |max_retries| times when requests fail with
244 // ERR_NETWORK_CHANGED. If ERR_NETWORK_CHANGED is received after having
245 // retried |max_retries| times then it is propagated to the observer.
246 virtual void SetAutomaticallyRetryOnNetworkChanges(int max_retries
) = 0;
248 // By default, the response is saved in a string. Call this method to save the
249 // response to a file instead. Must be called before Start().
250 // |file_task_runner| will be used for all file operations.
251 // To save to a temporary file, use SaveResponseToTemporaryFile().
252 // The created file is removed when the URLFetcher is deleted unless you
253 // take ownership by calling GetResponseAsFilePath().
254 virtual void SaveResponseToFileAtPath(
255 const base::FilePath
& file_path
,
256 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) = 0;
258 // By default, the response is saved in a string. Call this method to save the
259 // response to a temporary file instead. Must be called before Start().
260 // |file_task_runner| will be used for all file operations.
261 // The created file is removed when the URLFetcher is deleted unless you
262 // take ownership by calling GetResponseAsFilePath().
263 virtual void SaveResponseToTemporaryFile(
264 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) = 0;
266 // By default, the response is saved in a string. Call this method to use the
267 // specified writer to save the response. Must be called before Start().
268 virtual void SaveResponseWithWriter(
269 scoped_ptr
<URLFetcherResponseWriter
> response_writer
) = 0;
271 // Retrieve the response headers from the request. Must only be called after
272 // the OnURLFetchComplete callback has run.
273 virtual HttpResponseHeaders
* GetResponseHeaders() const = 0;
275 // Retrieve the remote socket address from the request. Must only
276 // be called after the OnURLFetchComplete callback has run and if
277 // the request has not failed.
278 virtual HostPortPair
GetSocketAddress() const = 0;
280 // Returns true if the request was delivered through a proxy. Must only
281 // be called after the OnURLFetchComplete callback has run and the request
283 virtual bool WasFetchedViaProxy() const = 0;
285 // Start the request. After this is called, you may not change any other
287 virtual void Start() = 0;
289 // Return the URL that we were asked to fetch.
290 virtual const GURL
& GetOriginalURL() const = 0;
292 // Return the URL that this fetcher is processing.
293 virtual const GURL
& GetURL() const = 0;
295 // The status of the URL fetch.
296 virtual const URLRequestStatus
& GetStatus() const = 0;
298 // The http response code received. Will return RESPONSE_CODE_INVALID
299 // if an error prevented any response from being received.
300 virtual int GetResponseCode() const = 0;
303 virtual const ResponseCookies
& GetCookies() const = 0;
305 // Reports that the received content was malformed.
306 virtual void ReceivedContentWasMalformed() = 0;
308 // Get the response as a string. Return false if the fetcher was not
309 // set to store the response as a string.
310 virtual bool GetResponseAsString(std::string
* out_response_string
) const = 0;
312 // Get the path to the file containing the response body. Returns false
313 // if the response body was not saved to a file. If take_ownership is
314 // true, caller takes responsibility for the file, and it will not
315 // be removed once the URLFetcher is destroyed. User should not take
316 // ownership more than once, or call this method after taking ownership.
317 virtual bool GetResponseAsFilePath(
319 base::FilePath
* out_response_path
) const = 0;
324 #endif // NET_URL_REQUEST_URL_FETCHER_H_