summaryrefslogtreecommitdiffstats
path: root/net/server/http_server.h
blob: 78f6e42d8edaaee577143abd4e6c5946ed2b377a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SERVER_HTTP_SERVER_H_
#define NET_SERVER_HTTP_SERVER_H_
#pragma once

#include <list>
#include <map>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "net/base/listen_socket.h"

namespace net {

class HttpServerRequestInfo;

class HttpServer : public ListenSocket::ListenSocketDelegate,
                   public base::RefCountedThreadSafe<HttpServer> {
 public:
  class Delegate {
   public:
    virtual void OnHttpRequest(int connection_id,
                               const HttpServerRequestInfo& info) = 0;

    virtual void OnWebSocketRequest(int connection_id,
                                    const HttpServerRequestInfo& info) = 0;

    virtual void OnWebSocketMessage(int connection_id,
                                    const std::string& data) = 0;

    virtual void OnClose(int connection_id) = 0;
   protected:
    virtual ~Delegate() {}
  };

  HttpServer(const std::string& host, int port, HttpServer::Delegate* del);
  virtual ~HttpServer();

  void AcceptWebSocket(int connection_id,
                       const HttpServerRequestInfo& request);
  void SendOverWebSocket(int connection_id, const std::string& data);
  void Send(int connection_id, const std::string& data);
  void Send(int connection_id, const char* bytes, int len);
  void Send200(int connection_id,
               const std::string& data,
               const std::string& mime_type);
  void Send404(int connection_id);
  void Send500(int connection_id, const std::string& message);
  void Close(int connection_id);

private:
  friend class base::RefCountedThreadSafe<HttpServer>;
  class Connection {
   private:
    static int lastId_;
    friend class HttpServer;

    Connection(HttpServer* server, ListenSocket* sock);
    ~Connection();

    void DetachSocket();

    void Shift(int num_bytes);

    HttpServer* server_;
    scoped_refptr<ListenSocket> socket_;
    bool is_web_socket_;
    std::string recv_data_;
    int id_;

    DISALLOW_COPY_AND_ASSIGN(Connection);
  };
  friend class Connection;


  // ListenSocketDelegate
  virtual void DidAccept(ListenSocket* server, ListenSocket* socket);
  virtual void DidRead(ListenSocket* socket, const char* data, int len);
  virtual void DidClose(ListenSocket* socket);

  // Expects the raw data to be stored in recv_data_. If parsing is successful,
  // will remove the data parsed from recv_data_, leaving only the unused
  // recv data.
  bool ParseHeaders(Connection* connection,
                    HttpServerRequestInfo* info,
                    int* ppos);

  Connection* FindConnection(int connection_id);
  Connection* FindConnection(ListenSocket* socket);

  HttpServer::Delegate* delegate_;
  scoped_refptr<ListenSocket> server_;
  typedef std::map<int, Connection*> IdToConnectionMap;
  IdToConnectionMap id_to_connection_;
  typedef std::map<ListenSocket*, Connection*> SocketToConnectionMap;
  SocketToConnectionMap socket_to_connection_;

  DISALLOW_COPY_AND_ASSIGN(HttpServer);
};

}  // namespace net

#endif // NET_SERVER_HTTP_SERVER_H_