blob: cb70b575731c7180944347f3e2e7a431240c53ec [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"
Johannes Henkelda8b9d32019-03-15 16:15:3317#include "base/strings/string_piece.h"
[email protected]e67d2172012-12-11 09:44:4118#include "net/http/http_status_code.h"
Ramin Halavati90aa08ba2018-02-07 06:16:1619#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]a33721a2011-02-03 17:23:2420
[email protected]b6dbb0a2011-04-18 00:17:4121namespace net {
22
[email protected]86c6a0b52011-08-02 19:49:2523class HttpConnection;
[email protected]a33721a2011-02-03 17:23:2424class HttpServerRequestInfo;
[email protected]c700fd152013-07-23 21:29:1125class HttpServerResponseInfo;
[email protected]7c17b692012-08-09 16:16:3026class IPEndPoint;
byungchul38c3ae72014-08-25 23:27:4627class ServerSocket;
28class StreamSocket;
[email protected]a33721a2011-02-03 17:23:2429
byungchul38c3ae72014-08-25 23:27:4630class HttpServer {
[email protected]a33721a2011-02-03 17:23:2431 public:
byungchul38c3ae72014-08-25 23:27:4632 // Delegate to handle http/websocket events. Beware that it is not safe to
33 // destroy the HttpServer in any of these callbacks.
[email protected]a33721a2011-02-03 17:23:2434 class Delegate {
35 public:
yzshen9f0ff742015-07-13 17:41:0836 virtual ~Delegate() {}
37
samuong0b4d9b952014-09-26 04:15:2938 virtual void OnConnect(int connection_id) = 0;
[email protected]a33721a2011-02-03 17:23:2439 virtual void OnHttpRequest(int connection_id,
40 const HttpServerRequestInfo& info) = 0;
[email protected]a33721a2011-02-03 17:23:2441 virtual void OnWebSocketRequest(int connection_id,
42 const HttpServerRequestInfo& info) = 0;
Johannes Henkel43b68c312019-03-15 22:05:3343 virtual void OnWebSocketMessage(int connection_id, 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,
Ramin Halavati90aa08ba2018-02-07 06:16:1656 const HttpServerRequestInfo& request,
57 NetworkTrafficAnnotationTag traffic_annotation);
58 void SendOverWebSocket(int connection_id,
Johannes Henkelda8b9d32019-03-15 16:15:3359 base::StringPiece data,
Ramin Halavati90aa08ba2018-02-07 06:16:1660 NetworkTrafficAnnotationTag traffic_annotation);
[email protected]a6948f92014-04-19 02:12:4061 // Sends the provided data directly to the given connection. No validation is
62 // performed that data constitutes a valid HTTP response. A valid HTTP
63 // response may be split across multiple calls to SendRaw.
Ramin Halavati90aa08ba2018-02-07 06:16:1664 void SendRaw(int connection_id,
65 const std::string& data,
66 NetworkTrafficAnnotationTag traffic_annotation);
byungchul38c3ae72014-08-25 23:27:4667 // TODO(byungchul): Consider replacing function name with SendResponseInfo
Ramin Halavati90aa08ba2018-02-07 06:16:1668 void SendResponse(int connection_id,
69 const HttpServerResponseInfo& response,
70 NetworkTrafficAnnotationTag traffic_annotation);
[email protected]e67d2172012-12-11 09:44:4171 void Send(int connection_id,
72 HttpStatusCode status_code,
73 const std::string& data,
Ramin Halavati90aa08ba2018-02-07 06:16:1674 const std::string& mime_type,
75 NetworkTrafficAnnotationTag traffic_annotation);
[email protected]a33721a2011-02-03 17:23:2476 void Send200(int connection_id,
77 const std::string& data,
Ramin Halavati90aa08ba2018-02-07 06:16:1678 const std::string& mime_type,
79 NetworkTrafficAnnotationTag traffic_annotation);
80 void Send404(int connection_id,
81 NetworkTrafficAnnotationTag traffic_annotation);
82 void Send500(int connection_id,
83 const std::string& message,
84 NetworkTrafficAnnotationTag traffic_annotation);
[email protected]e67d2172012-12-11 09:44:4185
[email protected]a33721a2011-02-03 17:23:2486 void Close(int connection_id);
87
Avi Drissman13fc8932015-12-20 04:40:4688 void SetReceiveBufferSize(int connection_id, int32_t size);
89 void SetSendBufferSize(int connection_id, int32_t size);
byungchul38c3ae72014-08-25 23:27:4690
[email protected]7c17b692012-08-09 16:16:3091 // Copies the local address to |address|. Returns a network error code.
92 int GetLocalAddress(IPEndPoint* address);
93
[email protected]a9813302012-04-28 09:29:2894 private:
byungchul38c3ae72014-08-25 23:27:4695 friend class HttpServerTest;
96
byungchul38c3ae72014-08-25 23:27:4697 void DoAcceptLoop();
98 void OnAcceptCompleted(int rv);
99 int HandleAcceptResult(int rv);
100
101 void DoReadLoop(HttpConnection* connection);
102 void OnReadCompleted(int connection_id, int rv);
103 int HandleReadResult(HttpConnection* connection, int rv);
104
Ramin Halavati90aa08ba2018-02-07 06:16:16105 void DoWriteLoop(HttpConnection* connection,
106 NetworkTrafficAnnotationTag traffic_annotation);
107 void OnWriteCompleted(int connection_id,
108 NetworkTrafficAnnotationTag traffic_annotation,
109 int rv);
byungchul38c3ae72014-08-25 23:27:46110 int HandleWriteResult(HttpConnection* connection, int rv);
[email protected]a9813302012-04-28 09:29:28111
[email protected]a33721a2011-02-03 17:23:24112 // Expects the raw data to be stored in recv_data_. If parsing is successful,
113 // will remove the data parsed from recv_data_, leaving only the unused
slanf955ff02016-09-16 23:36:09114 // recv data. If all data has been consumed successfully, but the headers are
115 // not fully parsed, *pos will be set to zero. Returns false if an error is
116 // encountered while parsing, true otherwise.
byungchul38c3ae72014-08-25 23:27:46117 bool ParseHeaders(const char* data,
118 size_t data_len,
[email protected]2053a1e2011-02-04 16:24:30119 HttpServerRequestInfo* info,
[email protected]86c6a0b52011-08-02 19:49:25120 size_t* pos);
[email protected]a33721a2011-02-03 17:23:24121
[email protected]86c6a0b52011-08-02 19:49:25122 HttpConnection* FindConnection(int connection_id);
[email protected]a33721a2011-02-03 17:23:24123
byungchul38c3ae72014-08-25 23:27:46124 // Whether or not Close() has been called during delegate callback processing.
125 bool HasClosedConnection(HttpConnection* connection);
126
danakja9850e12016-04-18 22:28:08127 const std::unique_ptr<ServerSocket> server_socket_;
128 std::unique_ptr<StreamSocket> accepted_socket_;
byungchul38c3ae72014-08-25 23:27:46129 HttpServer::Delegate* const delegate_;
130
131 int last_id_;
avic39cece2016-09-16 23:10:15132 std::map<int, std::unique_ptr<HttpConnection>> id_to_connection_;
byungchul38c3ae72014-08-25 23:27:46133
Jeremy Romand54000b22019-07-08 18:40:16134 base::WeakPtrFactory<HttpServer> weak_ptr_factory_{this};
[email protected]a33721a2011-02-03 17:23:24135
136 DISALLOW_COPY_AND_ASSIGN(HttpServer);
137};
138
[email protected]b6dbb0a2011-04-18 00:17:41139} // namespace net
140
[email protected]a33721a2011-02-03 17:23:24141#endif // NET_SERVER_HTTP_SERVER_H_