blob: 54773ad1f557b597764c829e0b0d75324b89bc3c [file] [log] [blame]
[email protected]a9813302012-04-28 09:29:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a33721a2011-02-03 17:23:242// 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]a33721a2011-02-03 17:23:247
8#include <list>
9#include <map>
10
11#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
[email protected]36633f42012-05-16 09:22:3013#include "net/base/stream_listen_socket.h"
[email protected]e67d2172012-12-11 09:44:4114#include "net/http/http_status_code.h"
[email protected]a33721a2011-02-03 17:23:2415
[email protected]b6dbb0a2011-04-18 00:17:4116namespace net {
17
[email protected]86c6a0b52011-08-02 19:49:2518class HttpConnection;
[email protected]a33721a2011-02-03 17:23:2419class HttpServerRequestInfo;
[email protected]7c17b692012-08-09 16:16:3020class IPEndPoint;
[email protected]86c6a0b52011-08-02 19:49:2521class WebSocket;
[email protected]a33721a2011-02-03 17:23:2422
[email protected]36633f42012-05-16 09:22:3023class HttpServer : public StreamListenSocket::Delegate,
[email protected]a33721a2011-02-03 17:23:2424 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]a9813302012-04-28 09:29:2838
[email protected]a33721a2011-02-03 17:23:2439 protected:
40 virtual ~Delegate() {}
41 };
42
[email protected]8bf9ce72012-05-23 17:05:3143 HttpServer(const StreamListenSocketFactory& socket_factory,
44 HttpServer::Delegate* delegate);
[email protected]a33721a2011-02-03 17:23:2445
46 void AcceptWebSocket(int connection_id,
47 const HttpServerRequestInfo& request);
48 void SendOverWebSocket(int connection_id, const std::string& data);
[email protected]e67d2172012-12-11 09:44:4149 void Send(int connection_id,
50 HttpStatusCode status_code,
51 const std::string& data,
52 const std::string& mime_type);
[email protected]a33721a2011-02-03 17:23:2453 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]e67d2172012-12-11 09:44:4158
[email protected]a33721a2011-02-03 17:23:2459 void Close(int connection_id);
60
[email protected]7c17b692012-08-09 16:16:3061 // Copies the local address to |address|. Returns a network error code.
62 int GetLocalAddress(IPEndPoint* address);
63
[email protected]a33721a2011-02-03 17:23:2464 // ListenSocketDelegate
[email protected]36633f42012-05-16 09:22:3065 virtual void DidAccept(StreamListenSocket* server,
66 StreamListenSocket* socket) OVERRIDE;
67 virtual void DidRead(StreamListenSocket* socket,
[email protected]f2cbbc82011-11-16 01:10:2968 const char* data,
69 int len) OVERRIDE;
[email protected]36633f42012-05-16 09:22:3070 virtual void DidClose(StreamListenSocket* socket) OVERRIDE;
[email protected]a33721a2011-02-03 17:23:2471
[email protected]a9813302012-04-28 09:29:2872 protected:
73 virtual ~HttpServer();
74
75 private:
76 friend class base::RefCountedThreadSafe<HttpServer>;
77 friend class HttpConnection;
78
[email protected]a33721a2011-02-03 17:23:2479 // 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]86c6a0b52011-08-02 19:49:2582 bool ParseHeaders(HttpConnection* connection,
[email protected]2053a1e2011-02-04 16:24:3083 HttpServerRequestInfo* info,
[email protected]86c6a0b52011-08-02 19:49:2584 size_t* pos);
[email protected]a33721a2011-02-03 17:23:2485
[email protected]86c6a0b52011-08-02 19:49:2586 HttpConnection* FindConnection(int connection_id);
[email protected]36633f42012-05-16 09:22:3087 HttpConnection* FindConnection(StreamListenSocket* socket);
[email protected]a33721a2011-02-03 17:23:2488
89 HttpServer::Delegate* delegate_;
[email protected]36633f42012-05-16 09:22:3090 scoped_refptr<StreamListenSocket> server_;
[email protected]86c6a0b52011-08-02 19:49:2591 typedef std::map<int, HttpConnection*> IdToConnectionMap;
[email protected]a33721a2011-02-03 17:23:2492 IdToConnectionMap id_to_connection_;
[email protected]36633f42012-05-16 09:22:3093 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap;
[email protected]a33721a2011-02-03 17:23:2494 SocketToConnectionMap socket_to_connection_;
95
96 DISALLOW_COPY_AND_ASSIGN(HttpServer);
97};
98
[email protected]b6dbb0a2011-04-18 00:17:4199} // namespace net
100
[email protected]a33721a2011-02-03 17:23:24101#endif // NET_SERVER_HTTP_SERVER_H_