1// Copyright (c) 2010 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#pragma once
8
9#include "base/string16.h"
10#include "net/base/completion_callback.h"
11#include "net/base/load_states.h"
12
13namespace net {
14
15class BoundNetLog;
16struct HttpRequestInfo;
17class HttpResponseInfo;
18class IOBuffer;
19class X509Certificate;
20class SSLHostInfo;
21
22// Represents a single HTTP transaction (i.e., a single request/response pair).
23// HTTP redirects are not followed and authentication challenges are not
24// answered.  Cookies are assumed to be managed by the caller.
25class HttpTransaction {
26 public:
27  // Stops any pending IO and destroys the transaction object.
28  virtual ~HttpTransaction() {}
29
30  // Starts the HTTP transaction (i.e., sends the HTTP request).
31  //
32  // Returns OK if the transaction could be started synchronously, which means
33  // that the request was served from the cache.  ERR_IO_PENDING is returned to
34  // indicate that the CompletionCallback will be notified once response info
35  // is available or if an IO error occurs.  Any other return value indicates
36  // that the transaction could not be started.
37  //
38  // Regardless of the return value, the caller is expected to keep the
39  // request_info object alive until Destroy is called on the transaction.
40  //
41  // NOTE: The transaction is not responsible for deleting the callback object.
42  //
43  // Profiling information for the request is saved to |net_log| if non-NULL.
44  virtual int Start(const HttpRequestInfo* request_info,
45                    CompletionCallback* callback,
46                    const BoundNetLog& net_log) = 0;
47
48  // Restarts the HTTP transaction, ignoring the last error.  This call can
49  // only be made after a call to Start (or RestartIgnoringLastError) failed.
50  // Once Read has been called, this method cannot be called.  This method is
51  // used, for example, to continue past various SSL related errors.
52  //
53  // Not all errors can be ignored using this method.  See error code
54  // descriptions for details about errors that can be ignored.
55  //
56  // NOTE: The transaction is not responsible for deleting the callback object.
57  //
58  virtual int RestartIgnoringLastError(CompletionCallback* callback) = 0;
59
60  // Restarts the HTTP transaction with a client certificate.
61  virtual int RestartWithCertificate(X509Certificate* client_cert,
62                                     CompletionCallback* callback) = 0;
63
64  // Restarts the HTTP transaction with authentication credentials.
65  virtual int RestartWithAuth(const string16& username,
66                              const string16& password,
67                              CompletionCallback* callback) = 0;
68
69  // Returns true if auth is ready to be continued. Callers should check
70  // this value anytime Start() completes: if it is true, the transaction
71  // can be resumed with RestartWithAuth(L"", L"", callback) to resume
72  // the automatic auth exchange. This notification gives the caller a
73  // chance to process the response headers from all of the intermediate
74  // restarts needed for authentication.
75  virtual bool IsReadyToRestartForAuth() = 0;
76
77  // Once response info is available for the transaction, response data may be
78  // read by calling this method.
79  //
80  // Response data is copied into the given buffer and the number of bytes
81  // copied is returned.  ERR_IO_PENDING is returned if response data is not
82  // yet available.  The CompletionCallback is notified when the data copy
83  // completes, and it is passed the number of bytes that were successfully
84  // copied.  Or, if a read error occurs, the CompletionCallback is notified of
85  // the error.  Any other negative return value indicates that the transaction
86  // could not be read.
87  //
88  // NOTE: The transaction is not responsible for deleting the callback object.
89  // If the operation is not completed immediately, the transaction must acquire
90  // a reference to the provided buffer.
91  //
92  virtual int Read(IOBuffer* buf, int buf_len,
93                   CompletionCallback* callback) = 0;
94
95  // Stops further caching of this request by the HTTP cache, if there is any.
96  virtual void StopCaching() = 0;
97
98  // Returns the response info for this transaction or NULL if the response
99  // info is not available.
100  virtual const HttpResponseInfo* GetResponseInfo() const = 0;
101
102  // Returns the load state for this transaction.
103  virtual LoadState GetLoadState() const = 0;
104
105  // Returns the upload progress in bytes.  If there is no upload data,
106  // zero will be returned.  This does not include the request headers.
107  virtual uint64 GetUploadProgress() const = 0;
108
109  // SetSSLHostInfo sets a object which reads and writes public information
110  // about an SSL server. It's used to implement Snap Start.
111  // TODO(agl): remove this.
112  virtual void SetSSLHostInfo(SSLHostInfo*) { };
113};
114
115}  // namespace net
116
117#endif  // NET_HTTP_HTTP_TRANSACTION_H_
118