xref: /aosp_15_r20/external/cronet/net/server/http_connection.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 #ifndef NET_SERVER_HTTP_CONNECTION_H_
6 #define NET_SERVER_HTTP_CONNECTION_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/containers/queue.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "net/base/io_buffer.h"
14 
15 namespace net {
16 
17 class StreamSocket;
18 class WebSocket;
19 
20 // A container which has all information of an http connection. It includes
21 // id, underlying socket, and pending read/write data.
22 class HttpConnection {
23  public:
24   // IOBuffer for data read.  It's a wrapper around GrowableIOBuffer, with more
25   // functions for buffer management.  It moves unconsumed data to the start of
26   // buffer.
27   class ReadIOBuffer : public IOBuffer {
28    public:
29     static const int kInitialBufSize = 1024;
30     static const int kMinimumBufSize = 128;
31     static const int kCapacityIncreaseFactor = 2;
32     static const int kDefaultMaxBufferSize = 1 * 1024 * 1024;  // 1 Mbytes.
33 
34     ReadIOBuffer();
35 
36     ReadIOBuffer(const ReadIOBuffer&) = delete;
37     ReadIOBuffer& operator=(const ReadIOBuffer&) = delete;
38 
39     // Capacity.
40     int GetCapacity() const;
41     void SetCapacity(int capacity);
42     // Increases capacity and returns true if capacity is not beyond the limit.
43     bool IncreaseCapacity();
44 
45     // Start of read data.
46     char* StartOfBuffer() const;
47     // Returns the bytes of read data.
48     int GetSize() const;
49     // More read data was appended.
50     void DidRead(int bytes);
51     // Capacity for which more read data can be appended.
52     int RemainingCapacity() const;
53 
54     // Removes consumed data and moves unconsumed data to the start of buffer.
55     void DidConsume(int bytes);
56 
57     // Limit of how much internal capacity can increase.
max_buffer_size()58     int max_buffer_size() const { return max_buffer_size_; }
set_max_buffer_size(int max_buffer_size)59     void set_max_buffer_size(int max_buffer_size) {
60       max_buffer_size_ = max_buffer_size;
61     }
62 
63    private:
64     ~ReadIOBuffer() override;
65 
66     scoped_refptr<GrowableIOBuffer> base_;
67     int max_buffer_size_ = kDefaultMaxBufferSize;
68   };
69 
70   // IOBuffer of pending data to write which has a queue of pending data. Each
71   // pending data is stored in std::string.  data() is the data of first
72   // std::string stored.
73   class QueuedWriteIOBuffer : public IOBuffer {
74    public:
75     static const int kDefaultMaxBufferSize = 1 * 1024 * 1024;  // 1 Mbytes.
76 
77     QueuedWriteIOBuffer();
78 
79     QueuedWriteIOBuffer(const QueuedWriteIOBuffer&) = delete;
80     QueuedWriteIOBuffer& operator=(const QueuedWriteIOBuffer&) = delete;
81 
82     // Whether or not pending data exists.
83     bool IsEmpty() const;
84 
85     // Appends new pending data and returns true if total size doesn't exceed
86     // the limit, |total_size_limit_|.  It would change data() if new data is
87     // the first pending data.
88     bool Append(const std::string& data);
89 
90     // Consumes data and changes data() accordingly.  It cannot be more than
91     // GetSizeToWrite().
92     void DidConsume(int size);
93 
94     // Gets size of data to write this time. It is NOT total data size.
95     int GetSizeToWrite() const;
96 
97     // Total size of all pending data.
total_size()98     int total_size() const { return total_size_; }
99 
100     // Limit of how much data can be pending.
max_buffer_size()101     int max_buffer_size() const { return max_buffer_size_; }
set_max_buffer_size(int max_buffer_size)102     void set_max_buffer_size(int max_buffer_size) {
103       max_buffer_size_ = max_buffer_size;
104     }
105 
106    private:
107     ~QueuedWriteIOBuffer() override;
108 
109     // This needs to indirect since we need pointer stability for the payload
110     // chunks, as they may be handed out via net::IOBuffer::data().
111     base::queue<std::unique_ptr<std::string>> pending_data_;
112     int total_size_ = 0;
113     int max_buffer_size_ = kDefaultMaxBufferSize;
114   };
115 
116   HttpConnection(int id, std::unique_ptr<StreamSocket> socket);
117 
118   HttpConnection(const HttpConnection&) = delete;
119   HttpConnection& operator=(const HttpConnection&) = delete;
120 
121   ~HttpConnection();
122 
id()123   int id() const { return id_; }
socket()124   StreamSocket* socket() const { return socket_.get(); }
read_buf()125   ReadIOBuffer* read_buf() const { return read_buf_.get(); }
write_buf()126   QueuedWriteIOBuffer* write_buf() const { return write_buf_.get(); }
127 
web_socket()128   WebSocket* web_socket() const { return web_socket_.get(); }
129   void SetWebSocket(std::unique_ptr<WebSocket> web_socket);
130 
131  private:
132   const int id_;
133   const std::unique_ptr<StreamSocket> socket_;
134   const scoped_refptr<ReadIOBuffer> read_buf_;
135   const scoped_refptr<QueuedWriteIOBuffer> write_buf_;
136 
137   std::unique_ptr<WebSocket> web_socket_;
138 };
139 
140 }  // namespace net
141 
142 #endif  // NET_SERVER_HTTP_CONNECTION_H_
143