CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
NetworkProtocols.cpp
Go to the documentation of this file.
1
7#include "NetworkProtocols.h"
8#include "PsyTime.h"
9#include "UnitTestFramework.h"
10
11namespace cmlabs{
12
14// Data Types
16
17//HTTPRequest::HTTPRequest(uint64 startRecTime) {
18// if (startRecTime)
19// time = startRecTime;
20// else
21// time = GetTimeNow();
22// endReceiveTime = 0;
23// this->source = 0;
24// type = 0;
25// headerLength = 0;
26// contentLength = 0;
27// ifModifiedSince = 0;
28// keepAlive = true;
29// data = NULL;
30//}
31
32HTTPRequest* HTTPRequest::CreateWebsocketRequest(const char* uri, const char* host, const char* protocolName, const char* origin) {
33 HTTPRequest* req = new HTTPRequest();
34 if (req->createWebsocketRequest(uri, host, protocolName, origin))
35 return req;
36 else {
37 delete req;
38 return NULL;
39 }
40}
41
42HTTPRequest::HTTPRequest(uint64 source, uint64 startRecTime) {
43 if (startRecTime)
44 time = startRecTime;
45 else
46 time = GetTimeNow();
48 this->source = source;
49 type = 0;
50 headerLength = 0;
51 contentLength = 0;
53 keepAlive = true;
54 data = NULL;
55}
56
58 time = req->time;
60 source = req->source;
61 type = req->type;
65 keepAlive = req->keepAlive;
66 data = new char[headerLength+contentLength+1];
67 memcpy(data, req->data, headerLength+contentLength+1);
68 entries = req->entries;
69 params = req->params;
70}
71
72
74 if (data != NULL)
75 delete [] data;
76 data = NULL;
77 std::map<std::string, HTTPPostEntry*>::iterator i = postEntries.begin(), e = postEntries.end();
78 while (i != e) {
79 delete(i->second);
80 i++;
81 }
82 postEntries.clear();
83}
84
85// The request line starts with the method (case-insensitive), compared within the received bytes only: the receive
86// buffer is not NUL-terminated when a request fills it, and stristr() read past it (ASan, 16 Nov 2026)
87static bool MethodIs(const char* buffer, uint32 size, const char* m) {
88 uint32 l = (uint32)strlen(m);
89 if (size < l) return false;
90 for (uint32 i = 0; i < l; i++) if ((buffer[i] | 32) != (m[i] | 32)) return false;
91 return true;
92}
93
94// Reading it in from a socket
95bool HTTPRequest::processHeader(const char* buffer, uint32 size, bool &isInvalid) {
96 isInvalid = false;
97 if (!buffer || (size < 5))
98 return false;
99
100 if (MethodIs(buffer, size, "GET "))
101 type = HTTP_GET;
102 else if (MethodIs(buffer, size, "PUT "))
103 type = HTTP_PUT;
104 else if (MethodIs(buffer, size, "POST "))
105 type = HTTP_POST;
106 else if (MethodIs(buffer, size, "HEAD "))
107 type = HTTP_HEAD;
108 else if (MethodIs(buffer, size, "DELETE "))
110 else if (MethodIs(buffer, size, "OPTIONS "))
112 else {
113 isInvalid = true;
114 return false;
115 }
116
117 // Find end of header
118 uint32 n;
119 for (n = 3; n < size; n++) {
120 if (buffer[n] == 10) {
121 if ( (size-n >= 2) && (buffer[n+1] == 10) ) {
122 // Just using LF, no CR
123 headerLength = n+2;
124 break;
125 }
126 else if ( (size-n >= 3) && (buffer[n+1] == 13) && (buffer[n+2] == 10) && (buffer[n-1] == 13) ) {
127 // Using CRLF
128 headerLength = n+3;
129 break;
130 }
131 }
132 }
133
134 if (headerLength == 0)
135 return false;
136
137 uint32 tempContentLength = 1024;
138 data = new char[headerLength+tempContentLength+1];
139 memcpy(data, buffer, headerLength);
140 data[headerLength] = 0; // temp sting end
141
142 // Now process header entries
143 std::string key, val, uri;
144 const char* s = data, *t;
145 n = 0;
146 uint32 crSize = 0;
147 uint32 left = headerLength;
148 size_t i, j;
149 while (n < headerLength) {
150 if (!utils::GetNextLineEnd(s, left, n, crSize))
151 break;
152 // Process line
153 if ((t = strchr(s, ' ')) && (t - s < (int32)n)) {
154 t -= 1;
155 if (*t != ':') {
156 t++;
157 key.assign(s, t - s);
158 }
159 else {
160 key.assign(s, t - s);
161 t++;
162 }
163 while (*t == 32) t++;
164 if (*t > 32)
165 val.assign(t, n + s - t);
166 else
167 val = "";
168 }
169 else {
170 key.assign(s, n);
171 val = "";
172 }
173 // Record entry
174 if (s == data) {
175 entries["http_fullrequest"] = val;
176 if ((i = val.find(' ')) != std::string::npos) {
177 uri = val.substr(0, i);
178 entries["http_uri"] = uri;
179 //printf("*** URI: %s\n", uri.c_str());
180 entries["http_protocol"] = val.substr(i+1);
181 if ((j = uri.find('?')) != std::string::npos) {
182 entries["http_request"] = uri.substr(1, j-1); // without the first /
183 parseURIParameters(uri.substr(j+1).c_str());
184 }
185 else
186 entries["http_request"] = uri.substr(1, i-1); // without the first /
187 }
188 else {
189 uri = val;
190 entries["http_uri"] = uri;
191 entries["http_protocol"] = "";
192 if ((j = uri.find('?')) != std::string::npos) {
193 entries["http_request"] = uri.substr(1, j-1); // without the first /
194 parseURIParameters(uri.substr(j+1).c_str());
195 }
196 else
197 entries["http_request"] = uri.substr(1); // without the first /
198 }
199 }
200 else if (key.length() > 0) {
201 if (entries.find(key) != entries.end())
202 entries[key] = entries[key] + ";" + val;
203 else
204 entries[key] = val;
205 // Check entry
206 if (stricmp(key.c_str(), "content-length") == 0)
207 contentLength = atoi(val.c_str());
208 else if (stricmp(key.c_str(), "if-modified-since") == 0)
209 ifModifiedSince = GetTimeFromString(val.c_str());
210 else if (stricmp(key.c_str(), "connection") == 0)
211 keepAlive = (stricmp(val.c_str(), "close") != 0);
212 else if (stricmp(key.c_str(), "content-type") == 0) {
213 std::multimap<std::string,std::string> conTypes = utils::TextMultiMapSplit(val.c_str(), ";", "=");
214 std::multimap<std::string,std::string>::iterator ci = conTypes.find("boundary");
215 if (ci != conTypes.end())
216 postBoundary = utils::StringFormat("--%s", ci->second.c_str());
217 }
218 }
219 // Next line
220 s += n + crSize;
221 left -= n + crSize;
222 }
223
224 if (contentLength > tempContentLength) {
225 delete [] data;
226 data = new char[headerLength+contentLength+1];
227 memcpy(data, buffer, headerLength);
228 data[headerLength] = 0; // temp string end
230 }
231
232 if (!contentLength)
234
235 return true;
236}
237
238bool HTTPRequest::processContent(const char* buffer, uint32 size) {
239 if (!buffer || (size != contentLength))
240 return false;
241 memcpy(data+headerLength, buffer, size);
245 return true;
246}
247
248const char* HTTPRequest::getContent(uint32& size) {
249 size = contentLength;
250 return data+headerLength;
251}
252
253const char* HTTPRequest::getRawContent(uint32& size) {
255 return data;
256}
257
258const char* HTTPRequest::getHeaderEntry(const char* entry) {
259 if (!entry)
260 return NULL;
261 std::map<std::string, std::string>::iterator it = entries.find(entry);
262 if (it != entries.end())
263 return it->second.c_str();
264 // HTTP header names are case-insensitive (RFC 9110 5.1): a Go reverse proxy (Tailscale serve, Caddy) forwards
265 // "Sec-Websocket-Key", which an exact lookup of "Sec-WebSocket-Key" missed, so the WebSocket upgrade never
266 // got its reply and the client timed out in the handshake.
267 for (it = entries.begin(); it != entries.end(); ++it)
268 if (stricmp(it->first.c_str(), entry) == 0)
269 return it->second.c_str();
270 return NULL;
271}
272
274 return getHeaderEntry("http_request");
275}
276
277const char* HTTPRequest::getURI() {
278 return getHeaderEntry("http_uri");
279}
280
282 return getHeaderEntry("http_protocol");
283}
284
286 const char* auth = getHeaderEntry("Authorization");
287 if (!auth || !utils::TextStartsWith(auth, "Basic ", false))
288 return NULL;
289 return auth + 6; // after 'Basic '
290}
291
293 const char* auth = getBasicAuthorization();
294 if (!auth) return "";
295 std::string authString = base64_decode(auth);
296 if (!authString.length() || (authString.find(":") == std::string::npos))
297 return "";
298 return authString;
299}
300
302 std::string authString = decodeBasicAuthorization();
303 if (!authString.length())
304 return "";
305 return utils::TextListSplit(authString.c_str(), ":").at(0);
306}
307
309 std::string authString = decodeBasicAuthorization();
310 if (!authString.length())
311 return "";
312 return utils::TextListSplit(authString.c_str(), ":").at(1);
313}
314
315bool HTTPRequest::setBasicAuthorization(const char* authB64) {
316 if (!authB64 || !strlen(authB64)) {
317 if (entries.find("Authorization") != entries.end())
318 entries.erase("Authorization");
319 }
320 else {
321 entries["Authorization"] = authB64;
322 }
323 return true;
324}
325
326bool HTTPRequest::setBasicAuthorization(const char* user, const char* password) {
327 if (!user || !strlen(user) || !password || !strlen(password))
328 return false;
329 std::string rawAuth = utils::StringFormat("%s:%s", user, password);
330 std::string auth = base64_encode(rawAuth);
331 entries["Authorization"] = auth;
332 return true;
333}
334
335bool HTTPRequest::parseContentParameters(const char* content, uint32 size) {
336// "------WebKitFormBoundarynP2yLpjkwmT3R8j5\r\nContent-Disposition: form-data; name="Username"\r\n\r\nundefined\r\n"
337// "------WebKitFormBoundarynP2yLpjkwmT3R8j5\r\nContent-Disposition: form-data; name="Password"\r\n\r\nundefined\r\n"
338// "------WebKitFormBoundarynP2yLpjkwmT3R8j5\r\nContent-Disposition: form-data; name="Authentication"\r\n\r\nundefined\r\n"
339// "------WebKitFormBoundarynP2yLpjkwmT3R8j5--\r\n"
340 if (!content)
341 return false;
342
343 // check main content-type
344 std::string contentType = entries["Content-Type"];
345
346 if (contentType.find("application/x-www-form-urlencoded") != std::string::npos) {
347 // post just contains one content chunk
348 parseContentChunk(content, size);
349 }
350 else if (contentType.find("multipart/form-data") != std::string::npos) {
351 // Check if using boundaries
352 if (!postBoundary.length() || (size < (2 * postBoundary.length())) )
353 return false;
354 //printf("[%u] %s\n", size, content);
355 uint32 boundarySize = (uint32)postBoundary.length();
356
357 // Check content
358 const char* boundary = postBoundary.c_str();
359 if (memcmp(content, boundary, boundarySize) != 0)
360 return false;
361
362 const char* lastStart = content + boundarySize + 2;
363 const char* c = lastStart;
364
365 for (uint32 n=boundarySize; n<size; n++) {
366 //if ((*c == '-') && (memcmp(c, boundary, boundarySize) == 0)) {
367 if (memcmp(c, boundary, boundarySize) == 0) {
368 parseContentChunk(lastStart, (uint32)(c - lastStart));
369 lastStart = c += boundarySize + 2;
370 n += boundarySize + 2;
371 }
372 else
373 c++;
374 }
375 }
376 else {
377 // Post content is something else
378 HTTPPostEntry* postEntry = new HTTPPostEntry();
379 postEntry->name = "POST";
380 postEntry->type = contentType;
381 postEntry->setContent(content, size);
382 postEntries[postEntry->name] = postEntry;
383 }
384
385
386 return true;
387}
388
389bool HTTPRequest::parseContentChunk(const char* chunk, uint32 size) {
390// "Content-Disposition: form-data; name="Username"\r\n\r\nundefined\r\n"
391
392 const char* valContent = strstr(chunk, "\r\n\r\n");
393 if (!valContent)
394 return false;
395 uint32 headerSize = (uint32)(valContent-chunk);
396 valContent += 4;
397 uint32 valContentSize = size - headerSize - 6;
398
399 HTTPPostEntry* postEntry = new HTTPPostEntry();
400 char* headerText = new char[headerSize+1];
401 memcpy(headerText, chunk, headerSize);
402 headerText[headerSize] = 0;
403
404 //printf("*** %s\n", headerText);
405
406 std::multimap<std::string, std::string> entryMap;
407 std::multimap<std::string, std::string>::iterator ei;
408
409 std::vector<std::string> headerEntries = utils::TextListSplit(headerText, "\r\n", true, true);
410 std::vector<std::string>::iterator i = headerEntries.begin(), e = headerEntries.end();
411 while (i != e) {
412 if (utils::TextStartsWith((*i).c_str(), "Content-Disposition:", false)) {
413 entryMap = utils::TextMultiMapSplit((*i).c_str(), ";", "=");
414 if ( (ei = entryMap.find("name")) != entryMap.end())
415 postEntry->name = utils::TextTrimQuotes(ei->second.c_str());
416 if ( (ei = entryMap.find("filename")) != entryMap.end())
417 postEntry->filename = utils::TextTrimQuotes(ei->second.c_str());
418 }
419 else if (utils::TextStartsWith((*i).c_str(), "Content-Type:", false)) {
420 postEntry->type = (*i).substr(14);
421 }
422 i++;
423 }
424 delete [] headerText;
425 if (postEntry->name.length())
426 postEntry->setContent(valContent, valContentSize);
427
428 if (postEntry->isValid()) {
429 postEntries[postEntry->name] = postEntry;
430 return true;
431 }
432 else {
433 delete(postEntry);
434 return false;
435 }
436
437
438 //CGIRequestEntry reqEntry;
439
440 //int pos;
441 //QStringList entries;
442 //for (int n=0; n<headerList.size(); n++) {
443 // if ((entry = headerList.at(n).trimmed()).length()) {
444 // if (entry.startsWith("Content-Disposition:", Qt::CaseInsensitive)) {
445 // entries = entry.split(";");
446 // for (int m=0; m<entries.size(); m++) {
447 // if ( (pos = entries.at(m).indexOf(" name=")) >= 0) {
448 // reqEntry.name = entries.at(m).mid(pos+6).replace("\"","");
449 // }
450 // else if ( (pos = entries.at(m).indexOf(" filename=")) >= 0) {
451 // reqEntry.filename = entries.at(m).mid(pos+10).replace("\"","");
452 // }
453 // }
454 // }
455 // else if (entry.startsWith("Content-Type:", Qt::CaseInsensitive)) {
456 // reqEntry.type = entry.split(":").at(1).trimmed().replace("\"","");
457 // }
458 // }
459 //}
460
461 //if (!reqEntry.name.length())
462 // return false;
463
464 //reqEntry.data = dataChunk.mid(doubleBreakPos+4);
465 //requestEntries[reqEntry.name] = reqEntry;
467 //return true;
468}
469
470bool HTTPRequest::parseURIParameters(const char* text) {
471 if (!text)
472 return false;
473
474 std::multimap<std::string, std::string> paramMap = utils::TextMultiMapSplit(text, "&", "=");
475 std::multimap<std::string, std::string>::iterator i = paramMap.begin(), e = paramMap.end();
476 while (i != e) {
477 params[html::DecodeHTML(i->first)] = html::DecodeHTML(i->second);
478 i++;
479 }
480 return true;
481
482// uint32 len = (uint32)strlen(text);
483// const char* s = text;
484// const char* e = strchr(s, '=');
485// const char* p = strchr(s, '&');
486
487// if (!e && !p) {
488// key.assign(s);
489// params[html::DecodeHTML(key)] = "";
490// return true;
491// }
492
493// std::string key, val;
494
495// while ( e ) {
496// key.assign(s, e-s);
497// if (p) {
498// val.assign(e+1, p-e-1);
499// params[html::DecodeHTML(key)] = html::DecodeHTML(val);
500// s = p+1;
501// e = strchr(s, '=');
502// p = strchr(s, '&');
503// }
504// else {
505// val.assign(e+1);
506// params[html::DecodeHTML(key)] = html::DecodeHTML(val);
507// break;
508// }
509// }
510// return true;
511}
512
514 DataMessage* msg = new DataMessage();
515 std::map<std::string, std::string>::iterator i, e;
516
517 msg->setInt("HTTP_OPERATION", type);
518 msg->setString("REQUEST", this->getRequest());
519 msg->setString("URI", this->getURI());
520 msg->setTime("SOURCE", this->source);
521
522 for (i=entries.begin(), e=entries.end(); i!=e; i++)
523 msg->setString(i->first.c_str(), i->second.c_str());
524
525 for (i=params.begin(), e=params.end(); i!=e; i++)
526 msg->setString(i->first.c_str(), i->second.c_str());
527
528 std::map<std::string, HTTPPostEntry*>::iterator pi = postEntries.begin(), pe = postEntries.end();
529 while (pi != pe) {
530 if (pi->second->type.length()) {
531 msg->setString((pi->first+"_CONTENT_TYPE_").c_str(), pi->second->type.c_str());
532 if (utils::stristr(pi->second->type.c_str(), "json"))
533 msg->setString(pi->first.c_str(), pi->second->content);
534 else if (utils::stristr(pi->second->type.c_str(), "xml"))
535 msg->setString(pi->first.c_str(), pi->second->content);
536 else if (utils::stristr(pi->second->type.c_str(), "text"))
537 msg->setString(pi->first.c_str(), pi->second->content);
538 else
539 msg->setData(pi->first.c_str(), pi->second->content, pi->second->contentSize);
540 }
541 else
542 msg->setString(pi->first.c_str(), pi->second->content);
543 pi++;
544 }
545 msg->setCreatedTime(time);
547
548 return msg;
549}
550
552 // Connection is a token list ("keep-alive, Upgrade" from Firefox and some proxies): look for the token
553 const char* connection = this->getHeaderEntry("Connection");
554 if (!connection)
555 return false;
556 std::string c = connection;
557 for (size_t i = 0; i < c.size(); i++) c[i] = (char)tolower((unsigned char)c[i]);
558 bool hasUpgrade = false;
559 size_t p0 = 0;
560 while (p0 <= c.size()) {
561 size_t p1 = c.find(',', p0);
562 std::string tok = c.substr(p0, p1 == std::string::npos ? std::string::npos : p1 - p0);
563 size_t a = tok.find_first_not_of(" \t"), b = tok.find_last_not_of(" \t");
564 if (a != std::string::npos && tok.substr(a, b - a + 1) == "upgrade") { hasUpgrade = true; break; }
565 if (p1 == std::string::npos) break;
566 p0 = p1 + 1;
567 }
568 if (!hasUpgrade)
569 return false;
570
571 const char* upgrade = this->getHeaderEntry("Upgrade");
572 if (!upgrade || stricmp(upgrade, "websocket"))
573 return false;
574
575 return true;
576}
577
578
579
580const char* HTTPRequest::getParameter(const char* entry) {
581 std::map<std::string, std::string>::iterator it = params.find(entry);
582 if (it != params.end())
583 return it->second.c_str();
584 else
585 return NULL;
586}
587
588const char* HTTPRequest::getPostData(const char* entry, uint32& size, const char** type) {
589 std::map<std::string, HTTPPostEntry*>::iterator i = postEntries.find(entry);
590 if (i == postEntries.end())
591 return NULL;
592 size = i->second->contentSize;
593 *type = i->second->type.c_str();
594 return i->second->content;
595}
596
597const char* HTTPRequest::getPostData(const char* entry, uint32& size) {
598 std::map<std::string, HTTPPostEntry*>::iterator i = postEntries.find(entry);
599 if (i == postEntries.end())
600 return NULL;
601 size = i->second->contentSize;
602 return i->second->content;
603}
604
605const char* HTTPRequest::getPostDataType(const char* entry) {
606 std::map<std::string, HTTPPostEntry*>::iterator i = postEntries.find(entry);
607 if (i == postEntries.end())
608 return NULL;
609 return i->second->type.c_str();
610}
611
612
614 uint8 type, const char* host, const char* uri, const char* content,
615 uint32 contentSize, bool keepAlive, uint64 ifModifiedSince) {
616
617 char timeString[1024];
618 if (!ifModifiedSince) {
619 if (!GetHTTPTime(TIME_YEAR_1970, timeString, 1024))
620 timeString[0] = 0;
621 }
622 else {
623 if (!GetHTTPTime(ifModifiedSince, timeString, 1024))
624 timeString[0] = 0;
625 }
626
627 if (data)
628 delete [] data;
629 data = new char[4096 + contentSize];
630 if (!content) {
631 snprintf(data, 4096+contentSize,
632"%s %s HTTP/1.1\r\n\
633Host: %s\r\n\
634Accept: */*\r\n\
635User-Agent: CMLabsHTTP/1.0\r\n\
636if-modified-since: %s\r\n\r\n",
637 HTTP_Type[type], uri, host, timeString);
638 }
639 else {
640 snprintf(data, 4096+contentSize,
641"%s %s HTTP/1.1\r\n\
642Host: %s\r\n\
643Accept: */*\r\n\
644User-Agent: CMLabsHTTP/1.0\r\n\
645Content-Type: application/x-www-form-urlencoded\r\n\
646Content-Length: %u\r\n\r\n",
647 HTTP_Type[type], uri, host, (uint32) (contentSize ? contentSize : strlen(content)));
648 }
649
650 headerLength = (uint32)strlen(data);
651 memcpy(data+headerLength, content, contentSize);
652 contentLength = contentSize;
654
655 return true;
656}
657
658bool HTTPRequest::createRequest(uint8 type, const char* host, const char* uri, std::map<std::string, std::string>& headerEntries, bool keepAlive, uint64 ifModifiedSince) {
659
660 // First create the header
661
662 char timeString[1024];
663 if (!ifModifiedSince) {
664 if (!GetHTTPTime(TIME_YEAR_1970, timeString, 1024))
665 timeString[0] = 0;
666 }
667 else {
668 if (!GetHTTPTime(ifModifiedSince, timeString, 1024))
669 timeString[0] = 0;
670 }
671
672 std::string headerContent = utils::StringFormat(
673 "%s %s HTTP/1.1\r\n\
674Host: %s\r\n\
675Accept: */*\r\n\
676User-Agent: CMLabsHTTP/1.0\r\n\
677if-modified-since: %s\r\n",
678HTTP_Type[type], uri, host, timeString);
679
680 std::map<std::string, std::string>::iterator hI = headerEntries.begin(), hE = headerEntries.end();
681 while (hI != hE) {
682 if ((stricmp(hI->first.c_str(), "content-type") != 0) &&
683 (stricmp(hI->first.c_str(), "content-length") != 0)) {
684 headerContent += utils::StringFormat("%s: %s\r\n", hI->first.c_str(), hI->second.c_str());
685 }
686 hI++;
687 }
688 // end of the header block: without this blank line a request with custom headers and no body never finished, so
689 // makeHTTPRequest(ops, url, timeout, headers, NULL, ...) timed out on every server (found 15 Nov 2026)
690 headerContent += "\r\n";
691 contentLength = 0;
692
693 // Calculate content size
694 headerLength = (uint32)headerContent.length();
695
696 if (data)
697 delete [] data;
698 data = new char[headerLength + 1];
699
700 memcpy(data, headerContent.c_str(), headerLength);
701 data[headerLength] = 0;
702 return true;
703}
704
705
706bool HTTPRequest::createRequest(uint8 type, const char* host, const char* uri, std::map<std::string, std::string>& headerEntries, HTTPPostEntry* bodyEntry, bool keepAlive, uint64 ifModifiedSince) {
707 if (!bodyEntry)
708 return createRequest(type, host, uri, headerEntries, keepAlive, ifModifiedSince);
709 else
710 return createRequest(type, host, uri, headerEntries, bodyEntry->content, bodyEntry->type.c_str(), bodyEntry->contentSize, keepAlive, ifModifiedSince);
711}
712
713bool HTTPRequest::createRequest(uint8 type, const char* host, const char* uri, std::map<std::string, std::string>& headerEntries, const char* content, const char* contentType, uint32 contentSize, bool keepAlive, uint64 ifModifiedSince) {
714
715 if (!contentSize)
716 return createRequest(type, host, uri, headerEntries, keepAlive, ifModifiedSince);
717
718 if (!content || !contentType)
719 return false;
720
721 // First create the header
722 char timeString[1024];
723 if (!ifModifiedSince) {
724 if (!GetHTTPTime(TIME_YEAR_1970, timeString, 1024))
725 timeString[0] = 0;
726 }
727 else {
728 if (!GetHTTPTime(ifModifiedSince, timeString, 1024))
729 timeString[0] = 0;
730 }
731
732 std::string headerContent = utils::StringFormat(
733 "%s %s HTTP/1.1\r\n\
734Host: %s\r\n\
735Accept: */*\r\n\
736User-Agent: CMLabsHTTP/1.0\r\n\
737Content-Type: %s\r\n\
738if-modified-since: %s\r\n",
739HTTP_Type[type], uri, host, contentType, timeString);
740
741 std::map<std::string, std::string>::iterator hI = headerEntries.begin(), hE = headerEntries.end();
742 while (hI != hE) {
743 if ((stricmp(hI->first.c_str(), "content-type") != 0) &&
744 (stricmp(hI->first.c_str(), "content-length") != 0)) {
745 headerContent += utils::StringFormat("%s: %s\r\n", hI->first.c_str(), hI->second.c_str());
746 }
747 hI++;
748 }
749
750 // Calculate content size
751 contentLength = contentSize;
752 headerContent += utils::StringFormat("Content-Length: %u\r\n\r\n", contentLength);
753 headerLength = (uint32)headerContent.length();
754
755 if (data)
756 delete [] data;
757 data = new char[headerLength + contentLength + 1];
758
759 memcpy(data, headerContent.c_str(), headerLength);
760 char* dst = data + headerLength;
761
762 memcpy(dst, content, contentLength);
764 return true;
765}
766
767
769 uint8 type, const char* host, const char* uri, std::map<std::string, std::string>& headerEntries,
770 std::map<std::string, HTTPPostEntry*>& bodyEntries, bool keepAlive, uint64 ifModifiedSince) {
771
772 // First create the header
773
774 std::string boundaryString = utils::StringFormat("----cmboundary%lld", utils::RandomInt(0, 10000000));
775 uint32 boundaryLength = (uint32)boundaryString.length();
776
777 char timeString[1024];
778 if (!ifModifiedSince) {
779 if (!GetHTTPTime(TIME_YEAR_1970, timeString, 1024))
780 timeString[0] = 0;
781 }
782 else {
783 if (!GetHTTPTime(ifModifiedSince, timeString, 1024))
784 timeString[0] = 0;
785 }
786
787 std::string headerContent = utils::StringFormat(
788 "%s %s HTTP/1.1\r\n\
789Host: %s\r\n\
790Accept: */*\r\n\
791User-Agent: CMLabsHTTP/1.0\r\n\
792Content-Type: multipart/form-data; boundary=%s\r\n\
793if-modified-since: %s\r\n",
794 HTTP_Type[type], uri, host, boundaryString.c_str(), timeString);
795
796 std::map<std::string, std::string>::iterator hI = headerEntries.begin(), hE = headerEntries.end();
797 while (hI != hE) {
798 if ((stricmp(hI->first.c_str(), "content-type") != 0) &&
799 (stricmp(hI->first.c_str(), "content-length") != 0)) {
800 headerContent += utils::StringFormat("%s: %s\r\n", hI->first.c_str(), hI->second.c_str());
801 }
802 hI++;
803 }
804
805 // Remember to add this to the header at the end... Content-Length: %u\r\n\r\n",
806
807 // Calculate content size
808 contentLength = 2 + boundaryLength + 2; // Add the first --boundary\r\n
809
810 uint32 filenameStatementLength = 0;
811
812 std::map<std::string, HTTPPostEntry*>::iterator bI = bodyEntries.begin(), bE = bodyEntries.end();
813 while (bI != bE) {
814 if (html::IsMimeBinary(bI->second->type.c_str()))
815 filenameStatementLength = 19;
816 else
817 filenameStatementLength = 0;
818 // Content-Type: image/jpeg\r\n
819 // Content-Disposition: form-data; name="image"; filename="nofile"\r\n\r\n
820 // <content>\r\n
821 // --boundary
822 contentLength += 14 + (uint32)bI->second->type.length() + 2
823 + 38 + (uint32)bI->second->name.length() + 5 + filenameStatementLength
824 + bI->second->contentSize + 2 + 2 + boundaryLength;
825 if (bI != bE) {
826 contentLength += 2;
827 }
828 bI++;
829 }
830 contentLength += 4; // for the last --\r\n
831
832 headerContent += utils::StringFormat("Content-Length: %u\r\n\r\n", contentLength);
833 headerLength = (uint32)headerContent.length();
834
835 if (data)
836 delete [] data;
837 data = new char[headerLength + contentLength + 1];
838
839 memcpy(data, headerContent.c_str(), headerLength);
840 char* dst = data + headerLength;
841
842 //sprintf(dst, "--%s\r\n", boundaryString.c_str());
843 snprintf(dst, 2 + boundaryLength + 2, "--%s\r\n", boundaryString.c_str());
844 dst += 2 + boundaryLength + 2;
845
846
847 bI = bodyEntries.begin();
848 while (bI != bE) {
849 // Content-Type: image/jpeg\r\n
850 // Content-Disposition: form-data; name="image"; filename="nofile"\r\n\r\n
851 // <content>\r\n
852 // --boundary
853 if (html::IsMimeBinary(bI->second->type.c_str())) {
854 filenameStatementLength = 19;
855 // sprintf(dst, "Content-Type: %s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"nofile\"\r\n\r\n",
856 snprintf(dst, 14 + bI->second->type.length() + 2 + 38 + bI->second->name.length() + 5 + 19, "Content-Type: %s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"nofile\"\r\n\r\n",
857 bI->second->type.c_str(),
858 bI->second->name.c_str());
859 }
860 else {
861 // sprintf(dst, "Content-Type: %s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n",
862 snprintf(dst, 14 + bI->second->type.length() + 2 + 38 + bI->second->name.length() + 5, "Content-Type: %s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n",
863 bI->second->type.c_str(),
864 bI->second->name.c_str());
865 filenameStatementLength = 0;
866 }
867
868 dst += 14 + bI->second->type.length() + 2
869 + 38 + bI->second->name.length() + 5 + filenameStatementLength;
870 memcpy(dst, bI->second->content, bI->second->contentSize);
871 dst += bI->second->contentSize;
872 // sprintf(dst, "\r\n--%s", boundaryString.c_str());
873 snprintf(dst, 2 + 2 + boundaryLength, "\r\n--%s", boundaryString.c_str());
874 dst += 2 + 2 + boundaryLength;
875 bI++;
876 if (bI != bE) {
877 // sprintf(dst, "\r\n");
878 snprintf(dst, 4, "\r\n"); // put 4 in instead of 2 to avoid compiler warning
879 dst += 2;
880 }
881 }
882 // sprintf(dst, "--\r\n");
883 snprintf(dst, 6, "--\r\n"); // put 6 in instead of 4 to avoid compiler warning
884 dst += 4;
886 //uint32 check = (uint32)(dst - data);
887 //uint32 check2 = headerLength + contentLength;
888 //printf("Memory size: %u - data size: %u\n", check2, check);
889 return true;
890}
891
892bool HTTPRequest::createWebsocketRequest(const char* uri, const char* host, const char* protocolName, const char* origin) {
893
894 //GET /chat HTTP/1.1
895 //Host: server.example.com
896 //Upgrade: websocket
897 //Connection: Upgrade
898 //Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
899 //Sec-WebSocket-Protocol: chat, superchat
900 //Sec-WebSocket-Version: 13
901 //Origin: http://example.com
902
903 if (!host)
904 return false;
905
906 std::string uriString;
907 if (uri && strlen(uri))
908 uriString = uri;
909 if (!utils::TextStartsWith(uriString.c_str(), "/", false))
910 uriString = utils::StringFormat("/%s", uriString.c_str());
911
912 std::string originString;
913 if (origin && strlen(origin))
914 originString = origin;
915 else
916 originString = utils::StringFormat("http://%s/", host);
917
918 std::string protocolNameString;
919 if (protocolName && strlen(protocolName))
920 protocolNameString = protocolName;
921 else
922 protocolNameString = "Default";
923
924 char* randomKey = new char[17];
925 for (uint32 n = 0; n < 16; n++)
926 randomKey[n] = (uint8)utils::RandomInt(33, 255);
927 randomKey[16] = 0;
928 std::string key = base64_encode(randomKey);
929 //std::string key = "x3JJHMbDL1EzLkh9GBhXDw==";
930
931 if (data)
932 delete [] data;
933 data = new char[4096];
934 snprintf(data, 4096,
935"GET %s HTTP/1.1\r\n\
936Host: %s\r\n\
937Upgrade: websocket\r\n\
938Connection: Upgrade\r\n\
939Sec-WebSocket-Key: %s\r\n\
940Sec-WebSocket-Protocol: %s\r\n\
941Sec-WebSocket-Version: 13\r\n\
942Origin: %s\r\n\r\n",
943 uriString.c_str(), host, key.c_str(), protocolNameString.c_str(), originString.c_str());
944
945 headerLength = (uint32)strlen(data);
946 contentLength = 0;
947 data[headerLength] = 0;
948 return true;
949}
950
951
953 if (data)
955 else
956 return 0;
957}
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977WebsocketData::WebsocketData(uint64 source, uint64 startRecTime) {
978 this->source = source;
979 headerLength = 0;
981 opcode = 0;
982 maskingKey = 0;
983 packages = 0;
984 data = NULL;
985 rawData = NULL;
986 dataType = NONE;
987 status = IDLE;
988 isFinal = false;
989}
990
992 headerLength = wsData->headerLength;
993 payloadSize = wsData->payloadSize;
994 contentSize = wsData->contentSize;
995 opcode = wsData->opcode;
996 maskingKey = wsData->maskingKey;
997 packages = wsData->packages;
998 if (wsData->data) {
999 data = new char[(uint32)payloadSize + 1];
1000 memcpy(data, wsData->data, (uint32)payloadSize);
1001 data[payloadSize] = 0;
1002 }
1003 dataType = wsData->dataType;
1004 if (wsData->rawData) {
1005 rawData = new char[(uint32)payloadSize + headerLength + 1];
1006 memcpy(rawData, wsData->rawData, (uint32)payloadSize + headerLength);
1008 }
1009 status = wsData->status;
1010 isFinal = wsData->isFinal;
1011}
1012
1014 if (data != NULL)
1015 delete [] data;
1016 data = NULL;
1017 if (rawData != NULL)
1018 // delete [], not delete: rawData is new char[headerLength+size+1] in
1019 // setData(). Scalar delete on an array allocation is an alloc-dealloc
1020 // mismatch - undefined behaviour, reported by ASan. Note `data` directly
1021 // above was already correct, which is how this one stayed unnoticed.
1022 delete [] rawData;
1023 rawData = NULL;
1024 status = IDLE;
1025}
1026
1028 WebsocketData* wsData = new WebsocketData();
1029 wsData->setData(wsData->CLOSE, false);
1030 return wsData;
1031}
1032
1034 WebsocketData* wsData = new WebsocketData();
1035 wsData->setData(wsData->PING, false);
1036 return wsData;
1037}
1038
1040 WebsocketData* wsData = new WebsocketData();
1041 wsData->setData(wsData->PONG, false);
1042 return wsData;
1043}
1044
1046 return (dataType == CLOSE);
1047}
1048
1052
1054 return (dataType == PING);
1055}
1056
1058 return (dataType == PONG);
1059}
1060
1061bool WebsocketData::setData(DataType dataType, bool maskData, const char* data, uint64 size) {
1062 // Builds the complete RFC 6455 frame into rawData:
1063 // byte 0: FIN | RSV1-3 | opcode (we always send FIN = single frame)
1064 // byte 1: MASK | 7-bit payload length (126 => 16-bit ext, 127 => 64-bit ext)
1065 // [2 or 8 bytes extended length, network byte order]
1066 // [4-byte masking key when maskData] (clients MUST mask per the RFC)
1067 // payload (XOR-masked with the key when maskData)
1068 if ((dataType == NONE))
1069 return false;
1070
1071 this->dataType = dataType;
1072 if (maskData && !maskingKey)
1074
1075 headerLength = basic_header_length; // +sizeof(uint32);
1076 if (maskData)
1077 headerLength += sizeof(uint32);
1078 uint32 mkOffset = basic_header_length;
1079 uint32 basic_value = 0;
1080
1081 if (size <= payload_size_basic) {
1082 basic_value = (uint8)size;
1083 }
1084 else if (size <= payload_size_extended) {
1085 mkOffset += 2;
1086 headerLength += 2;
1087 basic_value = payload_size_code_16bit;
1088 }
1089 else {
1090 headerLength += 8;
1091 mkOffset += 8;
1092 basic_value = payload_size_code_64bit;
1093 }
1094
1095 if (rawData)
1096 delete [] rawData; // new char[] below - see the destructor comment
1097
1098 uint8 opcode = (uint8)dataType;
1099
1100 rawData = new char[headerLength + (uint32)size + 1];
1101 memset(rawData, 0, headerLength);
1102
1103 uint8* b0 = (uint8*)rawData;
1104 uint8* b1 = (uint8*)(rawData + 1);
1105
1106 *b0 |= BHB0_FIN;
1107 *b0 |= (opcode & BHB0_OPCODE);
1108 if (maskData)
1109 *b1 |= BHB1_MASK;
1110
1111 *b1 |= basic_value;
1112
1113 if (maskData)
1114 *(uint32*)(rawData + mkOffset) = maskingKey;
1115
1116 contentSize = payloadSize = size;
1117
1118 if (!size)
1119 return true;
1120
1121 // Write the extended length field (network byte order): 16-bit for 126..65535 bytes,
1122 // 64-bit above. (16 Nov 2026: the 64-bit case used to be unreachable - the 16-bit test
1123 // came first - so a frame over 64 KB went out with its length truncated to 16 bits in
1124 // the 64-bit slot, and the receiver read garbage and waited forever; found with Mimir
1125 // attachments.) ntohs/ntoh64 are their own inverses, so they serve as hton here.
1126 if (size > payload_size_extended) {
1127 uint64* h3 = (uint64*)(rawData + 2);
1128 *h3 = utils::ntoh64(&size);
1129 }
1130 else if (size > payload_size_basic) {
1131 uint16* h2 = (uint16*)(rawData + 2);
1132 *h2 = ntohs((uint16)size);
1133 }
1134
1135 if (maskData) {
1136 // RFC 6455 masking: payload byte i is XORed with key byte (i mod 4).
1137 // The same loop unmasks on the receive side (XOR is symmetric).
1138 uint8* mask = (uint8*)(&maskingKey);
1139 uint8* src = (uint8*)data;
1140 uint8* dst = (uint8*)(rawData + headerLength);
1141 for (uint64 n = 0; n < size; n++) {
1142 *dst = *src ^ mask[n % 4];
1143 dst++;
1144 src++;
1145 }
1146 }
1147 else {
1148 memcpy(rawData + headerLength, data, (uint32)size);
1149 }
1151 status = COMPLETE;
1152 return true;
1153}
1154
1155const char* WebsocketData::getRawData(uint64& size) {
1156 if (status != COMPLETE) {
1157 size = 0;
1158 return NULL;
1159 }
1160 size = headerLength + payloadSize;
1161 return rawData;
1162}
1163
1164
1166 return contentSize;
1167}
1168
1170 return payloadSize;
1171}
1172
1173
1175 return (status == COMPLETE);
1176}
1177
1178bool WebsocketData::processHeader(const char* buffer, uint64 size, bool &isInvalid) {
1179 // Decodes the RFC 6455 basic header (2 bytes) plus, depending on the 7-bit
1180 // length code, a 16-bit (code 126) or 64-bit (code 127) extended length in
1181 // network byte order, then the 4-byte masking key when the MASK bit is set.
1182 // Requires at least 4 bytes buffered even though a minimal unmasked frame
1183 // is only 2 — callers always have the masked client header available.
1184 // On a continuation frame (status != IDLE) the opcode is 0, so dataType is
1185 // only latched from the first fragment.
1186 isInvalid = false;
1187 if (!buffer || (size < 4))
1188 return false;
1189
1190// utils::WriteAFile("d:/ws.bin", buffer, size, true);
1191
1192 time = GetTimeNow();
1193
1194 uint8 b0 = *(const unsigned char*)buffer;
1195 uint8 b1 = *(const unsigned char*)(buffer + 1);
1196
1197 isFinal = b0 & BHB0_FIN;
1198 uint8 opcode = b0 & BHB0_OPCODE;
1199
1200 // if this is the first package
1201 if (status == IDLE) {
1202 if (opcode == 1)
1203 dataType = TEXT;
1204 else if (opcode == 2)
1205 dataType = BINARY;
1206 else if (opcode == 8)
1207 dataType = CLOSE;
1208 else if (opcode == 9)
1209 dataType = PING;
1210 else if (opcode == 10)
1211 dataType = PONG;
1212 }
1213
1214 bool mask = b1 & BHB1_MASK;
1215 uint8 basic_length = b1 & BHB1_PAYLOAD;
1216 payloadSize = 0;
1217
1218 uint32 mkOffset = basic_header_length;
1219 uint32 payloadOffset = basic_header_length;
1220
1221 if (basic_length <= payload_size_basic) {
1222 payloadSize = basic_length;
1223 }
1224 else if (basic_length == payload_size_code_16bit) {
1225 uint16 h2 = *(const uint16*)(buffer + 2);
1226 payloadSize = ntohs(h2);
1227 mkOffset += 2;
1228 payloadOffset += 2;
1229 // get_extended_size(e);
1230 }
1231 else {
1232 uint64 h3 = *(const uint64*)(buffer + 2);
1234 mkOffset += 8;
1235 payloadOffset += 8;
1236 // get_jumbo_size(e);
1237 }
1238
1239 // if just a control message
1240 if (!payloadSize) {
1241 status = COMPLETE;
1242 return true;
1243 }
1244
1245 maskingKey = 0;
1246 if (mask) {
1247 maskingKey = *(uint32*)(buffer + mkOffset);
1248 //maskingKey = 2112143814;
1249 payloadOffset += 4;
1250 }
1251
1252 // printf("%s\n\n", utils::PrintBitFieldString(buffer, size, 8 * size, "ws").c_str());
1253
1254/* LogPrint(0, LOG_NETWORK, 3, "%s: [%u] (%u) size: %llu offset: %u total size: %llu\n",
1255 isFinal ? "Final" : "Continue",
1256 opcode,
1257 maskingKey,
1258 payloadSize,
1259 payloadOffset,
1260 size
1261 );*/
1262
1263 headerLength = payloadOffset;
1264 //contentSize = payloadSize;
1265
1266 return true;
1267}
1268
1269bool WebsocketData::processContent(const char* buffer, uint64 size) {
1270 // Unmasks (XOR with the 4-byte key; a no-op when maskingKey == 0, i.e.
1271 // unmasked server-to-client frames) and appends this fragment's payload.
1272 // Fragmented messages are reassembled here: each continuation reallocates
1273 // data to contentSize + payloadSize and copies the previous bytes over, so
1274 // the final buffer holds the whole logical message; status only becomes
1275 // COMPLETE on the FIN fragment.
1276 if (!buffer || (size != payloadSize))
1277 return false;
1278
1279// utils::WriteAFile("d:/ws_payload.bin", buffer, size, true);
1280
1281 uint8* mask = (uint8*)(&maskingKey);
1282
1283 uint8* src = (uint8*)buffer;
1284 uint8* dst;
1285
1286 if (status == IDLE) {
1287 if (data)
1288 delete [] data;
1289 data = new char[(uint32)payloadSize + 1];
1290 dst = (uint8*)data;
1291 }
1292 else {
1293 char* newData = new char[(uint32)contentSize + (uint32)payloadSize + 1];
1294 memcpy(newData, data, (uint32)contentSize);
1295 delete [] data;
1296 data = newData;
1297 dst = (uint8*)(data+ (uint32)contentSize);
1298 }
1299
1300 for (uint64 n = 0; n < payloadSize; n++) {
1301 *dst = *src ^ mask[n % 4];
1302 dst++;
1303 src++;
1304 }
1306 data[contentSize] = 0;
1308 if (isFinal)
1309 status = COMPLETE;
1310 else
1312 packages++;
1313
1314 //LogPrint(0, LOG_NETWORK, 0, "Content received: %s\n",
1315 // data
1316 //);
1317
1318 if (isFinal) {
1319 if (packages == 1)
1320 LogPrint(0, LOG_NETWORK, 3, "Single Websocket package content received: %llu -> %llu\n", size, contentSize);
1321 else
1322 LogPrint(0, LOG_NETWORK, 3, "Last Websocket content package received: %llu -> %llu (%u)\n", size, contentSize, packages);
1323 }
1324 else {
1325 if (packages == 1)
1326 LogPrint(0, LOG_NETWORK, 3, "First Websocket content received: %llu -> %llu\n", size, contentSize);
1327 // else
1328 // LogPrint(0, LOG_NETWORK, 0, "Next content received: %llu -> %llu\n", size, contentSize);
1329 }
1330
1331// utils::WriteAFile("d:/ws_content.bin", data, contentSize, true);
1332
1333 return true;
1334}
1335
1336
1337
1338const char* WebsocketData::getContent(uint64& size) {
1339 size = 0;
1340 if (!data || !payloadSize || (status != COMPLETE))
1341 return NULL;
1342 size = contentSize;
1343 return data;
1344}
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1358 this->source = source;
1359 data = NULL;
1360 time = GetTimeNow();
1361 source = 0;
1362 size = 0;
1363 user = 0;
1364 cr[0] = 13;
1365 cr[1] = 10;
1366 cr[2] = 0;
1367}
1368
1370 if (data != NULL)
1371 delete [] data;
1372 data=NULL;
1373}
1374
1375bool TelnetLine::setCR(uint8 type) {
1376 switch(type) {
1377 default:
1378 case TELNET_WINDOWS:
1379 cr[0] = 13;
1380 cr[1] = 10;
1381 cr[2] = 0;
1382 break;
1383 case TELNET_UNIX:
1384 cr[0] = 10;
1385 cr[1] = 0;
1386 break;
1387 }
1388 return true;
1389}
1390
1392 if (!data)
1393 return 0;
1394 if (size)
1395 return size;
1396 else
1397 return (uint32)strlen(data);
1398}
1399
1400bool TelnetLine::setLine(const char* buffer, bool addCR) {
1401 return setLine(buffer, (uint32)strlen(buffer), addCR);
1402}
1403
1404bool TelnetLine::setLine(const char* buffer, uint32 len, bool addCR) {
1405 if (data)
1406 delete [] data;
1407
1408 data = new char[len+3];
1409 if (buffer && len > 0) {
1410 memcpy(data, buffer, len);
1411 if (addCR)
1412 memcpy(data+len, cr, 3);
1413 else
1414 data[len] = 0;
1415 }
1416 else
1417 data[len] = 0;
1418 return true;
1419}
1420
1421bool TelnetLine::giveData(char* buffer, uint32 len) {
1422 if (data)
1423 delete [] data;
1424 data = buffer;
1425 size = len;
1426 return true;
1427}
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1441 HTTPReply* reply = new HTTPReply((uint64)0);
1442 reply->type = type;
1443 return reply;
1444}
1445
1447 HTTPReply* reply = new HTTPReply((uint64)0);
1448 reply->createAuthorizationReply(realm);
1449 return reply;
1450}
1451
1452HTTPReply* HTTPReply::CreateWebsocketHTTPReply(const char* key, const char* version) {
1453 HTTPReply* reply = new HTTPReply((uint64)0);
1454 reply->createWebsocketHTTPReply(key, version);
1455 return reply;
1456}
1457
1458
1460 time = GetTimeNow();
1461 this->source = source;
1462 data = NULL;
1463 type = 0;
1465 keepAlive = true;
1466 chunked = false;
1467}
1468
1470 time = reply->time;
1471 source = reply->source;
1472 type = reply->type;
1473 headerLength = reply->headerLength;
1475 keepAlive = reply->keepAlive;
1476 chunked = reply->chunked;
1477 data = new char[headerLength+contentLength+1];
1478 if (reply->data)
1479 memcpy(data, reply->data, headerLength+contentLength+1); // incl. string end
1480 else
1481 memset(data, 0, headerLength+contentLength+1);
1482 entries = reply->entries;
1483 params = reply->params;
1484}
1485
1487 if (data != NULL)
1488 delete [] data;
1489 data = NULL;
1490}
1491
1492// Generating the data
1493
1494bool HTTPReply::createOptionsResponse(uint8 status, uint64 time, const char* serverName, bool keepAlive, const char* origin, const char* operations) {
1495
1496 //OPTIONS /cors HTTP/1.1
1497 //Origin: http://api.bob.com
1498 //Access-Control-Request-Method: PUT
1499 //Access-Control-Request-Headers: X-Custom-Header
1500 //Host: api.alice.com
1501 //Accept-Language: en-US
1502 //Connection: keep-alive
1503 //User-Agent: Mozilla/5.0...
1504 //
1505 //Preflight Response:
1506 //
1507 //Access-Control-Allow-Origin: http://api.bob.com
1508 //Access-Control-Allow-Methods: GET, POST, PUT
1509 //Access-Control-Allow-Headers: X-Custom-Header
1510 //Content-Type: text/html; charset=utf-8
1511
1512 char timeString[1024];
1513 if (!GetHTTPTime(time, timeString, 1024))
1514 timeString[0] = 0;
1515
1516 if (data)
1517 delete [] data;
1518 data = new char[4096];
1519 snprintf(data, 4096,
1520"Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, PUT, POST, DELETE\r\nAccess-Control-Allow-Headers: accept, authorization, origin\r\n\r\n");
1521//"Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept\r\n\r\n");
1522
1523//"%s\r\nDate: %s\r\n
1524//Server: %s\r\n
1525//Connection: %s\r\n
1526//"Access-Control-Allow-Origin: %s\r\n
1527//Access-Control-Allow-Methods: %s\r\n
1528//Access-Control-Allow-Headers: X-Custom-Header\r\n
1529//Content-Type: text/html\r\n\r\n",
1530// //HTTP_Status[status], timeString, serverName,
1531// //keepAlive ? "Keep-Alive" : "close",
1532// origin, operations);
1533
1534 headerLength = (uint32)strlen(data);
1535 contentLength = 0;
1536 return true;
1537}
1538
1539
1540bool HTTPReply::createErrorPage(uint8 status, uint64 time, const char* serverName, bool keepAlive) {
1541
1542 char timeString[1024];
1543 if (!GetHTTPTime(time, timeString, 1024))
1544 timeString[0] = 0;
1545
1546 if (data)
1547 delete [] data;
1548 data = new char[4096];
1549 snprintf(data, 4096,
1550 "%s\r\nDate: %s\r\nServer: %s\r\n"
1551 "X-Frame-Options: SAMEORIGIN\r\n"
1552 "Connection: %s\r\n\r\n",
1553 HTTP_Status[status], timeString, serverName,
1554 keepAlive ? "Keep-Alive" : "close");
1555
1556 headerLength = (uint32)strlen(data);
1557 contentLength = 0;
1558 return true;
1559}
1560
1562
1563 char timeString[1024];
1564 if (!GetHTTPTime(time, timeString, 1024))
1565 timeString[0] = 0;
1566
1567 if (data)
1568 delete [] data;
1569 data = new char[4096];
1570 snprintf(data, 4096,
1571 "%s\r\nWWW-Authenticate: Basic realm=\"%s\"\r\nContent-Length: 0\r\n\r\n",
1573
1574 headerLength = (uint32)strlen(data);
1575 contentLength = 0;
1576 return true;
1577}
1578
1579bool HTTPReply::createWebsocketHTTPReply(const char* key, const char* version) {
1580 // RFC 6455 §4.2.2 handshake: the server proves it understood the upgrade by
1581 // concatenating the client's Sec-WebSocket-Key with the fixed magic GUID
1582 // 258EAFA5-E914-47DA-95CA-C5AB0DC85B11, SHA-1 hashing the result and
1583 // returning it base64-encoded as Sec-WebSocket-Accept in a 101 reply.
1584
1585 if (!key || !strlen(key))
1586 return false;
1587
1588 std::string magicString = utils::StringFormat("%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key);
1589 hash::SHA1 sha1;
1590 sha1.reset();
1591 sha1.add(magicString.c_str(), magicString.size());
1592 std::string rawMagic = sha1.getHashRaw();
1593
1594 std::string acceptKey = base64_encode(rawMagic);
1595
1596 if (data)
1597 delete [] data;
1598 data = new char[4096];
1599 snprintf(data, 4096,
1600 "%s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n",
1601 HTTP_Status[HTTP_SWITCH_PROTOCOL], acceptKey.c_str());
1602
1603 headerLength = (uint32)strlen(data);
1604 contentLength = 0;
1605 return true;
1606}
1607
1608bool HTTPReply::createPage(uint8 status, uint64 time, const char* serverName,
1609 uint64 lastMod, bool keepAlive, bool cache, const char* contentType,
1610 const char* content, uint32 contentSize, const char* additionalHeaderEntries) {
1611
1612 char timeString[1024];
1613 if (!GetHTTPTime(time, timeString, 1024))
1614 timeString[0] = 0;
1615
1616 contentLength = (contentSize > 0) ? contentSize : (uint32)strlen(content);
1617
1618 if (data)
1619 delete [] data;
1620 data = new char[4096 + contentLength];
1621 if (additionalHeaderEntries && strlen(additionalHeaderEntries)) {
1622 snprintf(data, 4096 + contentLength,
1623 "%s\r\nDate: %s\r\nServer: %s\r\n"
1624 "X-Frame-Options: SAMEORIGIN\r\n"
1625 "Last-Modified: %s\r\nContent-Length: %u\r\n"
1626 "Connection: %s\r\nContent-Type: %s\r\nCache-Control: %s\r\n"
1627 "%s\r\n\r\n",
1628 HTTP_Status[status], timeString, serverName, timeString, contentLength,
1629 keepAlive ? "Keep-Alive" : "close", contentType, cache ? "Public" : "No-Cache",
1630 additionalHeaderEntries);
1631 }
1632 else {
1633 snprintf(data, 4096 + contentLength,
1634 "%s\r\nDate: %s\r\nServer: %s\r\n"
1635 "X-Frame-Options: SAMEORIGIN\r\n"
1636 "Last-Modified: %s\r\nContent-Length: %u\r\n"
1637 "Connection: %s\r\nContent-Type: %s\r\nCache-Control: %s\r\n\r\n",
1638 HTTP_Status[status], timeString, serverName, timeString, contentLength,
1639 keepAlive ? "Keep-Alive" : "close", contentType, cache ? "Public" : "No-Cache");
1640 }
1641
1642 headerLength = (uint32)strlen(data);
1643 memcpy(data+headerLength, content, contentLength);
1645
1646 return true;
1647}
1648
1649bool HTTPReply::createFromFile(uint64 time, const char* serverName,
1650 uint64 ifLastMod, bool keepAlive, bool cache,
1651 const char* filename) {
1652
1653 std::string actualFilename = filename;
1654 const char* question = strstr(filename, "?");
1655 if (question)
1656 actualFilename = std::string(filename, question - filename);
1657
1658 uint64 lastModifiedTime = 0;
1659 if (ifLastMod) {
1660 utils::FileDetails fileInfo = utils::GetFileDetails(actualFilename.c_str());
1661 if (fileInfo.doesExist && !fileInfo.isDirectory && (fileInfo.lastModifyTime < ifLastMod) )
1662 return createErrorPage(HTTP_USE_LOCAL_COPY, time, serverName, keepAlive);
1663 }
1664
1665 char* filedata = NULL;
1666 uint32 datasize = 0;
1667 std::string html;
1668 bool isBinary = false;
1669 char* exttype = new char[1024];
1670 if (!html::GetMimeType(exttype, actualFilename.c_str(), 1024, isBinary)) {
1671 html = utils::StringFormat("Unsupported mime type for file '%s'...", actualFilename.c_str());
1672 }
1673 else if (!isBinary) {
1674 if (!(html = utils::ReadAFileString(actualFilename.c_str())).size()) {
1675 //html = utils::StringFormat("Could read file '%s'", actualFilename.c_str());
1676 }
1677 }
1678 else {
1679 if ( !(filedata = utils::ReadAFile(actualFilename.c_str(), datasize, true))) {
1680 //html = utils::StringFormat("Could read file '%s'", actualFilename.c_str());
1681 }
1682 }
1683
1684 if (filedata)
1685 createPage(HTTP_OK, GetTimeNow(), serverName, 0, keepAlive, cache, exttype, filedata, datasize);
1686 else if (html.length())
1687 createPage(HTTP_OK, GetTimeNow(), serverName, 0, keepAlive, cache, exttype, html.c_str());
1688 else {
1689 html = "<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>";
1690 createPage(HTTP_FILE_NOT_FOUND, GetTimeNow(), serverName, 0, keepAlive, cache, "text/html", html.c_str());
1691 }
1692
1693 delete [] filedata;
1694 delete [] exttype;
1695
1696 return true;
1697}
1698
1701 return false;
1702
1703 // Connection is a token list ("keep-alive, Upgrade" from Firefox and some proxies): look for the token
1704 const char* connection = this->getHeaderEntry("Connection");
1705 if (!connection)
1706 return false;
1707 std::string c = connection;
1708 for (size_t i = 0; i < c.size(); i++) c[i] = (char)tolower((unsigned char)c[i]);
1709 bool hasUpgrade = false;
1710 size_t p0 = 0;
1711 while (p0 <= c.size()) {
1712 size_t p1 = c.find(',', p0);
1713 std::string tok = c.substr(p0, p1 == std::string::npos ? std::string::npos : p1 - p0);
1714 size_t a = tok.find_first_not_of(" \t"), b = tok.find_last_not_of(" \t");
1715 if (a != std::string::npos && tok.substr(a, b - a + 1) == "upgrade") { hasUpgrade = true; break; }
1716 if (p1 == std::string::npos) break;
1717 p0 = p1 + 1;
1718 }
1719 if (!hasUpgrade)
1720 return false;
1721
1722 const char* upgrade = this->getHeaderEntry("Upgrade");
1723 if (!upgrade || stricmp(upgrade, "websocket"))
1724 return false;
1725
1726 return true;
1727}
1728
1729
1730// Reading it in from a socket
1731bool HTTPReply::processHeader(const char* buffer, uint32 size, bool &isInvalid) {
1732 isInvalid = false;
1733 if (!buffer || (size < 5))
1734 return false;
1735
1736 // Find end of header
1737 uint32 n;
1738 for (n = 3; n < size; n++) {
1739 if (buffer[n] == 10) {
1740 if ( (size-n >= 2) && (buffer[n+1] == 10) ) {
1741 // Just using LF, no CR
1742 headerLength = n+2;
1743 break;
1744 }
1745 else if ( (size-n >= 3) && (buffer[n+1] == 13) && (buffer[n+2] == 10) && (buffer[n-1] == 13) ) {
1746 // Using CRLF
1747 headerLength = n+3;
1748 break;
1749 }
1750 }
1751 }
1752
1753 if (headerLength == 0) {
1754 isInvalid = true;
1755 return false;
1756 }
1757
1758 uint32 tempContentLength = 4096;
1759 data = new char[headerLength+tempContentLength+1];
1760 memcpy(data, buffer, headerLength);
1761 data[headerLength] = 0; // temp string end
1762
1763 // Now process header entries
1764 std::string key, val, uri;
1765 const char* s = data, *t;
1766 n = 0;
1767 uint32 crSize = 0;
1768 uint32 left = headerLength;
1769 while (n < headerLength) {
1770 if (!utils::GetNextLineEnd(s, left, n, crSize))
1771 break;
1772 // Process line
1773 //if (t = strchr(s, ' ')) {
1774 if ((t = strchr(s, ' ')) && (t - s < (int32)n)) {
1775 t -= 1;
1776 if (*t != ':') {
1777 t++;
1778 key.assign(s, t-s);
1779 }
1780 else {
1781 key.assign(s, t-s);
1782 t++;
1783 }
1784 while (*t == 32) t++;
1785 if (*t > 32)
1786 val.assign(t, n+s-t);
1787 else
1788 val = "";
1789 }
1790 else {
1791 key.assign(s, n);
1792 val = "";
1793 }
1794 // Record entry
1795 if (s == data) {
1796 entries["http_protocol"] = key;
1797 if (val.find("200") != val.npos)
1798 type = HTTP_OK;
1799 else if (val.find("101") != val.npos)
1801 else if (val.find("301") != val.npos)
1803 else if (val.find("304") != val.npos)
1805 else if (val.find("403") != val.npos)
1807 else if (val.find("404") != val.npos)
1809 else if (val.find("500") != val.npos)
1811 // 400/401/501/503/504 used to fall through to atoi("504 Gateway Timeout") = 504,
1812 // which wraps to 248 in the uint8 status id and matches no HTTP_* constant.
1813 else if (val.find("400") != val.npos)
1815 else if (val.find("401") != val.npos)
1817 else if (val.find("501") != val.npos)
1819 else if (val.find("503") != val.npos)
1821 else if (val.find("504") != val.npos)
1823 else
1824 type = atoi(val.c_str());
1825 }
1826 else {
1827 entries[key] = val;
1828 // Check entry
1829 if (stricmp(key.c_str(), "content-length") == 0)
1830 contentLength = atoi(val.c_str());
1831 else if (stricmp(key.c_str(), "transfer-encoding") == 0) {
1832 // Body arrives as HTTP/1.1 chunked framing (no Content-Length)
1833 std::string te = val;
1834 for (size_t c = 0; c < te.length(); c++) te[c] = (char)tolower((unsigned char)te[c]);
1835 if (te.find("chunked") != std::string::npos)
1836 chunked = true;
1837 }
1838 else if (stricmp(key.c_str(), "date") == 0)
1839 time = GetTimeFromString(val.c_str());
1840 else if (stricmp(key.c_str(), "connection") == 0)
1841 keepAlive = (stricmp(val.c_str(), "close") != 0);
1842 }
1843 // Next line
1844 s += n + crSize;
1845 left -= n + crSize;
1846 }
1847
1848 if (contentLength > tempContentLength) {
1849 delete [] data;
1850 data = new char[headerLength+contentLength+1];
1851 memcpy(data, buffer, headerLength);
1852 data[headerLength] = 0; // temp string end
1854 }
1855
1856 return true;
1857}
1858
1859bool HTTPReply::processContent(const char* buffer, uint32 size) {
1860 if (!buffer || (size != contentLength))
1861 return false;
1862 memcpy(data+headerLength, buffer, size);
1864 return true;
1865}
1866
1867bool HTTPReply::setDecodedContent(const char* buffer, uint32 size) {
1868 if (!data || !headerLength)
1869 return false;
1870 char* newData = new char[headerLength+size+1];
1871 memcpy(newData, data, headerLength);
1872 if (size && buffer)
1873 memcpy(newData+headerLength, buffer, size);
1874 newData[headerLength+size] = 0;
1875 delete [] data;
1876 data = newData;
1877 contentLength = size;
1878 return true;
1879}
1880
1881const char* HTTPReply::getContent(uint32& size) {
1882 size = contentLength;
1883 return data+headerLength;
1884}
1885
1886const char* HTTPReply::getRawContent(uint32& size) {
1887 size = contentLength + headerLength;
1888 return data;
1889}
1890
1891const char* HTTPReply::getHeaderEntry(const char* entry) {
1892 std::map<std::string, std::string>::iterator it = entries.find(entry);
1893 if (it != entries.end())
1894 return it->second.c_str();
1895 else
1896 return NULL;
1897}
1898
1900 return getHeaderEntry("http_protocol");
1901}
1902
1904 if (data)
1905 return headerLength + contentLength;
1906 else
1907 return 0;
1908}
1909
1910
1912// HTTP Server Protocol
1914
1915bool HTTPProtocol::CheckBufferForCompatibility(const char* buffer, uint32 length) {
1916 if (length < 4)
1917 return false;
1918 return ( utils::stristr(buffer, "GET ") == buffer ||
1919 utils::stristr(buffer, "PUT ") == buffer ||
1920 utils::stristr(buffer, "POST ") == buffer ||
1921 utils::stristr(buffer, "DELETE ") == buffer ||
1922 utils::stristr(buffer, "OPTIONS ") == buffer ||
1923 utils::stristr(buffer, "HEAD ") == buffer );
1924}
1925
1927 return false;
1928}
1929
1931 if (!con || !reply)
1932 return false;
1933 //return con->send((char*)reply->data, reply->getSize());
1934 if (con->send((char*)reply->data, reply->getSize())) {
1935 LogPrint(0, LOG_NETWORK, 4, "Replying to HTTP request with %u bytes", reply->getSize());
1936 // printf("--- HTTP sent reply (%u)...\n", reply->contentLength);
1937 return true;
1938 }
1939 else {
1940 LogPrint(0, LOG_NETWORK, 2, "Failed to send reply to HTTP request with %u bytes", reply->getSize());
1941 // printf("--- HTTP failed to send reply (%u)...\n", reply->contentLength);
1942 return false;
1943 }
1944
1945}
1946
1948
1949 if (!con || !con->waitForDataToRead(timeout))
1950 return NULL;
1951 // return HTTPReply::CreateErrorReply(HTTP_SERVER_UNAVAILABLE);
1952
1953 uint32 maxSize = 4096, size;
1954 char* buffer = new char[maxSize];
1955
1956 uint64 start = GetTimeNow();
1957 uint32 wait = 0;
1958
1959 bool isInvalid = false;
1960 bool sawData = false;
1961 HTTPReply* reply = NULL;
1962 while (true) {
1963 if (!con->receiveAvailable(buffer, size, maxSize, wait, true)) {
1964 delete [] buffer;
1966 }
1967 if (size > 0) {
1968 sawData = true;
1969 reply = new HTTPReply(con->getRemoteAddress());
1970 if (reply->processHeader(buffer, size, isInvalid))
1971 break;
1972 delete(reply);
1973 reply = NULL;
1974 if (isInvalid) {
1975 // ##############
1976 }
1977 }
1978 if (GetTimeAgeMS(start) >= (int32)timeout) {
1979 delete [] buffer;
1980 // No application data at all inside the window. This happens on
1981 // spurious readability wakeups (e.g. TLS 1.3 session tickets that
1982 // carry no application bytes): it is not a protocol error, there is
1983 // simply nothing to read yet, so let the caller poll again instead
1984 // of queueing a NOREPLY error reply for a response still in flight.
1985 if (!sawData)
1986 return NULL;
1988 }
1989 wait = 20;
1990 }
1991
1992 // Now we know how much to actually expect
1993 // First read the header proper and ignore
1994 if (!con->discard(reply->headerLength)) {
1995 delete [] buffer;
1996 delete(reply);
1998 }
1999
2000// printf("Receiving HTTPReply content (%u)...\n\n", reply->contentLength);
2001 // Then read the rest of the content
2002 if (reply->chunked) {
2003 // HTTP/1.1 chunked transfer encoding: repeated <hex-size>[;ext]CRLF <data>CRLF,
2004 // terminated by a zero-size chunk. Trailers (if any) are left unread; the
2005 // client paths using this function tear the connection down afterwards.
2006 std::string body;
2007 char peekBuf[4096];
2008 uint64 chunkStart = GetTimeNow();
2009 bool failed = false;
2010 while (true) {
2011 // Read the chunk-size line: peek until a LF is visible, then consume it
2012 std::string line;
2013 while (true) {
2014 uint32 avail = 0;
2015 if (!con->receiveAvailable(peekBuf, avail, sizeof(peekBuf), 20, true)) {
2016 failed = true;
2017 break;
2018 }
2019 const char* lf = avail ? (const char*)memchr(peekBuf, '\n', avail) : NULL;
2020 if (lf) {
2021 line.assign(peekBuf, (size_t)(lf-peekBuf));
2022 con->discard((uint32)(lf-peekBuf)+1);
2023 break;
2024 }
2025 if (GetTimeAgeMS(chunkStart) >= 30000) {
2026 failed = true;
2027 break;
2028 }
2029 }
2030 if (failed)
2031 break;
2032 while (line.length() && (line[line.length()-1] == '\r'))
2033 line.erase(line.length()-1);
2034 uint32 chunkSize = (uint32)strtoul(line.c_str(), NULL, 16);
2035 if (!chunkSize)
2036 break; // final chunk
2037 char* chunkBuf = new char[chunkSize+2]; // chunk data + trailing CRLF
2038 if (!con->receive(chunkBuf, chunkSize+2, 10000)) {
2039 delete [] chunkBuf;
2040 failed = true;
2041 break;
2042 }
2043 body.append(chunkBuf, chunkSize);
2044 delete [] chunkBuf;
2045 if (GetTimeAgeMS(chunkStart) >= 30000) {
2046 failed = true;
2047 break;
2048 }
2049 }
2050 if (failed || !reply->setDecodedContent(body.data(), (uint32)body.length())) {
2051 printf("Error receiving chunked HTTPReply content (%u so far)...\n\n", (uint32)body.length());
2052 delete [] buffer;
2053 delete(reply);
2055 }
2056 }
2057 else if (reply->contentLength) {
2058 if (reply->contentLength > maxSize - reply->headerLength) {
2059 delete [] buffer;
2060 buffer = new char[reply->contentLength];
2061 // We have to read the rest, otherwise we will mess up the connection
2062 if (!con->receive(buffer, reply->contentLength, 3000)) {
2063 printf("Error receiving HTTPReply content (%u)...\n\n", reply->contentLength);
2064 delete [] buffer;
2065 delete(reply);
2067 }
2068
2069 if (!reply->processContent(buffer, reply->contentLength)) {
2070 delete [] buffer;
2071 delete(reply);
2073 }
2074 }
2075 else {
2076 // We have to read the rest, otherwise we will mess up the connection
2077 if (!con->receive(buffer, reply->contentLength, 3000)) {
2078 printf("Error receiving HTTPReply content (%u)...\n\n", reply->contentLength);
2079 delete [] buffer;
2080 delete(reply);
2082 }
2083
2084 if (!reply->processContent(buffer, reply->contentLength)) {
2085 delete [] buffer;
2086 delete(reply);
2088 }
2089 }
2090 }
2091
2092// printf("Got HTTPReply...\n\n");
2093 delete [] buffer;
2094 return reply;
2095}
2096
2098 if (!con || !req)
2099 return false;
2100 return con->send((char*)req->data, req->getSize());
2101}
2102
2104
2105 // ##########################
2106
2107 if (!con || !con->waitForDataToRead(timeout)) {
2108 return NULL;
2109 }
2110
2111 uint32 maxSize = 4096, size;
2112 char* buffer = new char[maxSize];
2113
2114 uint64 start = GetTimeNow();
2115 uint64 startReceive = 0;
2116 uint32 wait = 0;
2117
2118 bool isInvalid;
2119 HTTPRequest* req = NULL;
2120 while (true) {
2121 if (!con->receiveAvailable(buffer, size, maxSize, wait, true)) {
2122 delete [] buffer;
2123 return NULL;
2124 }
2125 if (size > 0) {
2126 LogPrint(0, LOG_NETWORK, 5, "Started receiving incoming HTTP request, size so far %u", size);
2127 if (!startReceive)
2128 startReceive = GetTimeNow();
2129 req = new HTTPRequest(con->getRemoteAddress(), startReceive);
2130 //utils::PrintBinary(buffer, size, false, "HTTP: ");
2131 if (req->processHeader(buffer, size, isInvalid)) {
2132 //printf("HTTP Done\n");
2133 break;
2134 }
2135 else if (isInvalid) {
2136 LogPrint(0, LOG_NETWORK, 2, "Received invalid incoming HTTP request, size %u", size);
2137 //utils::WriteAFile("d:\\http_invalid.bin", buffer, size, true);
2138 //DataMessage* test = new DataMessage(buffer);
2139 delete [] buffer;
2140 con->discard(size);
2141 return req;
2142 }
2143 else if (size == maxSize) {
2144 LogPrint(0, LOG_NETWORK, 2, "Received incoming HTTP request, buffer needs resizing %u", size);
2145 // maxSize isn't big enough to satisfy processHeader
2146 //printf("HTTP MaxSize, waiting for more...\n");
2147 //utils::PrintBinary(buffer, size, false, "HTTP MaxSize: ");
2148 //utils::WriteAFile("d:\\http_maxsize.bin", buffer, size, true);
2149 utils::Sleep(10);
2150 }
2151 else {
2152 LogPrint(0, LOG_NETWORK, 5, "Received incoming HTTP request, got %u, waiting for more...", size);
2153 //utils::PrintBinary(buffer, size, false, "HTTP WaitMore: ");
2154 //printf("HTTP waiting for more...\n");
2155 //utils::WriteAFile("d:\\http_notenough.bin", buffer, size, true);
2156 utils::Sleep(10);
2157 }
2158 delete(req);
2159 req = NULL;
2160 }
2161 // give up only once the timeout has passed: the test was inverted, so a request whose header came in more than
2162 // one piece (common on Windows) was dropped at once (wsservice/httpservice on Windows, 16 Nov 2026)
2163 if (GetTimeAgeMS(start) >= (int32)timeout) {
2164 delete [] buffer;
2165 return NULL;
2166 }
2167 wait = 20;
2168 }
2169
2170 // Now we know how much to actually expect
2171 // First read the header proper and ignore
2172 if (!con->discard(req->headerLength)) {
2173 delete [] buffer;
2174 delete(req);
2175 return NULL;
2176 }
2177 // Then read the rest of the content
2178 if (req->contentLength) {
2179 if (req->contentLength > maxSize) {
2180 delete [] buffer;
2181 buffer = new char[req->contentLength];
2182 }
2183 // We have to read the rest, otherwise we will mess up the connection
2184 if (!con->receive(buffer, req->contentLength, 30000)) {
2185 LogPrint(0, LOG_NETWORK, 2, "Received HTTP header of %u, couldn't read the content of size %u", req->headerLength, req->contentLength);
2186 delete [] buffer;
2187 delete(req);
2188 return NULL;
2189 }
2190
2191 if (!req->processContent(buffer, req->contentLength)) {
2192 LogPrint(0, LOG_NETWORK, 2, "Received HTTP header of %u, content of size %u, failed processing the content buffer", req->headerLength, req->contentLength);
2193 delete [] buffer;
2194 delete(req);
2195 return NULL;
2196 }
2197 }
2198
2199 delete [] buffer;
2200 return req;
2201}
2202
2203
2204
2206
2207 if (!con || !con->waitForDataToRead(timeout)) {
2208 return NULL;
2209 }
2210
2211 uint32 maxSize = 4096, size;
2212 char* buffer = new char[maxSize];
2213
2214 uint64 start = GetTimeNow();
2215 uint64 startReceive = 0;
2216 uint32 wait = 0;
2217 uint64 remoteAddress;
2218 bool isInvalid;
2219 WebsocketData* wsData = NULL;
2220 while (!wsData || !wsData->isComplete()) {
2221 while (true) {
2222 if (!wsData && !con->receiveAvailable(buffer, size, maxSize, wait, true)) {
2223 delete[] buffer;
2224 return NULL;
2225 }
2226 else
2227 con->receiveAvailable(buffer, size, maxSize, wait, true);
2228 if (size > 0) {
2229 //utils::WriteAFile("d:/c/ws.bin", buffer, size, true);
2230 if (!wsData) {
2231 if (!startReceive)
2232 startReceive = GetTimeNow();
2233 wsData = new WebsocketData(con->getRemoteAddress(), startReceive);
2234 //utils::PrintBinary(buffer, size, false, "HTTP: ");
2235 }
2236
2237 if (wsData->processHeader(buffer, size, isInvalid)) {
2238 //printf("HTTP Done\n");
2239 break;
2240 }
2241 else if (isInvalid) {
2242 remoteAddress = con->getRemoteAddress();
2243 LogPrint(0, LOG_NETWORK, 1, "Invalid data received on Websocket from %u.%u.%u.%u", GETIPADDRESSQUAD(remoteAddress));
2244 //utils::WriteAFile("d:\\http_invalid.bin", buffer, size, true);
2245 //DataMessage* test = new DataMessage(buffer);
2246 delete[] buffer;
2247 con->discard(size);
2248 return wsData;
2249 }
2250 else if (size == maxSize) {
2251 // maxSize isn't big enough to satisfy processHeader
2252 //printf("HTTP MaxSize, waiting for more...\n");
2253 //utils::PrintBinary(buffer, size, false, "HTTP MaxSize: ");
2254 //utils::WriteAFile("d:\\http_maxsize.bin", buffer, size, true);
2255 utils::Sleep(10);
2256 }
2257 else {
2258 //utils::PrintBinary(buffer, size, false, "HTTP WaitMore: ");
2259 //printf("HTTP waiting for more...\n");
2260 //utils::WriteAFile("d:\\http_notenough.bin", buffer, size, true);
2261 utils::Sleep(10);
2262 }
2263 delete(wsData);
2264 wsData = NULL;
2265 }
2266 // (was "<": a frame whose header came in pieces was dropped as a timeout at once)
2267 if (!wsData && (GetTimeAgeMS(start) >= (int32)timeout)) {
2268 remoteAddress = con->getRemoteAddress();
2269 LogPrint(0, LOG_NETWORK, 1, "Timeout receiving data on Websocket from %u.%u.%u.%u", GETIPADDRESSQUAD(remoteAddress));
2270 delete[] buffer;
2271 return NULL;
2272 }
2273 wait = 20;
2274 }
2275
2276 // Now we know how much to actually expect
2277 // First read the header proper and ignore
2278 if (!con->discard(wsData->headerLength)) {
2279 delete[] buffer;
2280 delete(wsData);
2281 return NULL;
2282 }
2283 size -= wsData->headerLength;
2284 // Then read the rest of the content
2285 if (wsData->payloadSize) {
2286 if (wsData->payloadSize > maxSize) {
2287 delete[] buffer;
2288 buffer = new char[(uint32)(wsData->payloadSize)];
2289 }
2290 // We have to read the rest, otherwise we will mess up the connection
2291 if (!con->receive(buffer, (uint32)wsData->payloadSize, 30000)) {
2292 remoteAddress = con->getRemoteAddress();
2293 LogPrint(0, LOG_NETWORK, 1, "Unable to receive payload data on Websocket from %u.%u.%u.%u", GETIPADDRESSQUAD(remoteAddress));
2294 delete[] buffer;
2295 delete(wsData);
2296 return NULL;
2297 }
2298
2299 if (!wsData->processContent(buffer, wsData->payloadSize)) {
2300 remoteAddress = con->getRemoteAddress();
2301 LogPrint(0, LOG_NETWORK, 1, "Unable to process payload data on Websocket from %u.%u.%u.%u", GETIPADDRESSQUAD(remoteAddress));
2302 delete[] buffer;
2303 delete(wsData);
2304 return NULL;
2305 }
2306 size -= (uint32)(wsData->payloadSize);
2307 }
2308 if (wsData->isComplete()) {
2309 break;
2310 }
2311 }
2312
2313 delete[] buffer;
2314 return wsData;
2315}
2316
2318 if (!con || !wsData)
2319 return false;
2320 uint64 size = 0;
2321 const char* data = wsData->getRawData(size);
2322 if (!data || !size)
2323 return false;
2324 //return con->send((char*)reply->data, reply->getSize());
2325 if (con->send(data, (uint32)size)) {
2326 // printf("--- HTTP sent reply (%u)...\n", reply->contentLength);
2327 return true;
2328 }
2329 else {
2330 // printf("--- HTTP failed to send reply (%u)...\n", reply->contentLength);
2331 return false;
2332 }
2333
2334}
2335
2336
2337
2338
2339
2341// HTTP Client Protocol
2343
2344bool HTTPClientProtocol::CheckBufferForCompatibility(const char* buffer, uint32 length) {
2345 return false;
2346}
2347
2351
2353 return false;
2354}
2355
2357 return NULL;
2358}
2359
2361 return false;
2362}
2363
2365 return NULL;
2366}
2367
2368
2369
2371// Message Protocol
2373
2374bool MessageProtocol::CheckBufferForCompatibility(const char* buffer, uint32 length) {
2375 if (length < 2*sizeof(uint32))
2376 return false;
2377 return (GetObjID(buffer) == DATAMESSAGEID) || (GetObjID(buffer) == PACKEDDATAMESSAGEID);
2378}
2379
2383
2384bool MessageProtocol::SendMessage(NetworkConnection* con, DataMessage* msg, uint64 receiver, bool packed) {
2385 if (!con || !msg)
2386 return false;
2387 if (packed) {
2388 uint32 n = 0;
2389 char* pk = msg->toPacked(n);
2390 bool ok = pk && con->send(pk, n, receiver);
2391 free(pk);
2392 return ok;
2393 }
2394// uint64 addr = con->getRemoteAddress();
2395// LogPrint(0,0,0,"Protocol sending msg %llu to %u.%u.%u.%u:%u", msg->getType(), GETIPADDRESSQUAD(addr), GETIPPORT(addr));
2396// LogPrint(0,0,0,"Protocol sending msg: %u", msg->getType()[15]);
2397 return con->send((char*)msg->data, msg->getSize(), receiver);
2398}
2399
2401// uint64 t = GetTimeNow();
2402 if (!con) {
2403// if (!con || !con->waitForDataToRead(timeout)) {
2404// printf("Failed ReceiveMessage: %ld\n", GetTimeAgeMS(t));
2405 return NULL;
2406 }
2407
2408 // Peek just the header (size + object id) into a small stack buffer so the
2409 // body allocation can be sized exactly once. The previous code malloc'd a 1KB
2410 // scratch buffer for every message and then freed and re-malloc'd it for any
2411 // message bigger than 1KB - two allocations plus a free per large message.
2412 char header[2*sizeof(uint32)];
2413 if (!con->receive(header, sizeof(header), timeout, true))
2414 return NULL;
2415 bool packed = (GetObjID(header) == PACKEDDATAMESSAGEID);
2416 if ((GetObjID(header) != DATAMESSAGEID) && !packed)
2417 return NULL;
2418 uint32 size = *(uint32*)header;
2419 if (size < sizeof(header))
2420 return NULL; // implausible size for a DataMessage header
2421
2422 char* buffer = (char*) malloc(size);
2423 if (!buffer)
2424 return NULL;
2425 if (!con->receive(buffer, size, timeout)) {
2426 free(buffer);
2427 return NULL;
2428 }
2429 if (packed) { // the compact form (a phone on mobile data): decode to a regular message
2430 DataMessage* m = DataMessage::FromPacked(buffer, size);
2431 free(buffer);
2432 return m;
2433 }
2434 // DataMessage adopts the buffer (copy=false) and frees it on destruction.
2435 DataMessage* msg = new DataMessage(buffer);
2436// uint64 addr = con->getRemoteAddress();
2437// LogPrint(0,0,0,"Protocol received msg %llu from %u.%u.%u.%u:%u", msg->getType(), GETIPADDRESSQUAD(addr), GETIPPORT(addr));
2438 //printf("Protocol received msg: %u.%u.%u (%p)...\n", msg->getType()[0], msg->getType()[1], msg->getType()[2], msg);
2439 return msg;
2440}
2441
2442
2444// Telnet Protocol
2446
2447bool TelnetProtocol::CheckBufferForCompatibility(const char* buffer, uint32 length) {
2448 if (length < 1)
2449 return false;
2450 return ((buffer[0] == 13) || (buffer[0] == 10));
2451}
2452
2456
2458 if (!con || !line)
2459 return false;
2460 return con->send((char*)line->data, line->getSize());
2461}
2462
2464 if (!con || !con->waitForDataToRead(timeout))
2465 return NULL;
2466
2467 uint32 maxSize = 1024, size;
2468 char* buffer = new char[maxSize];
2469 TelnetLine* line = NULL;
2470
2471 uint64 start = GetTimeNow();
2472 uint32 wait = 0, n;
2473
2474 while (GetTimeAgeMS(start) < (int32)timeout) {
2475 if (!con->receiveAvailable(buffer, size, maxSize, wait, true)) {
2476 delete [] buffer;
2477 return NULL;
2478 }
2479 for ( n=0; n<size; n++) {
2480 if (buffer[n] == 13) {
2481 line = new TelnetLine(con->getRemoteAddress());
2482 line->setCR(TELNET_WINDOWS);
2483 line->setLine(buffer, n);
2484 if ( (n < size-1) && (buffer[n+1] == 10) )
2485 n++;
2486 con->discard(n+1);
2487 delete [] buffer;
2488 // printf("\n");
2489 return line;
2490 }
2491 else if (buffer[n] == 10) {
2492 line = new TelnetLine(con->getRemoteAddress());
2493 line->setCR(TELNET_UNIX);
2494 line->setLine(buffer, n);
2495 if ( (n < size-1) && (buffer[n+1] == 13) )
2496 n++;
2497 con->discard(n+1);
2498 delete [] buffer;
2499 // printf("\n");
2500 return line;
2501 }
2502 //else
2503 // printf("[%c]", buffer[n]);
2504 }
2505 if (size == maxSize) {
2506 delete [] buffer;
2507 maxSize *= 4;
2508 buffer = new char[maxSize];
2509 wait = 0;
2510 }
2511 else
2512 wait = 20;
2513 }
2514 delete [] buffer;
2515 return NULL;
2516}
2517
2519 if (!con || !con->waitForDataToRead(timeout))
2520 return NULL;
2521
2522 char* buffer = new char[size];
2523
2524 if (!con->receive(buffer, size, timeout)) {
2525 delete [] buffer;
2526 return NULL;
2527 }
2528
2529 TelnetLine* reply = new TelnetLine(con->getRemoteAddress());
2530 reply->data = buffer;
2531 reply->size = size;
2532 return reply;
2533}
2534
2535
2536
2537
2538
2539
2541 jmData = NULL;
2542 jmSize = mOffset = 0;
2543}
2544
2545JSONM::JSONM(const char* data, uint64 size) {
2546 jmSize = size;
2547 if (jmSize) {
2548 jmData = new char[(uint32)jmSize];
2549 memcpy(jmData, data, (uint32)jmSize);
2550 mOffset = strlen(data) + 1;
2551 extractMultipartInfo();
2552 }
2553 else
2554 jmData = NULL;
2555}
2556
2557JSONM::JSONM(const char* json) {
2558 jmData = NULL;
2559 jmSize = 0;
2560 if (json)
2561 setJSON(json);
2562 else
2563 setJSON("{}");
2564}
2565
2567 mutex.enter(1000);
2568 if (jmData)
2569 delete[] jmData;
2570 jmData = NULL;
2571 jmSize = 0;
2572 mOffset = 0;
2573}
2574
2576 if (!mutex.enter(1000)) return 0;
2577 uint64 s = jmSize;
2578 mutex.leave();
2579 return s;
2580}
2582 if (!mutex.enter(1000)) return 0;
2583 uint32 s = (uint32)entries.size();
2584 mutex.leave();
2585 return s;
2586}
2587
2589 if (!jmData || !jmSize)
2590 return false;
2591 if (!strchr(jmData, '{') || !utils::laststrstr(jmData, "}"))
2592 return false;
2593 // Check that the sizes add up to the correct full size
2594 uint64 jsonLength = strlen(jmData);
2595 uint64 multipartLength = 0;
2596 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2597 while (i != e) {
2598 multipartLength += i->second.size;
2599 i++;
2600 }
2601 if (jsonLength + 1 + multipartLength != jmSize) {
2602 LogPrint(0, LOG_NETWORK, 0, "Invalid JSONM structure: JSON(%llu) + 1 + multiparts(%llu) != %llu", jsonLength, multipartLength, jmSize);
2603 return false;
2604 }
2605 return true;
2606}
2607
2608std::string JSONM::getJSON() {
2609 if (!jmData || !jmSize)
2610 return "";
2611 if (!strchr(jmData, '{') || !utils::laststrstr(jmData, "}"))
2612 return "";
2613 if (strlen(jmData) > jmSize)
2614 return std::string(jmData, jmSize);
2615 return jmData;
2616}
2617
2618const char* JSONM::getData(const char* name, uint64& size) {
2619 if (!mutex.enter(1000)) return NULL;
2620 size = 0;
2621 if (!name || !jmData || !jmSize) {
2622 mutex.leave();
2623 return NULL;
2624 }
2625 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2626 while (i != e) {
2627 if (stricmp(i->second.name.c_str(), name) == 0) {
2628 if (mOffset + i->second.offset + i->second.size > jmSize) {
2629 LogPrint(0, LOG_NETWORK, 0, "Invalid JSONM structure: Entry '%s' offset %llu size %llu exceeds the JSONM size %llu",
2630 name, i->second.offset, i->second.size, jmSize);
2631 mutex.leave();
2632 return NULL;
2633 }
2634 size = i->second.size;
2635 mutex.leave();
2636 return jmData + mOffset + i->second.offset;
2637 }
2638 i++;
2639 }
2640 mutex.leave();
2641 return NULL;
2642}
2643
2644std::string JSONM::getDataType(const char* name) {
2645 if (!mutex.enter(1000)) return "";
2646 if (!name || !jmData || !jmSize) {
2647 mutex.leave();
2648 return NULL;
2649 }
2650 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2651 while (i != e) {
2652 if (stricmp(i->second.name.c_str(), name) == 0) {
2653 mutex.leave();
2654 return i->second.type;
2655 }
2656 i++;
2657 }
2658 mutex.leave();
2659 return "";
2660}
2661
2662std::string JSONM::getDataType(uint32 chunk) {
2663 if (!mutex.enter(1000)) return NULL;
2664 if (!jmData || !jmSize || !chunk)
2665 return "";
2666
2667 std::map<uint32, JSONMEntry>::iterator i = entries.find(chunk);
2668 if (i == entries.end()) {
2669 mutex.leave();
2670 return "";
2671 }
2672
2673 mutex.leave();
2674 return i->second.type;
2675}
2676
2677std::string JSONM::getDataName(uint32 chunk) {
2678 if (!mutex.enter(1000)) return NULL;
2679 if (!jmData || !jmSize || !chunk) {
2680 mutex.leave();
2681 return "";
2682 }
2683
2684 std::map<uint32, JSONMEntry>::iterator i = entries.find(chunk);
2685 if (i == entries.end()) {
2686 mutex.leave();
2687 return "";
2688 }
2689
2690 mutex.leave();
2691 return i->second.name;
2692}
2693
2694
2695
2696const char* JSONM::getData(uint32 chunk, uint64& size) {
2697 if (!mutex.enter(1000)) return NULL;
2698 size = 0;
2699 if (!jmData || !jmSize) {
2700 mutex.leave();
2701 return NULL;
2702 }
2703
2704 std::map<uint32, JSONMEntry>::iterator i = entries.find(chunk);
2705 if (i == entries.end()) {
2706 mutex.leave();
2707 return NULL;
2708 }
2709
2710 if (mOffset + i->second.offset + i->second.size > jmSize) {
2711 LogPrint(0, LOG_NETWORK, 0, "Invalid JSONM structure: Entry %u '%s' offset %llu size %llu exceeds the JSONM size %llu",
2712 chunk, i->second.name.c_str(), i->second.offset, i->second.size, jmSize);
2713 mutex.leave();
2714 return NULL;
2715 }
2716
2717 size = i->second.size;
2718 mutex.leave();
2719 return jmData + mOffset + i->second.offset;
2720}
2721
2722bool JSONM::setRawJSON(const char* json) {
2723 if (!json)
2724 return false;
2725 if (!jmData) {
2726 jmSize = mOffset = strlen(json) + 1;
2727 jmData = new char[(uint32)jmSize];
2728 utils::strcpyavail(jmData, json, (uint32)jmSize, true);
2729 return true;
2730 }
2731 // else we have JSON and possibly data already
2732 uint64 newMOffset = strlen(json) + 1;
2733 uint64 newSize = newMOffset + (jmSize - mOffset);
2734 char* newData = new char[(uint32)newSize];
2735 utils::strcpyavail(newData, json, (uint32)newMOffset, true);
2736 // copy in old data
2737 memcpy(newData + (uint32)newMOffset, jmData + (uint32)mOffset, (uint32)(jmSize - mOffset));
2738 delete[] jmData;
2739 jmData = newData;
2740 mOffset = newMOffset;
2741 jmSize = newSize;
2742 return true;
2743}
2744
2745bool JSONM::setJSON(const char* json) {
2746 if (!json)
2747 return false;
2748 if (!mutex.enter(1000)) return false;
2749 if (setRawJSON(json))
2750 updateMultipartJSON();
2751 mutex.leave();
2752 return true;
2753}
2754
2755uint32 JSONM::addData(const char* name, const char* data, uint64 size, const char* type) {
2756 if (!mutex.enter(1000)) return 0;
2757 // check for name in use
2758 uint64 s;
2759 if (getData(name, s)) {
2760 mutex.leave();
2761 return 0;
2762 }
2763 if (!jmData)
2764 setJSON("{}");
2765 JSONMEntry entry;
2766 entry.chunk = getCount() + 1;
2767 entry.name = name;
2768 entry.size = size;
2769 entry.type = type;
2770 entry.offset = jmSize - this->mOffset;
2771 uint64 newSize = jmSize + size;
2772 char* newData = new char[(uint32)newSize];
2773 memcpy(newData, jmData, (uint32)jmSize);
2774 memcpy(newData + (uint32)jmSize, data, (uint32)size);
2775 entries[entry.chunk] = entry;
2776 delete[] jmData;
2777 jmData = newData;
2778 jmSize = newSize;
2779 updateMultipartJSON();
2780 mutex.leave();
2781 return entry.chunk;
2782}
2783
2785 if (!mutex.enter(1000)) return "";
2786 if (!jmData || !jmSize) {
2787 mutex.leave();
2788 return "";
2789 }
2790 std::string info = utils::StringFormat("JSON length: %u\nTotal size: %s\nChunks: %u total: %s\n%s\n",
2791 mOffset - 1,
2792 utils::BytifySize((uint32)jmSize).c_str(),
2793 getCount(),
2794 utils::BytifySize((uint32)(jmSize - mOffset)).c_str(),
2795 jmData);
2796 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2797 while (i != e) {
2798 info += i->second.getInfoString();
2799 i++;
2800 }
2801 mutex.leave();
2802 return info;
2803}
2804
2805
2806
2807
2808bool JSONM::extractMultipartInfo() {
2809 // Assume multipart info is in JSON
2810 // read and create entries from this
2811 // also assume mutex is already locked
2812
2813 if (!jmData || !jmSize || (jmSize - mOffset < 10))
2814 return false;
2815
2816 jsmn_parser parser;
2817 jsmntok_t* tokens = new jsmntok_t[1024];
2818 int tokenCount = 0;
2819 //const char* val;
2820 //int valSize;
2821 jsmn_init(&parser);
2822 if ((tokenCount = jsmn_parse(&parser, jmData, strlen(jmData), tokens, 1024)) <= 0) {
2823 // No JSON found
2824 delete[] tokens;
2825 return false;
2826 }
2827
2828 JSONMEntry entry;
2829 //int token;
2830 std::vector<int> jsonArrayIdx;
2831 std::vector<int>::iterator i, e;
2832 jsonArrayIdx = GetJSONChildArrayIndexes(tokens, tokenCount, jmData, "_multipart_", 0, JSMN_UNDEFINED);
2833 if (!jsonArrayIdx.size()) {
2834 // No info found
2835 delete[] tokens;
2836 return false;
2837 }
2838 i = jsonArrayIdx.begin();
2839 e = jsonArrayIdx.end();
2840
2841 uint64 offset = 0;
2842 while (i != e) {
2843 if ((entry.name = GetJSONChildValueString(tokens, tokenCount, jmData, "name", *i)).length()) {
2844 entry.type = GetJSONChildValueString(tokens, tokenCount, jmData, "mimetype", *i);
2845 entry.size = GetJSONChildValueUint64(tokens, tokenCount, jmData, "bytesize", *i);
2846 entry.chunk = (uint32)entries.size() + 1;
2847 entry.offset = offset;
2848 entries[entry.chunk] = entry;
2849 offset += entry.size;
2850 }
2851 i++;
2852 }
2853 return true;
2854}
2855
2856bool JSONM::updateMultipartJSON() {
2857 // Assume entries and binary chunks updated
2858 // create new JSON entry and replace old one, if present
2859 // also assume mutex is already locked
2860
2861 std::string newJSON;
2862
2863 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2864 while (i != e) {
2865 if (newJSON.length())
2866 newJSON += ",";
2867 newJSON += i->second.toJSON();
2868 i++;
2869 }
2870
2871 jsmn_parser parser;
2872 jsmntok_t* tokens = new jsmntok_t[1024];
2873 int tokenCount = 0;
2874 //const char* val;
2875 //int valSize;
2876 jsmn_init(&parser);
2877 if ((tokenCount = jsmn_parse(&parser, jmData, strlen(jmData), tokens, 1024)) <= 0) {
2878 // No JSON found
2879 delete[] tokens;
2880 return false;
2881 }
2882
2883 std::string fullJSON = jmData;
2884 JSONMEntry entry;
2885 int token = GetJSONToken(tokens, tokenCount, jmData, "_multipart_", 0);
2886 if (token > 0) {
2887 uint32 begin = tokens[token].start;
2888 uint32 end = tokens[token].end;
2889 //uint32 end = tokens[tokens[tokens[token].parent].parent].end;
2890 fullJSON = utils::StringFormat("%s[%s]%s",
2891 fullJSON.substr(0, begin).c_str(),
2892 newJSON.c_str(),
2893 fullJSON.substr(end).c_str()
2894 );
2895 }
2896 else {
2897 // just add at the end
2898 const char* lastBracket = utils::laststrstr(jmData, "}");
2899 if (!lastBracket) {
2900 // Broken JSON found
2901 delete[] tokens;
2902 return false;
2903 }
2904 fullJSON = utils::StringFormat("%s,\"_multipart_\": [%s]}", fullJSON.substr(0, lastBracket-jmData).c_str(), newJSON.c_str());
2905 }
2906 this->setRawJSON(fullJSON.c_str());
2907 return true;
2908}
2909
2911 DataMessage* msg = new DataMessage();
2912 uint64 now = GetTimeNow();
2913
2914 msg->setString("POST", this->getJSON().c_str());
2915 msg->setString("POST_CONTENT_TYPE_", "application/json");
2916
2917 std::map<uint32, JSONMEntry>::iterator i = entries.begin(), e = entries.end();
2918 while (i != e) {
2919 msg->setData(i->second.name.c_str(), jmData + mOffset + i->second.offset, (uint32)i->second.size);
2920 msg->setString((i->second.name + "_CONTENT_TYPE_").c_str(), i->second.type.c_str());
2921 i++;
2922 }
2923 msg->setCreatedTime(now);
2924 msg->setRecvTime(now);
2925 return msg;
2926}
2927
2928
2929// ################# Unit Test #################
2930
2931// Encode a WebsocketData frame, then decode it back through the
2932// processHeader/processContent path and verify the recovered payload matches.
2933// All in-memory, no sockets. Returns true on success.
2934static bool TestWebsocketRoundTrip(WebsocketData::DataType type, bool maskData,
2935 const char* payload, uint64 payloadSize) {
2936
2937 WebsocketData enc;
2938 if (!enc.setData(type, maskData, payload, payloadSize)) {
2939 unittest::fail("WebsocketData::setData returned false (type %d, mask %d, size %llu)",
2940 (int)type, (int)maskData, payloadSize);
2941 return false;
2942 }
2943 if (!enc.isComplete()) {
2944 unittest::fail("encoded WebsocketData is not COMPLETE");
2945 return false;
2946 }
2947
2948 uint64 rawSize = 0;
2949 const char* raw = enc.getRawData(rawSize);
2950 if (!raw || !rawSize) {
2951 unittest::fail("getRawData returned no bytes");
2952 return false;
2953 }
2954 if (rawSize != enc.headerLength + payloadSize) {
2955 unittest::fail("raw size %llu != header %u + payload %llu",
2956 rawSize, enc.headerLength, payloadSize);
2957 return false;
2958 }
2959
2960 // First byte must carry FIN + opcode; opcode must equal the data type.
2961 uint8 b0 = *(const unsigned char*)raw;
2962 if (!(b0 & WebsocketData::BHB0_FIN)) {
2963 unittest::fail("encoded frame missing FIN bit");
2964 return false;
2965 }
2966 if ((b0 & WebsocketData::BHB0_OPCODE) != (uint8)type) {
2967 unittest::fail("encoded opcode %u != type %u", (uint8)(b0 & WebsocketData::BHB0_OPCODE), (uint8)type);
2968 return false;
2969 }
2970
2971 // Decode: split the raw bytes into header + payload, exactly as the socket
2972 // reader would, but feeding from the in-memory buffer.
2973 WebsocketData dec;
2974 bool isInvalid = false;
2975 if (!dec.processHeader(raw, rawSize, isInvalid) || isInvalid) {
2976 unittest::fail("processHeader failed (invalid=%d)", (int)isInvalid);
2977 return false;
2978 }
2979 if (dec.dataType != type) {
2980 unittest::fail("decoded dataType %d != %d", (int)dec.dataType, (int)type);
2981 return false;
2982 }
2983 if (dec.getPayloadSize() != payloadSize) {
2984 unittest::fail("decoded payloadSize %llu != %llu", dec.getPayloadSize(), payloadSize);
2985 return false;
2986 }
2987
2988 if (payloadSize) {
2989 if (!dec.processContent(raw + dec.headerLength, payloadSize)) {
2990 unittest::fail("processContent failed");
2991 return false;
2992 }
2993 uint64 outSize = 0;
2994 const char* out = dec.getContent(outSize);
2995 if (!out || (outSize != payloadSize)) {
2996 unittest::fail("getContent size %llu != %llu", outSize, payloadSize);
2997 return false;
2998 }
2999 if (memcmp(out, payload, (size_t)payloadSize) != 0) {
3000 unittest::fail("decoded payload bytes differ from original");
3001 return false;
3002 }
3003 }
3004 return true;
3005}
3006
3008
3009 // ---- Phase 1: WebsocketData encode/decode round-trips ----
3010 unittest::progress(5, "websocket frames");
3011
3012 const char* shortText = "hello websocket";
3013 if (!TestWebsocketRoundTrip(WebsocketData::TEXT, false, shortText, strlen(shortText)))
3014 return false;
3015 unittest::detail("websocket short unmasked TEXT round-trip ok (%zu bytes)", strlen(shortText));
3016
3017 // Masked frame: exercises the client->server masking path.
3018 if (!TestWebsocketRoundTrip(WebsocketData::TEXT, true, shortText, strlen(shortText)))
3019 return false;
3020 unittest::detail("websocket short masked TEXT round-trip ok");
3021
3022 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, false, shortText, strlen(shortText)))
3023 return false;
3024
3025 unittest::progress(25, "websocket extended frames");
3026
3027 // Extended (16-bit length) payload: > 125 bytes forces the 2-byte length field.
3028 std::string big;
3029 big.reserve(4096);
3030 for (uint32 i = 0; i < 4000; i++)
3031 big.push_back((char)('A' + (i % 26)));
3032 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, false, big.data(), big.size()))
3033 return false;
3034 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, true, big.data(), big.size()))
3035 return false;
3036 unittest::detail("websocket extended (16-bit length) masked+unmasked round-trip ok (%zu bytes)", big.size());
3037
3038 // Jumbo (64-bit length) payload: > 65535 bytes, and the 65535/65536 boundary.
3039 std::string jumbo;
3040 jumbo.reserve(200000);
3041 for (uint32 i = 0; i < 200000; i++)
3042 jumbo.push_back((char)((i * 7) & 0xFF));
3043 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, false, jumbo.data(), jumbo.size()))
3044 return false;
3045 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, true, jumbo.data(), jumbo.size()))
3046 return false;
3047 if (!TestWebsocketRoundTrip(WebsocketData::BINARY, false, jumbo.data(), 65535) ||
3048 !TestWebsocketRoundTrip(WebsocketData::BINARY, true, jumbo.data(), 65536))
3049 return false;
3050 unittest::detail("websocket jumbo (64-bit length) masked+unmasked round-trip ok (%zu bytes, 65535/65536 boundary)", jumbo.size());
3051
3052 // ---- Phase 2: HTTP request build/parse round-trip ----
3053 unittest::progress(50, "http request");
3054
3055 const char* body = "field1=value1&field2=value2";
3056 uint32 bodyLen = (uint32)strlen(body);
3057
3058 // Put query parameters in the URI so processHeader -> parseURIParameters
3059 // populates params; the urlencoded body is carried verbatim and checked
3060 // byte-for-byte below.
3061 const char* fullURI = "/path/script.cgi?home=Cosby&favorite=flies";
3062
3063 HTTPRequest reqEnc;
3064 if (!reqEnc.createRequest(HTTP_POST, "example.com", fullURI,
3065 body, bodyLen, true, 0)) {
3066 unittest::fail("HTTPRequest::createRequest returned false");
3067 return false;
3068 }
3069
3070 uint32 rawSize = 0;
3071 const char* raw = reqEnc.getRawContent(rawSize);
3072 if (!raw || !rawSize) {
3073 unittest::fail("HTTPRequest::getRawContent returned no bytes");
3074 return false;
3075 }
3076
3077 // Parse it back through a fresh request, feeding header then content
3078 // from the in-memory buffer (header ends at reqEnc.headerLength).
3079 HTTPRequest reqDec;
3080 bool isInvalid = false;
3081 if (!reqDec.processHeader(raw, rawSize, isInvalid) || isInvalid) {
3082 unittest::fail("HTTPRequest::processHeader failed (invalid=%d)", (int)isInvalid);
3083 return false;
3084 }
3085 if (reqDec.type != HTTP_POST) {
3086 unittest::fail("parsed method %u != HTTP_POST", reqDec.type);
3087 return false;
3088 }
3089 const char* uri = reqDec.getURI();
3090 if (!uri || strcmp(uri, fullURI) != 0) {
3091 unittest::fail("parsed URI '%s' != '%s'", uri ? uri : "(null)", fullURI);
3092 return false;
3093 }
3094 const char* request = reqDec.getRequest();
3095 if (!request || strcmp(request, "path/script.cgi") != 0) {
3096 unittest::fail("parsed request '%s' != 'path/script.cgi'", request ? request : "(null)");
3097 return false;
3098 }
3099 if (reqDec.contentLength != bodyLen) {
3100 unittest::fail("parsed Content-Length %u != %u", reqDec.contentLength, bodyLen);
3101 return false;
3102 }
3103
3104 unittest::progress(70, "http content");
3105
3106 // Feed the body (located right after the header in the same buffer).
3107 if (!reqDec.processContent(raw + reqDec.headerLength, bodyLen)) {
3108 unittest::fail("HTTPRequest::processContent failed");
3109 return false;
3110 }
3111 uint32 outSize = 0;
3112 const char* outBody = reqDec.getContent(outSize);
3113 if (!outBody || (outSize != bodyLen) || (memcmp(outBody, body, bodyLen) != 0)) {
3114 unittest::fail("parsed body differs from original (size %u != %u)", outSize, bodyLen);
3115 return false;
3116 }
3117 // query-string params (from the URI) should have been parsed.
3118 const char* p = reqDec.getParameter("home");
3119 if (!p || strcmp(p, "Cosby") != 0) {
3120 unittest::fail("parsed URI param home='%s' != 'Cosby'", p ? p : "(null)");
3121 return false;
3122 }
3123 unittest::detail("http POST build/parse round-trip ok (body %u bytes)", bodyLen);
3124
3125 // ---- Phase 3: parse throughput metric ----
3126 unittest::progress(85, "throughput");
3127
3128 const uint32 iters = 2000;
3129 uint64 t0 = GetTimeNow();
3130 for (uint32 i = 0; i < iters; i++) {
3131 HTTPRequest r;
3132 bool inv = false;
3133 if (!r.processHeader(raw, reqEnc.headerLength, inv) || inv) {
3134 unittest::fail("throughput loop processHeader failed at iter %u", i);
3135 return false;
3136 }
3137 }
3138 double us = (double)(GetTimeNow() - t0);
3139 if (us > 0.0)
3140 unittest::metric("http_header_parse_throughput", (double)iters / us * 1e6, "ops/s", true);
3141
3142 unittest::progress(100, "done");
3143 return true;
3144}
3145
3148 "networkprotocols", NetworkProtocols_UnitTest,
3149 "HTTP request and WebSocket frame build/parse round-trips", "network");
3150}
3151
3152}
std::string base64_decode(const char *encoded_string)
Decode Base64 text.
Definition Base64.cpp:49
std::string base64_encode(std::string string_to_encode)
Base64-encode a string's bytes.
Definition Base64.cpp:44
Wire-protocol layer: HTTP request/reply, WebSocket frames, Telnet lines, binary DataMessages,...
#define HTTP_OK
200 OK
#define HTTP_SERVICE_NOT_AVAILABLE
503 Service Unavailable
#define HTTP_MALFORMED_URL
400 Bad Request
#define HTTP_GATEWAY_TIMEOUT
504 Gateway Timeout
#define HTTP_SERVER_MALFORMED_REPLY
500 (malformed backend reply)
#define HTTP_HEAD
HEAD.
#define HTTP_SWITCH_PROTOCOL
101 Switching Protocols (WebSocket upgrade)
#define HTTP_INTERNAL_ERROR
500 Internal Server Error (page generation)
#define HTTP_OPTIONS
OPTIONS.
#define TELNET_UNIX
LF (\n) line endings.
#define HTTP_DELETE
DELETE.
#define TELNET_WINDOWS
CRLF (\r\n) line endings.
#define HTTP_MOVED_PERMANENTLY
301 Moved Permanently
#define HTTP_GET
GET.
#define HTTP_USE_LOCAL_COPY
304 Not Modified (use cached copy)
#define HTTP_ACCESS_DENIED
403 Forbidden
#define HTTP_FILE_NOT_FOUND
404 Not Found
#define HTTP_UNAUTHORIZED
401 Unauthorized (triggers Basic auth)
#define HTTP_PUT
PUT.
#define HTTP_SERVER_NOREPLY
500 (no reply from backend server)
#define HTTP_POST
POST.
#define HTTP_NOT_IMPLEMENTED
501 Not Implemented
#define PACKEDDATAMESSAGEID
Packed (compact) wire form of a DataMessage, see DataMessage::toPacked().
Definition ObjectIDs.h:77
#define DATAMESSAGEID
Definition ObjectIDs.h:75
#define GetObjID(data)
Extract the cid field from a binary object block: the uint32 at byte offset 4 (after the leading size...
Definition ObjectIDs.h:26
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
#define MAXVALUINT32
Definition Types.h:87
Small, dependency-free unit test harness used by all CMSDK object tests.
#define GETIPADDRESSQUAD(a)
Definition Utils.h:1688
#define stricmp
Definition Utils.h:132
#define LogPrint
Definition Utils.h:330
#define LOG_NETWORK
Definition Utils.h:215
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setTime(const char *key, uint64 value)
setTime(const char* key, uint64 value)
bool setString(const char *key, const char *value)
setString(const char* key, const char* value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
DataMessageHeader * data
Pointer to the message's flat memory block (header + user entries).
uint32 getSize()
getSize() Get message size Many types of data of any size can be put into a message as user entries; ...
bool setRecvTime(uint64 time)
setRecvTime(uint64 time)
char * toPacked(uint32 &size) const
Encode this message in the packed wire form.
bool setCreatedTime(uint64 t)
setCreatedTime(uint64 t)
bool setData(const char *key, const char *value, uint32 size)
setData(const char* key, const char* value, uint32 size)
static DataMessage * FromPacked(const char *data, uint32 size)
Decode a packed message (as written by toPacked() or the Kotlin codec).
static HTTPRequest * ReceiveHTTPRequest(NetworkConnection *con, uint32 timeout)
Read a full request from the connection.
static bool SendHTTPRequest(NetworkConnection *con, HTTPRequest *req)
Serialise and send a request.
static HTTPReply * ReceiveHTTPReply(NetworkConnection *con, uint32 timeout)
Read a full reply from the connection.
static bool SendHTTPReply(NetworkConnection *con, HTTPReply *reply)
Serialise and send a reply.
static bool CheckBufferForCompatibility(const char *buffer, uint32 length)
static bool InitialiseConversation(NetworkConnection *con)
No initial exchange needed.
One named entry of an HTTP POST body (form field or uploaded file part).
std::string type
MIME Content-Type of this part.
bool setContent(const char *content, uint32 size)
Copy content bytes into this entry (NUL-terminated internally).
std::string filename
Original filename for file uploads (empty for plain fields).
uint32 contentSize
Size of content in bytes.
bool isValid()
std::string name
Form field name (Content-Disposition name attribute).
char * content
Owned content bytes (NUL-terminated for convenience).
static bool InitialiseConversation(NetworkConnection *con)
No initial exchange needed for HTTP.
static bool SendHTTPReply(NetworkConnection *con, HTTPReply *reply)
Serialise and send a reply.
static HTTPReply * ReceiveHTTPReply(NetworkConnection *con, uint32 timeout)
Read a full reply from the connection.
static bool SendHTTPRequest(NetworkConnection *con, HTTPRequest *req)
Serialise and send a request.
static HTTPRequest * ReceiveHTTPRequest(NetworkConnection *con, uint32 timeout)
Read a full request from the connection.
static bool CheckBufferForCompatibility(const char *buffer, uint32 length)
static WebsocketData * ReceiveWebsocketData(NetworkConnection *con, uint32 timeout)
Read one complete WebSocket message (reassembling fragments).
static bool SendWebsocketData(NetworkConnection *con, WebsocketData *wsData)
Send a WebSocket frame.
A parsed or generated HTTP response.
uint32 contentLength
Body length in bytes.
const char * getRawContent(uint32 &size)
Get the raw, untransformed body bytes.
bool createAuthorizationReply(const char *realm)
Build a 401 Basic-auth challenge.
const char * getProtocol()
const char * getContent(uint32 &size)
Get the (decoded) response body.
uint8 type
HTTP_* status id of this reply.
uint64 time
Timestamp of creation/receipt (ms epoch).
bool processHeader(const char *buffer, uint32 size, bool &isInvalid)
Parse the response header block from raw bytes.
uint32 headerLength
Header block length within data, in bytes.
bool setDecodedContent(const char *buffer, uint32 size)
Replace the body with already-decoded bytes (e.g.
static HTTPReply * CreateErrorReply(uint8 type)
Build a canned error reply.
static HTTPReply * CreateWebsocketHTTPReply(const char *key, const char *version)
Build the "101 Switching Protocols" reply for a WebSocket handshake.
std::map< std::string, std::string > params
Auxiliary parsed parameters.
bool chunked
Body uses HTTP/1.1 chunked transfer encoding.
bool processContent(const char *buffer, uint32 size)
Append body bytes after the header has been parsed.
bool createFromFile(uint64 time, const char *serverName, uint64 ifLastMod, bool keepAlive, bool cache, const char *filename)
Build a response by reading a file from disk (MIME type from extension).
static HTTPReply * CreateAuthorizationReply(const char *realm)
Build a 401 reply requesting Basic authentication.
bool createOptionsResponse(uint8 status, uint64 time, const char *serverName, bool keepAlive, const char *origin, const char *operations)
Build a CORS-preflight (OPTIONS) response.
bool createWebsocketHTTPReply(const char *key, const char *version)
Build the 101 WebSocket handshake completion in-place.
char * data
Owned raw reply bytes (header followed by body).
bool keepAlive
Whether the connection stays open after this reply.
std::map< std::string, std::string > entries
Parsed header fields (name to value).
HTTPReply(uint64 source=0)
Construct an empty reply.
bool createPage(uint8 status, uint64 time, const char *serverName, uint64 lastMod, bool keepAlive, bool cache, const char *contentType, const char *content, uint32 contentSize=0, const char *additionalHeaderEntries=NULL)
Build a complete response with headers and content.
const char * getHeaderEntry(const char *entry)
Look up a header field (case-insensitive).
bool createErrorPage(uint8 status, uint64 time, const char *serverName, bool keepAlive)
Build a canned error page for the given status.
uint64 source
Packed uint64 endpoint of the sender (0 if local).
A parsed or generated HTTP request (also used for WebSocket upgrade handshakes).
bool parseURIParameters(const char *text)
Parse "a=1&b=2" style parameters from a URI query string into params.
const char * getBasicAuthorization()
const char * getContent(uint32 &size)
Get the (decoded) request body.
const char * getRawContent(uint32 &size)
Get the raw, untransformed body bytes as received.
bool parseContentParameters(const char *content, uint32 size)
Parse a URL-encoded form body into params.
uint64 time
Timestamp when reception/creation started (ms epoch).
std::string getBasicAuthorizationPassword()
const char * getHeaderEntry(const char *entry)
Look up a header field (case-insensitive).
DataMessage * convertToMessage()
Convert this HTTP request into a binary DataMessage (URI, params and content mapped to message fields...
std::map< std::string, std::string > entries
Parsed header fields (name to value).
HTTPRequest(uint64 source=0, uint64 startRecTime=0)
Construct an empty request.
uint32 headerLength
Length of the header block in data, in bytes.
uint32 contentLength
Body length (from Content-Length / parsing), in bytes.
bool parseContentChunk(const char *chunk, uint32 size)
Parse one chunk of a multipart body (between boundaries) into postEntries.
const char * getPostData(const char *entry, uint32 &size, const char **type)
Get a multipart POST part's content, size and MIME type.
static HTTPRequest * CreateWebsocketRequest(const char *uri, const char *host, const char *protocolName, const char *origin)
Build a client-side WebSocket upgrade request (RFC 6455 handshake).
uint8 type
HTTP_* method id.
bool createWebsocketRequest(const char *uri, const char *host, const char *protocolName, const char *origin)
Fill this object with a WebSocket upgrade handshake request.
char * data
Owned raw request bytes (header followed by body).
uint64 ifModifiedSince
Parsed If-Modified-Since timestamp (ms epoch; 0 = absent).
std::map< std::string, HTTPPostEntry * > postEntries
Multipart POST parts by name (owned).
std::string decodeBasicAuthorization()
Decode the Basic Authorization header.
bool processContent(const char *buffer, uint32 size)
Append body bytes after the header has been parsed.
bool processHeader(const char *buffer, uint32 size, bool &isInvalid)
Parse the HTTP header block from raw bytes.
std::string getBasicAuthorizationUser()
bool createRequest(uint8 type, const char *host, const char *uri, const char *content, uint32 contentSize, bool keepAlive, uint64 ifModifiedSince)
Build a simple request with optional raw body.
std::map< std::string, std::string > params
URI query and form parameters (name to value).
uint64 source
Packed uint64 endpoint of the sender (0 if local).
uint64 endReceiveTime
Timestamp when the request was fully received (ms epoch).
bool createMultipartRequest(uint8 type, const char *host, const char *uri, std::map< std::string, std::string > &headerEntries, std::map< std::string, HTTPPostEntry * > &bodyEntries, bool keepAlive, uint64 ifModifiedSince)
Build a multipart/form-data request from several named parts.
std::string postBoundary
Multipart boundary string, when applicable.
const char * getPostDataType(const char *entry)
bool setBasicAuthorization(const char *authB64)
Set the Authorization header from a pre-encoded base64 credential.
bool keepAlive
Whether the connection should be kept open after replying.
const char * getParameter(const char *entry)
Look up a URI query or form parameter by name.
const char * getData(const char *name, uint64 &size)
Get an attachment's bytes by name.
uint64 mOffset
Byte offset where the binary multipart section begins.
uint32 addData(const char *name, const char *data, uint64 size, const char *type)
Append a binary attachment.
uint64 jmSize
Total size of jmData in bytes.
DataMessage * convertToMessage()
Convert this container into a binary DataMessage.
std::string getDataType(const char *name)
std::string getInfoString()
std::map< uint32, JSONMEntry > entries
Attachment metadata by chunk index.
std::string getDataName(uint32 chunk)
std::string getJSON()
char * jmData
Owned serialised container buffer (JSON + attachments).
bool setJSON(const char *json)
Replace the JSON text portion (multipart metadata is regenerated).
static bool SendMessage(NetworkConnection *con, DataMessage *msg, uint64 receiver=0, bool packed=false)
Serialise and send one message.
static bool CheckBufferForCompatibility(const char *buffer, uint32 length)
static bool InitialiseConversation(NetworkConnection *con)
Perform the initial message-protocol greeting.
static DataMessage * ReceiveMessage(NetworkConnection *con, uint32 timeout)
Read one full message frame.
Abstract base class for all point-to-point network connections.
virtual bool send(const char *data, uint32 size, uint64 receiver=0)=0
Send raw bytes on the connection.
virtual bool receiveAvailable(char *data, uint32 &size, uint32 maxSize, uint32 timeout, bool peek=false)
Receive whatever bytes are available (up to maxSize).
virtual bool receive(char *data, uint32 size, uint32 timeout, bool peek=false)
Receive exactly size bytes into data, waiting up to timeout ms.
virtual bool waitForDataToRead(uint32 timeout)
Block until data is readable (buffered or on the socket).
virtual bool discard(uint32 size)
Drop size bytes from the front of the receive buffer (after a peek).
One line of Telnet-style text traffic.
bool giveData(char *buffer, uint32 len)
Take ownership of an existing buffer as the line text (no copy).
uint32 size
Line length in bytes.
TelnetLine(uint64 source)
Construct an empty line.
char cr[3]
Active line-ending characters (\r\n or \n, NUL-terminated).
uint32 user
Optional user/session tag for multi-user telnet servers.
bool setCR(uint8 type)
Select the line-ending convention.
char * data
Owned line text.
uint64 time
Timestamp of creation/receipt (ms epoch).
uint64 source
Packed uint64 endpoint of the sender.
bool setLine(const char *buffer, bool addCR)
Set the line text from a NUL-terminated string.
static TelnetLine * ReceiveTelnetLine(NetworkConnection *con, uint32 timeout)
Read one line terminated by CR/LF.
static bool CheckBufferForCompatibility(const char *buffer, uint32 length)
static bool SendTelnetLine(NetworkConnection *con, TelnetLine *line)
Send one line (with its line ending).
static bool InitialiseConversation(NetworkConnection *con)
Send any initial prompt/negotiation.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true, uint32 timeoutSec=0)
Register a test with the runner.
One WebSocket frame/message (RFC 6455): parsing, generation and control frames.
uint64 endReceiveTime
Timestamp when the frame was fully received (ms epoch).
const char * getRawData(uint64 &size)
Get the serialised on-the-wire frame bytes (header + masked payload).
uint64 payloadSize
Declared payload length from the header.
static uint8 const payload_size_code_16bit
Basic length 126: a 16-bit big-endian length follows.
static uint8 const BHB1_PAYLOAD
Byte 1: 7-bit basic payload length / extension code.
static uint8 const payload_size_code_64bit
Basic length 127: a 64-bit big-endian length follows.
static uint8 const BHB0_OPCODE
Byte 0: 4-bit opcode (see DataType).
uint64 contentSize
Payload bytes accumulated so far.
static uint8 const payload_size_basic
Maximum size of a basic WebSocket payload.
const char * getContent(uint64 &size)
Get the decoded (unmasked) payload.
uint32 headerLength
Parsed frame header length in bytes (2..14).
static WebsocketData * CreatePing()
bool isFinal
FIN bit: true when this is the last fragment of a message.
static uint8 const basic_header_length
char * data
Owned decoded payload bytes.
uint8 opcode
RFC 6455 opcode (see DataType).
DataType
Frame content type, mapping to the RFC 6455 opcode.
static WebsocketData * CreatePong()
enum cmlabs::WebsocketData::DataType dataType
uint32 maskingKey
32-bit masking key (0 when unmasked, i.e.
bool processContent(const char *buffer, uint64 size)
Append (and unmask) payload bytes after the header has been parsed.
static WebsocketData * CreateTerminationConfirmation()
char * rawData
Owned serialised frame bytes (for sending).
static uint8 const BHB0_FIN
Byte 0: FIN — set on the final fragment of a message.
WebsocketData(uint64 source=0, uint64 startRecTime=0)
Construct an empty frame.
static uint16 const payload_size_extended
Maximum size of an extended WebSocket payload (basic payload = 126).
bool setData(DataType dataType, bool maskData, const char *data=NULL, uint64 size=0)
Set the payload and build the serialised frame for sending.
static uint8 const BHB1_MASK
Byte 1: MASK — payload is XOR-masked (required client-to-server).
uint64 time
Timestamp when reception/creation started (ms epoch).
enum cmlabs::WebsocketData::WSSTATUS status
uint64 source
Packed uint64 endpoint of the sender (0 if local).
bool processHeader(const char *buffer, uint64 size, bool &isInvalid)
Parse the frame header from raw bytes (handles 16/64-bit extended lengths).
uint32 packages
Number of fragments accumulated into this message.
compute SHA1 hash
Definition sha1.h:45
std::string getHashRaw()
return latest hash as raw characters
void reset()
restart
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
#define TIME_YEAR_1970
Same value as USEC_YEAR_0_TO_1970: the PsyTime timestamp of 1970-01-01 00:00:00 UTC.
Definition PsyTime.h:94
uint32 GetHTTPTime(uint64 time, char *buffer, uint32 size)
Format a timestamp as an HTTP-date (RFC 7231) string, e.g.
Definition PsyTime.cpp:518
uint64 GetTimeFromString(const char *str)
Parse a textual date/time into a PsyTime timestamp.
Definition PsyTime.cpp:438
int32 GetTimeAgeMS(uint64 t)
Age of a timestamp relative to now, in milliseconds.
Definition PsyTime.cpp:35
const char * laststrstr(const char *str1, const char *str2)
Find the last occurrence of str2 in str1.
Definition Utils.cpp:9636
bool TextStartsWith(const char *str, const char *start, bool caseSensitive=true)
Test whether str starts with start.
Definition Utils.cpp:9693
bool Sleep(uint32 ms)
Suspend the calling thread.
Definition Utils.cpp:4078
const char * stristr(const char *str, const char *substr, uint32 len=0)
Case-insensitive strstr.
Definition Utils.cpp:8470
bool GetNextLineEnd(const char *str, uint32 size, uint32 &len, uint32 &crSize)
Find the end of the current line.
Definition Utils.cpp:8509
uint64 ntoh64(const uint64 *input)
Convert a 64-bit value from network to host byte order.
Definition Utils.cpp:3659
uint32 strcpyavail(char *dst, const char *src, uint32 maxlen, bool copyAvailable)
Bounded strcpy that always NUL-terminates.
Definition Utils.cpp:8491
@ JSMN_UNDEFINED
Definition jsmn.h:29
std::string GetJSONChildValueString(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:441
std::vector< int > GetJSONChildArrayIndexes(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:492
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens)
Run JSON parser.
Definition jsmn.cpp:156
uint64 GetJSONChildValueUint64(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:453
void jsmn_init(jsmn_parser *parser)
Create JSON parser over an array of tokens.
Definition jsmn.cpp:311
int GetJSONToken(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:328
bool GetMimeType(char *dest, const char *filename, uint32 maxsize)
Look up the MIME type for a filename based on its extension.
Definition HTML.cpp:137
bool IsMimeBinary(const char *type)
Determine whether a MIME type denotes binary content.
Definition HTML.cpp:130
std::string DecodeHTML(std::string str)
Decode HTML entities in a string (e.g.
Definition HTML.cpp:12
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
std::string ReadAFileString(std::string filename)
Read an entire file into a std::string.
Definition Utils.cpp:9232
std::vector< std::string > TextListSplit(const char *text, const char *split, bool keepEmpty=true, bool autoTrim=false)
Split text on a separator.
Definition Utils.cpp:8946
FileDetails GetFileDetails(const char *filename)
Stat a file.
Definition Utils.cpp:9817
std::multimap< std::string, std::string > TextMultiMapSplit(const char *text, const char *outersplit, const char *innersplit)
Split "k=v<sep>k=v..." text into a multimap (duplicate keys preserved).
Definition Utils.cpp:8619
std::string TextTrimQuotes(const char *text)
Strip a single pair of surrounding quotes if present.
Definition Utils.cpp:8586
std::string BytifySize(double val)
Format a byte count with binary units, e.g.
Definition Utils.cpp:10177
std::string StringFormat(const char *format,...)
printf into a std::string.
Definition Utils.cpp:9061
char * ReadAFile(const char *filename, uint32 &length, bool binary=false)
Read an entire file into a new buffer.
Definition Utils.cpp:9253
int64 RandomInt(int64 from=0, int64 to=100)
Uniform random integer in [from,to].
Definition Utils.cpp:10168
bool NetworkProtocols_UnitTest()
static bool MethodIs(const char *buffer, uint32 size, const char *m)
static char HTTP_Status[][128]
Full HTTP status lines (with inline error bodies for error cases), indexed by the HTTP_* status ids a...
static char HTTP_Type[][8]
HTTP method name strings, indexed by the HTTP_* method ids (index 0 unused).
void Register_NetworkProtocols_Tests()
static bool TestWebsocketRoundTrip(WebsocketData::DataType type, bool maskData, const char *payload, uint64 payloadSize)
Metadata for one binary attachment chunk inside a JSONM container.
uint64 size
Attachment size in bytes.
std::string type
MIME type of the attachment.
uint64 offset
Byte offset of the attachment within the container data.
uint32 chunk
Chunk index within the container (1-based).
std::string name
Attachment name.
Existence, type, permission and timestamp information for one file, as returned by GetFileDetails().
Definition Utils.h:1908
JSON parser.
Definition jsmn.h:65
JSON token description.
Definition jsmn.h:51
int start
Definition jsmn.h:53
int end
Definition jsmn.h:54