Commit 7f71eb56 authored by jan.koester's avatar jan.koester
Browse files

test

parent f39b4b57
Loading
Loading
Loading
Loading
+19 −34
Changes for src/http.cpp: 19 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@

#include <algorithm>
#include <sstream>
#include <charconv>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
@@ -187,14 +188,12 @@ bool libhttppp::HttpUrl::operator<(const HttpUrl& other) const {
}

std::string libhttppp::HttpUrl::print() const{
  std::stringstream ss;
  if(_protocol==HTTP){
    ss << "http://";
  }else{
    ss << "https://";
  }
  ss << _host << ":" << _port << _path;
  return ss.str();
  std::string out = (_protocol == HTTP) ? "http://" : "https://";
  out += _host;
  out += ':';
  out += std::to_string(_port);
  out += _path;
  return out;
}


@@ -826,11 +825,8 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
        _streamH3InDataFrame = false;
        _streamH3DataRemaining = 0;

        std::stringstream host;
        host << _url.getHost() << ":" << _url.getPort();

        if (!nreq.getHeaderData("host"))
            nreq.setHeaderData("host")->push_back(host.str());
            nreq.setHeaderData("host")->push_back(_url.getHost() + ":" + std::to_string(_url.getPort()));
        nreq.setRequestType(GETREQUEST);

        if (nreq.getRequestURL().empty())
@@ -841,8 +837,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            std::string path = nreq.getRequestURL();
            if (path.empty()) path = _url.getPath();
            std::string scheme = (_url.getProtocol() == HttpUrl::HTTP) ? "http" : "https";
            std::stringstream authority;
            authority << _url.getHost() << ":" << _url.getPort();
            std::string authority = _url.getHost() + ":" + std::to_string(_url.getPort());

            std::vector<qpack::HeaderField> extra;
            for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
@@ -854,7 +849,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            }

            std::vector<uint8_t> headers = qpack::Encoder::encodeRequestHeaders(
                "GET", scheme, authority.str(), path, extra, false, 0);
                "GET", scheme, authority, path, extra, false, 0);

            std::vector<uint8_t> stream_payload;
            h3AppendFrame(stream_payload, 0x01, headers);
@@ -1011,8 +1006,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            std::string path = nreq.getRequestURL();
            if (path.empty()) path = _url.getPath();
            std::string scheme = (_url.getProtocol() == HttpUrl::HTTPS) ? "https" : "http";
            std::stringstream auth;
            auth << _url.getHost() << ":" << _url.getPort();
            std::string auth = _url.getHost() + ":" + std::to_string(_url.getPort());

            std::vector<hpack::HeaderField> extra;
            for (HttpHeader::HeaderData *hd = nreq.getfirstHeaderData(); hd; hd = hd->nextHeaderData()) {
@@ -1024,7 +1018,7 @@ libhttppp::HttpResponse libhttppp::HttpClient::GetStream(libhttppp::HttpRequest
            }

            std::string hpack_block = hpack::Encoder::encodeRequestHeaders(
                "GET", path, scheme, auth.str(), extra);
                "GET", path, scheme, auth, extra);

            uint32_t stream_id = _h2NextStreamId;
            _h2NextStreamId += 2;
@@ -1330,11 +1324,8 @@ libhttppp::HttpResponse libhttppp::HttpClient::PostStream(libhttppp::HttpRequest
            throw he;
        }

        std::stringstream host;
        host << _url.getHost() << ":" << _url.getPort();

        if (!nreq.getHeaderData("host"))
            nreq.setHeaderData("host")->push_back(host.str());
            nreq.setHeaderData("host")->push_back(_url.getHost() + ":" + std::to_string(_url.getPort()));
        nreq.setRequestType(POSTREQUEST);

        if (nreq.getRequestURL().empty())
@@ -2492,8 +2483,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(

        std::string scheme = (_url.getProtocol() == HttpUrl::HTTPS) ? "https" : "http";

        std::stringstream auth;
        auth << _url.getHost() << ":" << _url.getPort();
        std::string auth = _url.getHost() + ":" + std::to_string(_url.getPort());

        // Collect extra headers from the request
        bool has_content_length = false;
@@ -2514,7 +2504,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(
        }

        std::string hpack_block = hpack::Encoder::encodeRequestHeaders(
            method, path, scheme, auth.str(), extra);
            method, path, scheme, auth, extra);

        uint32_t stream_id = _h2NextStreamId;
        _h2NextStreamId += 2;  // Client streams use odd IDs: 1, 3, 5, ...
@@ -2818,8 +2808,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(

      std::string scheme = (_url.getProtocol() == HttpUrl::HTTP) ? "http" : "https";

      std::stringstream authority;
      authority << _url.getHost() << ":" << _url.getPort();
      std::string authority = _url.getHost() + ":" + std::to_string(_url.getPort());

      bool include_content_length = false;
      size_t content_length = 0;
@@ -2846,7 +2835,7 @@ const std::vector<char> libhttppp::HttpClient::_h2Request(
      }

      std::vector<uint8_t> headers = qpack::Encoder::encodeRequestHeaders(
        method, scheme, authority.str(), path, extra, include_content_length, content_length);
        method, scheme, authority, path, extra, include_content_length, content_length);

      std::vector<uint8_t> stream_payload;
      h3AppendFrame(stream_payload, 0x01, headers);
@@ -3328,15 +3317,11 @@ void libhttppp::HttpHeader::HeaderData::push_back(const std::string &val){
}

void libhttppp::HttpHeader::HeaderData::push_back(int val){
  std::stringstream ss;
  ss << val;
  push_back(ss.str());
  push_back(std::to_string(val));
}

void libhttppp::HttpHeader::HeaderData::push_back(size_t val){
  std::stringstream ss;
  ss << val;
  push_back(ss.str());
  push_back(std::to_string(val));
}


+16 −8
Changes for src/https.cpp: 16 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -30,14 +30,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <openssl/x509.h>

#include "https.h"
#include "exception.h"

libhttppp::HTTPS::HTTPS(){
  _CTX=NULL;
}

libhttppp::HTTPS::~HTTPS(){
  // EVP_cleanup() used to be called here, but that tears down OpenSSL's
  // global algorithm tables for the entire process -- any other HTTPS/TLS
  // connection still alive elsewhere in the same process (e.g. HttpClient's
  // own netplus::ssl-based connections) would break the moment any HTTPS
  // instance is destroyed. Global library teardown does not belong in a
  // per-object destructor.
  SSL_CTX_free(_CTX);
  EVP_cleanup();
}

void libhttppp::HTTPS::createContext(){
@@ -47,9 +53,9 @@ void libhttppp::HTTPS::createContext(){
  method = SSLv23_server_method();
  _CTX = SSL_CTX_new(method);
  if (!_CTX) {
    perror("Unable to create SSL context");
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
    HTTPException ee;
    ee[HTTPException::Error] << "Unable to create SSL context";
    throw ee;
  }
  SSL_CTX_set_ecdh_auto(_CTX, 1);
}
@@ -66,15 +72,17 @@ void libhttppp::HTTPS::setKey(const unsigned char* key,size_t keylen){

void libhttppp::HTTPS::loadCertfile(const char* crtpath){
  if (SSL_CTX_use_certificate_file(_CTX, crtpath, SSL_FILETYPE_PEM) <= 0) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
    HTTPException ee;
    ee[HTTPException::Error] << "Unable to load certificate file: " << crtpath;
    throw ee;
  }
}

void libhttppp::HTTPS::loadKeyfile(const char* keyfile){
  if (SSL_CTX_use_PrivateKey_file(_CTX, keyfile, SSL_FILETYPE_PEM) <= 0 ) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
    HTTPException ee;
    ee[HTTPException::Error] << "Unable to load private key file: " << keyfile;
    throw ee;
  }
}