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