forked from deltadev/bbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_session.h
68 lines (46 loc) · 1.47 KB
/
http_session.h
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
#ifndef _HTTP_SESSION_H_
#define _HTTP_SESSION_H_
#include <iostream>
#include <map>
#include <string>
#define ASIO_STANDALONE
#include <asio.hpp>
namespace dpj {
namespace http {
typedef std::map<std::string, std::string> header_map;
class session
{
session(session const&) = delete;
session& operator=(session const&) = delete;
public:
// Constructs socket from host and port strings.
//
session(std::string host, std::string port, bool debug = false);
// Synchronous request.
//
// Returns the status code after successfully reading the status line of the response.
//
// Otherwise throws...
//
// Errors: throws runtime_errors and asio errors.
//
unsigned request(std::string type, std::string resource, header_map const& = header_map{});
header_map read_headers();
std::vector<char> read_body(unsigned content_length);
void connect();
bool debug;
private:
std::string host, port;
void resolve_endpoint();
// Called by request. Will recursively call request if the
// connection was closed or on other errors.
std::pair<asio::error_code, unsigned> read_status_line();
asio::io_service io_service;
asio::streambuf sbuf;
asio::ip::tcp::socket socket;
asio::ip::tcp::resolver::iterator endpoint_iterator;
};
void print_headers(header_map const& header_map, std::ostream &os = std::cout);
}
}
#endif /* _HTTP_SESSION_H_ */