Repository navigation
Expand file tree
/
Copy pathHTTPServer.h
More file actions
142 lines (119 loc) · 3.48 KB
/
Copy pathHTTPServer.h
File metadata and controls
142 lines (119 loc) · 3.48 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include "proxygen/httpserver/HTTPServerOptions.h"
#include "proxygen/lib/ssl/SSLContextConfig.h"
#include <folly/io/async/EventBase.h>
#include <thread>
#include <thrift/lib/cpp/async/TAsyncServerSocket.h>
namespace proxygen {
class SignalHandler;
class HTTPServerAcceptor;
/**
* HTTPServer based on proxygen http libraries
*/
class HTTPServer final {
public:
/**
* For each IP you can specify HTTP protocol to use. You can use plain old
* HTTP/1.1 protocol or SPDY/3.1 for now.
*/
enum class Protocol: uint8_t {
HTTP,
SPDY,
};
struct IPConfig {
IPConfig(folly::SocketAddress a,
Protocol p)
: address(a),
protocol(p) {}
folly::SocketAddress address;
Protocol protocol;
std::vector<SSLContextConfig> sslConfigs;
};
/**
* Create a new HTTPServer
*/
explicit HTTPServer(HTTPServerOptions options);
~HTTPServer();
/**
* Bind server to the following addresses. Can be called from any thread.
*
* Throws exception on error (say port is already busy). You can try binding
* to different set of ports. Though once it succeeds, it is a FATAL to call
* it again.
*
* The list is updated in-place to contain final port server bound to if
* ephemeral port was given. If the call fails, the list might be partially
* updated.
*/
void bind(std::vector<IPConfig>& addrs);
/**
* Start HTTPServer.
*
* Note this is a blocking call and the current thread will be used to listen
* for incoming connections. Throws exception if something goes wrong (say
* somebody else is already listening on that socket).
*
* `onSuccess` callback will be invoked from the event loop which shows that
* all the setup was successfully done.
*
* `onError` callback will be invoked if some errors occurs while starting the
* server instead of throwing exception.
*/
void start(std::function<void()> onSuccess = nullptr,
std::function<void(std::exception_ptr)> onError = nullptr);
/**
* Stop HTTPServer.
*
* Can be called from any thread. Server will stop listening for new
* connections and will wait for running requests to finish.
*
* TODO: Separate method to do hard shutdown?
*/
void stop();
/**
* Get the list of addresses server is listening on. Empty if sockets are not
* bound yet.
*/
std::vector<IPConfig> addresses() const {
return addresses_;
}
private:
HTTPServerOptions options_;
/**
* Event base in which we binded server sockets.
*/
folly::EventBase* mainEventBase_{nullptr};
/**
* Server socket
*/
std::vector<apache::thrift::async::TAsyncServerSocket::UniquePtr>
serverSockets_;
/**
* Struct to hold info about handle threads
*/
struct HandlerThread {
std::thread thread;
folly::EventBase* eventBase{nullptr};
std::vector<std::unique_ptr<HTTPServerAcceptor>> acceptors;
uint32_t acceptorsRunning{0};
};
std::vector<HandlerThread> handlerThreads_;
/**
* Optional signal handlers on which we should shutdown server
*/
std::unique_ptr<SignalHandler> signalHandler_;
/**
* Addresses we are listening on
*/
std::vector<IPConfig> addresses_;
};
}