blob: 09a2f8a8aa83428a1855530cc6601101c3906bb4 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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_
7#pragma once
8
9#include <list>
10#include <map>
11
12#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]a33721a2011-02-03 17:23:2414#include "net/base/listen_socket.h"
15
[email protected]b6dbb0a2011-04-18 00:17:4116namespace net {
17
[email protected]a33721a2011-02-03 17:23:2418class HttpServerRequestInfo;
19
20class 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
54private:
55 friend class base::RefCountedThreadSafe<HttpServer>;
56 class Connection {
57 private:
58 static int lastId_;
59 friend class HttpServer;
60
[email protected]b1404392011-02-03 18:31:1961 explicit Connection(HttpServer* server, ListenSocket* sock);
[email protected]a33721a2011-02-03 17:23:2462 ~Connection();
63
64 void DetachSocket();
65
[email protected]2053a1e2011-02-04 16:24:3066 void Shift(int num_bytes);
67
[email protected]b1404392011-02-03 18:31:1968 HttpServer* server_;
[email protected]a33721a2011-02-03 17:23:2469 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]b1404392011-02-03 18:31:1976 friend class Connection;
[email protected]a33721a2011-02-03 17:23:2477
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]2053a1e2011-02-04 16:24:3087 bool ParseHeaders(Connection* connection,
88 HttpServerRequestInfo* info,
89 int* ppos);
[email protected]a33721a2011-02-03 17:23:2490
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]b6dbb0a2011-04-18 00:17:41104} // namespace net
105
[email protected]a33721a2011-02-03 17:23:24106#endif // NET_SERVER_HTTP_SERVER_H_