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_SERVER_HTTP_SERVER_H_ 6 #define NET_SERVER_HTTP_SERVER_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <memory> 13 #include <string> 14 #include <string_view> 15 16 #include "base/memory/raw_ptr.h" 17 #include "base/memory/weak_ptr.h" 18 #include "net/http/http_status_code.h" 19 #include "net/traffic_annotation/network_traffic_annotation.h" 20 21 namespace net { 22 23 class HttpConnection; 24 class HttpServerRequestInfo; 25 class HttpServerResponseInfo; 26 class IPEndPoint; 27 class ServerSocket; 28 class StreamSocket; 29 30 class HttpServer { 31 public: 32 // Delegate to handle http/websocket events. Beware that it is not safe to 33 // destroy the HttpServer in any of these callbacks. 34 class Delegate { 35 public: 36 virtual ~Delegate() = default; 37 38 virtual void OnConnect(int connection_id) = 0; 39 virtual void OnHttpRequest(int connection_id, 40 const HttpServerRequestInfo& info) = 0; 41 virtual void OnWebSocketRequest(int connection_id, 42 const HttpServerRequestInfo& info) = 0; 43 virtual void OnWebSocketMessage(int connection_id, std::string data) = 0; 44 virtual void OnClose(int connection_id) = 0; 45 }; 46 47 // Instantiates a http server with |server_socket| which already started 48 // listening, but not accepting. This constructor schedules accepting 49 // connections asynchronously in case when |delegate| is not ready to get 50 // callbacks yet. 51 HttpServer(std::unique_ptr<ServerSocket> server_socket, 52 HttpServer::Delegate* delegate); 53 54 HttpServer(const HttpServer&) = delete; 55 HttpServer& operator=(const HttpServer&) = delete; 56 57 ~HttpServer(); 58 59 void AcceptWebSocket(int connection_id, 60 const HttpServerRequestInfo& request, 61 NetworkTrafficAnnotationTag traffic_annotation); 62 void SendOverWebSocket(int connection_id, 63 std::string_view data, 64 NetworkTrafficAnnotationTag traffic_annotation); 65 // Sends the provided data directly to the given connection. No validation is 66 // performed that data constitutes a valid HTTP response. A valid HTTP 67 // response may be split across multiple calls to SendRaw. 68 void SendRaw(int connection_id, 69 const std::string& data, 70 NetworkTrafficAnnotationTag traffic_annotation); 71 // TODO(byungchul): Consider replacing function name with SendResponseInfo 72 void SendResponse(int connection_id, 73 const HttpServerResponseInfo& response, 74 NetworkTrafficAnnotationTag traffic_annotation); 75 void Send(int connection_id, 76 HttpStatusCode status_code, 77 const std::string& data, 78 const std::string& mime_type, 79 NetworkTrafficAnnotationTag traffic_annotation); 80 void Send200(int connection_id, 81 const std::string& data, 82 const std::string& mime_type, 83 NetworkTrafficAnnotationTag traffic_annotation); 84 void Send404(int connection_id, 85 NetworkTrafficAnnotationTag traffic_annotation); 86 void Send500(int connection_id, 87 const std::string& message, 88 NetworkTrafficAnnotationTag traffic_annotation); 89 90 void Close(int connection_id); 91 92 void SetReceiveBufferSize(int connection_id, int32_t size); 93 void SetSendBufferSize(int connection_id, int32_t size); 94 95 // Copies the local address to |address|. Returns a network error code. 96 int GetLocalAddress(IPEndPoint* address); 97 98 private: 99 friend class HttpServerTest; 100 101 void DoAcceptLoop(); 102 void OnAcceptCompleted(int rv); 103 int HandleAcceptResult(int rv); 104 105 void DoReadLoop(HttpConnection* connection); 106 void OnReadCompleted(int connection_id, int rv); 107 int HandleReadResult(HttpConnection* connection, int rv); 108 109 void DoWriteLoop(HttpConnection* connection, 110 NetworkTrafficAnnotationTag traffic_annotation); 111 void OnWriteCompleted(int connection_id, 112 NetworkTrafficAnnotationTag traffic_annotation, 113 int rv); 114 int HandleWriteResult(HttpConnection* connection, int rv); 115 116 // Expects the raw data to be stored in recv_data_. If parsing is successful, 117 // will remove the data parsed from recv_data_, leaving only the unused 118 // recv data. If all data has been consumed successfully, but the headers are 119 // not fully parsed, *pos will be set to zero. Returns false if an error is 120 // encountered while parsing, true otherwise. 121 bool ParseHeaders(const char* data, 122 size_t data_len, 123 HttpServerRequestInfo* info, 124 size_t* pos); 125 126 HttpConnection* FindConnection(int connection_id); 127 128 // Whether or not Close() has been called during delegate callback processing. 129 bool HasClosedConnection(HttpConnection* connection); 130 131 const std::unique_ptr<ServerSocket> server_socket_; 132 std::unique_ptr<StreamSocket> accepted_socket_; 133 const raw_ptr<HttpServer::Delegate> delegate_; 134 135 int last_id_ = 0; 136 std::map<int, std::unique_ptr<HttpConnection>> id_to_connection_; 137 138 base::WeakPtrFactory<HttpServer> weak_ptr_factory_{this}; 139 }; 140 141 } // namespace net 142 143 #endif // NET_SERVER_HTTP_SERVER_H_ 144