1 // Copyright 2012 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_HTTP_CONNECTION_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ 7 8 #include <memory> 9 10 #include "base/functional/callback_forward.h" 11 #include "base/memory/weak_ptr.h" 12 #include "net/base/completion_once_callback.h" 13 #include "net/test/embedded_test_server/embedded_test_server_connection_listener.h" 14 #include "net/test/embedded_test_server/http_request.h" 15 #include "net/test/embedded_test_server/http_response.h" 16 17 namespace net { 18 19 class StreamSocket; 20 21 namespace test_server { 22 23 class EmbeddedTestServer; 24 25 // Wraps the connection socket. Accepts incoming data and sends responses. 26 // If a valid request is parsed, then |callback_| is invoked. 27 class HttpConnection { 28 public: 29 enum class Protocol { kHttp1, kHttp2 }; 30 31 HttpConnection() = default; 32 virtual ~HttpConnection() = default; 33 HttpConnection(HttpConnection&) = delete; 34 virtual HttpConnection& operator=(HttpConnection&) = delete; 35 36 // Construct the correct connection based on the server's protocol. 37 static std::unique_ptr<HttpConnection> Create( 38 std::unique_ptr<StreamSocket> socket, 39 EmbeddedTestServerConnectionListener* listener, 40 EmbeddedTestServer* server, 41 Protocol protocol); 42 43 // Notify that the socket is ready to receive data (which may not be 44 // immediately, due to SSL handshake). May call the delegate's HandleRequest() 45 // or CloseConnection() methods, and may call them asynchronously. 46 virtual void OnSocketReady() = 0; 47 48 // Pass ownership of the socket. This will likely invalidate the connection. 49 virtual std::unique_ptr<StreamSocket> TakeSocket() = 0; 50 51 virtual StreamSocket* Socket() = 0; 52 virtual base::WeakPtr<HttpConnection> GetWeakPtr() = 0; 53 }; 54 55 } // namespace test_server 56 } // namespace net 57 58 #endif // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_ 59