1 // Copyright (c) 2012 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.
5 #ifndef NET_SERVER_HTTP_CONNECTION_H_
6 #define NET_SERVER_HTTP_CONNECTION_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/io_buffer.h"
21 // A container which has all information of an http connection. It includes
22 // id, underlying socket, and pending read/write data.
23 class HttpConnection
{
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
28 class ReadIOBuffer
: public IOBuffer
{
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.
38 int GetCapacity() const;
39 void SetCapacity(int capacity
);
40 // Increases capacity and returns true if capacity is not beyond the limit.
41 bool IncreaseCapacity();
43 // Start of read data.
44 char* StartOfBuffer() const;
45 // Returns the bytes of read data.
47 // More read data was appended.
48 void DidRead(int bytes
);
49 // Capacity for which more read data can be appended.
50 int RemainingCapacity() const;
52 // Removes consumed data and moves unconsumed data to the start of buffer.
53 void DidConsume(int bytes
);
55 // Limit of how much internal capacity can increase.
56 int max_buffer_size() const { return max_buffer_size_
; }
57 void set_max_buffer_size(int max_buffer_size
) {
58 max_buffer_size_
= max_buffer_size
;
62 ~ReadIOBuffer() override
;
64 scoped_refptr
<GrowableIOBuffer
> base_
;
67 DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer
);
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
{
75 static const int kDefaultMaxBufferSize
= 1 * 1024 * 1024; // 1 Mbytes.
77 QueuedWriteIOBuffer();
79 // Whether or not pending data exists.
82 // Appends new pending data and returns true if total size doesn't exceed
83 // the limit, |total_size_limit_|. It would change data() if new data is
84 // the first pending data.
85 bool Append(const std::string
& data
);
87 // Consumes data and changes data() accordingly. It cannot be more than
89 void DidConsume(int size
);
91 // Gets size of data to write this time. It is NOT total data size.
92 int GetSizeToWrite() const;
94 // Total size of all pending data.
95 int total_size() const { return total_size_
; }
97 // Limit of how much data can be pending.
98 int max_buffer_size() const { return max_buffer_size_
; }
99 void set_max_buffer_size(int max_buffer_size
) {
100 max_buffer_size_
= max_buffer_size
;
104 ~QueuedWriteIOBuffer() override
;
106 std::queue
<std::string
> pending_data_
;
108 int max_buffer_size_
;
110 DISALLOW_COPY_AND_ASSIGN(QueuedWriteIOBuffer
);
113 HttpConnection(int id
, scoped_ptr
<StreamSocket
> socket
);
116 int id() const { return id_
; }
117 StreamSocket
* socket() const { return socket_
.get(); }
118 ReadIOBuffer
* read_buf() const { return read_buf_
.get(); }
119 QueuedWriteIOBuffer
* write_buf() const { return write_buf_
.get(); }
121 WebSocket
* web_socket() const { return web_socket_
.get(); }
122 void SetWebSocket(scoped_ptr
<WebSocket
> web_socket
);
126 const scoped_ptr
<StreamSocket
> socket_
;
127 const scoped_refptr
<ReadIOBuffer
> read_buf_
;
128 const scoped_refptr
<QueuedWriteIOBuffer
> write_buf_
;
130 scoped_ptr
<WebSocket
> web_socket_
;
132 DISALLOW_COPY_AND_ASSIGN(HttpConnection
);
137 #endif // NET_SERVER_HTTP_CONNECTION_H_