[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 | |
| 8 | #include <list> |
| 9 | #include <map> |
| 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | 8b62f8e | 2013-09-03 19:03:06 | [diff] [blame^] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 13 | #include "net/http/http_status_code.h" |
[email protected] | 1946d9e | 2013-04-09 01:43:37 | [diff] [blame] | 14 | #include "net/socket/stream_listen_socket.h" |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 15 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 16 | namespace net { |
| 17 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 18 | class HttpConnection; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 19 | class HttpServerRequestInfo; |
[email protected] | c700fd15 | 2013-07-23 21:29:11 | [diff] [blame] | 20 | class HttpServerResponseInfo; |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 21 | class IPEndPoint; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 22 | class WebSocket; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 23 | |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 24 | class HttpServer : public StreamListenSocket::Delegate, |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 25 | public base::RefCountedThreadSafe<HttpServer> { |
| 26 | public: |
| 27 | class Delegate { |
| 28 | public: |
| 29 | virtual void OnHttpRequest(int connection_id, |
| 30 | const HttpServerRequestInfo& info) = 0; |
| 31 | |
| 32 | virtual void OnWebSocketRequest(int connection_id, |
| 33 | const HttpServerRequestInfo& info) = 0; |
| 34 | |
| 35 | virtual void OnWebSocketMessage(int connection_id, |
| 36 | const std::string& data) = 0; |
| 37 | |
| 38 | virtual void OnClose(int connection_id) = 0; |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 39 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 40 | protected: |
| 41 | virtual ~Delegate() {} |
| 42 | }; |
| 43 | |
[email protected] | 8bf9ce7 | 2012-05-23 17:05:31 | [diff] [blame] | 44 | HttpServer(const StreamListenSocketFactory& socket_factory, |
| 45 | HttpServer::Delegate* delegate); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 46 | |
| 47 | void AcceptWebSocket(int connection_id, |
| 48 | const HttpServerRequestInfo& request); |
| 49 | void SendOverWebSocket(int connection_id, const std::string& data); |
[email protected] | c700fd15 | 2013-07-23 21:29:11 | [diff] [blame] | 50 | void SendResponse(int connection_id, const HttpServerResponseInfo& response); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 51 | void Send(int connection_id, |
| 52 | HttpStatusCode status_code, |
| 53 | const std::string& data, |
| 54 | const std::string& mime_type); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 55 | void Send200(int connection_id, |
| 56 | const std::string& data, |
| 57 | const std::string& mime_type); |
| 58 | void Send404(int connection_id); |
| 59 | void Send500(int connection_id, const std::string& message); |
[email protected] | e67d217 | 2012-12-11 09:44:41 | [diff] [blame] | 60 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 61 | void Close(int connection_id); |
| 62 | |
[email protected] | 7c17b69 | 2012-08-09 16:16:30 | [diff] [blame] | 63 | // Copies the local address to |address|. Returns a network error code. |
| 64 | int GetLocalAddress(IPEndPoint* address); |
| 65 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 66 | // ListenSocketDelegate |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 67 | virtual void DidAccept(StreamListenSocket* server, |
[email protected] | 8b62f8e | 2013-09-03 19:03:06 | [diff] [blame^] | 68 | scoped_ptr<StreamListenSocket> socket) OVERRIDE; |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 69 | virtual void DidRead(StreamListenSocket* socket, |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 70 | const char* data, |
| 71 | int len) OVERRIDE; |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 72 | virtual void DidClose(StreamListenSocket* socket) OVERRIDE; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 73 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 74 | protected: |
| 75 | virtual ~HttpServer(); |
| 76 | |
| 77 | private: |
| 78 | friend class base::RefCountedThreadSafe<HttpServer>; |
| 79 | friend class HttpConnection; |
| 80 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 81 | // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 82 | // will remove the data parsed from recv_data_, leaving only the unused |
| 83 | // recv data. |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 84 | bool ParseHeaders(HttpConnection* connection, |
[email protected] | 2053a1e | 2011-02-04 16:24:30 | [diff] [blame] | 85 | HttpServerRequestInfo* info, |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 86 | size_t* pos); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 87 | |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 88 | HttpConnection* FindConnection(int connection_id); |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 89 | HttpConnection* FindConnection(StreamListenSocket* socket); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 90 | |
| 91 | HttpServer::Delegate* delegate_; |
[email protected] | 8b62f8e | 2013-09-03 19:03:06 | [diff] [blame^] | 92 | scoped_ptr<StreamListenSocket> server_; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 93 | typedef std::map<int, HttpConnection*> IdToConnectionMap; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 94 | IdToConnectionMap id_to_connection_; |
[email protected] | 36633f4 | 2012-05-16 09:22:30 | [diff] [blame] | 95 | typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 96 | SocketToConnectionMap socket_to_connection_; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 99 | }; |
| 100 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame] | 101 | } // namespace net |
| 102 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 103 | #endif // NET_SERVER_HTTP_SERVER_H_ |