Commit 62d4298f authored by jan.koester's avatar jan.koester
Browse files

test

parent c3487e8a
Loading
Loading
Loading
Loading
+23 −10
Changes for src/http.cpp: 23 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -397,11 +397,25 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

        int recv, chunklen = 0;

        // --- TIMEOUT WATCHDOG SETUP ---
        using namespace std::chrono;
        auto start = steady_clock::now();
        auto timeout = seconds(5); // 5 second maximum wait time for the initial response header
        // --- END WATCHDOG SETUP ---


        // Read HTTP response header
        try{
            for(;;){
                // 🛑 CRITICAL CHECK: Enforce the overall timeout
                if (steady_clock::now() - start > timeout) {
                    throw netplus::NetException()
                        [netplus::NetException::Error] << "Timeout waiting for server response header (5s limit exceeded).";
                }

                try{
                    recv=_cltsock->recvData(data);

                    // Check for immediate EOF after sending request
                    if (recv <= 0) {
                        throw netplus::NetException()[netplus::NetException::Error] << "Server closed connection before sending response.";
@@ -411,7 +425,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                    if(re.getErrorType()!=netplus::NetException::Note){
                        throw re;
                    }else{
                        // FIX: Increased sleep for polite waiting
                        // FIX: Wait politely (10ms) and try again
                        std::this_thread::sleep_for(std::chrono::milliseconds(10));
                        continue;
                    }
@@ -426,12 +440,14 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
        libhttppp::HttpResponse res;
        hsize=res.parse(data.data.buf,recv);

        recv-=++hsize;
        recv-=hsize;

        // Shift remaining data to the front of the buffer
        std::move(data.data.buf+hsize,data.data.buf+BLOCKSIZE,data.data.buf);

        cpos=0;

        // Determine if response is chunked or has Content-Length
        try{
            for(libhttppp::HttpHeader::HeaderData::Values *chdat=res.getTransferEncoding(); chdat; chdat=chdat->nextvalue()){
                if(chdat->getvalue()=="chunked")
@@ -455,9 +471,8 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                            try{
                                recv=_cltsock->recvData(data);

                                // 🛑 CRITICAL FIX: Check if recv is 0 (EOF) or less
                                // 🛑 CRITICAL EOF CHECK: connection closed before Content-Length was satisfied
                                if (recv <= 0) {
                                    // Premature EOF check: connection closed before Content-Length was satisfied
                                    throw netplus::NetException()
                                    [netplus::NetException::Error] << "Premature EOF while reading HTTP response body.";
                                }
@@ -465,7 +480,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                                break;
                            }catch(netplus::NetException &e){
                                if(e.getErrorType()==netplus::NetException::Note){
                                    // FIX: Increased sleep for polite waiting
                                    // FIX: Wait politely (10ms)
                                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                                    continue;
                                }
@@ -488,10 +503,9 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

            for(;;){
                if(recv - cpos > 0){

                    if(readed==chunklen){
                        if( (chunklen=readchunk(data.data.buf,recv,cpos)) == 0 ){
                            break; // All chunks read, including the final 0-byte chunk
                            break;
                        }
                        readed=0;
                    }
@@ -507,9 +521,8 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                        try{
                            recv=_cltsock->recvData(data);

                            // 🛑 CRITICAL FIX: Check if recv is 0 (EOF) or less
                            // 🛑 CRITICAL EOF CHECK: premature connection close
                            if (recv <= 0) {
                                // If we get EOF mid-chunk or while expecting a chunk header, it's an error
                                throw netplus::NetException()
                                    [netplus::NetException::Error] << "Premature EOF while reading chunked body.";
                            }
@@ -521,7 +534,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                                ee[libhttppp::HTTPException::Error] << e.what();
                                throw ee;
                            }else{
                                // FIX: Increased sleep for polite waiting
                                // FIX: Wait politely (10ms)
                                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                                continue;
                            }