blob: 51bec95688926397444704793a7f96d24b4b1039 [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]8b62f8e2013-09-03 19:03:0612#include "base/memory/scoped_ptr.h"
[email protected]e67d2172012-12-11 09:44:4113#include "net/http/http_status_code.h"
[email protected]1946d9e2013-04-09 01:43:3714#include "net/socket/stream_listen_socket.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]c700fd152013-07-23 21:29:1120class HttpServerResponseInfo;
[email protected]7c17b692012-08-09 16:16:3021class IPEndPoint;
[email protected]86c6a0b52011-08-02 19:49:2522class WebSocket;
[email protected]a33721a2011-02-03 17:23:2423
[email protected]36633f42012-05-16 09:22:3024class HttpServer : public StreamListenSocket::Delegate,
[email protected]a33721a2011-02-03 17:23:2425 public base::RefCountedThreadSafe<HttpServer> {
26 public:
27 class Delegate {
28 public:
29 virtual void OnHttpRequest(int connection_id,
30 const HttpServerRequestInfo& info) = 0;
31
32 virtual void OnWebSocketRequest(int connection_id,
33 const HttpServerRequestInfo& info) = 0;
34
35 virtual void OnWebSocketMessage(int connection_id,
36 const std::string& data) = 0;
37
38 virtual void OnClose(int connection_id) = 0;
[email protected]a9813302012-04-28 09:29:2839
[email protected]a33721a2011-02-03 17:23:2440 protected:
41 virtual ~Delegate() {}
42 };
43
[email protected]8bf9ce72012-05-23 17:05:3144 HttpServer(const StreamListenSocketFactory& socket_factory,
45 HttpServer::Delegate* delegate);
[email protected]a33721a2011-02-03 17:23:2446
47 void AcceptWebSocket(int connection_id,
48 const HttpServerRequestInfo& request);
49 void SendOverWebSocket(int connection_id, const std::string& data);
[email protected]c700fd152013-07-23 21:29:1150 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
[email protected]e67d2172012-12-11 09:44:4151 void Send(int connection_id,
52 HttpStatusCode status_code,
53 const std::string& data,
54 const std::string& mime_type);
[email protected]a33721a2011-02-03 17:23:2455 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);
[email protected]e67d2172012-12-11 09:44:4160
[email protected]a33721a2011-02-03 17:23:2461 void Close(int connection_id);
62
[email protected]7c17b692012-08-09 16:16:3063 // Copies the local address to |address|. Returns a network error code.
64 int GetLocalAddress(IPEndPoint* address);
65
[email protected]a33721a2011-02-03 17:23:2466 // ListenSocketDelegate
[email protected]36633f42012-05-16 09:22:3067 virtual void DidAccept(StreamListenSocket* server,
[email protected]8b62f8e2013-09-03 19:03:0668 scoped_ptr<StreamListenSocket> socket) OVERRIDE;
[email protected]36633f42012-05-16 09:22:3069 virtual void DidRead(StreamListenSocket* socket,
[email protected]f2cbbc82011-11-16 01:10:2970 const char* data,
71 int len) OVERRIDE;
[email protected]36633f42012-05-16 09:22:3072 virtual void DidClose(StreamListenSocket* socket) OVERRIDE;
[email protected]a33721a2011-02-03 17:23:2473
[email protected]a9813302012-04-28 09:29:2874 protected:
75 virtual ~HttpServer();
76
77 private:
78 friend class base::RefCountedThreadSafe<HttpServer>;
79 friend class HttpConnection;
80
[email protected]a33721a2011-02-03 17:23:2481 // 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
83 // recv data.
[email protected]86c6a0b52011-08-02 19:49:2584 bool ParseHeaders(HttpConnection* connection,
[email protected]2053a1e2011-02-04 16:24:3085 HttpServerRequestInfo* info,
[email protected]86c6a0b52011-08-02 19:49:2586 size_t* pos);
[email protected]a33721a2011-02-03 17:23:2487
[email protected]86c6a0b52011-08-02 19:49:2588 HttpConnection* FindConnection(int connection_id);
[email protected]36633f42012-05-16 09:22:3089 HttpConnection* FindConnection(StreamListenSocket* socket);
[email protected]a33721a2011-02-03 17:23:2490
91 HttpServer::Delegate* delegate_;
[email protected]8b62f8e2013-09-03 19:03:0692 scoped_ptr<StreamListenSocket> server_;
[email protected]86c6a0b52011-08-02 19:49:2593 typedef std::map<int, HttpConnection*> IdToConnectionMap;
[email protected]a33721a2011-02-03 17:23:2494 IdToConnectionMap id_to_connection_;
[email protected]36633f42012-05-16 09:22:3095 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap;
[email protected]a33721a2011-02-03 17:23:2496 SocketToConnectionMap socket_to_connection_;
97
98 DISALLOW_COPY_AND_ASSIGN(HttpServer);
99};
100
[email protected]b6dbb0a2011-04-18 00:17:41101} // namespace net
102
[email protected]a33721a2011-02-03 17:23:24103#endif // NET_SERVER_HTTP_SERVER_H_