xref: /aosp_15_r20/external/cronet/net/http/http_basic_state.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A class that stores the common state between HttpBasicStream and
6 // WebSocketBasicHandshakeStream.
7 
8 #ifndef NET_HTTP_HTTP_BASIC_STATE_H_
9 #define NET_HTTP_HTTP_BASIC_STATE_H_
10 
11 #include <memory>
12 #include <set>
13 #include <string>
14 
15 #include "base/memory/scoped_refptr.h"
16 #include "net/base/net_export.h"
17 #include "net/base/request_priority.h"
18 #include "net/traffic_annotation/network_traffic_annotation.h"
19 #include "url/gurl.h"
20 
21 namespace net {
22 
23 class ClientSocketHandle;
24 class GrowableIOBuffer;
25 class HttpStreamParser;
26 struct HttpRequestInfo;
27 class NetLogWithSource;
28 
29 class NET_EXPORT_PRIVATE HttpBasicState {
30  public:
31   HttpBasicState(std::unique_ptr<ClientSocketHandle> connection,
32                  bool is_for_get_to_http_proxy);
33 
34   HttpBasicState(const HttpBasicState&) = delete;
35   HttpBasicState& operator=(const HttpBasicState&) = delete;
36 
37   ~HttpBasicState();
38 
39   // Initialize() must be called before using any of the other methods.
40   void Initialize(const HttpRequestInfo* request_info,
41                   RequestPriority priority,
42                   const NetLogWithSource& net_log);
43 
parser()44   HttpStreamParser* parser() const { return parser_.get(); }
45 
46   // Returns true if this request is a non-tunneled HTTP request via a proxy.
is_for_get_to_http_proxy()47   bool is_for_get_to_http_proxy() const { return is_for_get_to_http_proxy_; }
48 
49   // Deletes |parser_| and sets it to NULL.
50   void DeleteParser();
51 
connection()52   ClientSocketHandle* connection() const { return connection_.get(); }
53 
54   std::unique_ptr<ClientSocketHandle> ReleaseConnection();
55 
56   scoped_refptr<GrowableIOBuffer> read_buf() const;
57 
58   // Generates a string of the form "METHOD PATH HTTP/1.1\r\n", based on the
59   // values of request_info_ and is_for_get_to_http_proxy_.
60   std::string GenerateRequestLine() const;
61 
traffic_annotation()62   MutableNetworkTrafficAnnotationTag traffic_annotation() {
63     return traffic_annotation_;
64   }
65 
66   // Returns true if the connection has been "reused" as defined by HttpStream -
67   // either actually reused, or has not been used yet, but has been idle for
68   // some time.
69   //
70   // TODO(mmenke): Consider renaming this concept, to avoid confusion with
71   // ClientSocketHandle::is_reused().
72   bool IsConnectionReused() const;
73 
74   // Retrieves any DNS aliases for the remote endpoint. Includes all known
75   // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for
76   // the connection, in no particular order.
77   const std::set<std::string>& GetDnsAliases() const;
78 
79  private:
80   scoped_refptr<GrowableIOBuffer> read_buf_;
81 
82   std::unique_ptr<ClientSocketHandle> connection_;
83 
84   std::unique_ptr<HttpStreamParser> parser_;
85 
86   const bool is_for_get_to_http_proxy_;
87 
88   GURL url_;
89   std::string request_method_;
90 
91   MutableNetworkTrafficAnnotationTag traffic_annotation_;
92 };
93 
94 }  // namespace net
95 
96 #endif  // NET_HTTP_HTTP_BASIC_STATE_H_
97