[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" |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 17 | #include "net/http/http_status_code.h" |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 18 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 19 | namespace net { |
| 20 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 21 | class HttpConnection; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 22 | class HttpServerRequestInfo; |
[email protected] | c700fd15 | 2013-07-23 21:29:11 | [diff] [blame] | 23 | class HttpServerResponseInfo; |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 24 | class IPEndPoint; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 25 | class ServerSocket; |
| 26 | class StreamSocket; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 27 | class WebSocket; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 28 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 29 | class HttpServer { |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 30 | public: |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 31 | // Delegate to handle http/websocket events. Beware that it is not safe to |
| 32 | // destroy the HttpServer in any of these callbacks. |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 33 | class Delegate { |
| 34 | public: |
yzshen | 9f0ff74 | 2015-07-13 17:41:08 | [diff] [blame] | 35 | virtual ~Delegate() {} |
| 36 | |
samuong | 0b4d9b95 | 2014-09-26 04:15:29 | [diff] [blame] | 37 | virtual void OnConnect(int connection_id) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 38 | virtual void OnHttpRequest(int connection_id, |
| 39 | const HttpServerRequestInfo& info) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 40 | virtual void OnWebSocketRequest(int connection_id, |
| 41 | const HttpServerRequestInfo& info) = 0; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 42 | virtual void OnWebSocketMessage(int connection_id, |
| 43 | const 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, |
| 56 | const HttpServerRequestInfo& request); |
| 57 | void SendOverWebSocket(int connection_id, const std::string& data); |
[email protected] | a6948f9 | 2014-04-19 02:12:40 | [diff] [blame] | 58 | // Sends the provided data directly to the given connection. No validation is |
| 59 | // performed that data constitutes a valid HTTP response. A valid HTTP |
| 60 | // response may be split across multiple calls to SendRaw. |
| 61 | void SendRaw(int connection_id, const std::string& data); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 62 | // TODO(byungchul): Consider replacing function name with SendResponseInfo |
[email protected] | c700fd15 | 2013-07-23 21:29:11 | [diff] [blame] | 63 | void SendResponse(int connection_id, const HttpServerResponseInfo& response); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 64 | void Send(int connection_id, |
| 65 | HttpStatusCode status_code, |
| 66 | const std::string& data, |
| 67 | const std::string& mime_type); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 68 | void Send200(int connection_id, |
| 69 | const std::string& data, |
| 70 | const std::string& mime_type); |
| 71 | void Send404(int connection_id); |
| 72 | void Send500(int connection_id, const std::string& message); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 73 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 74 | void Close(int connection_id); |
| 75 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 76 | void SetReceiveBufferSize(int connection_id, int32_t size); |
| 77 | void SetSendBufferSize(int connection_id, int32_t size); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 78 | |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 79 | // Copies the local address to |address|. Returns a network error code. |
| 80 | int GetLocalAddress(IPEndPoint* address); |
| 81 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 82 | private: |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 83 | friend class HttpServerTest; |
| 84 | |
| 85 | typedef std::map<int, HttpConnection*> IdToConnectionMap; |
| 86 | |
| 87 | void DoAcceptLoop(); |
| 88 | void OnAcceptCompleted(int rv); |
| 89 | int HandleAcceptResult(int rv); |
| 90 | |
| 91 | void DoReadLoop(HttpConnection* connection); |
| 92 | void OnReadCompleted(int connection_id, int rv); |
| 93 | int HandleReadResult(HttpConnection* connection, int rv); |
| 94 | |
| 95 | void DoWriteLoop(HttpConnection* connection); |
| 96 | void OnWriteCompleted(int connection_id, int rv); |
| 97 | int HandleWriteResult(HttpConnection* connection, int rv); |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 98 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 99 | // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 100 | // will remove the data parsed from recv_data_, leaving only the unused |
| 101 | // recv data. |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 102 | bool ParseHeaders(const char* data, |
| 103 | size_t data_len, |
[email protected] | 2053a1e | 2011-02-04 16:24:30 | [diff] [blame] | 104 | HttpServerRequestInfo* info, |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 105 | size_t* pos); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 106 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 107 | HttpConnection* FindConnection(int connection_id); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 108 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 109 | // Whether or not Close() has been called during delegate callback processing. |
| 110 | bool HasClosedConnection(HttpConnection* connection); |
| 111 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame^] | 112 | const std::unique_ptr<ServerSocket> server_socket_; |
| 113 | std::unique_ptr<StreamSocket> accepted_socket_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 114 | HttpServer::Delegate* const delegate_; |
| 115 | |
| 116 | int last_id_; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 117 | IdToConnectionMap id_to_connection_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 118 | |
| 119 | base::WeakPtrFactory<HttpServer> weak_ptr_factory_; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 122 | }; |
| 123 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 124 | } // namespace net |
| 125 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 126 | #endif // NET_SERVER_HTTP_SERVER_H_ |