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