proxygen
proxygen::ScopedHTTPServer Class Referencefinal

#include <ScopedHTTPServer.h>

Public Member Functions

int getPort () const
 
std::vector< HTTPServer::IPConfiggetAddresses () const
 
 ~ScopedHTTPServer ()
 

Static Public Member Functions

template<typename HandlerType >
static std::unique_ptr< ScopedHTTPServerstart (HandlerType handler, int port=0, int numThreads=4, std::unique_ptr< wangle::SSLContextConfig > sslCfg=nullptr)
 
static std::unique_ptr< ScopedHTTPServerstart (HTTPServer::IPConfig cfg, HTTPServerOptions options)
 

Private Member Functions

 ScopedHTTPServer (std::thread thread, std::unique_ptr< HTTPServer > server)
 

Private Attributes

std::thread thread_
 
std::unique_ptr< HTTPServerserver_
 

Detailed Description

A basic server that can be used for testing http clients. Since most such servers are short lived, this server takes care of starting and stopping automatically.

Definition at line 88 of file ScopedHTTPServer.h.

Constructor & Destructor Documentation

proxygen::ScopedHTTPServer::~ScopedHTTPServer ( )
inline

Definition at line 124 of file ScopedHTTPServer.h.

124  {
125  server_->stop();
126  thread_.join();
127  }
std::unique_ptr< HTTPServer > server_
proxygen::ScopedHTTPServer::ScopedHTTPServer ( std::thread  thread,
std::unique_ptr< HTTPServer server 
)
inlineprivate

Definition at line 130 of file ScopedHTTPServer.h.

132  : thread_(std::move(thread)),
133  server_(std::move(server)) {
134  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< HTTPServer > server_

Member Function Documentation

std::vector<HTTPServer::IPConfig> proxygen::ScopedHTTPServer::getAddresses ( ) const
inline

Get the addresses for the server.

Definition at line 118 of file ScopedHTTPServer.h.

118  {
119  auto addresses = server_->addresses();
120  CHECK(!addresses.empty());
121  return addresses;
122  }
std::unique_ptr< HTTPServer > server_
int proxygen::ScopedHTTPServer::getPort ( ) const
inline

Get the port the server is listening on. This is helpful if the port was randomly chosen.

Definition at line 111 of file ScopedHTTPServer.h.

111  {
112  return getAddresses()[0].address.getPort();
113  }
std::vector< HTTPServer::IPConfig > getAddresses() const
template<typename HandlerType >
std::unique_ptr< ScopedHTTPServer > proxygen::ScopedHTTPServer::start ( HandlerType  handler,
int  port = 0,
int  numThreads = 4,
std::unique_ptr< wangle::SSLContextConfig sslCfg = nullptr 
)
inlinestatic

Start a server listening on the requested port. If port is 0, it will choose a random port.

Definition at line 141 of file ScopedHTTPServer.h.

References f, handler(), folly::gen::move, and start.

145  {
146 
147  std::unique_ptr<RequestHandlerFactory> f =
148  std::make_unique<ScopedHandlerFactory<HandlerType>>(handler);
149  return start(std::move(f), port, numThreads, std::move(sslCfg));
150 }
auto f
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void handler(int, siginfo_t *, void *)
static std::unique_ptr< ScopedHTTPServer > start(HandlerType handler, int port=0, int numThreads=4, std::unique_ptr< wangle::SSLContextConfig > sslCfg=nullptr)
std::unique_ptr< ScopedHTTPServer > proxygen::ScopedHTTPServer::start ( HTTPServer::IPConfig  cfg,
HTTPServerOptions  options 
)
inlinestatic

Start a server listening with the requested IPConfig and server opts

Definition at line 179 of file ScopedHTTPServer.h.

References folly::gen::move, folly::setThreadName(), and folly::pushmi::detail::t.

181  {
182 
183  std::vector<HTTPServer::IPConfig> IPs = { std::move(cfg) };
184 
185  auto server = std::make_unique<HTTPServer>(std::move(options));
186  server->bind(IPs);
187 
188  // Start the server
189  std::exception_ptr eptr;
190  auto barrier = std::make_shared<boost::barrier>(2);
191 
192  std::thread t = std::thread([&, barrier] () {
193  server->start(
194  [&, barrier] () {
195  folly::setThreadName("http-acceptor");
196  barrier->wait();
197  },
198  [&, barrier] (std::exception_ptr ex) {
199  eptr = ex;
200  barrier->wait();
201  });
202  });
203 
204  // Wait for server to start
205  barrier->wait();
206  if (eptr) {
207  t.join();
208 
210  }
211 
212  return std::unique_ptr<ScopedHTTPServer>(
213  new ScopedHTTPServer(std::move(t), std::move(server)));
214 }
void rethrow_exception(std::exception_ptr ep)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
ScopedHTTPServer(std::thread thread, std::unique_ptr< HTTPServer > server)
bool setThreadName(std::thread::id tid, StringPiece name)
Definition: ThreadName.cpp:109

Member Data Documentation

std::unique_ptr<HTTPServer> proxygen::ScopedHTTPServer::server_
private

Definition at line 137 of file ScopedHTTPServer.h.

std::thread proxygen::ScopedHTTPServer::thread_
private

Definition at line 136 of file ScopedHTTPServer.h.


The documentation for this class was generated from the following file: