Chromium Code Reviews| 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" |
|
mmenke
2014/08/14 16:36:02
Should include scoped_refptr
byungchul
2014/08/14 18:44:02
Done.
| |
| 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's a wrapper around GrowableIOBuffer, with more | |
| 25 // functions for buffer management. It moves unconsumed data to the start of | |
| 26 // buffer. | |
| 27 class ReadIOBuffer : public IOBuffer { | |
| 28 public: | |
| 29 static const int kInitialBufSize = 1024; | |
| 30 static const int kMinimumBufSize = 128; | |
| 31 static const int kCapacityIncreaseFactor = 2; | |
| 32 static const int kDefaultMaxBufferSize = 1 * 1024 * 1024; // 1 Mbytes. | |
| 33 | |
| 34 ReadIOBuffer(); | |
| 35 | |
| 36 // Capacity. | |
| 37 int GetCapacity() const; | |
| 38 void SetCapacity(int capacity); | |
| 39 // Increases capacity and returns true if capacity is not beyond the limit. | |
| 40 bool IncreaseCapacity(); | |
| 41 | |
| 42 // Start of read data. | |
| 43 char* StartOfBuffer() const; | |
| 44 // Returns the bytes of read data. | |
| 45 int GetSize() const; | |
| 46 // More read data was appended. | |
| 47 void DidRead(int bytes); | |
| 48 // Capacity for which more read data can be appended. | |
| 49 int RemainingCapacity() const; | |
| 50 | |
| 51 // Removes consumed data and moves unconsumed data to the start of buffer. | |
| 52 void DidConsume(int bytes); | |
| 53 | |
| 54 // Limit of how much internal capacity can increase. | |
| 55 int max_buffer_size() const { return max_buffer_size_; } | |
| 56 void set_max_buffer_size(int max_buffer_size) { | |
| 57 max_buffer_size_ = max_buffer_size; | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 virtual ~ReadIOBuffer(); | |
| 62 | |
| 63 scoped_refptr<GrowableIOBuffer> base_; | |
| 64 int max_buffer_size_; | |
| 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/14 16:36:02
optional: "QueuedWriteIOBuffer" may be clearer, f
byungchul
2014/08/14 18:44:02
Done.
| |
| 73 public: | |
| 74 static const int kDefaultMaxBufferSize = 1 * 1024 * 1024; // 1 Mbytes. | |
| 75 | |
| 76 PendingWriteIOBuffer(); | |
| 77 | |
| 78 // Whether or not pending data exists. | |
| 79 bool IsEmpty() const; | |
| 80 | |
| 81 // Appends new pending data and returns true if total size doesn't exceed | |
| 82 // the limit, |total_size_limit_|. It would change data() if new data is | |
| 83 // the first pending data. | |
| 84 bool Append(const std::string& data); | |
| 85 | |
| 86 // Consumes data and changes data() accordingly. It cannot be more than | |
| 87 // GetSizeToWrite(). | |
| 88 void DidConsume(int size); | |
| 89 | |
| 90 // Gets size of data to write this time. It is NOT total data size. | |
| 91 int GetSizeToWrite() const; | |
| 92 | |
| 93 // Total size of all pending data. | |
| 94 int total_size() const { return total_size_; } | |
| 95 | |
| 96 // Limit of how much data can be pending. | |
| 97 int max_buffer_size() const { return max_buffer_size_; } | |
| 98 void set_max_buffer_size(int max_buffer_size) { | |
| 99 max_buffer_size_ = max_buffer_size; | |
| 100 } | |
| 101 | |
| 102 private: | |
| 103 virtual ~PendingWriteIOBuffer(); | |
| 104 | |
| 105 std::queue<std::string> pending_data_; | |
| 106 int total_size_; | |
| 107 int max_buffer_size_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(PendingWriteIOBuffer); | |
| 110 }; | |
| 111 | |
| 112 HttpConnection(int id, scoped_ptr<StreamSocket> socket); | |
| 23 ~HttpConnection(); | 113 ~HttpConnection(); |
| 24 | 114 |
| 25 void Send(const std::string& data); | 115 int id() const { return id_; } |
| 26 void Send(const char* bytes, int len); | 116 StreamSocket* socket() const { return socket_.get(); } |
| 27 void Send(const HttpServerResponseInfo& response); | 117 ReadIOBuffer* read_buf() const { return read_buf_.get(); } |
| 118 PendingWriteIOBuffer* write_buf() const { return write_buf_.get(); } | |
| 28 | 119 |
| 29 void Shift(int num_bytes); | 120 WebSocket* web_socket() const { return web_socket_.get(); } |
| 30 | 121 void SetWebSocket(scoped_ptr<WebSocket> web_socket); |
| 31 const std::string& recv_data() const { return recv_data_; } | |
| 32 int id() const { return id_; } | |
| 33 | 122 |
| 34 private: | 123 private: |
| 35 friend class HttpServer; | 124 const int id_; |
| 36 static int last_id_; | 125 const scoped_ptr<StreamSocket> socket_; |
| 126 const scoped_refptr<ReadIOBuffer> read_buf_; | |
| 127 const scoped_refptr<PendingWriteIOBuffer> write_buf_; | |
| 37 | 128 |
| 38 HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); | 129 scoped_ptr<WebSocket> web_socket_; |
| 39 | 130 |
| 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); | 131 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
| 46 }; | 132 }; |
| 47 | 133 |
| 48 } // namespace net | 134 } // namespace net |
| 49 | 135 |
| 50 #endif // NET_SERVER_HTTP_CONNECTION_H_ | 136 #endif // NET_SERVER_HTTP_CONNECTION_H_ |
| OLD | NEW |