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.
4
5#ifndef NET_HTTP_HTTP_TRANSACTION_H_
6#define NET_HTTP_HTTP_TRANSACTION_H_
7
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"
14
15namespace net {
16
17class AuthCredentials;
18class BoundNetLog;
19class HttpRequestHeaders;
20struct HttpRequestInfo;
21class HttpResponseInfo;
22class IOBuffer;
23struct LoadTimingInfo;
24class X509Certificate;
25
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.
29class NET_EXPORT_PRIVATE HttpTransaction {
30 public:
31  // Stops any pending IO and destroys the transaction object.
32  virtual ~HttpTransaction() {}
33
34  // Starts the HTTP transaction (i.e., sends the HTTP request).
35  //
36  // Returns OK if the transaction could be started synchronously, which means
37  // that the request was served from the cache.  ERR_IO_PENDING is returned to
38  // indicate that the CompletionCallback will be notified once response info is
39  // available or if an IO error occurs.  Any other return value indicates that
40  // the transaction could not be started.
41  //
42  // Regardless of the return value, the caller is expected to keep the
43  // request_info object alive until Destroy is called on the transaction.
44  //
45  // NOTE: The transaction is not responsible for deleting the callback object.
46  //
47  // Profiling information for the request is saved to |net_log| if non-NULL.
48  virtual int Start(const HttpRequestInfo* request_info,
49                    const CompletionCallback& callback,
50                    const BoundNetLog& net_log) = 0;
51
52  // Restarts the HTTP transaction, ignoring the last error.  This call can
53  // only be made after a call to Start (or RestartIgnoringLastError) failed.
54  // Once Read has been called, this method cannot be called.  This method is
55  // used, for example, to continue past various SSL related errors.
56  //
57  // Not all errors can be ignored using this method.  See error code
58  // descriptions for details about errors that can be ignored.
59  //
60  // NOTE: The transaction is not responsible for deleting the callback object.
61  //
62  virtual int RestartIgnoringLastError(const CompletionCallback& callback) = 0;
63
64  // Restarts the HTTP transaction with a client certificate.
65  virtual int RestartWithCertificate(X509Certificate* client_cert,
66                                     const CompletionCallback& callback) = 0;
67
68  // Restarts the HTTP transaction with authentication credentials.
69  virtual int RestartWithAuth(const AuthCredentials& credentials,
70                              const CompletionCallback& callback) = 0;
71
72  // Returns true if auth is ready to be continued. Callers should check
73  // this value anytime Start() completes: if it is true, the transaction
74  // can be resumed with RestartWithAuth(L"", L"", callback) to resume
75  // the automatic auth exchange. This notification gives the caller a
76  // chance to process the response headers from all of the intermediate
77  // restarts needed for authentication.
78  virtual bool IsReadyToRestartForAuth() = 0;
79
80  // Once response info is available for the transaction, response data may be
81  // read by calling this method.
82  //
83  // Response data is copied into the given buffer and the number of bytes
84  // copied is returned.  ERR_IO_PENDING is returned if response data is not
85  // yet available.  The CompletionCallback is notified when the data copy
86  // completes, and it is passed the number of bytes that were successfully
87  // copied.  Or, if a read error occurs, the CompletionCallback is notified of
88  // the error.  Any other negative return value indicates that the transaction
89  // could not be read.
90  //
91  // NOTE: The transaction is not responsible for deleting the callback object.
92  // If the operation is not completed immediately, the transaction must acquire
93  // a reference to the provided buffer.
94  //
95  virtual int Read(IOBuffer* buf, int buf_len,
96                   const CompletionCallback& callback) = 0;
97
98  // Stops further caching of this request by the HTTP cache, if there is any.
99  virtual void StopCaching() = 0;
100
101  // Gets the full request headers sent to the server.  This is guaranteed to
102  // work only if Start returns success and the underlying transaction supports
103  // it.  (Right now, this is only network transactions, not cache ones.)
104  //
105  // Returns true and overwrites headers if it can get the request headers;
106  // otherwise, returns false and does not modify headers.
107  virtual bool GetFullRequestHeaders(HttpRequestHeaders* headers) const = 0;
108
109  // Called to tell the transaction that we have successfully reached the end
110  // of the stream. This is equivalent to performing an extra Read() at the end
111  // that should return 0 bytes. This method should not be called if the
112  // transaction is busy processing a previous operation (like a pending Read).
113  //
114  // DoneReading may also be called before the first Read() to notify that the
115  // entire response body is to be ignored (e.g., in a redirect).
116  virtual void DoneReading() = 0;
117
118  // Returns the response info for this transaction or NULL if the response
119  // info is not available.
120  virtual const HttpResponseInfo* GetResponseInfo() const = 0;
121
122  // Returns the load state for this transaction.
123  virtual LoadState GetLoadState() const = 0;
124
125  // Returns the upload progress in bytes.  If there is no upload data,
126  // zero will be returned.  This does not include the request headers.
127  virtual UploadProgress GetUploadProgress() const = 0;
128
129  // Populates all of load timing, except for request start times and receive
130  // headers time.
131  // |load_timing_info| must have all null times when called.  Returns false and
132  // does not modify |load_timing_info| if there's no timing information to
133  // provide.
134  virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
135
136  // Called when the priority of the parent job changes.
137  virtual void SetPriority(RequestPriority priority) = 0;
138
139  // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
140  // request.  Only relevant to WebSocket transactions. Must be called before
141  // Start(). Ownership of |create_helper| remains with the caller.
142  virtual void SetWebSocketHandshakeStreamCreateHelper(
143      WebSocketHandshakeStreamBase::CreateHelper* create_helper) = 0;
144};
145
146}  // namespace net
147
148#endif  // NET_HTTP_HTTP_TRANSACTION_H_
149