http_connection.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  3. // found in the LICENSE file.
  4. #ifndef NET_SERVER_HTTP_CONNECTION_H_
  5. #define NET_SERVER_HTTP_CONNECTION_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/queue.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "net/base/io_buffer.h"
  11. namespace net {
  12. class StreamSocket;
  13. class WebSocket;
  14. // A container which has all information of an http connection. It includes
  15. // id, underlying socket, and pending read/write data.
  16. class HttpConnection {
  17. public:
  18. // IOBuffer for data read. It's a wrapper around GrowableIOBuffer, with more
  19. // functions for buffer management. It moves unconsumed data to the start of
  20. // buffer.
  21. class ReadIOBuffer : public IOBuffer {
  22. public:
  23. static const int kInitialBufSize = 1024;
  24. static const int kMinimumBufSize = 128;
  25. static const int kCapacityIncreaseFactor = 2;
  26. static const int kDefaultMaxBufferSize = 1 * 1024 * 1024; // 1 Mbytes.
  27. ReadIOBuffer();
  28. ReadIOBuffer(const ReadIOBuffer&) = delete;
  29. ReadIOBuffer& operator=(const ReadIOBuffer&) = delete;
  30. // Capacity.
  31. int GetCapacity() const;
  32. void SetCapacity(int capacity);
  33. // Increases capacity and returns true if capacity is not beyond the limit.
  34. bool IncreaseCapacity();
  35. // Start of read data.
  36. char* StartOfBuffer() const;
  37. // Returns the bytes of read data.
  38. int GetSize() const;
  39. // More read data was appended.
  40. void DidRead(int bytes);
  41. // Capacity for which more read data can be appended.
  42. int RemainingCapacity() const;
  43. // Removes consumed data and moves unconsumed data to the start of buffer.
  44. void DidConsume(int bytes);
  45. // Limit of how much internal capacity can increase.
  46. int max_buffer_size() const { return max_buffer_size_; }
  47. void set_max_buffer_size(int max_buffer_size) {
  48. max_buffer_size_ = max_buffer_size;
  49. }
  50. private:
  51. ~ReadIOBuffer() override;
  52. scoped_refptr<GrowableIOBuffer> base_;
  53. int max_buffer_size_ = kDefaultMaxBufferSize;
  54. };
  55. // IOBuffer of pending data to write which has a queue of pending data. Each
  56. // pending data is stored in std::string. data() is the data of first
  57. // std::string stored.
  58. class QueuedWriteIOBuffer : public IOBuffer {
  59. public:
  60. static const int kDefaultMaxBufferSize = 1 * 1024 * 1024; // 1 Mbytes.
  61. QueuedWriteIOBuffer();
  62. QueuedWriteIOBuffer(const QueuedWriteIOBuffer&) = delete;
  63. QueuedWriteIOBuffer& operator=(const QueuedWriteIOBuffer&) = delete;
  64. // Whether or not pending data exists.
  65. bool IsEmpty() const;
  66. // Appends new pending data and returns true if total size doesn't exceed
  67. // the limit, |total_size_limit_|. It would change data() if new data is
  68. // the first pending data.
  69. bool Append(const std::string& data);
  70. // Consumes data and changes data() accordingly. It cannot be more than
  71. // GetSizeToWrite().
  72. void DidConsume(int size);
  73. // Gets size of data to write this time. It is NOT total data size.
  74. int GetSizeToWrite() const;
  75. // Total size of all pending data.
  76. int total_size() const { return total_size_; }
  77. // Limit of how much data can be pending.
  78. int max_buffer_size() const { return max_buffer_size_; }
  79. void set_max_buffer_size(int max_buffer_size) {
  80. max_buffer_size_ = max_buffer_size;
  81. }
  82. private:
  83. ~QueuedWriteIOBuffer() override;
  84. // This needs to indirect since we need pointer stability for the payload
  85. // chunks, as they may be handed out via net::IOBuffer::data().
  86. base::queue<std::unique_ptr<std::string>> pending_data_;
  87. int total_size_ = 0;
  88. int max_buffer_size_ = kDefaultMaxBufferSize;
  89. };
  90. HttpConnection(int id, std::unique_ptr<StreamSocket> socket);
  91. HttpConnection(const HttpConnection&) = delete;
  92. HttpConnection& operator=(const HttpConnection&) = delete;
  93. ~HttpConnection();
  94. int id() const { return id_; }
  95. StreamSocket* socket() const { return socket_.get(); }
  96. ReadIOBuffer* read_buf() const { return read_buf_.get(); }
  97. QueuedWriteIOBuffer* write_buf() const { return write_buf_.get(); }
  98. WebSocket* web_socket() const { return web_socket_.get(); }
  99. void SetWebSocket(std::unique_ptr<WebSocket> web_socket);
  100. private:
  101. const int id_;
  102. const std::unique_ptr<StreamSocket> socket_;
  103. const scoped_refptr<ReadIOBuffer> read_buf_;
  104. const scoped_refptr<QueuedWriteIOBuffer> write_buf_;
  105. std::unique_ptr<WebSocket> web_socket_;
  106. };
  107. } // namespace net
  108. #endif // NET_SERVER_HTTP_CONNECTION_H_