Commit 4b50f411 authored by jan.koester's avatar jan.koester
Browse files

test

parent e548ded2
Loading
Loading
Loading
Loading
+8 −126
Changes for src/http.cpp: 8 added lines, 126 removed lines.
Original line number Diff line number Diff line
@@ -1765,135 +1765,17 @@ void libhttppp::HttpRequest::send(const HttpUrl &dest,
    size_t off = 0;

    netplus::x509cert cert;

    // ---------------------------------------------------------
    // helper: connect (nonblock) + handshake + flush progress
    // ---------------------------------------------------------
    auto progress_connect_and_handshake = [&]() -> void {

        // 1) connect step: solange connect nicht fertig, wird connect()
        //    entweder erfolgreich returnen oder Note werfen.
        for (;;) {
            try {
    if(!sock || sock->fd()<0){
                    if (dest.getProtocol() == HttpUrl::HTTPS) {
                        sock = std::make_unique<netplus::ssl>(cert);
                    } else {
                        sock = std::make_unique<netplus::tcp>();
                    }
                }
                break; // connected
            } catch (netplus::NetException &e) {
                if (e.getErrorType() == netplus::NetException::Note) {
                    std::this_thread::sleep_for(std::chrono::milliseconds(1));
                    continue;
                }
                throw; // real error
            }
        }

        // 2) TLS handshake progress
        while (!sock->getHandshakeDone()) {
            try {
                sock->handshake_after_accept();

                // falls handshake was queued hat
                if (sock->hasPendingWrite())
                    sock->flush_out();

            } catch (netplus::NetException &e) {
                if (e.getErrorType() == netplus::NetException::Note) {
                    // would block -> warten, dann nochmal
                    std::this_thread::sleep_for(std::chrono::milliseconds(1));
                    continue;
                }
                throw;
            }
        }

        // 3) flush remaining queued stuff
        while (sock->hasPendingWrite()) {
            try {
                sock->flush_out();
            } catch (netplus::NetException &e) {
                if (e.getErrorType() == netplus::NetException::Note) {
                    std::this_thread::sleep_for(std::chrono::milliseconds(1));
                    continue;
                }
                throw;
            }
        }
    };

    // ---------------------------------------------------------
    // 1) Ensure connect + handshake ready (blocking)
    // ---------------------------------------------------------
    try {
        progress_connect_and_handshake();
    } catch (netplus::NetException &e) {
        HTTPException ex;
        ex[HTTPException::Error] << "RequestSend connect/handshake failed: " << e.what();
        throw ex;
    }

    // ---------------------------------------------------------
    // 2) Send full request (blocking loop with Note retry)
    // ---------------------------------------------------------
    while (off < full.size()) {
        try {
            netplus::buffer chunk(full.data() + off, full.size() - off);

            size_t n = sock->sendData(chunk, 0);
            if (n == 0) {
                netplus::NetException ne;
                ne[netplus::NetException::Error]
                    << "HttpRequest::send: sendData returned 0 (peer closed?)";
                throw ne;
            }

            off += n;

            // TLS: flush queued ciphertext records
            if (sock->hasPendingWrite()) {
                sock->flush_out();
            }

        } catch (netplus::NetException &e) {
            if (e.getErrorType() == netplus::NetException::Note) {

                // wichtig: bei SSL zuerst flush versuchen (falls send queued)
                try {
                    if (sock->hasPendingWrite())
                        sock->flush_out();
                } catch (netplus::NetException &fe) {
                    if (fe.getErrorType() != netplus::NetException::Note)
                        throw;
                }

                std::this_thread::sleep_for(std::chrono::milliseconds(1));
                continue;
            }

            HTTPException ex;
            ex[HTTPException::Error] << "RequestSend Socket Error: " << e.what();
            throw ex;
        }
    }

    // ---------------------------------------------------------
    // 3) Final flush (TLS)
    // ---------------------------------------------------------
    try {
        while (sock->hasPendingWrite()) {
            sock->flush_out();
        }
    } catch (netplus::NetException &e) {
        if (e.getErrorType() != netplus::NetException::Note) {
            HTTPException ex;
            ex[HTTPException::Error] << "RequestSend final flush failed: " << e.what();
            throw ex;
        if(!sock){
            if(dest.getProtocol()==HttpUrl::HTTPS)
                sock=std::make_unique<netplus::ssl>(cert,-1);
            else
                sock=std::make_unique<netplus::tcp>(-1);
        }
        sock->connect(dest.getHost(),dest.getPort(),true);
    }
    netplus::buffer buf(full.c_str(),full.length());
    sock->sendData(buf,0);
}

void libhttppp::HttpRequest::setRequestType(int req){