BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
connection.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <memory>
3 #include <string>
4 #include <vector>
5 
6 namespace fc {
7  namespace ip { class endpoint; }
8  class tcp_socket;
9 
10  namespace http {
11 
12  struct header
13  {
14  header( std::string k, std::string v )
15  :key(std::move(k)),val(std::move(v)){}
16  header(){}
17  std::string key;
18  std::string val;
19  };
20 
21  typedef std::vector<header> headers;
22 
23  struct reply
24  {
25  enum status_code {
26  OK = 200,
27  RecordCreated = 201,
28  NoContent = 204,
29  BadRequest = 400,
30  NotAuthorized = 401,
31  NotFound = 404,
32  Found = 302,
33  InternalServerError = 500
34  };
35  reply( status_code c = OK):status(c){}
36  int status;
37  std::vector<header> headers;
38  std::vector<char> body;
39  std::string body_as_string;
40  };
41 
42  struct request
43  {
44  std::string get_header( const std::string& key )const;
45  std::string remote_endpoint;
46  std::string method;
47  std::string domain;
48  std::string path;
49  std::vector<header> headers;
50  std::vector<char> body;
51  };
52 
53  std::vector<header> parse_urlencoded_params( const std::string& f );
54 
59  class connection
60  {
61  public:
62  connection();
63  ~connection();
64  // used for clients
65  void connect_to( const fc::ip::endpoint& ep );
66  http::reply request( const std::string& method, const std::string& url, const std::string& body = std::string(), const headers& = headers());
67 
68  // used for servers
69  fc::tcp_socket& get_socket()const;
70 
71  http::request read_request()const;
72 
73  class impl;
74  private:
75  std::unique_ptr<impl> my;
76  };
77 
78  typedef std::shared_ptr<connection> connection_ptr;
79 
80 } } // fc::http
81 
82 #include <fc/reflect/reflect.hpp>
83 FC_REFLECT( fc::http::header, (key)(val) )
84 FC_REFLECT( fc::http::reply, (status)(headers)(body)(body_as_string) )
std::vector< header > parse_urlencoded_params(const std::string &f)
std::string path
Definition: connection.hpp:48
std::vector< header > headers
Definition: connection.hpp:49
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
Defines types and macros used to provide reflection.
std::shared_ptr< connection > connection_ptr
Definition: connection.hpp:78
std::vector< char > body
Definition: connection.hpp:50
std::string method
Definition: connection.hpp:46
std::vector< char > body
Definition: connection.hpp:38
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:240
std::string remote_endpoint
Definition: connection.hpp:45
Definition: api.hpp:15
std::vector< header > headers
Definition: connection.hpp:21
Definition: url.hpp:22
std::string val
Definition: connection.hpp:18
std::string body_as_string
Definition: connection.hpp:39
header(std::string k, std::string v)
Definition: connection.hpp:14
reply(status_code c=OK)
Definition: connection.hpp:35
std::string key
Definition: connection.hpp:17
std::vector< header > headers
Definition: connection.hpp:37
std::string domain
Definition: connection.hpp:47