[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <list> |
| 10 | #include <map> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 14 | #include "net/base/listen_socket.h" |
| 15 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame^] | 16 | namespace net { |
| 17 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 18 | class HttpServerRequestInfo; |
| 19 | |
| 20 | class HttpServer : public ListenSocket::ListenSocketDelegate, |
| 21 | public base::RefCountedThreadSafe<HttpServer> { |
| 22 | public: |
| 23 | class Delegate { |
| 24 | public: |
| 25 | virtual void OnHttpRequest(int connection_id, |
| 26 | const HttpServerRequestInfo& info) = 0; |
| 27 | |
| 28 | virtual void OnWebSocketRequest(int connection_id, |
| 29 | const HttpServerRequestInfo& info) = 0; |
| 30 | |
| 31 | virtual void OnWebSocketMessage(int connection_id, |
| 32 | const std::string& data) = 0; |
| 33 | |
| 34 | virtual void OnClose(int connection_id) = 0; |
| 35 | protected: |
| 36 | virtual ~Delegate() {} |
| 37 | }; |
| 38 | |
| 39 | HttpServer(const std::string& host, int port, HttpServer::Delegate* del); |
| 40 | virtual ~HttpServer(); |
| 41 | |
| 42 | void AcceptWebSocket(int connection_id, |
| 43 | const HttpServerRequestInfo& request); |
| 44 | void SendOverWebSocket(int connection_id, const std::string& data); |
| 45 | void Send(int connection_id, const std::string& data); |
| 46 | void Send(int connection_id, const char* bytes, int len); |
| 47 | void Send200(int connection_id, |
| 48 | const std::string& data, |
| 49 | const std::string& mime_type); |
| 50 | void Send404(int connection_id); |
| 51 | void Send500(int connection_id, const std::string& message); |
| 52 | void Close(int connection_id); |
| 53 | |
| 54 | private: |
| 55 | friend class base::RefCountedThreadSafe<HttpServer>; |
| 56 | class Connection { |
| 57 | private: |
| 58 | static int lastId_; |
| 59 | friend class HttpServer; |
| 60 | |
[email protected] | b140439 | 2011-02-03 18:31:19 | [diff] [blame] | 61 | explicit Connection(HttpServer* server, ListenSocket* sock); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 62 | ~Connection(); |
| 63 | |
| 64 | void DetachSocket(); |
| 65 | |
[email protected] | 2053a1e | 2011-02-04 16:24:30 | [diff] [blame] | 66 | void Shift(int num_bytes); |
| 67 | |
[email protected] | b140439 | 2011-02-03 18:31:19 | [diff] [blame] | 68 | HttpServer* server_; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 69 | scoped_refptr<ListenSocket> socket_; |
| 70 | bool is_web_socket_; |
| 71 | std::string recv_data_; |
| 72 | int id_; |
| 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 75 | }; |
[email protected] | b140439 | 2011-02-03 18:31:19 | [diff] [blame] | 76 | friend class Connection; |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 77 | |
| 78 | |
| 79 | // ListenSocketDelegate |
| 80 | virtual void DidAccept(ListenSocket* server, ListenSocket* socket); |
| 81 | virtual void DidRead(ListenSocket* socket, const char* data, int len); |
| 82 | virtual void DidClose(ListenSocket* socket); |
| 83 | |
| 84 | // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 85 | // will remove the data parsed from recv_data_, leaving only the unused |
| 86 | // recv data. |
[email protected] | 2053a1e | 2011-02-04 16:24:30 | [diff] [blame] | 87 | bool ParseHeaders(Connection* connection, |
| 88 | HttpServerRequestInfo* info, |
| 89 | int* ppos); |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 90 | |
| 91 | Connection* FindConnection(int connection_id); |
| 92 | Connection* FindConnection(ListenSocket* socket); |
| 93 | |
| 94 | HttpServer::Delegate* delegate_; |
| 95 | scoped_refptr<ListenSocket> server_; |
| 96 | typedef std::map<int, Connection*> IdToConnectionMap; |
| 97 | IdToConnectionMap id_to_connection_; |
| 98 | typedef std::map<ListenSocket*, Connection*> SocketToConnectionMap; |
| 99 | SocketToConnectionMap socket_to_connection_; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 102 | }; |
| 103 | |
[email protected] | b6dbb0a | 2011-04-18 00:17:41 | [diff] [blame^] | 104 | } // namespace net |
| 105 | |
[email protected] | a33721a | 2011-02-03 17:23:24 | [diff] [blame] | 106 | #endif // NET_SERVER_HTTP_SERVER_H_ |