Commit c3487e8a authored by jan.koester's avatar jan.koester
Browse files

test

parent cca25718
Loading
Loading
Loading
Loading
+24 −14
Changes for src/http.cpp: 24 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -385,6 +385,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

        size_t sended=0;

        // Send POST body data
        try {
            while(sended!=post.size()){
                netplus::buffer data(post.data()+sended,post.size()-sended);
@@ -396,16 +397,22 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

        int recv,chunklen=0;

        // Read HTTP response header
        try{
            for(;;){
                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.";
                    }
                    break;
                }catch(netplus::NetException &re){
                    if(re.getErrorType()!=netplus::NetException::Note){
                        throw re;
                    }else{
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
                        // FIX: Increased sleep for polite waiting
                        std::this_thread::sleep_for(std::chrono::milliseconds(10));
                        continue;
                    }
                }
@@ -419,7 +426,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
        libhttppp::HttpResponse res;
        hsize=res.parse(data.data.buf,recv);

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

        std::move(data.data.buf+hsize,data.data.buf+BLOCKSIZE,data.data.buf);

@@ -427,11 +434,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

        try{
            for(libhttppp::HttpHeader::HeaderData::Values *chdat=res.getTransferEncoding(); chdat; chdat=chdat->nextvalue()){
        std::string clstr;
        std::transform(chdat->getvalue().begin(), chdat->getvalue().end(), clstr.begin(),
            [](unsigned char c){ return static_cast<char>(std::tolower(c)); }
        );
        if(clstr=="chunked")
                if(chdat->getvalue()=="chunked")
                    chunked=true;
            }
            if(!chunked)
@@ -440,7 +443,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
            rlen=res.getContentLength();
        }


        // --- Non-Chunked Body Reading (Content-Length) ---
        if(!chunked){
            do{
                try{
@@ -452,7 +455,9 @@ 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
                                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.";
                                }
@@ -460,6 +465,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
                                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                                    continue;
                                }
@@ -474,8 +480,10 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                    throw ee;
                }
            }while(rlen>0);
    }else{
        }

        // --- Chunked Body Reading (Transfer-Encoding: chunked) ---
        else{
            int readed=0;

            for(;;){
@@ -483,7 +491,7 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq

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

                            // 🛑 CRITICAL FIX: Check if recv is 0 (EOF) or less
                            if (recv <= 0) {
                    if (readed > 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.";
                            }
                    break;
              }

                            break;
                        }catch(netplus::NetException &e){
                            if(e.getErrorType()!=netplus::NetException::Note){
@@ -512,7 +521,8 @@ const std::vector<char> libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq
                                ee[libhttppp::HTTPException::Error] << e.what();
                                throw ee;
                            }else{
                   std::this_thread::sleep_for(std::chrono::milliseconds(1));
                                // FIX: Increased sleep for polite waiting
                                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                                continue;
                            }
                        }