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 "net/base/completion_callback.h"
9 #include "net/base/load_states.h"
10 #include "net/base/net_export.h"
11 #include "net/base/request_priority.h"
12 #include "net/base/upload_progress.h"
13 #include "net/websockets/websocket_handshake_stream_base.h"
17 class AuthCredentials
;
19 class HttpRequestHeaders
;
20 struct HttpRequestInfo
;
21 class HttpResponseInfo
;
23 struct LoadTimingInfo
;
24 class X509Certificate
;
26 // Represents a single HTTP transaction (i.e., a single request/response pair).
27 // HTTP redirects are not followed and authentication challenges are not
28 // answered. Cookies are assumed to be managed by the caller.
29 class NET_EXPORT_PRIVATE HttpTransaction
{
31 // If |*defer| is set to true, the transaction will wait until
32 // ResumeNetworkStart is called before establishing a connection.
33 typedef base::Callback
<void(bool* defer
)> BeforeNetworkStartCallback
;
35 // Stops any pending IO and destroys the transaction object.
36 virtual ~HttpTransaction() {}
38 // Starts the HTTP transaction (i.e., sends the HTTP request).
40 // Returns OK if the transaction could be started synchronously, which means
41 // that the request was served from the cache. ERR_IO_PENDING is returned to
42 // indicate that the CompletionCallback will be notified once response info is
43 // available or if an IO error occurs. Any other return value indicates that
44 // the transaction could not be started.
46 // Regardless of the return value, the caller is expected to keep the
47 // request_info object alive until Destroy is called on the transaction.
49 // NOTE: The transaction is not responsible for deleting the callback object.
51 // Profiling information for the request is saved to |net_log| if non-NULL.
52 virtual int Start(const HttpRequestInfo
* request_info
,
53 const CompletionCallback
& callback
,
54 const BoundNetLog
& net_log
) = 0;
56 // Restarts the HTTP transaction, ignoring the last error. This call can
57 // only be made after a call to Start (or RestartIgnoringLastError) failed.
58 // Once Read has been called, this method cannot be called. This method is
59 // used, for example, to continue past various SSL related errors.
61 // Not all errors can be ignored using this method. See error code
62 // descriptions for details about errors that can be ignored.
64 // NOTE: The transaction is not responsible for deleting the callback object.
66 virtual int RestartIgnoringLastError(const CompletionCallback
& callback
) = 0;
68 // Restarts the HTTP transaction with a client certificate.
69 virtual int RestartWithCertificate(X509Certificate
* client_cert
,
70 const CompletionCallback
& callback
) = 0;
72 // Restarts the HTTP transaction with authentication credentials.
73 virtual int RestartWithAuth(const AuthCredentials
& credentials
,
74 const CompletionCallback
& callback
) = 0;
76 // Returns true if auth is ready to be continued. Callers should check
77 // this value anytime Start() completes: if it is true, the transaction
78 // can be resumed with RestartWithAuth(L"", L"", callback) to resume
79 // the automatic auth exchange. This notification gives the caller a
80 // chance to process the response headers from all of the intermediate
81 // restarts needed for authentication.
82 virtual bool IsReadyToRestartForAuth() = 0;
84 // Once response info is available for the transaction, response data may be
85 // read by calling this method.
87 // Response data is copied into the given buffer and the number of bytes
88 // copied is returned. ERR_IO_PENDING is returned if response data is not
89 // yet available. The CompletionCallback is notified when the data copy
90 // completes, and it is passed the number of bytes that were successfully
91 // copied. Or, if a read error occurs, the CompletionCallback is notified of
92 // the error. Any other negative return value indicates that the transaction
95 // NOTE: The transaction is not responsible for deleting the callback object.
96 // If the operation is not completed immediately, the transaction must acquire
97 // a reference to the provided buffer.
99 virtual int Read(IOBuffer
* buf
, int buf_len
,
100 const CompletionCallback
& callback
) = 0;
102 // Stops further caching of this request by the HTTP cache, if there is any.
103 virtual void StopCaching() = 0;
105 // Gets the full request headers sent to the server. This is guaranteed to
106 // work only if Start returns success and the underlying transaction supports
107 // it. (Right now, this is only network transactions, not cache ones.)
109 // Returns true and overwrites headers if it can get the request headers;
110 // otherwise, returns false and does not modify headers.
111 virtual bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const = 0;
113 // Get the number of bytes received from network.
114 virtual int64
GetTotalReceivedBytes() const = 0;
116 // Called to tell the transaction that we have successfully reached the end
117 // of the stream. This is equivalent to performing an extra Read() at the end
118 // that should return 0 bytes. This method should not be called if the
119 // transaction is busy processing a previous operation (like a pending Read).
121 // DoneReading may also be called before the first Read() to notify that the
122 // entire response body is to be ignored (e.g., in a redirect).
123 virtual void DoneReading() = 0;
125 // Returns the response info for this transaction or NULL if the response
126 // info is not available.
127 virtual const HttpResponseInfo
* GetResponseInfo() const = 0;
129 // Returns the load state for this transaction.
130 virtual LoadState
GetLoadState() const = 0;
132 // Returns the upload progress in bytes. If there is no upload data,
133 // zero will be returned. This does not include the request headers.
134 virtual UploadProgress
GetUploadProgress() const = 0;
136 // Populates all of load timing, except for request start times and receive
138 // |load_timing_info| must have all null times when called. Returns false and
139 // does not modify |load_timing_info| if there's no timing information to
141 virtual bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const = 0;
143 // Called when the priority of the parent job changes.
144 virtual void SetPriority(RequestPriority priority
) = 0;
146 // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
147 // request. Only relevant to WebSocket transactions. Must be called before
148 // Start(). Ownership of |create_helper| remains with the caller.
149 virtual void SetWebSocketHandshakeStreamCreateHelper(
150 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) = 0;
152 // Set the callback to receive notification just before network use.
153 virtual void SetBeforeNetworkStartCallback(
154 const BeforeNetworkStartCallback
& callback
) = 0;
156 // Resumes the transaction after being deferred.
157 virtual int ResumeNetworkStart() = 0;
162 #endif // NET_HTTP_HTTP_TRANSACTION_H_