OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SERVER_HTTP_CONNECTION_H_ | 5 #ifndef NET_SERVER_HTTP_CONNECTION_H_ |
6 #define NET_SERVER_HTTP_CONNECTION_H_ | 6 #define NET_SERVER_HTTP_CONNECTION_H_ |
7 | 7 |
8 #include <queue> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "net/http/http_status_code.h" | 13 #include "net/base/io_buffer.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 class HttpServer; | 17 class StreamSocket; |
17 class HttpServerResponseInfo; | |
18 class StreamListenSocket; | |
19 class WebSocket; | 18 class WebSocket; |
20 | 19 |
20 // A container which has all information of an http connection. It includes | |
21 // id, underlying socket, and pending read/write data. | |
21 class HttpConnection { | 22 class HttpConnection { |
22 public: | 23 public: |
24 // IOBuffer for data read. It is similar to GrowableIOBuffer, and has more | |
25 // functions for buffer management. It moves unconsumed data to the start of | |
mmenke
2014/08/08 18:35:43
Maybe replace second sentence with "It's a wrapper
byungchul
2014/08/12 21:36:54
Done.
| |
26 // buffer. | |
27 class ReadIOBuffer : public IOBuffer { | |
28 public: | |
29 ReadIOBuffer(); | |
30 | |
31 // Capacity. | |
32 int GetCapacity() const; | |
33 void SetCapacity(int capacity); | |
34 // Increases capacity and returns true if capacity is not beyond the limit. | |
35 bool IncreaseCapacity(); | |
36 | |
37 // Start of read data. | |
38 char* StartOfBuffer() const; | |
39 // Returns the bytes of read data. | |
40 int GetSize() const; | |
41 // More read data is appended. | |
mmenke
2014/08/08 18:35:43
This doesn't actually append more data. Maybe "wa
byungchul
2014/08/12 21:36:53
Done.
| |
42 void DidRead(int bytes); | |
43 // Capacity for which more read data can be appended. | |
44 int RemainingCapacity() const; | |
45 | |
46 // Removes consumed data and moves unconsumed data to the start of buffer. | |
47 void DidConsume(int bytes); | |
48 | |
49 // Limit of how much internal capacity can increase. | |
50 int capacity_limit() const { return capacity_limit_; } | |
mmenke
2014/08/08 18:35:43
Really should use consistent terminology here and
byungchul
2014/08/12 21:36:54
Changed to max_buffer_size.
| |
51 void set_capacity_limit(int limit) { capacity_limit_ = limit; } | |
mmenke
2014/08/08 18:35:43
nit: limit->capacity_limit
byungchul
2014/08/12 21:36:54
Done.
| |
52 | |
53 private: | |
54 friend class HttpConnectionTest; | |
55 | |
56 static const int kInitialBufSize = 1024; | |
57 static const int kMinimumBufSize = 128; | |
58 static const int kCapacityIncreaseFactor = 2; | |
59 static const int kDefaultCapacityLimit = 1 * 1024 * 1024; // 1 Mbytes. | |
60 | |
61 virtual ~ReadIOBuffer(); | |
62 | |
63 scoped_refptr<GrowableIOBuffer> base_; | |
64 int capacity_limit_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer); | |
67 }; | |
68 | |
69 // IOBuffer of pending data to write which has a queue of pending data. Each | |
70 // pending data is stored in std::string. data() is the data of first | |
71 // std::string stored. | |
72 class PendingWriteIOBuffer : public IOBuffer { | |
mmenke
2014/08/08 18:35:43
I wonder if this really should be an IOBuffer - th
byungchul
2014/08/12 21:36:53
IOBuffer has only data() and doesn't imply anythin
| |
73 public: | |
74 PendingWriteIOBuffer(); | |
75 | |
76 // Whether or not pending data exists. | |
77 bool IsEmpty() const; | |
78 | |
79 // Appends new pending data and returns true if total size doesn't exceed | |
80 // the limit, |total_size_limit_|. It would change data() if new data is | |
81 // the first pending data. | |
82 bool Append(const std::string& data); | |
83 | |
84 // Consumes data and changes data() accordingly. | |
mmenke
2014/08/08 18:35:43
Worth mentioning it can't be mroe than GetSizeToWr
byungchul
2014/08/12 21:36:54
Done.
| |
85 void DidConsume(int size); | |
86 | |
87 // Gets size of data to write this time. It is NOT total data size. | |
88 int GetSizeToWrite() const; | |
89 | |
90 int total_size() const { return total_size_; } | |
mmenke
2014/08/08 18:35:43
I don't think this is used.
byungchul
2014/08/12 21:36:54
Used in unittests.
| |
91 // Limit of how much total_size can increase. | |
92 int total_size_limit() const { return total_size_limit_; } | |
mmenke
2014/08/08 18:35:43
Is this used anywhere?
byungchul
2014/08/12 21:36:53
in unittests.
| |
93 void set_total_size_limit(int limit) { total_size_limit_ = limit; } | |
mmenke
2014/08/08 18:35:43
Suggest renaming total_size_limit_ to max_buffer_s
byungchul
2014/08/12 21:36:53
Done.
| |
94 | |
95 private: | |
96 friend class HttpConnectionTest; | |
97 | |
98 static const int kDefaultTotalSizeLimit = 1 * 1024 * 1024; // 1 Mbytes. | |
99 | |
100 virtual ~PendingWriteIOBuffer(); | |
101 | |
102 std::queue<std::string> pending_data_; | |
103 int total_size_; | |
mmenke
2014/08/08 18:35:43
Is this used for anything other than sanity checks
byungchul
2014/08/12 21:36:53
Used in Append() not to queue more than capacity.
| |
104 int total_size_limit_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(PendingWriteIOBuffer); | |
107 }; | |
108 | |
109 HttpConnection(int id, scoped_ptr<StreamSocket> socket); | |
23 ~HttpConnection(); | 110 ~HttpConnection(); |
24 | 111 |
25 void Send(const std::string& data); | 112 int id() const { return id_; } |
26 void Send(const char* bytes, int len); | 113 StreamSocket* socket() const { return socket_.get(); } |
27 void Send(const HttpServerResponseInfo& response); | 114 ReadIOBuffer* read_buf() const { return read_buf_.get(); } |
115 PendingWriteIOBuffer* write_buf() const { return write_buf_.get(); } | |
28 | 116 |
29 void Shift(int num_bytes); | 117 WebSocket* web_socket() const { return web_socket_.get(); } |
30 | 118 void SetWebSocket(scoped_ptr<WebSocket> web_socket); |
31 const std::string& recv_data() const { return recv_data_; } | |
32 int id() const { return id_; } | |
33 | 119 |
34 private: | 120 private: |
35 friend class HttpServer; | 121 const int id_; |
36 static int last_id_; | 122 const scoped_ptr<StreamSocket> socket_; |
123 const scoped_refptr<ReadIOBuffer> read_buf_; | |
124 const scoped_refptr<PendingWriteIOBuffer> write_buf_; | |
37 | 125 |
38 HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); | 126 scoped_ptr<WebSocket> web_socket_; |
39 | 127 |
40 HttpServer* server_; | |
41 scoped_ptr<StreamListenSocket> socket_; | |
42 scoped_ptr<WebSocket> web_socket_; | |
43 std::string recv_data_; | |
44 int id_; | |
45 DISALLOW_COPY_AND_ASSIGN(HttpConnection); | 128 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
46 }; | 129 }; |
47 | 130 |
48 } // namespace net | 131 } // namespace net |
49 | 132 |
50 #endif // NET_SERVER_HTTP_CONNECTION_H_ | 133 #endif // NET_SERVER_HTTP_CONNECTION_H_ |
OLD | NEW |