Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / server / http_connection.h
blobc7225e1a01d24d9088e5c10d92a92da5b89cd385
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_
8 #include <queue>
9 #include <string>
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"
16 namespace net {
18 class StreamSocket;
19 class WebSocket;
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 {
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 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.
35 ReadIOBuffer();
37 // Capacity.
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.
46 int GetSize() const;
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;
61 private:
62 virtual ~ReadIOBuffer();
64 scoped_refptr<GrowableIOBuffer> base_;
65 int max_buffer_size_;
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 {
74 public:
75 static const int kDefaultMaxBufferSize = 1 * 1024 * 1024; // 1 Mbytes.
77 QueuedWriteIOBuffer();
79 // Whether or not pending data exists.
80 bool IsEmpty() const;
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
88 // GetSizeToWrite().
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;
103 private:
104 virtual ~QueuedWriteIOBuffer();
106 std::queue<std::string> pending_data_;
107 int total_size_;
108 int max_buffer_size_;
110 DISALLOW_COPY_AND_ASSIGN(QueuedWriteIOBuffer);
113 HttpConnection(int id, scoped_ptr<StreamSocket> socket);
114 ~HttpConnection();
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);
124 private:
125 const int id_;
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);
135 } // namespace net
137 #endif // NET_SERVER_HTTP_CONNECTION_H_