linehttptransport.cpp
8.35 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <sstream>
#include <limits>
#include <debug.h>
#include <thrift/transport/TTransportException.h>
#include "thrift_line/TalkService.h"
#include "constants.hpp"
#include "linehttptransport.hpp"
LineHttpTransport::LineHttpTransport(
PurpleAccount *acct,
PurpleConnection *conn,
std::string host,
uint16_t port,
bool ls_mode) :
acct(acct),
conn(conn),
host(host),
port(port),
ls_mode(ls_mode),
auth_token(""),
ssl(NULL),
connection_id(0),
connection_close(false),
status_code_(0),
content_length_(0)
{
}
LineHttpTransport::~LineHttpTransport() {
close();
}
void LineHttpTransport::set_auth_token(std::string token) {
this->auth_token = token;
}
int LineHttpTransport::status_code() {
return status_code_;
}
int LineHttpTransport::content_length() {
return content_length_;
}
void LineHttpTransport::open() {
if (ssl)
return;
in_progress = false;
connection_id++;
ssl = purple_ssl_connect(
acct,
host.c_str(),
port,
WRAPPER(LineHttpTransport::ssl_connect),
WRAPPER_TYPE(LineHttpTransport::ssl_error, end),
(gpointer)this);
}
void LineHttpTransport::close() {
if (!ssl)
return;
purple_ssl_close(ssl);
ssl = NULL;
connection_id++;
x_ls = "";
request_buf.str("");
response_str = "";
response_buf.str("");
}
uint32_t LineHttpTransport::read_virt(uint8_t *buf, uint32_t len) {
return (uint32_t )response_buf.sgetn((char *)buf, len);
}
void LineHttpTransport::write_virt(const uint8_t *buf, uint32_t len) {
request_buf.sputn((const char *)buf, len);
}
void LineHttpTransport::request(std::string method, std::string path, std::function<void()> callback) {
Request req;
req.method = method;
req.path = path;
req.data = request_buf.str();
req.callback = callback;
request_queue.push(req);
request_buf.str("");
send_next();
}
/*const uin8_t* LineHttpTransport::borrow_virt(uint8_t *buf, uint32_t *len) {
}
void LineHttpTransport::consume_virt(uint32_t len) {
}*/
void LineHttpTransport::send_next() {
if (!ssl) {
open();
return;
}
if (in_progress || request_queue.empty())
return;
connection_close = false;
status_code_ = -1;
content_length_ = -1;
Request &next_req = request_queue.front();
std::ostringstream data;
data
<< next_req.method << " " << next_req.path << " HTTP/1.1" "\r\n";
if (next_req.method == "POST")
data << "Content-Length: " << next_req.data.size() << "\r\n";
if (ls_mode) {
if (x_ls != "")
{
data << "X-LS: " << x_ls << "\r\n";
}
else
{
data
<< "Connection: Keep-Alive" "\r\n"
<< "Content-Type: application/x-thrift" "\r\n"
<< "Host: " << host << ":" << port << "\r\n"
<< "Accept: application/x-thrift" "\r\n"
<< "User-Agent: " LINE_USER_AGENT "\r\n"
<< "X-Line-Application: " LINE_APPLICATION "\r\n";
if (auth_token != "")
data << "X-Line-Access: " << auth_token << "\r\n";
}
} else {
data
<< "Connection: Keep-Alive" "\r\n"
<< "Host: " << host << ":" << port << "\r\n"
<< "User-Agent: " LINE_USER_AGENT "\r\n"
<< "X-Line-Application: " LINE_APPLICATION "\r\n";
if (auth_token != "")
data << "X-Line-Access: " << auth_token << "\r\n";
}
data
<< "\r\n"
<< next_req.data;
std::string data_str = data.str();
in_progress = true;
purple_ssl_write(ssl, data_str.c_str(), data_str.size());
}
int LineHttpTransport::reconnect() {
close();
in_progress = false;
open();
// Don't repeat when using as timeout callback
return FALSE;
}
void LineHttpTransport::ssl_connect(PurpleSslConnection *, PurpleInputCondition) {
purple_ssl_input_add(ssl, WRAPPER(LineHttpTransport::ssl_input), (gpointer)this);
send_next();
}
void LineHttpTransport::ssl_input(PurpleSslConnection *, PurpleInputCondition cond) {
if (!ssl || cond != PURPLE_INPUT_READ)
return;
bool any = false;
while (true) {
size_t count = purple_ssl_read(ssl, (void *)buf, BUFFER_SIZE);
if (count == 0) {
if (any)
break;
// Disconnected from server.
close();
// If there was a request in progress, re-open immediately to try again.
if (in_progress) {
purple_debug_info("line", "Reconnecting immediately to re-send.\n");
open();
}
return;
}
if (count == (size_t)-1)
break;
any = true;
response_str.append((const char *)buf, count);
try_parse_response_header();
if (content_length_ >= 0 && response_str.size() >= (size_t)content_length_) {
if (status_code_ == 403) {
// Don't try to reconnect because this usually means the user has logged in from
// elsewhere.
// TODO: Check actual reason
conn->wants_to_die = TRUE;
purple_connection_error(conn, "Session died.");
return;
}
response_buf.str(response_str.substr(0, content_length_));
response_str.erase(0, content_length_);
int connection_id_before = connection_id;
try {
request_queue.front().callback();
} catch (line::TalkException &err) {
std::string msg = "LINE: TalkException: ";
msg += err.reason;
if (err.code == line::ErrorCode::NOT_AUTHORIZED_DEVICE) {
if (err.reason == "AUTHENTICATION_DIVESTED_BY_OTHER_DEVICE") {
msg = "LINE: You have been logged out because "
"you logged in from another device.";
} else if (err.reason == "REVOKE") {
msg = "LINE: This device was logged out via the mobile app.";
}
}
conn->wants_to_die = TRUE;
purple_connection_error(conn, msg.c_str());
return;
} catch (apache::thrift::TApplicationException &err) {
std::string msg = "LINE: Application error: ";
msg += err.what();
conn->wants_to_die = TRUE;
purple_connection_error(conn, msg.c_str());
return;
} catch (apache::thrift::transport::TTransportException &err) {
std::string msg = "LINE: Transport error: ";
msg += err.what();
conn->wants_to_die = TRUE;
purple_connection_error(conn, msg.c_str());
return;
}
request_queue.pop();
in_progress = false;
if (connection_id != connection_id_before)
break; // Callback closed connection, don't try to continue reading
if (connection_close) {
close();
send_next();
break;
}
send_next();
}
}
}
void LineHttpTransport::ssl_error(PurpleSslConnection *, PurpleSslErrorType err) {
purple_debug_warning("line", "SSL error: %s\n", purple_ssl_strerror(err));
ssl = nullptr;
purple_connection_ssl_error(conn, err);
}
void LineHttpTransport::try_parse_response_header() {
size_t header_end = response_str.find("\r\n\r\n");
if (header_end == std::string::npos)
return;
std::istringstream stream(response_str.substr(0, header_end));
stream.ignore(256, ' ');
stream >> status_code_;
stream.ignore(256, '\n');
while (stream) {
std::string name, value;
std::getline(stream, name, ':');
stream.ignore(256, ' ');
if (name == "Content-Length")
stream >> content_length_;
if (name == "X-LS")
std::getline(stream, x_ls, '\r');
if (name == "Connection") {
std::string value;
std::getline(stream, value, '\r');
if (value == "Close" || value == "close")
connection_close = true;
}
stream.ignore(256, '\n');
}
response_str.erase(0, header_end + 4);
}