Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / http / http_transaction.h
blobb4bc41519b0d0d97b422d63af5b5c82efcc0e11e
1 // Copyright (c) 2011 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_HTTP_HTTP_TRANSACTION_H_
6 #define NET_HTTP_HTTP_TRANSACTION_H_
8 #include <stdint.h>
10 #include "net/base/completion_callback.h"
11 #include "net/base/load_states.h"
12 #include "net/base/net_export.h"
13 #include "net/base/request_priority.h"
14 #include "net/base/upload_progress.h"
15 #include "net/socket/connection_attempts.h"
16 #include "net/websockets/websocket_handshake_stream_base.h"
18 namespace net {
20 class AuthCredentials;
21 class BoundNetLog;
22 class HttpRequestHeaders;
23 struct HttpRequestInfo;
24 class HttpResponseInfo;
25 class IOBuffer;
26 struct LoadTimingInfo;
27 class ProxyInfo;
28 class QuicServerInfo;
29 class X509Certificate;
31 // Represents a single HTTP transaction (i.e., a single request/response pair).
32 // HTTP redirects are not followed and authentication challenges are not
33 // answered. Cookies are assumed to be managed by the caller.
34 class NET_EXPORT_PRIVATE HttpTransaction {
35 public:
36 // If |*defer| is set to true, the transaction will wait until
37 // ResumeNetworkStart is called before establishing a connection.
38 typedef base::Callback<void(bool* defer)> BeforeNetworkStartCallback;
40 // Provides an opportunity to add proxy-specific request headers. Called after
41 // it is determined that a proxy is being used and before the request headers
42 // are sent. |proxy_info| contains information about the proxy being used,
43 // and additional headers may be added to |request_headers|.
44 typedef base::Callback<void(
45 const ProxyInfo& proxy_info,
46 HttpRequestHeaders* request_headers)> BeforeProxyHeadersSentCallback;
48 // Stops any pending IO and destroys the transaction object.
49 virtual ~HttpTransaction() {}
51 // Starts the HTTP transaction (i.e., sends the HTTP request).
53 // Returns OK if the transaction could be started synchronously, which means
54 // that the request was served from the cache. ERR_IO_PENDING is returned to
55 // indicate that the CompletionCallback will be notified once response info is
56 // available or if an IO error occurs. Any other return value indicates that
57 // the transaction could not be started.
59 // Regardless of the return value, the caller is expected to keep the
60 // request_info object alive until Destroy is called on the transaction.
62 // NOTE: The transaction is not responsible for deleting the callback object.
64 // Profiling information for the request is saved to |net_log| if non-NULL.
65 virtual int Start(const HttpRequestInfo* request_info,
66 const CompletionCallback& callback,
67 const BoundNetLog& net_log) = 0;
69 // Restarts the HTTP transaction, ignoring the last error. This call can
70 // only be made after a call to Start (or RestartIgnoringLastError) failed.
71 // Once Read has been called, this method cannot be called. This method is
72 // used, for example, to continue past various SSL related errors.
74 // Not all errors can be ignored using this method. See error code
75 // descriptions for details about errors that can be ignored.
77 // NOTE: The transaction is not responsible for deleting the callback object.
79 virtual int RestartIgnoringLastError(const CompletionCallback& callback) = 0;
81 // Restarts the HTTP transaction with a client certificate.
82 virtual int RestartWithCertificate(X509Certificate* client_cert,
83 const CompletionCallback& callback) = 0;
85 // Restarts the HTTP transaction with authentication credentials.
86 virtual int RestartWithAuth(const AuthCredentials& credentials,
87 const CompletionCallback& callback) = 0;
89 // Returns true if auth is ready to be continued. Callers should check
90 // this value anytime Start() completes: if it is true, the transaction
91 // can be resumed with RestartWithAuth(L"", L"", callback) to resume
92 // the automatic auth exchange. This notification gives the caller a
93 // chance to process the response headers from all of the intermediate
94 // restarts needed for authentication.
95 virtual bool IsReadyToRestartForAuth() = 0;
97 // Once response info is available for the transaction, response data may be
98 // read by calling this method.
100 // Response data is copied into the given buffer and the number of bytes
101 // copied is returned. ERR_IO_PENDING is returned if response data is not
102 // yet available. The CompletionCallback is notified when the data copy
103 // completes, and it is passed the number of bytes that were successfully
104 // copied. Or, if a read error occurs, the CompletionCallback is notified of
105 // the error. Any other negative return value indicates that the transaction
106 // could not be read.
108 // NOTE: The transaction is not responsible for deleting the callback object.
109 // If the operation is not completed immediately, the transaction must acquire
110 // a reference to the provided buffer.
112 virtual int Read(IOBuffer* buf, int buf_len,
113 const CompletionCallback& callback) = 0;
115 // Stops further caching of this request by the HTTP cache, if there is any.
116 virtual void StopCaching() = 0;
118 // Gets the full request headers sent to the server. This is guaranteed to
119 // work only if Start returns success and the underlying transaction supports
120 // it. (Right now, this is only network transactions, not cache ones.)
122 // Returns true and overwrites headers if it can get the request headers;
123 // otherwise, returns false and does not modify headers.
124 virtual bool GetFullRequestHeaders(HttpRequestHeaders* headers) const = 0;
126 // Get the number of bytes received from network.
127 virtual int64 GetTotalReceivedBytes() const = 0;
129 // Get the number of bytes sent over the network.
130 virtual int64_t GetTotalSentBytes() const = 0;
132 // Called to tell the transaction that we have successfully reached the end
133 // of the stream. This is equivalent to performing an extra Read() at the end
134 // that should return 0 bytes. This method should not be called if the
135 // transaction is busy processing a previous operation (like a pending Read).
137 // DoneReading may also be called before the first Read() to notify that the
138 // entire response body is to be ignored (e.g., in a redirect).
139 virtual void DoneReading() = 0;
141 // Returns the response info for this transaction. Must not be called until
142 // |Start| completes.
143 virtual const HttpResponseInfo* GetResponseInfo() const = 0;
145 // Returns the load state for this transaction.
146 virtual LoadState GetLoadState() const = 0;
148 // Returns the upload progress in bytes. If there is no upload data,
149 // zero will be returned. This does not include the request headers.
150 virtual UploadProgress GetUploadProgress() const = 0;
152 // SetQuicServerInfo sets a object which reads and writes public information
153 // about a QUIC server.
154 virtual void SetQuicServerInfo(QuicServerInfo* quic_server_info) = 0;
156 // Populates all of load timing, except for request start times and receive
157 // headers time.
158 // |load_timing_info| must have all null times when called. Returns false and
159 // does not modify |load_timing_info| if there's no timing information to
160 // provide.
161 virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
163 // Called when the priority of the parent job changes.
164 virtual void SetPriority(RequestPriority priority) = 0;
166 // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
167 // request. Only relevant to WebSocket transactions. Must be called before
168 // Start(). Ownership of |create_helper| remains with the caller.
169 virtual void SetWebSocketHandshakeStreamCreateHelper(
170 WebSocketHandshakeStreamBase::CreateHelper* create_helper) = 0;
172 // Set the callback to receive notification just before network use.
173 virtual void SetBeforeNetworkStartCallback(
174 const BeforeNetworkStartCallback& callback) = 0;
176 // Set the callback to receive notification just before a proxy request
177 // is to be sent.
178 virtual void SetBeforeProxyHeadersSentCallback(
179 const BeforeProxyHeadersSentCallback& callback) = 0;
181 // Resumes the transaction after being deferred.
182 virtual int ResumeNetworkStart() = 0;
184 virtual void GetConnectionAttempts(ConnectionAttempts* out) const = 0;
187 } // namespace net
189 #endif // NET_HTTP_HTTP_TRANSACTION_H_