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" |
| 12 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "net/http/http_status_code.h" | 14 #include "net/base/io_buffer.h" |
13 | 15 |
14 namespace net { | 16 namespace net { |
15 | 17 |
16 class HttpServer; | 18 class StreamSocket; |
17 class HttpServerResponseInfo; | |
18 class StreamListenSocket; | |
19 class WebSocket; | 19 class WebSocket; |
20 | 20 |
| 21 // A container which has all information of an http connection. It includes |
| 22 // id, underlying socket, and pending read/write data. |
21 class HttpConnection { | 23 class HttpConnection { |
22 public: | 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. |
| 34 |
| 35 ReadIOBuffer(); |
| 36 |
| 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(); |
| 42 |
| 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; |
| 51 |
| 52 // Removes consumed data and moves unconsumed data to the start of buffer. |
| 53 void DidConsume(int bytes); |
| 54 |
| 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; |
| 59 } |
| 60 |
| 61 private: |
| 62 virtual ~ReadIOBuffer(); |
| 63 |
| 64 scoped_refptr<GrowableIOBuffer> base_; |
| 65 int max_buffer_size_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer); |
| 68 }; |
| 69 |
| 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. |
| 76 |
| 77 QueuedWriteIOBuffer(); |
| 78 |
| 79 // Whether or not pending data exists. |
| 80 bool IsEmpty() const; |
| 81 |
| 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); |
| 86 |
| 87 // Consumes data and changes data() accordingly. It cannot be more than |
| 88 // GetSizeToWrite(). |
| 89 void DidConsume(int size); |
| 90 |
| 91 // Gets size of data to write this time. It is NOT total data size. |
| 92 int GetSizeToWrite() const; |
| 93 |
| 94 // Total size of all pending data. |
| 95 int total_size() const { return total_size_; } |
| 96 |
| 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; |
| 101 } |
| 102 |
| 103 private: |
| 104 virtual ~QueuedWriteIOBuffer(); |
| 105 |
| 106 std::queue<std::string> pending_data_; |
| 107 int total_size_; |
| 108 int max_buffer_size_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(QueuedWriteIOBuffer); |
| 111 }; |
| 112 |
| 113 HttpConnection(int id, scoped_ptr<StreamSocket> socket); |
23 ~HttpConnection(); | 114 ~HttpConnection(); |
24 | 115 |
25 void Send(const std::string& data); | 116 int id() const { return id_; } |
26 void Send(const char* bytes, int len); | 117 StreamSocket* socket() const { return socket_.get(); } |
27 void Send(const HttpServerResponseInfo& response); | 118 ReadIOBuffer* read_buf() const { return read_buf_.get(); } |
| 119 QueuedWriteIOBuffer* write_buf() const { return write_buf_.get(); } |
28 | 120 |
29 void Shift(int num_bytes); | 121 WebSocket* web_socket() const { return web_socket_.get(); } |
30 | 122 void SetWebSocket(scoped_ptr<WebSocket> web_socket); |
31 const std::string& recv_data() const { return recv_data_; } | |
32 int id() const { return id_; } | |
33 | 123 |
34 private: | 124 private: |
35 friend class HttpServer; | 125 const int id_; |
36 static int last_id_; | 126 const scoped_ptr<StreamSocket> socket_; |
| 127 const scoped_refptr<ReadIOBuffer> read_buf_; |
| 128 const scoped_refptr<QueuedWriteIOBuffer> write_buf_; |
37 | 129 |
38 HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); | 130 scoped_ptr<WebSocket> web_socket_; |
39 | 131 |
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); | 132 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
46 }; | 133 }; |
47 | 134 |
48 } // namespace net | 135 } // namespace net |
49 | 136 |
50 #endif // NET_SERVER_HTTP_CONNECTION_H_ | 137 #endif // NET_SERVER_HTTP_CONNECTION_H_ |
OLD | NEW |