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