[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 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_ |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 11 | #include <map> |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 12 | #include <memory> |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 13 | #include <string> |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 14 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 15 | #include "base/macros.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
Johannes Henkel | da8b9d3 | 2019-03-15 16:15:33 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 18 | #include "net/http/http_status_code.h" |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 19 | #include "net/traffic_annotation/network_traffic_annotation.h" |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 20 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 21 | namespace net { |
| 22 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 23 | class HttpConnection; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 24 | class HttpServerRequestInfo; |
[email protected] | c700fd15 | 2013-07-23 21:29:11 | [diff] [blame] | 25 | class HttpServerResponseInfo; |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 26 | class IPEndPoint; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 27 | class ServerSocket; |
| 28 | class StreamSocket; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 29 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 30 | class HttpServer { |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 31 | public: |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 32 | // Delegate to handle http/websocket events. Beware that it is not safe to |
| 33 | // destroy the HttpServer in any of these callbacks. |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 34 | class Delegate { |
| 35 | public: |
yzshen | 9f0ff74 | 2015-07-13 17:41:08 | [diff] [blame] | 36 | virtual ~Delegate() {} |
| 37 | |
samuong | 0b4d9b95 | 2014-09-26 04:15:29 | [diff] [blame] | 38 | virtual void OnConnect(int connection_id) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 39 | virtual void OnHttpRequest(int connection_id, |
| 40 | const HttpServerRequestInfo& info) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 41 | virtual void OnWebSocketRequest(int connection_id, |
| 42 | const HttpServerRequestInfo& info) = 0; |
Johannes Henkel | 43b68c31 | 2019-03-15 22:05:33 | [diff] [blame] | 43 | virtual void OnWebSocketMessage(int connection_id, std::string data) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 44 | virtual void OnClose(int connection_id) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 45 | }; |
| 46 | |
byungchul | baa7546 | 2014-10-31 19:38:09 | [diff] [blame] | 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. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 51 | HttpServer(std::unique_ptr<ServerSocket> server_socket, |
[email protected] | 8bf9ce7 | 2012-05-23 17:05:31 | [diff] [blame] | 52 | HttpServer::Delegate* delegate); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 53 | ~HttpServer(); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 54 | |
| 55 | void AcceptWebSocket(int connection_id, |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 56 | const HttpServerRequestInfo& request, |
| 57 | NetworkTrafficAnnotationTag traffic_annotation); |
| 58 | void SendOverWebSocket(int connection_id, |
Johannes Henkel | da8b9d3 | 2019-03-15 16:15:33 | [diff] [blame] | 59 | base::StringPiece data, |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 60 | NetworkTrafficAnnotationTag traffic_annotation); |
[email protected] | a6948f9 | 2014-04-19 02:12:40 | [diff] [blame] | 61 | // Sends the provided data directly to the given connection. No validation is |
| 62 | // performed that data constitutes a valid HTTP response. A valid HTTP |
| 63 | // response may be split across multiple calls to SendRaw. |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 64 | void SendRaw(int connection_id, |
| 65 | const std::string& data, |
| 66 | NetworkTrafficAnnotationTag traffic_annotation); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 67 | // TODO(byungchul): Consider replacing function name with SendResponseInfo |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 68 | void SendResponse(int connection_id, |
| 69 | const HttpServerResponseInfo& response, |
| 70 | NetworkTrafficAnnotationTag traffic_annotation); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 71 | void Send(int connection_id, |
| 72 | HttpStatusCode status_code, |
| 73 | const std::string& data, |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 74 | const std::string& mime_type, |
| 75 | NetworkTrafficAnnotationTag traffic_annotation); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 76 | void Send200(int connection_id, |
| 77 | const std::string& data, |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 78 | const std::string& mime_type, |
| 79 | NetworkTrafficAnnotationTag traffic_annotation); |
| 80 | void Send404(int connection_id, |
| 81 | NetworkTrafficAnnotationTag traffic_annotation); |
| 82 | void Send500(int connection_id, |
| 83 | const std::string& message, |
| 84 | NetworkTrafficAnnotationTag traffic_annotation); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 85 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 86 | void Close(int connection_id); |
| 87 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 88 | void SetReceiveBufferSize(int connection_id, int32_t size); |
| 89 | void SetSendBufferSize(int connection_id, int32_t size); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 90 | |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 91 | // Copies the local address to |address|. Returns a network error code. |
| 92 | int GetLocalAddress(IPEndPoint* address); |
| 93 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 94 | private: |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 95 | friend class HttpServerTest; |
| 96 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 97 | void DoAcceptLoop(); |
| 98 | void OnAcceptCompleted(int rv); |
| 99 | int HandleAcceptResult(int rv); |
| 100 | |
| 101 | void DoReadLoop(HttpConnection* connection); |
| 102 | void OnReadCompleted(int connection_id, int rv); |
| 103 | int HandleReadResult(HttpConnection* connection, int rv); |
| 104 | |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 105 | void DoWriteLoop(HttpConnection* connection, |
| 106 | NetworkTrafficAnnotationTag traffic_annotation); |
| 107 | void OnWriteCompleted(int connection_id, |
| 108 | NetworkTrafficAnnotationTag traffic_annotation, |
| 109 | int rv); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 110 | int HandleWriteResult(HttpConnection* connection, int rv); |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 111 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 112 | // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 113 | // will remove the data parsed from recv_data_, leaving only the unused |
slan | f955ff0 | 2016-09-16 23:36:09 | [diff] [blame] | 114 | // recv data. If all data has been consumed successfully, but the headers are |
| 115 | // not fully parsed, *pos will be set to zero. Returns false if an error is |
| 116 | // encountered while parsing, true otherwise. |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 117 | bool ParseHeaders(const char* data, |
| 118 | size_t data_len, |
[email protected] | 2053a1e | 2011-02-04 16:24:30 | [diff] [blame] | 119 | HttpServerRequestInfo* info, |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 120 | size_t* pos); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 121 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 122 | HttpConnection* FindConnection(int connection_id); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 123 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 124 | // Whether or not Close() has been called during delegate callback processing. |
| 125 | bool HasClosedConnection(HttpConnection* connection); |
| 126 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 127 | const std::unique_ptr<ServerSocket> server_socket_; |
| 128 | std::unique_ptr<StreamSocket> accepted_socket_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 129 | HttpServer::Delegate* const delegate_; |
| 130 | |
| 131 | int last_id_; |
avi | c39cece | 2016-09-16 23:10:15 | [diff] [blame] | 132 | std::map<int, std::unique_ptr<HttpConnection>> id_to_connection_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 133 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 134 | base::WeakPtrFactory<HttpServer> weak_ptr_factory_{this}; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 137 | }; |
| 138 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 139 | } // namespace net |
| 140 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 141 | #endif // NET_SERVER_HTTP_SERVER_H_ |