2013-06-05 19:19:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <fc/vector.hpp>
|
|
|
|
|
#include <fc/string.hpp>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
namespace fc {
|
|
|
|
|
namespace ip { class endpoint; }
|
|
|
|
|
class tcp_socket;
|
|
|
|
|
|
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
|
|
struct header
|
|
|
|
|
{
|
|
|
|
|
header( fc::string k, fc::string v )
|
|
|
|
|
:key(fc::move(k)),val(fc::move(v)){}
|
|
|
|
|
header(){}
|
|
|
|
|
fc::string key;
|
|
|
|
|
fc::string val;
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-27 18:19:08 +00:00
|
|
|
typedef std::vector<header> headers;
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
struct reply
|
|
|
|
|
{
|
|
|
|
|
enum status_code {
|
|
|
|
|
OK = 200,
|
|
|
|
|
RecordCreated = 201,
|
|
|
|
|
NotFound = 404,
|
|
|
|
|
Found = 302,
|
|
|
|
|
InternalServerError = 500
|
|
|
|
|
};
|
|
|
|
|
reply( status_code c = OK):status(c){}
|
|
|
|
|
int status;
|
2013-06-27 18:19:08 +00:00
|
|
|
std::vector<header> headers;
|
|
|
|
|
std::vector<char> body;
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct request
|
|
|
|
|
{
|
|
|
|
|
fc::string get_header( const fc::string& key )const;
|
|
|
|
|
fc::string method;
|
|
|
|
|
fc::string domain;
|
|
|
|
|
fc::string path;
|
2013-06-27 18:19:08 +00:00
|
|
|
std::vector<header> headers;
|
|
|
|
|
std::vector<char> body;
|
2013-06-05 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
2013-06-27 18:19:08 +00:00
|
|
|
std::vector<header> parse_urlencoded_params( const fc::string& f );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connections have reference semantics, all copies refer to the same
|
|
|
|
|
* underlying socket.
|
|
|
|
|
*/
|
|
|
|
|
class connection
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
connection();
|
|
|
|
|
~connection();
|
|
|
|
|
// used for clients
|
|
|
|
|
void connect_to( const fc::ip::endpoint& ep );
|
2013-12-09 05:48:28 +00:00
|
|
|
http::reply request( const fc::string& method, const fc::string& url, const fc::string& body = std::string(), const headers& = headers());
|
2013-06-05 19:19:00 +00:00
|
|
|
|
|
|
|
|
// used for servers
|
|
|
|
|
fc::tcp_socket& get_socket()const;
|
|
|
|
|
|
|
|
|
|
http::request read_request()const;
|
|
|
|
|
|
|
|
|
|
class impl;
|
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<impl> my;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef std::shared_ptr<connection> connection_ptr;
|
|
|
|
|
|
|
|
|
|
} } // fc::http
|
|
|
|
|
|