forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSockets.cpp
More file actions
331 lines (262 loc) · 9.73 KB
/
StringSockets.cpp
File metadata and controls
331 lines (262 loc) · 9.73 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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "StringSockets.h"
///////////////////////////////////////////////////////////////////////////////
//
// HttpSocket
//
///////////////////////////////////////////////////////////////////////////////
HttpSocket::HttpSocket(const BinarySocket& obj) :
BinarySocket(obj)
{
stringstream ss;
ss << "POST / HTTP/1.1" << "\r\n";
ss << "Host: " << addr_ << "\r\n";
ss << "Content-type: text/html; charset=UTF-8" << "\r\n";
ss << "Content-Length: ";
http_header_ = move(ss.str());
}
///////////////////////////////////////////////////////////////////////////////
int32_t HttpSocket::makePacket(char** packet, const char* msg)
{
if (packet == nullptr)
return -1;
stringstream ss;
ss << strlen(msg);
ss << "\r\n\r\n";
*packet = new char[strlen(msg) +
ss.str().size() +
http_header_.size() +
1];
memcpy(*packet, http_header_.c_str(), http_header_.size());
size_t pos = http_header_.size();
memcpy(*packet + pos, ss.str().c_str(), ss.str().size());
pos += ss.str().size();
memcpy(*packet + pos, msg, strlen(msg));
pos += strlen(msg);
memset(*packet + pos, 0, 1);
return pos;
}
///////////////////////////////////////////////////////////////////////////////
string HttpSocket::getBody(vector<uint8_t> msg)
{
/***
Always expect text data (null terminated)
***/
//look for double crlf http header end, return everything after that
typedef vector<uint8_t>::iterator vcIter;
//let's use a move iterator, enough copies already as it is
string htmlstr(
move_iterator<vcIter>(msg.begin()),
move_iterator<vcIter>(msg.end()));
size_t pos = htmlstr.find("\r\n\r\n");
if (pos == string::npos)
{
//no html break, check for error marker
pos = htmlstr.find("error:");
if (pos != string::npos)
throw runtime_error(htmlstr);
throw runtime_error("unexpected return value");
}
return htmlstr.substr(pos + 4);
}
///////////////////////////////////////////////////////////////////////////////
string HttpSocket::writeAndRead(const string& msg, SOCKET sockfd)
{
char* packet = nullptr;
auto packetSize = makePacket(&packet, msg.c_str());
typedef vector<char>::iterator vecIterType;
packetData packetPtr;
while (1)
{
if (sockfd == SOCK_MAX)
sockfd = openSocket(false);
if (sockfd == SOCK_MAX)
throw SocketError("failed to connect socket");
packetPtr.clear();
try
{
auto processHttpPacket = [&packetPtr]
(const vector<uint8_t>& socketData)->bool
{
auto& httpData = packetPtr.httpData;
if (socketData.size() == 0)
return true;
{
httpData.insert(
httpData.end(), socketData.begin(), socketData.end());
if (packetPtr.content_length == -1)
{
//if content_length is -1, we have not read the content-length in the
//http header yet, let's find that
for (unsigned i = 0; i < httpData.size(); i++)
{
if (httpData[i] == '\r')
{
if (httpData.size() - i < 3)
break;
if (httpData[i + 1] == '\n' &&
httpData[i + 2] == '\r' &&
httpData[i + 3] == '\n')
{
packetPtr.header_len = i + 4;
break;
}
}
}
if (packetPtr.header_len == 0)
throw HttpError("couldn't find http header in response");
string header_str((char*)&httpData[0], packetPtr.header_len);
packetPtr.get_content_len(header_str);
}
if (packetPtr.content_length == -1)
throw HttpError("failed to find http header response packet");
//check the total amount of data read matches the advertised
//data in the http header
}
bool done = false;
if (httpData.size() >= packetPtr.content_length + packetPtr.header_len)
{
httpData.resize(packetPtr.content_length + packetPtr.header_len);
done = true;
}
return done;
};
BinarySocket::writeAndRead(sockfd,
(uint8_t*)packet, packetSize, processHttpPacket);
break;
}
catch (HttpError &e)
{
LOGERR << "HttpSocket::writeAndRead HttpError: " << e.what();
continue;
}
catch (SocketError&)
{
continue;
}
}
closeSocket(sockfd);
auto&& retmsg = getBody(move(packetPtr.httpData));
return retmsg;
}
///////////////////////////////////////////////////////////////////////////////
//
// FcgiSocket
//
///////////////////////////////////////////////////////////////////////////////
FcgiSocket::FcgiSocket(const HttpSocket& obj) :
HttpSocket(obj)
{}
///////////////////////////////////////////////////////////////////////////////
string FcgiSocket::writeAndRead(const string& msg, SOCKET sockfd)
{
auto&& fcgiMsg = FcgiMessage::makePacket(msg.c_str());
auto serdata = fcgiMsg.serialize();
auto serdatalength = fcgiMsg.getSerializedDataLength();
packetStruct packetPtr;
packetPtr.fcgiid = fcgiMsg.id();
while (1)
{
packetPtr.clear();
if (sockfd == SOCK_MAX)
sockfd = openSocket(false);
if (sockfd == SOCK_MAX)
throw SocketError("can't connect socket");
try
{
auto processFcgiPacket =
[&packetPtr](
const vector<uint8_t>& socketData)->bool
{
if (socketData.size() == 0)
return true;
packetPtr.fcgidata.insert(
packetPtr.fcgidata.end(), socketData.begin(), socketData.end());
while (packetPtr.ptroffset + FCGI_HEADER_LEN <=
packetPtr.fcgidata.size())
{
//grab fcgi header
auto* fcgiheader = &packetPtr.fcgidata[packetPtr.ptroffset];
packetPtr.ptroffset += FCGI_HEADER_LEN;
//make sure fcgi version and request id match
if (fcgiheader[0] != FCGI_VERSION_1)
throw FcgiError("unexpected fcgi header version");
uint16_t requestid;
requestid = (uint8_t)fcgiheader[3] + (uint8_t)fcgiheader[2] * 256;
if (requestid != packetPtr.fcgiid)
throw FcgiError("request id mismatch");
//check packet type
bool abortParse = false;
switch (fcgiheader[1])
{
case FCGI_END_REQUEST:
{
packetPtr.endpacket++;
break;
}
case FCGI_STDOUT:
{
//get packetsize and padding
uint16_t packetsize = 0, padding;
packetsize |= (uint8_t)fcgiheader[5];
packetsize |= (uint16_t)(fcgiheader[4] << 8);
padding = (uint8_t)fcgiheader[6];
if (packetsize > 0)
{
//do not process this fcgi packet if we dont have enough
//data in the read buffer to cover the advertized length
if (packetsize + padding + packetPtr.ptroffset >
packetPtr.fcgidata.size())
{
packetPtr.ptroffset -= FCGI_HEADER_LEN;
abortParse = true;
break;
}
//extract http data
packetPtr.httpData.insert(packetPtr.httpData.end(),
packetPtr.fcgidata.begin() + packetPtr.ptroffset,
packetPtr.fcgidata.begin() + packetPtr.ptroffset + packetsize);
//advance index to next header
packetPtr.ptroffset += packetsize + padding;
}
break;
}
case FCGI_ABORT_REQUEST:
throw FcgiError("received FCGI_ABORT_REQUEST packet");
//we do not handle the other request types as a client
default:
throw FcgiError("unknown fcgi header request byte");
}
if (packetPtr.endpacket >= 1 || abortParse)
break;
}
if (packetPtr.endpacket >= 1)
return true;
return false;
};
BinarySocket::writeAndRead(sockfd,
serdata, serdatalength, processFcgiPacket);
//if we got this far we're all good
break;
}
catch (FcgiError &e)
{
LOGERR << "FcgiSocket::writeAndRead FcgiError: " << e.what();
}
catch (future_error& e)
{
cout << e.what();
int abc = 0;
}
closeSocket(sockfd);
}
closeSocket(sockfd);
fcgiMsg.clear();
return HttpSocket::getBody(move(packetPtr.httpData));
}