2012-12-15 01:05:09 +00:00
|
|
|
#pragma once
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/network/http/connection.hpp>
|
2012-12-15 01:05:09 +00:00
|
|
|
#include <fc/shared_ptr.hpp>
|
|
|
|
|
#include <functional>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <memory>
|
2012-12-15 01:05:09 +00:00
|
|
|
|
|
|
|
|
namespace fc { namespace http {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Listens on a given port for incomming http
|
|
|
|
|
* connections and then calls a user provided callback
|
|
|
|
|
* function for every http request.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-06-05 19:19:00 +00:00
|
|
|
class server
|
|
|
|
|
{
|
2012-12-15 01:05:09 +00:00
|
|
|
public:
|
|
|
|
|
server();
|
|
|
|
|
server( uint16_t port );
|
|
|
|
|
server( server&& s );
|
|
|
|
|
~server();
|
|
|
|
|
|
|
|
|
|
server& operator=(server&& s);
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
class response
|
|
|
|
|
{
|
2012-12-15 01:05:09 +00:00
|
|
|
public:
|
|
|
|
|
class impl;
|
|
|
|
|
|
|
|
|
|
response();
|
|
|
|
|
response( const fc::shared_ptr<impl>& my);
|
|
|
|
|
response( const response& r);
|
|
|
|
|
response( response&& r );
|
|
|
|
|
~response();
|
|
|
|
|
response& operator=(const response& );
|
|
|
|
|
response& operator=( response&& );
|
|
|
|
|
|
|
|
|
|
void add_header( const fc::string& key, const fc::string& val )const;
|
|
|
|
|
void set_status( const http::reply::status_code& s )const;
|
|
|
|
|
void set_length( uint64_t s )const;
|
|
|
|
|
|
|
|
|
|
void write( const char* data, uint64_t len )const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
fc::shared_ptr<impl> my;
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-27 05:55:52 +00:00
|
|
|
void listen( const fc::ip::endpoint& p );
|
2014-06-03 23:00:22 +00:00
|
|
|
fc::ip::endpoint get_local_endpoint() const;
|
2012-12-15 01:05:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the callback to be called for every http request made.
|
|
|
|
|
*/
|
|
|
|
|
void on_request( const std::function<void(const http::request&, const server::response& s )>& cb );
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
class impl;
|
2013-06-05 19:19:00 +00:00
|
|
|
std::unique_ptr<impl> my;
|
2012-12-15 01:05:09 +00:00
|
|
|
};
|
2013-06-05 19:19:00 +00:00
|
|
|
typedef std::shared_ptr<server> server_ptr;
|
2012-12-15 01:05:09 +00:00
|
|
|
|
|
|
|
|
} }
|