summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/net')
-rw-r--r--src/net/HttpClient.cpp42
-rw-r--r--src/net/HttpParser.cpp292
-rw-r--r--src/net/TgLongPoll.cpp14
-rw-r--r--src/net/Url.cpp88
4 files changed, 218 insertions, 218 deletions
diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp
index 5027b09..b9813ad 100644
--- a/src/net/HttpClient.cpp
+++ b/src/net/HttpClient.cpp
@@ -32,36 +32,36 @@ using namespace boost::asio::local;
namespace TgBot {
HttpClient& HttpClient::getInstance() {
- static HttpClient result;
- return result;
+ static HttpClient result;
+ return result;
}
string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
- ssl::context context(ssl::context::sslv23);
- context.set_default_verify_paths();
+ ssl::context context(ssl::context::sslv23);
+ context.set_default_verify_paths();
- ssl::stream<tcp::socket> socket(_ioService, context);
- tcp::resolver resolver(_ioService);
- tcp::resolver::query query(url.host, url.protocol);
+ ssl::stream<tcp::socket> socket(_ioService, context);
+ tcp::resolver resolver(_ioService);
+ tcp::resolver::query query(url.host, url.protocol);
- connect(socket.lowest_layer(), resolver.resolve(query));
+ connect(socket.lowest_layer(), resolver.resolve(query));
- socket.set_verify_mode(ssl::verify_none);
- socket.set_verify_callback(ssl::rfc2818_verification(url.host));
- socket.handshake(ssl::stream<tcp::socket>::client);
+ socket.set_verify_mode(ssl::verify_none);
+ socket.set_verify_callback(ssl::rfc2818_verification(url.host));
+ socket.handshake(ssl::stream<tcp::socket>::client);
- string requestText = HttpParser::getInstance().generateRequest(url, args, false);
- write(socket, buffer(requestText.c_str(), requestText.length()));
+ string requestText = HttpParser::getInstance().generateRequest(url, args, false);
+ write(socket, buffer(requestText.c_str(), requestText.length()));
- string response;
- char buff[1024];
- boost::system::error_code error;
- while (!error) {
- size_t bytes = read(socket, buffer(buff), error);
- response += string(buff, bytes);
- }
+ string response;
+ char buff[1024];
+ boost::system::error_code error;
+ while (!error) {
+ size_t bytes = read(socket, buffer(buff), error);
+ response += string(buff, bytes);
+ }
- return HttpParser::getInstance().parseResponse(response);
+ return HttpParser::getInstance().parseResponse(response);
}
}
diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp
index ae9de62..a2d9108 100644
--- a/src/net/HttpParser.cpp
+++ b/src/net/HttpParser.cpp
@@ -32,173 +32,173 @@ using namespace boost;
namespace TgBot {
HttpParser& HttpParser::getInstance() {
- static HttpParser result;
- return result;
+ static HttpParser result;
+ return result;
}
string HttpParser::generateRequest(const Url& url, const vector<HttpReqArg>& args, bool isKeepAlive) {
- string result;
- if (args.empty()) {
- result += "GET ";
- } else {
- result += "POST ";
- }
- result += url.path;
- result += url.query.empty() ? "" : "?" + url.query;
- result += " HTTP/1.1\r\n";
- result += "Host: ";
- result += url.host;
- result += "\r\nConnection: ";
- if (isKeepAlive) {
- result += "keep-alive";
- } else {
- result += "close";
- }
- result += "\r\n";
- if (args.empty()) {
- result += "\r\n";
- } else {
- string requestData;
-
- string bondary = generateMultipartBoundary(args);
- if (bondary.empty()) {
- result += "Content-Type: application/x-www-form-urlencoded\r\n";
- requestData = generateWwwFormUrlencoded(args);
- } else {
- result += "Content-Type: multipart/form-data; boundary=";
- result += bondary;
- result += "\r\n";
- requestData = generateMultipartFormData(args, bondary);
- }
-
- result += "Content-Length: ";
- result += lexical_cast<string>(requestData.length());
- result += "\r\n\r\n";
- result += requestData;
- }
- return result;
+ string result;
+ if (args.empty()) {
+ result += "GET ";
+ } else {
+ result += "POST ";
+ }
+ result += url.path;
+ result += url.query.empty() ? "" : "?" + url.query;
+ result += " HTTP/1.1\r\n";
+ result += "Host: ";
+ result += url.host;
+ result += "\r\nConnection: ";
+ if (isKeepAlive) {
+ result += "keep-alive";
+ } else {
+ result += "close";
+ }
+ result += "\r\n";
+ if (args.empty()) {
+ result += "\r\n";
+ } else {
+ string requestData;
+
+ string bondary = generateMultipartBoundary(args);
+ if (bondary.empty()) {
+ result += "Content-Type: application/x-www-form-urlencoded\r\n";
+ requestData = generateWwwFormUrlencoded(args);
+ } else {
+ result += "Content-Type: multipart/form-data; boundary=";
+ result += bondary;
+ result += "\r\n";
+ requestData = generateMultipartFormData(args, bondary);
+ }
+
+ result += "Content-Length: ";
+ result += lexical_cast<string>(requestData.length());
+ result += "\r\n\r\n";
+ result += requestData;
+ }
+ return result;
}
string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, const string& bondary) {
- string result;
- for (const HttpReqArg& item : args) {
- result += "--";
- result += bondary;
- result += "\r\nContent-Disposition: form-data; name=\"";
- result += item.name;
- result += "\"\r\n";
- if (item.isFile) {
- result += "Content-Type: ";
- result += item.mimeType;
- result += "\r\n";
- }
- result += "\r\n";
- result += item.value;
- result += "\r\n";
- }
- return result;
+ string result;
+ for (const HttpReqArg& item : args) {
+ result += "--";
+ result += bondary;
+ result += "\r\nContent-Disposition: form-data; name=\"";
+ result += item.name;
+ result += "\"\r\n";
+ if (item.isFile) {
+ result += "Content-Type: ";
+ result += item.mimeType;
+ result += "\r\n";
+ }
+ result += "\r\n";
+ result += item.value;
+ result += "\r\n";
+ }
+ return result;
}
string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) {
- string result;
- srand((unsigned int) time(nullptr));
- for (const HttpReqArg& item : args) {
- if (item.isFile) {
- while (result.empty() || item.value.find(result) != item.value.npos) {
- result += StringTools::generateRandomString(4);
- }
- }
- }
- return result;
+ string result;
+ srand((unsigned int) time(nullptr));
+ for (const HttpReqArg& item : args) {
+ if (item.isFile) {
+ while (result.empty() || item.value.find(result) != item.value.npos) {
+ result += StringTools::generateRandomString(4);
+ }
+ }
+ }
+ return result;
}
string HttpParser::generateWwwFormUrlencoded(const vector<HttpReqArg>& args) {
- string result;
-
- bool firstRun = true;
- for (const HttpReqArg& item : args) {
- if (firstRun) {
- firstRun = false;
- } else {
- result += '&';
- }
- result += StringTools::urlEncode(item.name);
- result += '=';
- result += StringTools::urlEncode(item.value);
- }
-
- return result;
+ string result;
+
+ bool firstRun = true;
+ for (const HttpReqArg& item : args) {
+ if (firstRun) {
+ firstRun = false;
+ } else {
+ result += '&';
+ }
+ result += StringTools::urlEncode(item.name);
+ result += '=';
+ result += StringTools::urlEncode(item.value);
+ }
+
+ return result;
}
string HttpParser::generateResponse(const string& data, const string& mimeType, unsigned short statusCode, const string& statusStr, bool isKeepAlive) {
- string result;
- result += "HTTP/1.1 ";
- result += lexical_cast<string>(statusCode);
- result += ' ';
- result += statusStr;
- result += "\r\nContent-Type: ";
- result += mimeType;
- result += "\r\nContent-Length: ";
- result += lexical_cast<string>(data.length());
- result += "\r\n\r\n";
- result += data;
- return result;
+ string result;
+ result += "HTTP/1.1 ";
+ result += lexical_cast<string>(statusCode);
+ result += ' ';
+ result += statusStr;
+ result += "\r\nContent-Type: ";
+ result += mimeType;
+ result += "\r\nContent-Length: ";
+ result += lexical_cast<string>(data.length());
+ result += "\r\n\r\n";
+ result += data;
+ return result;
}
string HttpParser::parseHttp(bool isRequest, const string& data, map<string, string>& headers) {
- bool onlyNewLineChar = false;
- size_t headerEnd = data.find("\r\n\r\n");
- if (headerEnd == data.npos) {
- headerEnd = data.find("\n\n");
- if (headerEnd != data.npos) {
- onlyNewLineChar = true;
- headerEnd += 2;
- }
- } else {
- headerEnd += 4;
- }
-
- size_t lineStart = 0;
- size_t lineEnd = 0;
- size_t lineSepPos = 0;
- size_t lastLineEnd = data.npos;
- while (lastLineEnd != lineEnd) {
- lastLineEnd = lineEnd;
- if (lineEnd == 0) {
- if (isRequest) {
- lineSepPos = data.find(' ');
- lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n");
- headers["method"] = data.substr(0, lineSepPos);
- headers["path"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1);
- } else {
- lineSepPos = data.find(' ');
- lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n");
- headers["status"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1);
- }
- } else {
- lineStart = lineEnd;
- lineStart += onlyNewLineChar ? 1 : 2;
- lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n", lineStart);
- lineSepPos = data.find(':', lineStart);
- if (lineEnd >= headerEnd || lastLineEnd == lineEnd || lineSepPos >= headerEnd) {
- break;
- }
- headers[to_lower_copy(data.substr(lineStart, lineSepPos - lineStart))] = trim_copy(data.substr(lineSepPos + 1, lineEnd - lineSepPos - 1));
- }
- }
-
- return headerEnd == data.npos ? "" : data.substr(headerEnd);
+ bool onlyNewLineChar = false;
+ size_t headerEnd = data.find("\r\n\r\n");
+ if (headerEnd == data.npos) {
+ headerEnd = data.find("\n\n");
+ if (headerEnd != data.npos) {
+ onlyNewLineChar = true;
+ headerEnd += 2;
+ }
+ } else {
+ headerEnd += 4;
+ }
+
+ size_t lineStart = 0;
+ size_t lineEnd = 0;
+ size_t lineSepPos = 0;
+ size_t lastLineEnd = data.npos;
+ while (lastLineEnd != lineEnd) {
+ lastLineEnd = lineEnd;
+ if (lineEnd == 0) {
+ if (isRequest) {
+ lineSepPos = data.find(' ');
+ lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n");
+ headers["method"] = data.substr(0, lineSepPos);
+ headers["path"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1);
+ } else {
+ lineSepPos = data.find(' ');
+ lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n");
+ headers["status"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1);
+ }
+ } else {
+ lineStart = lineEnd;
+ lineStart += onlyNewLineChar ? 1 : 2;
+ lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n", lineStart);
+ lineSepPos = data.find(':', lineStart);
+ if (lineEnd >= headerEnd || lastLineEnd == lineEnd || lineSepPos >= headerEnd) {
+ break;
+ }
+ headers[to_lower_copy(data.substr(lineStart, lineSepPos - lineStart))] = trim_copy(data.substr(lineSepPos + 1, lineEnd - lineSepPos - 1));
+ }
+ }
+
+ return headerEnd == data.npos ? "" : data.substr(headerEnd);
}
string HttpParser::parseHttp(bool isRequest, const string& data) {
- size_t headerEnd = data.find("\r\n\r\n");
- if (headerEnd == data.npos) {
- headerEnd = data.find("\n\n");
- }
- if (headerEnd == data.npos) {
- headerEnd = 0;
- }
- return data.substr(headerEnd);
+ size_t headerEnd = data.find("\r\n\r\n");
+ if (headerEnd == data.npos) {
+ headerEnd = data.find("\n\n");
+ }
+ if (headerEnd == data.npos) {
+ headerEnd = 0;
+ }
+ return data.substr(headerEnd);
}
}
diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp
index f420e92..91bf058 100644
--- a/src/net/TgLongPoll.cpp
+++ b/src/net/TgLongPoll.cpp
@@ -31,13 +31,13 @@ TgLongPoll::TgLongPoll(const Bot& bot) : TgLongPoll(&bot.getApi(), &bot.getEvent
}
void TgLongPoll::start() {
- std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, 100, 60);
- for (Update::Ptr& item : updates) {
- if (item->updateId >= _lastUpdateId) {
- _lastUpdateId = item->updateId + 1;
- }
- _eventHandler->handleUpdate(item);
- }
+ std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, 100, 60);
+ for (Update::Ptr& item : updates) {
+ if (item->updateId >= _lastUpdateId) {
+ _lastUpdateId = item->updateId + 1;
+ }
+ _eventHandler->handleUpdate(item);
+ }
}
}
diff --git a/src/net/Url.cpp b/src/net/Url.cpp
index c773f3b..44d9089 100644
--- a/src/net/Url.cpp
+++ b/src/net/Url.cpp
@@ -29,52 +29,52 @@ using namespace std;
namespace TgBot {
Url::Url(const string& url) {
- bool isProtocolParsed = false;
- bool isHostParsed = false;
- bool isPathParsed = false;
- bool isQueryParsed = false;
+ bool isProtocolParsed = false;
+ bool isHostParsed = false;
+ bool isPathParsed = false;
+ bool isQueryParsed = false;
- for (size_t i = 0, count = url.length(); i < count; ++i) {
- char c = url[i];
+ for (size_t i = 0, count = url.length(); i < count; ++i) {
+ char c = url[i];
- if (!isProtocolParsed) {
- if (c == ':') {
- isProtocolParsed = true;
- i += 2;
- } else {
- protocol += c;
- }
- } else if (!isHostParsed) {
- if (c == '/') {
- isHostParsed = true;
- path += '/';
- } else if (c == '?') {
- isHostParsed = isPathParsed = true;
- path += '/';
- } else if (c == '#') {
- isHostParsed = isPathParsed = isQueryParsed = true;
- path += '/';
- } else {
- host += c;
- }
- } else if (!isPathParsed) {
- if (c == '?') {
- isPathParsed = true;
- } else if (c == '#') {
- isPathParsed = isQueryParsed = true;
- } else {
- path += c;
- }
- } else if (!isQueryParsed) {
- if (c == '#') {
- isQueryParsed = true;
- } else {
- query += c;
- }
- } else {
- fragment += c;
- }
- }
+ if (!isProtocolParsed) {
+ if (c == ':') {
+ isProtocolParsed = true;
+ i += 2;
+ } else {
+ protocol += c;
+ }
+ } else if (!isHostParsed) {
+ if (c == '/') {
+ isHostParsed = true;
+ path += '/';
+ } else if (c == '?') {
+ isHostParsed = isPathParsed = true;
+ path += '/';
+ } else if (c == '#') {
+ isHostParsed = isPathParsed = isQueryParsed = true;
+ path += '/';
+ } else {
+ host += c;
+ }
+ } else if (!isPathParsed) {
+ if (c == '?') {
+ isPathParsed = true;
+ } else if (c == '#') {
+ isPathParsed = isQueryParsed = true;
+ } else {
+ path += c;
+ }
+ } else if (!isQueryParsed) {
+ if (c == '#') {
+ isQueryParsed = true;
+ } else {
+ query += c;
+ }
+ } else {
+ fragment += c;
+ }
+ }
host = StringTools::urlEncode(host, ".");
path = StringTools::urlEncode(path, "/");