This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeb.cpp
254 lines (224 loc) · 8.17 KB
/
Web.cpp
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
//
// Created by Frank Steiler on 4/17/17 as part of NOOBS4IoT (https://github.com/steilerDev/NOOBS4IoT)
//
// Web.cpp:
// This file contains several classes and helper functions providing basic socket communication based on standard
// C sockets. Besides the getIP() function (which uses QtNetwork) no external non-standard library is required.
// For more information see https://github.com/steilerDev/NOOBS4IoT/wiki.
//
// This file is licensed under a GNU General Public License v3.0 (c) Frank Steiler.
// See https://raw.githubusercontent.com/steilerDev/NOOBS4IoT/master/LICENSE for more information.
//
#include "Web.h"
#include "../easylogging++.h"
using namespace std;
bool Web::Web::send() {
LINFO << "Sending http message";
char header_buffer[HEADER_BUFFER];
// build http message
sprintf(header_buffer, "%s\r\n", headerLine.c_str());
// append headers
for(auto const& x: header) {
sprintf(&header_buffer[strlen(header_buffer)], "%s: %s\r\n", x.first.c_str(), x.second.c_str());
}
// append extra crlf to indicate start of body
strcat(header_buffer, "\r\n");
LDEBUG << "HTTP message:\n" << header_buffer << body.c_str();
LINFO << "Sending HTTP message";
ssize_t t = 0;
t += write(_socket, header_buffer, strlen(header_buffer));
t += write(_socket, body.c_str(), strlen(body.c_str()));
if(t <= 0) {
LERROR << "Unable to send HTTP message!";
return false;
} else {
LDEBUG << "Send HTTP message of size " << t;
return true;
}
}
bool Web::Web::receive() {
_bytesRead = 0;
LINFO << "Receiving HTTP message";
string bufferString;
if(!readReceive(&bufferString)) {
LERROR << "Unable to read receive from socket";
return false;
}
if(!parseReceive(bufferString, false)) {
LERROR << "Unable to parse receive";
return false;
}
bool continueRead = false;
int messageSize = -1;
if(header.find("Expect") != header.end() && header["Expect"].compare("100-continue\r") == 0) {
LDEBUG << "Found expect header, building continue response";
Web expectResponse(_socket);
expectResponse.headerLine = "HTTP/1.0 100 Continue";
LDEBUG << "Sending continue response";
expectResponse.send();
continueRead = true;
}
if(header.find("Content-Length") != header.end() && std::stoi(header["Content-Length"]) > _bytesRead) {
messageSize = std::stoi(header["Content-Length"]);
// Removing last \n
if(!this->body.empty()) {
this->body.pop_back();
}
LDEBUG << "Complete message not yet read (" << _bytesRead << " vs. " << messageSize << "), continuing read";
continueRead = true;
}
if(continueRead) {
LDEBUG << "Continuing reading";
bufferString.clear();
do {
if (!readReceive(&bufferString)) {
LERROR << "Unable to continue to read from socket";
return false;
}
} while(messageSize != -1 && _bytesRead < messageSize);
if(!parseReceive(bufferString, true)) {
LERROR << "Unable to parse continue request";
return false;
}
}
return true;
}
bool Web::Web::readReceive(string *bufferString) {
LDEBUG << "Reading HTTP message from socket";
static char buffer[BUFFER_SIZE];
fd_set set;
int rv;
// Clear the set
FD_ZERO(&set);
// Add our socket to the set
FD_SET(_socket, &set);
ssize_t thisRoundRead = 0;
do {
// Redefine timeout struct, since it is consumed by select
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
thisRoundRead = 0;
// Clear the buffer every time
memset(buffer, 0, BUFFER_SIZE);
// Wait for incoming IO or timeout (If content == buffer size we would re-run the loop and read would wait for
// additional information, but none would be available, blocking the execution forever.
rv = select(_socket + 1, &set, NULL, NULL, &timeout);
if(rv == -1) {
LERROR << "An error occurred within select";
thisRoundRead = -1;
} else if (rv == 0) {
LWARNING << "Read timeout occurred, this should be okay and due to the fact, that the buffer size == request size";
thisRoundRead = 0;
} else {
thisRoundRead += read(_socket, buffer, BUFFER_SIZE);
LDEBUG << "Read " << thisRoundRead << " bytes this round";
bufferString->append(buffer);
}
_bytesRead += thisRoundRead;
} while (thisRoundRead > 0);
LDEBUG << "Finished read";
if(thisRoundRead < 0) {
LERROR << "Error during read!";
return false;
}
LDEBUG << "Read: \n" << *bufferString;
return true;
}
bool Web::Web::parseReceive(const string &bufferString, const bool skipHeader) {
LDEBUG << "Parsing received HTTP message line by line";
// Indicates if we are still in the header
bool header = !skipHeader;
// Indicates if we are at the request line
bool methodLine = true;
istringstream bufferStream(bufferString);
for(string line; getline(bufferStream, line); ) {
if(header) {
if(methodLine) { //Processing method line
headerLine = line;
methodLine = false;
} else { // Processing headers
if (!(line.size() == 1 && line[0] == '\r')) { // Processing a header field
if(!parseHeaderLine(line)) {
LERROR << "Unable to parse header line: " << line;
return false;
}
} else { // Found start of body
LDEBUG << "Found start of body";
header = false;
}
}
} else { // Processing body line
if(!parseBodyLine(line)) {
LERROR << "Unable to parse body line: " << line;
return false;
}
}
}
if(header) {
LERROR << "Didn't finish read of header, this will be a problem!";
return false;
}
LDEBUG << "Finished parsing request";
return true;
}
bool Web::Web::parseHeaderLine(const string &headerLineString) {
vector<string> headerLineVector = split(headerLineString, ':');
if(headerLineVector.size() >= 2) {
string key = headerLineVector[0];
string value = headerLineVector[1];
if (headerLineVector.size() > 2) {
LDEBUG << "Additional splits occurred, re-merging";
for (uint i = 2; i < headerLineVector.size(); i++) {
value.append(":" + headerLineVector[i]);
}
}
// Removing unnecessary space at the start
if (value[0] == ' ') {
value = value.substr(1, value.length());
}
header[key] = value;
LDEBUG << "Found header field: Key = " << key << ", value = " << value;
} else {
LWARNING << "Split resulted in single result, unable to process: " << headerLineString;
// Not returning false, only ignoring this specific line
}
return true;
}
bool Web::Web::parseBodyLine(const string &bodyLineString) {
//LDEBUG << "Found body line: " << bodyLineString;
this->body.append(bodyLineString);
this->body.append("\n");
return true;
}
vector<string> Web::split(const string &text, const char sep) {
vector<string> tokens;
size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != string::npos) {
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
#ifdef QT_CORE_LIB
QString Web::getIP() {
QString ip;
QList<QHostAddress> ipAddresses = QNetworkInterface::allAddresses();
foreach(QHostAddress address, ipAddresses) {
if(ip.isEmpty()) {
ip.append(address.toString());
} else {
if(!ip.startsWith("[")) {
ip.prepend("[");
}
ip.append("|");
ip.append(address.toString());
}
}
if(ip.startsWith("[")) {
ip.append("]");
}
return ip;
}
#endif