1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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" 13 #include "base/memory/ref_counted.h" 14 #include "net/base/listen_socket.h" 15 16 namespace net { 17 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: ~Delegate()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 61 explicit Connection(HttpServer* server, ListenSocket* sock); 62 ~Connection(); 63 64 void DetachSocket(); 65 66 void Shift(int num_bytes); 67 68 HttpServer* server_; 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 }; 76 friend class Connection; 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. 87 bool ParseHeaders(Connection* connection, 88 HttpServerRequestInfo* info, 89 int* ppos); 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 104 } // namespace net 105 106 #endif // NET_SERVER_HTTP_SERVER_H_ 107