some TODOs solved

This commit is contained in:
vaclavt
2022-02-17 20:41:47 +01:00
parent 2d26c59df6
commit 765f2bc673
6 changed files with 49 additions and 47 deletions

View File

@@ -5,7 +5,6 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <cstdio>
#include <iostream>
#include <regex>
#include <sstream>
@@ -16,8 +15,6 @@
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
// TODO user streams instead of printf's
std::pair<int, std::string> HttpClient::doRequest(const std::string &method, const std::string &url, const std::map<std::string, std::string> &headers, const std::string &request_body) {
// split url to parts
parseURL(url);
@@ -39,7 +36,6 @@ std::pair<int, std::string> HttpClient::doRequest(const std::string &method, con
return std::make_pair(403, "");
}
// get headers
std::string::size_type position = ssl_read_packet.find("\r\n\r\n");
if (position == std::string::npos)
@@ -198,7 +194,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (!s) {
printf("sslRequest, error creating socket.\n");
std::cerr << "HttpClient::sslRequest, error creating socket" << std::endl;
return -1;
}
@@ -213,7 +209,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
// connect to server
if (connect(s, (struct sockaddr *) &sa, socklen)) {
printf("sslRequest, error connecting to server.\n");
std::cerr << "HttpClient::sslRequest, error connecting to server" << std::endl;
return -1;
}
@@ -224,7 +220,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
SSL_CTX *ctx = SSL_CTX_new(meth);
ssl = SSL_new(ctx);
if (!ssl) {
printf("sslRequest, error creating SSL.\n");
std::cerr << "HttpClient::sslRequest, error creating SSL" << std::endl;
logSSL();
return -1;
}
@@ -235,23 +231,23 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
int err = SSL_connect(ssl);
if (err <= 0) {
printf("sslRequest, error creating SSL connection. err=%x\n", err);
std::cerr << "HttpClient::sslRequest, error creating SSL connection. " << err << std::endl;
logSSL();
fflush(stdout);
return -1;
}
// log cipher
// printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
// std::cerr << "HttpClient::sslRequest, SSL connection using" << SSL_get_cipher (ssl) << std::endl;
// showCerts(ssl);
// send request
// printf ("SSL sending request %s\n", request.c_str());
// std::cerr << "HttpClient::sslRequest, SSL sending request: " << request << std::endl;
int written_bytes = sslSendPacket(request);
if (written_bytes != request.length()) {
printf("sslRequest, error sending request\n");
std::cerr << "HttpClient::sslRequest, error sending request" << std::endl;
return -1;
}
@@ -275,15 +271,15 @@ void HttpClient::showCerts(SSL* ssl) {
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( cert != NULL ) {
printf("Server certificates:\n");
std::cerr << "HttpClient::showCerts, Server certificates: " << std::endl;
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %s\n", line);
std::cerr << "HttpClient::showCerts, Subject: " << line << std::endl;
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %s\n", line);
std::cerr << "HttpClient::showCerts, Issuer: " << line << std::endl;
free(line); /* free the malloc'ed string */
X509_free(cert); /* free the malloc'ed certificate copy */
} else {
printf("No certificates.\n");
std::cerr << "HttpClient::showCerts, No certificates." << std::endl;
}
}