Commit 84c0618a authored by Lily Houghton's avatar Lily Houghton Committed by Commit Bot

Add defintions for network::server::HttpServer::Set*BufferSize functions.

Bug: 821000
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_mojo
Change-Id: I73d89322a3fb7a965000d7eff9e76edf07924c94
Reviewed-on: https://chromium-review.googlesource.com/1033422
Commit-Queue: Lily Houghton <lilyhoughton@chromium.org>
Reviewed-by: default avatarHelen Li <xunjieli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555172}
parent ebe018ff
......@@ -48,22 +48,32 @@ class HttpConnection {
WebSocket* web_socket() const { return web_socket_.get(); }
void SetWebSocket(std::unique_ptr<WebSocket> web_socket);
void SetReadBufferSize(size_t buf_size) { max_read_buf_size_ = buf_size; }
void SetWriteBufferSize(size_t buf_size) { max_write_buf_size_ = buf_size; }
size_t ReadBufferSize() const { return max_read_buf_size_; }
size_t WriteBufferSize() const { return max_write_buf_size_; }
const size_t kMaxBufferSize = 1 * 1024 * 1024; // 1 Mbyte
private:
const int id_;
const mojom::TCPConnectedSocketPtr socket_;
const mojo::ScopedDataPipeConsumerHandle socket_receive_handle_;
mojo::SimpleWatcher receive_pipe_watcher_;
const mojo::ScopedDataPipeProducerHandle socket_send_handle_;
mojo::SimpleWatcher send_pipe_watcher_;
const net::IPEndPoint peer_addr_;
// Stores data that has been read from the server but not yet parsed into an
// HTTP request.
std::string read_buf_;
size_t max_read_buf_size_ = kMaxBufferSize;
const mojo::ScopedDataPipeConsumerHandle socket_receive_handle_;
mojo::SimpleWatcher receive_pipe_watcher_;
// Stores data that has been marked for sending but has not yet been sent to
// the network.
std::string write_buf_;
size_t max_write_buf_size_ = kMaxBufferSize;
const mojo::ScopedDataPipeProducerHandle socket_send_handle_;
mojo::SimpleWatcher send_pipe_watcher_;
const net::IPEndPoint peer_addr_;
std::unique_ptr<WebSocket> web_socket_;
......
......@@ -95,7 +95,7 @@ void HttpServer::SendRaw(int connection_id,
return;
if (connection->write_buf().size() + data.size() >
connection->kMaxBufferSize) {
connection->WriteBufferSize()) {
LOG(ERROR) << "Write buffer is full.";
return;
}
......@@ -164,6 +164,20 @@ void HttpServer::Close(int connection_id) {
connection.release());
}
bool HttpServer::SetReceiveBufferSize(int connection_id, int32_t size) {
HttpConnection* connection = FindConnection(connection_id);
if (connection)
connection->SetReadBufferSize(size);
return connection;
}
bool HttpServer::SetSendBufferSize(int connection_id, int32_t size) {
HttpConnection* connection = FindConnection(connection_id);
if (connection)
connection->SetWriteBufferSize(size);
return connection;
}
void HttpServer::GetLocalAddress(
mojom::TCPServerSocket::GetLocalAddressCallback callback) {
server_socket_->GetLocalAddress(std::move(callback));
......@@ -223,7 +237,8 @@ void HttpServer::OnReadable(int connection_id, MojoResult result) {
return;
}
if (connection->read_buf().size() + bytes_read > connection->kMaxBufferSize) {
if (connection->read_buf().size() + bytes_read >
connection->ReadBufferSize()) {
LOG(ERROR) << "Read buffer is full.";
connection->receive_handle().EndReadData(bytes_read);
return;
......
......@@ -92,8 +92,11 @@ class COMPONENT_EXPORT(NETWORK_CPP) HttpServer {
void Close(int connection_id);
void SetReceiveBufferSize(int connection_id, int32_t size);
void SetSendBufferSize(int connection_id, int32_t size);
// These two functions set the read and write buffers of the HttpConnection
// identified by |connection_id|. These return |true| on success, and return
// |false| if there is no object indexed by |connection_id|.
bool SetReceiveBufferSize(int connection_id, int32_t size);
bool SetSendBufferSize(int connection_id, int32_t size);
// Asynchronously gets the local address of this http server. On completion,
// |callback| will be called with a network error code and the local address
......
......@@ -265,6 +265,10 @@ class HttpServerTest : public testing::Test, public HttpServer::Delegate {
return requests_[request_index].second;
}
HttpConnection* FindConnection(int connection_id) {
return server_->FindConnection(connection_id);
}
std::unordered_map<int, bool>& connection_map() { return connection_map_; }
protected:
......@@ -300,10 +304,23 @@ class WebSocketTest : public HttpServerTest {
}
};
TEST_F(HttpServerTest, SetNonexistingConnectionBuffer) {
EXPECT_FALSE(server_->SetReceiveBufferSize(1, 1000));
EXPECT_FALSE(server_->SetSendBufferSize(1, 1000));
}
TEST_F(HttpServerTest, Request) {
TestHttpClient client;
ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk());
client.Send("GET /test HTTP/1.1\r\n\r\n");
int connection_id = connection_map_.begin()->first;
HttpConnection* conn = FindConnection(connection_id);
EXPECT_TRUE(server_->SetReceiveBufferSize(connection_id, 5u * 1024 * 1024));
EXPECT_TRUE(server_->SetSendBufferSize(connection_id, 5u * 1024 * 1024));
EXPECT_EQ(conn->ReadBufferSize(), 5u * 1024u * 1024u);
EXPECT_EQ(conn->WriteBufferSize(), 5u * 1024u * 1024u);
RunUntilRequestsReceived(1);
ASSERT_EQ("GET", GetRequest(0).method);
ASSERT_EQ("/test", GetRequest(0).path);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment