1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_HTTP1_CONNECTION_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP1_CONNECTION_H_ 7 8 #include <memory> 9 10 #include "base/functional/callback.h" 11 #include "base/functional/callback_forward.h" 12 #include "base/memory/raw_ptr.h" 13 #include "base/memory/scoped_refptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/strings/string_piece.h" 16 #include "net/base/completion_once_callback.h" 17 #include "net/base/io_buffer.h" 18 #include "net/test/embedded_test_server/embedded_test_server_connection_listener.h" 19 #include "net/test/embedded_test_server/http_connection.h" 20 #include "net/test/embedded_test_server/http_request.h" 21 #include "net/test/embedded_test_server/http_response.h" 22 23 namespace net { 24 25 class StreamSocket; 26 27 namespace test_server { 28 29 class EmbeddedTestServer; 30 31 // Wraps the connection socket. Accepts incoming data and sends responses via 32 // HTTP/1.1. 33 // 34 // Should be owned by the EmbeddedTestServer passed to the constructor. 35 // 36 // Lifetime of this connection (and the delegate) is one request/response pair. 37 // Calling FinishResponse will immediately send a signal to the owning 38 // EmbeddedTestServer that the connection can be safely destroyed and the socket 39 // can taken by a connection listener (if it has not already closed and a 40 // listener exists). The connection will also immediately signal the server 41 // to destroy the connection if the socket closes early or returns an error on 42 // read or write. 43 class Http1Connection : public HttpConnection, public HttpResponseDelegate { 44 public: 45 Http1Connection(std::unique_ptr<StreamSocket> socket, 46 EmbeddedTestServerConnectionListener* connection_listener, 47 EmbeddedTestServer* server_delegate); 48 ~Http1Connection() override; 49 Http1Connection(Http1Connection&) = delete; 50 Http1Connection& operator=(Http1Connection&) = delete; 51 52 // HttpConnection 53 void OnSocketReady() override; 54 std::unique_ptr<StreamSocket> TakeSocket() override; 55 StreamSocket* Socket() override; 56 base::WeakPtr<HttpConnection> GetWeakPtr() override; 57 58 // HttpResponseDelegate 59 void AddResponse(std::unique_ptr<HttpResponse> response) override; 60 void SendResponseHeaders(HttpStatusCode status, 61 const std::string& status_reason, 62 const base::StringPairs& headers) override; 63 void SendRawResponseHeaders(const std::string& headers) override; 64 void SendContents(const std::string& contents, 65 base::OnceClosure callback) override; 66 void FinishResponse() override; 67 void SendContentsAndFinish(const std::string& contents) override; 68 void SendHeadersContentAndFinish(HttpStatusCode status, 69 const std::string& status_reason, 70 const base::StringPairs& headers, 71 const std::string& contents) override; 72 73 private: 74 void ReadData(); 75 void OnReadCompleted(int rv); 76 bool HandleReadResult(int rv); 77 void SendInternal(base::OnceClosure callback, 78 scoped_refptr<DrainableIOBuffer> buffer); 79 void OnSendInternalDone(base::OnceClosure callback, 80 scoped_refptr<DrainableIOBuffer> buffer, 81 int rv); 82 83 std::unique_ptr<StreamSocket> socket_; 84 raw_ptr<EmbeddedTestServerConnectionListener, AcrossTasksDanglingUntriaged> 85 connection_listener_; 86 raw_ptr<EmbeddedTestServer> server_delegate_; 87 HttpRequestParser request_parser_; 88 scoped_refptr<IOBufferWithSize> read_buf_; 89 std::vector<std::unique_ptr<HttpResponse>> responses_; 90 91 base::WeakPtrFactory<Http1Connection> weak_factory_{this}; 92 }; 93 94 } // namespace test_server 95 } // namespace net 96 97 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_1_CONNECTION_H_ 98