1 // Copyright (c) 2012 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.
5 #ifndef NET_SERVER_HTTP_SERVER_H_
6 #define NET_SERVER_HTTP_SERVER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/http/http_status_code.h"
14 #include "net/socket/stream_listen_socket.h"
19 class HttpServerRequestInfo;
20 class HttpServerResponseInfo;
24 class HttpServer : public StreamListenSocket::Delegate,
25 public base::RefCountedThreadSafe<HttpServer> {
29 virtual void OnHttpRequest(int connection_id,
30 const HttpServerRequestInfo& info) = 0;
32 virtual void OnWebSocketRequest(int connection_id,
33 const HttpServerRequestInfo& info) = 0;
35 virtual void OnWebSocketMessage(int connection_id,
36 const std::string& data) = 0;
38 virtual void OnClose(int connection_id) = 0;
41 virtual ~Delegate() {}
44 HttpServer(const StreamListenSocketFactory& socket_factory,
45 HttpServer::Delegate* delegate);
47 void AcceptWebSocket(int connection_id,
48 const HttpServerRequestInfo& request);
49 void SendOverWebSocket(int connection_id, const std::string& data);
50 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
51 void Send(int connection_id,
52 HttpStatusCode status_code,
53 const std::string& data,
54 const std::string& mime_type);
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);
61 void Close(int connection_id);
63 // Copies the local address to |address|. Returns a network error code.
64 int GetLocalAddress(IPEndPoint* address);
66 // ListenSocketDelegate
67 virtual void DidAccept(StreamListenSocket* server,
68 scoped_ptr<StreamListenSocket> socket) OVERRIDE;
69 virtual void DidRead(StreamListenSocket* socket,
72 virtual void DidClose(StreamListenSocket* socket) OVERRIDE;
75 virtual ~HttpServer();
78 friend class base::RefCountedThreadSafe<HttpServer>;
79 friend class HttpConnection;
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
84 bool ParseHeaders(HttpConnection* connection,
85 HttpServerRequestInfo* info,
88 HttpConnection* FindConnection(int connection_id);
89 HttpConnection* FindConnection(StreamListenSocket* socket);
91 HttpServer::Delegate* delegate_;
92 scoped_ptr<StreamListenSocket> server_;
93 typedef std::map<int, HttpConnection*> IdToConnectionMap;
94 IdToConnectionMap id_to_connection_;
95 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap;
96 SocketToConnectionMap socket_to_connection_;
98 DISALLOW_COPY_AND_ASSIGN(HttpServer);
103 #endif // NET_SERVER_HTTP_SERVER_H_