forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSockets.h
More file actions
120 lines (100 loc) · 3.22 KB
/
StringSockets.h
File metadata and controls
120 lines (100 loc) · 3.22 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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef _H_STRING_SOCKETS
#define _H_STRING_SOCKETS
#include <string.h>
#include "SocketObject.h"
#include "FcgiMessage.h"
///////////////////////////////////////////////////////////////////////////////
struct HttpError : public SocketError
{
public:
HttpError(const string& e) : SocketError(e)
{}
};
///////////////////////////////////////////////////////////////////////////////
struct FcgiError : public SocketError
{
public:
FcgiError(const string& e) : SocketError(e)
{}
};
///////////////////////////////////////////////////////////////////////////////
class HttpSocket : public BinarySocket
{
friend class FcgiSocket;
private:
string http_header_;
struct packetData
{
vector<uint8_t> httpData;
int content_length = -1;
size_t header_len = 0;
void clear(void)
{
httpData.clear();
content_length = -1;
header_len = 0;
}
void get_content_len(const string& header_str)
{
string err504("HTTP/1.1 504");
if (header_str.compare(0, err504.size(), err504) == 0)
throw HttpError("connection timed out");
string search_tok_caps("Content-Length: ");
auto tokpos = header_str.find(search_tok_caps);
if (tokpos != string::npos)
{
content_length = atoi(header_str.c_str() +
tokpos + search_tok_caps.size());
return;
}
string search_tok("content-length: ");
tokpos = header_str.find(search_tok);
if (tokpos != string::npos)
{
content_length = atoi(header_str.c_str() +
tokpos + search_tok.size());
return;
}
}
};
private:
int32_t makePacket(char** packet, const char* msg);
string getBody(vector<uint8_t>);
public:
HttpSocket(const BinarySocket&);
virtual string writeAndRead(const string&, SOCKET sockfd = SOCK_MAX);
virtual SocketType type(void) const { return SocketHttp; }
};
///////////////////////////////////////////////////////////////////////////////
class FcgiSocket : public HttpSocket
{
private:
void addStringParam(const string& name, const string& val);
struct packetStruct
{
vector<uint8_t> fcgidata;
vector<uint8_t> httpData;
int endpacket = 0;
size_t ptroffset = 0;
uint16_t fcgiid = 0;
void clear(void)
{
fcgidata.clear();
httpData.clear();
endpacket = 0;
ptroffset = 0;
}
};
public:
FcgiSocket(const HttpSocket&);
string writeAndRead(const string&, SOCKET sfd = SOCK_MAX);
SocketType type(void) const { return SocketFcgi; }
};
#endif