blob: e01e9745f95224ca49be1f2de97ea6415bac7f2b [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
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9#include <stdint.h>
10
[email protected]a33721a2011-02-03 17:23:2411#include <map>
danakja9850e12016-04-18 22:28:0812#include <memory>
byungchul38c3ae72014-08-25 23:27:4613#include <string>
[email protected]a33721a2011-02-03 17:23:2414
byungchul38c3ae72014-08-25 23:27:4615#include "base/macros.h"
byungchul38c3ae72014-08-25 23:27:4616#include "base/memory/weak_ptr.h"
[email protected]e67d2172012-12-11 09:44:4117#include "net/http/http_status_code.h"
[email protected]a33721a2011-02-03 17:23:2418
[email protected]b6dbb0a2011-04-18 00:17:4119namespace net {
20
[email protected]86c6a0b52011-08-02 19:49:2521class HttpConnection;
[email protected]a33721a2011-02-03 17:23:2422class HttpServerRequestInfo;
[email protected]c700fd152013-07-23 21:29:1123class HttpServerResponseInfo;
[email protected]7c17b692012-08-09 16:16:3024class IPEndPoint;
byungchul38c3ae72014-08-25 23:27:4625class ServerSocket;
26class StreamSocket;
[email protected]86c6a0b52011-08-02 19:49:2527class WebSocket;
[email protected]a33721a2011-02-03 17:23:2428
byungchul38c3ae72014-08-25 23:27:4629class HttpServer {
[email protected]a33721a2011-02-03 17:23:2430 public:
byungchul38c3ae72014-08-25 23:27:4631 // Delegate to handle http/websocket events. Beware that it is not safe to
32 // destroy the HttpServer in any of these callbacks.
[email protected]a33721a2011-02-03 17:23:2433 class Delegate {
34 public:
yzshen9f0ff742015-07-13 17:41:0835 virtual ~Delegate() {}
36
samuong0b4d9b952014-09-26 04:15:2937 virtual void OnConnect(int connection_id) = 0;
[email protected]a33721a2011-02-03 17:23:2438 virtual void OnHttpRequest(int connection_id,
39 const HttpServerRequestInfo& info) = 0;
[email protected]a33721a2011-02-03 17:23:2440 virtual void OnWebSocketRequest(int connection_id,
41 const HttpServerRequestInfo& info) = 0;
[email protected]a33721a2011-02-03 17:23:2442 virtual void OnWebSocketMessage(int connection_id,
43 const std::string& data) = 0;
[email protected]a33721a2011-02-03 17:23:2444 virtual void OnClose(int connection_id) = 0;
[email protected]a33721a2011-02-03 17:23:2445 };
46
byungchulbaa75462014-10-31 19:38:0947 // Instantiates a http server with |server_socket| which already started
48 // listening, but not accepting. This constructor schedules accepting
49 // connections asynchronously in case when |delegate| is not ready to get
50 // callbacks yet.
danakja9850e12016-04-18 22:28:0851 HttpServer(std::unique_ptr<ServerSocket> server_socket,
[email protected]8bf9ce72012-05-23 17:05:3152 HttpServer::Delegate* delegate);
byungchul38c3ae72014-08-25 23:27:4653 ~HttpServer();
[email protected]a33721a2011-02-03 17:23:2454
55 void AcceptWebSocket(int connection_id,
56 const HttpServerRequestInfo& request);
57 void SendOverWebSocket(int connection_id, const std::string& data);
[email protected]a6948f92014-04-19 02:12:4058 // Sends the provided data directly to the given connection. No validation is
59 // performed that data constitutes a valid HTTP response. A valid HTTP
60 // response may be split across multiple calls to SendRaw.
61 void SendRaw(int connection_id, const std::string& data);
byungchul38c3ae72014-08-25 23:27:4662 // TODO(byungchul): Consider replacing function name with SendResponseInfo
[email protected]c700fd152013-07-23 21:29:1163 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
[email protected]e67d2172012-12-11 09:44:4164 void Send(int connection_id,
65 HttpStatusCode status_code,
66 const std::string& data,
67 const std::string& mime_type);
[email protected]a33721a2011-02-03 17:23:2468 void Send200(int connection_id,
69 const std::string& data,
70 const std::string& mime_type);
71 void Send404(int connection_id);
72 void Send500(int connection_id, const std::string& message);
[email protected]e67d2172012-12-11 09:44:4173
[email protected]a33721a2011-02-03 17:23:2474 void Close(int connection_id);
75
Avi Drissman13fc8932015-12-20 04:40:4676 void SetReceiveBufferSize(int connection_id, int32_t size);
77 void SetSendBufferSize(int connection_id, int32_t size);
byungchul38c3ae72014-08-25 23:27:4678
[email protected]7c17b692012-08-09 16:16:3079 // Copies the local address to |address|. Returns a network error code.
80 int GetLocalAddress(IPEndPoint* address);
81
[email protected]a9813302012-04-28 09:29:2882 private:
byungchul38c3ae72014-08-25 23:27:4683 friend class HttpServerTest;
84
byungchul38c3ae72014-08-25 23:27:4685 void DoAcceptLoop();
86 void OnAcceptCompleted(int rv);
87 int HandleAcceptResult(int rv);
88
89 void DoReadLoop(HttpConnection* connection);
90 void OnReadCompleted(int connection_id, int rv);
91 int HandleReadResult(HttpConnection* connection, int rv);
92
93 void DoWriteLoop(HttpConnection* connection);
94 void OnWriteCompleted(int connection_id, int rv);
95 int HandleWriteResult(HttpConnection* connection, int rv);
[email protected]a9813302012-04-28 09:29:2896
[email protected]a33721a2011-02-03 17:23:2497 // Expects the raw data to be stored in recv_data_. If parsing is successful,
98 // will remove the data parsed from recv_data_, leaving only the unused
99 // recv data.
byungchul38c3ae72014-08-25 23:27:46100 bool ParseHeaders(const char* data,
101 size_t data_len,
[email protected]2053a1e2011-02-04 16:24:30102 HttpServerRequestInfo* info,
[email protected]86c6a0b52011-08-02 19:49:25103 size_t* pos);
[email protected]a33721a2011-02-03 17:23:24104
[email protected]86c6a0b52011-08-02 19:49:25105 HttpConnection* FindConnection(int connection_id);
[email protected]a33721a2011-02-03 17:23:24106
byungchul38c3ae72014-08-25 23:27:46107 // Whether or not Close() has been called during delegate callback processing.
108 bool HasClosedConnection(HttpConnection* connection);
109
danakja9850e12016-04-18 22:28:08110 const std::unique_ptr<ServerSocket> server_socket_;
111 std::unique_ptr<StreamSocket> accepted_socket_;
byungchul38c3ae72014-08-25 23:27:46112 HttpServer::Delegate* const delegate_;
113
114 int last_id_;
avic39cece2016-09-16 23:10:15115 std::map<int, std::unique_ptr<HttpConnection>> id_to_connection_;
byungchul38c3ae72014-08-25 23:27:46116
117 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
[email protected]a33721a2011-02-03 17:23:24118
119 DISALLOW_COPY_AND_ASSIGN(HttpServer);
120};
121
[email protected]b6dbb0a2011-04-18 00:17:41122} // namespace net
123
[email protected]a33721a2011-02-03 17:23:24124#endif // NET_SERVER_HTTP_SERVER_H_