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. Not to move data when part of data has been |
| 25 // consumed, It has 3 parts: |
| 26 // |
| 27 // 1) consumed data: [base_->StartOfBuffer(), data()] |
| 28 // It is part cosumed by caller. If caller doesn't consume all data read, |
| 29 // i.e. length of unconsumed data is greater than zero, ReadIOBuffer |
| 30 // moves only data pointer, but not move data itself. |
| 31 // |
| 32 // 2) unconsumed data: [data(), base_->data()] |
| 33 // It is part read from underlying socket but not consumed by caller yet. |
| 34 // |
| 35 // 3) not used buffer: [base_->data(), |
| 36 // base_->data() + base_->RemainingCapacity()] |
| 37 // It is part available for next reading operation of underlying socket. |
| 38 class ReadIOBuffer : public IOBuffer { |
| 39 public: |
| 40 ReadIOBuffer(); |
| 41 |
| 42 // Returns IOBuffer to append more data read. |
| 43 IOBuffer* GetUnusedIOBuffer() const; |
| 44 // Returns capacity not used yet. |
| 45 int GetUnusedCapacity() const; |
| 46 |
| 47 // Capacity of |base_|. |
| 48 int GetCapacity() const; |
| 49 void SetCapacity(int capacity); |
| 50 // Increases capacity and returns true if capacity is not beyond the limit. |
| 51 bool IncreaseCapacity(); |
| 52 |
| 53 // More data is appended. Refresh |data_| if base_->data() has changed. |
| 54 void DidRead(int bytes); |
| 55 |
| 56 // Returns the bytes of unconsumed data. |
| 57 int GetUnconsumedSize() const; |
| 58 |
| 59 // Changes |data_| so that |data_| always points to the first unconsumed |
| 60 // byte. |
| 61 void DidConsume(int bytes); |
| 62 |
| 63 // Limit of how much internal capacity can increase. |
| 64 int capacity_limit() const { return capacity_limit_; } |
| 65 void set_capacity_limit(int limit) { capacity_limit_ = limit; } |
| 66 |
| 67 private: |
| 68 friend class HttpConnectionTest; |
| 69 |
| 70 static const int kInitialBufSize = 1024; |
| 71 static const int kMinimumBufSize = 128; |
| 72 static const int kCapacityIncreaseFactor = 2; |
| 73 static const int kDefaultCapacityLimit = 1 * 1024 * 1024; // 1 Mbytes. |
| 74 |
| 75 virtual ~ReadIOBuffer(); |
| 76 |
| 77 scoped_refptr<GrowableIOBuffer> base_; |
| 78 int capacity_limit_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer); |
| 81 }; |
| 82 |
| 83 // IOBuffer of pending data to write which has a queue of pending data. Each |
| 84 // pending data is stored in std::string. data() is the data of first |
| 85 // std::string stored. |
| 86 class PendingWriteIOBuffer : public IOBuffer { |
| 87 public: |
| 88 PendingWriteIOBuffer(); |
| 89 |
| 90 // Whether or not pending data exists. |
| 91 bool IsEmpty() const; |
| 92 |
| 93 // Appends new pending data and returns true if total size doesn't exceed |
| 94 // the limit, |total_size_limit_|. It would change data() if new data is |
| 95 // the first pending data. |
| 96 bool Append(const std::string& data); |
| 97 |
| 98 // Consumes data and changes data() accordingly. |
| 99 void DidConsume(int size); |
| 100 |
| 101 // Gets size of data to write this time. It is NOT total data size. |
| 102 int GetSizeToWrite() const; |
| 103 |
| 104 int total_size() const { return total_size_; } |
| 105 // Limit of how much total_size can increase. |
| 106 int total_size_limit() const { return total_size_limit_; } |
| 107 void set_total_size_limit(int limit) { total_size_limit_ = limit; } |
| 108 |
| 109 private: |
| 110 friend class HttpConnectionTest; |
| 111 |
| 112 static const int kDefaultTotalSizeLimit = 1 * 1024 * 1024; // 1 Mbytes. |
| 113 |
| 114 virtual ~PendingWriteIOBuffer(); |
| 115 |
| 116 std::queue<std::string> pending_data_; |
| 117 int total_size_; |
| 118 int total_size_limit_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(PendingWriteIOBuffer); |
| 121 }; |
| 122 |
| 123 HttpConnection(int id, scoped_ptr<StreamSocket> socket); |
23 ~HttpConnection(); | 124 ~HttpConnection(); |
24 | 125 |
25 void Send(const std::string& data); | 126 int id() const { return id_; } |
26 void Send(const char* bytes, int len); | 127 StreamSocket* socket() const { return socket_.get(); } |
27 void Send(const HttpServerResponseInfo& response); | 128 ReadIOBuffer* read_buf() const { return read_buf_.get(); } |
| 129 PendingWriteIOBuffer* write_buf() const { return write_buf_.get(); } |
28 | 130 |
29 void Shift(int num_bytes); | 131 WebSocket* web_socket() const { return web_socket_.get(); } |
30 | 132 void SetWebSocket(scoped_ptr<WebSocket> web_socket); |
31 const std::string& recv_data() const { return recv_data_; } | |
32 int id() const { return id_; } | |
33 | 133 |
34 private: | 134 private: |
35 friend class HttpServer; | 135 const int id_; |
36 static int last_id_; | 136 const scoped_ptr<StreamSocket> socket_; |
| 137 const scoped_refptr<ReadIOBuffer> read_buf_; |
| 138 const scoped_refptr<PendingWriteIOBuffer> write_buf_; |
37 | 139 |
38 HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); | 140 scoped_ptr<WebSocket> web_socket_; |
39 | 141 |
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); | 142 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
46 }; | 143 }; |
47 | 144 |
48 } // namespace net | 145 } // namespace net |
49 | 146 |
50 #endif // NET_SERVER_HTTP_CONNECTION_H_ | 147 #endif // NET_SERVER_HTTP_CONNECTION_H_ |
OLD | NEW |