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