REMOVED USER_STD macros, code formating, csv changes, few file functions
This commit is contained in:
@@ -17,59 +17,60 @@
|
||||
#include <string>
|
||||
|
||||
|
||||
HttpClient::HttpClient(){};
|
||||
HttpClient::HttpClient() {};
|
||||
|
||||
std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) {
|
||||
std::pair<int, std::string>
|
||||
HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) {
|
||||
// https://stackoverflow.com/questions/25896916/parse-http-headers-in-c
|
||||
|
||||
|
||||
std::regex rgx{R"(^(?:((?:https?|s?ftp):)//)([^:/\s]+)(?::(\d*))?(?:/([^\s?#]+)?([?][^?#]*)?(#.*)?)?)"};
|
||||
std::smatch matches;
|
||||
|
||||
if (std::regex_search(url, matches, rgx)) {
|
||||
for (size_t i = 0; i < matches.size(); ++i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
full_url = matches[i].str();
|
||||
break;
|
||||
case 1:
|
||||
proto = matches[i].str();
|
||||
break;
|
||||
case 2:
|
||||
server = matches[i].str();
|
||||
break;
|
||||
case 3:
|
||||
port = matches[i].str();
|
||||
break;
|
||||
case 4:
|
||||
uri = matches[i].str();
|
||||
break;
|
||||
case 5:
|
||||
params = matches[i].str();
|
||||
break;
|
||||
case 6:
|
||||
href = matches[i].str();
|
||||
break;
|
||||
case 0:
|
||||
full_url = matches[i].str();
|
||||
break;
|
||||
case 1:
|
||||
proto = matches[i].str();
|
||||
break;
|
||||
case 2:
|
||||
server = matches[i].str();
|
||||
break;
|
||||
case 3:
|
||||
port = matches[i].str();
|
||||
break;
|
||||
case 4:
|
||||
uri = matches[i].str();
|
||||
break;
|
||||
case 5:
|
||||
params = matches[i].str();
|
||||
break;
|
||||
case 6:
|
||||
href = matches[i].str();
|
||||
break;
|
||||
}
|
||||
// std::cout << i << ": '" << matches[i].str() << "'\n";
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Match not found" << std::endl; // TODO better message
|
||||
std::cerr << "Match not found" << std::endl; // TODO better message
|
||||
}
|
||||
|
||||
std::string headers_string = "";
|
||||
for (auto it = headers.begin(); it != headers.end(); ++it) {
|
||||
headers_string.append("\r\n" + it->first + ": " + it->second);
|
||||
// std::cerr << "KEY: `" << it->first << "`, VALUE: `" << it->second << '`' << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string request = "GET " + full_url + " HTTP/1.0\r\nHost: " + server + headers_string + "\r\n\r\n";
|
||||
std::string request = "GET " + full_url + " HTTP/1.0\r\nHost: " + server + headers_string + "\r\n\r\n";
|
||||
|
||||
// TODO memory leaks ???
|
||||
int bytes_read = sslRequest(server, request);
|
||||
if (bytes_read <= 0) {
|
||||
std::cerr << "no data read" << std::endl;
|
||||
return std::make_pair(403, "");
|
||||
std::cerr << "no data read" << std::endl;
|
||||
return std::make_pair(403, "");
|
||||
}
|
||||
|
||||
std::string::size_type position = ssl_read_packet.find("\r\n\r\n");
|
||||
@@ -88,8 +89,8 @@ std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, con
|
||||
std::smatch status_matches;
|
||||
if (std::regex_search(status_str, status_matches, status_rgx)) {
|
||||
if (status_matches.size() > 1) {
|
||||
auto sta = status_matches[1].str(); // string "200"
|
||||
// std::cout << "status: " << sta << std::endl;
|
||||
auto sta = status_matches[1].str(); // string "200"
|
||||
// std::cout << "status: " << sta << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ std::string HttpClient::inetAddress(std::string hostname) {
|
||||
std::cerr << hostname << " is unavailable" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
in_addr *address = (in_addr *)record->h_addr;
|
||||
in_addr *address = (in_addr *) record->h_addr;
|
||||
std::string ip_address = inet_ntoa(*address);
|
||||
|
||||
return ip_address;
|
||||
@@ -131,8 +132,8 @@ int HttpClient::sslRecvPacket() {
|
||||
do {
|
||||
len = SSL_read(ssl, buf, len);
|
||||
if (len >= 0) {
|
||||
buf[len] = 0;
|
||||
ssl_read_packet.append((const char *)buf);
|
||||
buf[len] = 0;
|
||||
ssl_read_packet.append((const char *) buf);
|
||||
}
|
||||
} while (len > 0);
|
||||
|
||||
@@ -154,15 +155,15 @@ int HttpClient::sslSendPacket(std::string buf) {
|
||||
if (len < 0) {
|
||||
int err = SSL_get_error(ssl, len);
|
||||
switch (err) {
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
return 0;
|
||||
case SSL_ERROR_WANT_READ:
|
||||
return 0;
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
case SSL_ERROR_SYSCALL:
|
||||
case SSL_ERROR_SSL:
|
||||
default:
|
||||
return -1;
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
return 0;
|
||||
case SSL_ERROR_WANT_READ:
|
||||
return 0;
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
case SSL_ERROR_SYSCALL:
|
||||
case SSL_ERROR_SSL:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,7 +189,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||
socklen_t socklen = sizeof(sa);
|
||||
|
||||
// connect to server
|
||||
if (connect(s, (struct sockaddr *)&sa, socklen)) {
|
||||
if (connect(s, (struct sockaddr *) &sa, socklen)) {
|
||||
printf("MlError connecting to server.\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -206,8 +207,8 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||
}
|
||||
sock = SSL_get_fd(ssl);
|
||||
SSL_set_fd(ssl, s);
|
||||
|
||||
SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, (void*)server.c_str());
|
||||
|
||||
SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, (void *) server.c_str());
|
||||
|
||||
int err = SSL_connect(ssl);
|
||||
if (err <= 0) {
|
||||
@@ -221,7 +222,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
|
||||
// printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
|
||||
|
||||
// send request
|
||||
//std::err << request << std::endl;
|
||||
//std::err << request << std::endl;
|
||||
sslSendPacket(request);
|
||||
|
||||
// read response and return its length
|
||||
|
||||
Reference in New Issue
Block a user