-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp2.cpp
More file actions
170 lines (157 loc) · 3.26 KB
/
http2.cpp
File metadata and controls
170 lines (157 loc) · 3.26 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "http2.h"
http2::http2()
{
this->num = 0;
this->close_call = std::bind(&http2::on_close, this, std::placeholders::_1);
this->error_call = std::bind(&http2::on_error, this, std::placeholders::_1);
this->connect_call = std::bind(&http2::on_connect, this, std::placeholders::_1);
}
void http2::on_connect(boost::asio::ip::tcp::resolver::iterator it)
{
for(auto m : this->ready)
{
this->exec(m);
}
}
void http2::on_close(uint32_t error_code)
{
if (--this->num == 0)
{
this->sess->shutdown();
}
}
int http2::add2ready(data_fild * data)
{
this->ready.push_back(data);
return 0;
}
void http2::on_error(const boost::system::error_code &ec)
{
this->error = ec.message();
this->sess->shutdown();
}
http2::~http2()
{
for(auto m : this->ready)
{
delete (m);
}
this->ready.clear();
if (this->sess)
delete(this->sess);
this->sess = NULL;
}
int http2::exec(data_fild *data)
{
if (!data)
{
return -1;
}
if (!this->sess)
{
return -3;
}
string event_id = data->id;
header_map h = {};
for(auto m : data->headers)
{
header_value a;
a.sensitive = false;
a.value = m.second;
h.insert(std::make_pair(m.first, a));
}
auto req = this->sess->submit(this->ec, data->method, data->url, data->body , h);
if (req == NULL)
{
return -2;
}
auto ptr = this;
req->on_response([ptr, event_id](const response& res){
ptr->response_code[event_id] = res.status_code();
res.on_data([ptr, event_id](const uint8_t *data, std::size_t len) {
if (len > 0)
{
string str;
str.append((const char*)data, len );
if (ptr->result.find(event_id) != ptr->result.end())
{
ptr->result[event_id].append(str);
}else{
ptr->result[event_id] = str;
}
}
});
});
req->on_close(this->close_call);
this->num++;
return 0;
}
int http2::wait_result()
{
if (!this->sess)
{
return -1;
}
if (this->ready.size() == 0)
{
return -2;
}
this->io_service.run();
return 0;
}
int http2::setHost(const char* h)
{
this->host = h;
return 0;
}
int http2::setPort(const char* p)
{
this->port = p;
return 0;
}
int http2::setPem(const char* pem)
{
this->pem_path = pem;
return 0;
}
string http2::get_password()
{
return this->password;
}
int http2::set_password(const char * str)
{
this->password = str;
return 0;
}
int http2::init()
{
boost::asio::ssl::context tls(boost::asio::ssl::context::sslv23);
tls.set_default_verify_paths();
if (this->pem_path.size() > 0)
{
tls.set_verify_mode(boost::asio::ssl::verify_peer);
if (this->password.size() > 0)
{
tls.set_password_callback(bind(&http2::get_password, this));
tls.use_private_key_file(this->pem_path, boost::asio::ssl::context::pem);
}
tls.use_certificate_file(this->pem_path, boost::asio::ssl::context::pem);
}
configure_tls_context(this->ec, tls);
if (this->host.size() == 0)
{
return -1;
}
if (this->port == "")
{
this->port = "443";
}
this->sess = new session(this->io_service, tls, this->host, this->port);
if (!this->sess)
{
return -2;
}
this->sess->on_connect(this->connect_call);
this->sess->on_error(this->error_call);
return 0;
}