Loading src/http.cpp +106 −25 Changes for src/http.cpp: 106 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -1702,42 +1702,123 @@ size_t libhttppp::HttpForm::getBoundarySize(){ } void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data) { std::vector<char> realboundary; realboundary.resize(_Boundary.size()+2); std::copy(_Boundary.begin(),_Boundary.end(),realboundary.begin()+2); realboundary[0]='-'; realboundary[1]='-'; // Build "--" + boundary std::vector<char> boundaryLine; boundaryLine.reserve(_Boundary.size() + 2); boundaryLine.push_back('-'); boundaryLine.push_back('-'); boundaryLine.insert(boundaryLine.end(), _Boundary.begin(), _Boundary.end()); const size_t B = boundaryLine.size(); // Make a working copy (your original did this) netplus::condata<char> req; std::copy(data.begin(), data.end(), std::inserter<netplus::condata<char>>(req, req.begin())); size_t realboundarypos=0; size_t oldpos=std::string::npos; for(size_t cr=0; cr < req.size(); cr++){ //check if boundary if((char)tolower(req[cr])==realboundary[realboundarypos++]){ //check if boundary completed if(realboundarypos==req.size()){ //ceck if boundary before found set data end if(oldpos!=std::string::npos) _parseMultiSection(req,oldpos,cr-realboundarypos); oldpos=cr; //check if boundary finished if(req[cr]=='-' && req[cr+1]=='-'){ //boundary finished realboundary.clear(); // Helper to compare a slice with boundaryLine auto matchBoundaryAt = [&](size_t pos) -> bool { if (pos + B > req.size()) return false; for (size_t i = 0; i < B; ++i) { if (req[pos + i] != boundaryLine[i]) return false; // case-sensitive! } return true; }; // RFC: the very first boundary may appear at start of body ("--BOUNDARY") // or after a preamble. The general delimiter preceding parts is "\r\n--BOUNDARY" size_t i = 0; size_t partStart = std::string::npos; // where current part body starts // Seek first boundary: allow either at 0 or after CRLF // Normalize: if body starts with "--boundary" (no CRLF), consume it and the trailing CRLF. if (matchBoundaryAt(0)) { size_t p = B; // after boundary: either "--" (closing immediately) or "\r\n" if (p + 1 < req.size() && req[p] == '-' && req[p + 1] == '-') { // no parts; closing immediately return; } if (p + 1 >= req.size() || req[p] != '\r' || req[p + 1] != '\n') { // malformed initial boundary line return; } i = p + 2; // position at start of first part headers partStart = i; // start of current part (headers parser will split) } else { //boundary reset if sign not correct realboundarypos=0; // search for "\r\n--boundary" while (true) { // find CRLF size_t crlf = std::string::npos; for (size_t k = i; k + 1 < req.size(); ++k) { if (req[k] == '\r' && req[k + 1] == '\n') { crlf = k; break; } } if (crlf == std::string::npos) return; size_t bpos = crlf + 2; if (!matchBoundaryAt(bpos)) { i = bpos; continue; } // After boundary: "--" (closing) or "\r\n" (next part) size_t p = bpos + B; if (p + 1 < req.size() && req[p] == '-' && req[p + 1] == '-') { // closing before any part → nothing to parse return; } if (p + 1 >= req.size() || req[p] != '\r' || req[p + 1] != '\n') return; // malformed i = p + 2; // start of first part headers partStart = i; break; } } // Now iterate over parts: each ends just before "\r\n--boundary" or "\r\n--boundary--" while (true) { // Find next delimiter line: look for "\r\n--boundary" from i onward size_t delimPos = std::string::npos; size_t closePos = std::string::npos; // scan for CRLF then boundary for (size_t k = i; k + 2 + B <= req.size(); ++k) { if (req[k] == '\r' && req[k + 1] == '\n' && matchBoundaryAt(k + 2)) { // is this closing? check for "--" after boundary size_t after = k + 2 + B; if (after + 1 < req.size() && req[after] == '-' && req[after + 1] == '-') { closePos = k; // boundary that closes the multipart break; // closing takes precedence } else { delimPos = k; // normal part delimiter break; } } } if (delimPos == std::string::npos && closePos == std::string::npos) { // No terminating boundary found → malformed return; } size_t partEnd = (closePos != std::string::npos) ? closePos : delimPos; // points to CR of the CRLF before boundary // Trim trailing CRLF from part body if present if (partEnd >= 2 && req[partEnd - 2] == '\r' && req[partEnd - 1] == '\n') { partEnd -= 2; } if (partStart != std::string::npos && partEnd >= partStart) { _parseMultiSection(req, partStart, partEnd); } if (closePos != std::string::npos) { // Reached closing boundary (“--boundary--”), done return; } // Move i to start of next part: skip CRLF + "--boundary" + CRLF size_t after = delimPos + 2 + B; // after "\r\n--boundary" if (after + 1 >= req.size() || req[after] != '\r' || req[after + 1] != '\n') { // malformed separator return; } i = after + 2; // start of next part headers partStart = i; } } void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t start, size_t end){ Loading Loading
src/http.cpp +106 −25 Changes for src/http.cpp: 106 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -1702,42 +1702,123 @@ size_t libhttppp::HttpForm::getBoundarySize(){ } void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data) { std::vector<char> realboundary; realboundary.resize(_Boundary.size()+2); std::copy(_Boundary.begin(),_Boundary.end(),realboundary.begin()+2); realboundary[0]='-'; realboundary[1]='-'; // Build "--" + boundary std::vector<char> boundaryLine; boundaryLine.reserve(_Boundary.size() + 2); boundaryLine.push_back('-'); boundaryLine.push_back('-'); boundaryLine.insert(boundaryLine.end(), _Boundary.begin(), _Boundary.end()); const size_t B = boundaryLine.size(); // Make a working copy (your original did this) netplus::condata<char> req; std::copy(data.begin(), data.end(), std::inserter<netplus::condata<char>>(req, req.begin())); size_t realboundarypos=0; size_t oldpos=std::string::npos; for(size_t cr=0; cr < req.size(); cr++){ //check if boundary if((char)tolower(req[cr])==realboundary[realboundarypos++]){ //check if boundary completed if(realboundarypos==req.size()){ //ceck if boundary before found set data end if(oldpos!=std::string::npos) _parseMultiSection(req,oldpos,cr-realboundarypos); oldpos=cr; //check if boundary finished if(req[cr]=='-' && req[cr+1]=='-'){ //boundary finished realboundary.clear(); // Helper to compare a slice with boundaryLine auto matchBoundaryAt = [&](size_t pos) -> bool { if (pos + B > req.size()) return false; for (size_t i = 0; i < B; ++i) { if (req[pos + i] != boundaryLine[i]) return false; // case-sensitive! } return true; }; // RFC: the very first boundary may appear at start of body ("--BOUNDARY") // or after a preamble. The general delimiter preceding parts is "\r\n--BOUNDARY" size_t i = 0; size_t partStart = std::string::npos; // where current part body starts // Seek first boundary: allow either at 0 or after CRLF // Normalize: if body starts with "--boundary" (no CRLF), consume it and the trailing CRLF. if (matchBoundaryAt(0)) { size_t p = B; // after boundary: either "--" (closing immediately) or "\r\n" if (p + 1 < req.size() && req[p] == '-' && req[p + 1] == '-') { // no parts; closing immediately return; } if (p + 1 >= req.size() || req[p] != '\r' || req[p + 1] != '\n') { // malformed initial boundary line return; } i = p + 2; // position at start of first part headers partStart = i; // start of current part (headers parser will split) } else { //boundary reset if sign not correct realboundarypos=0; // search for "\r\n--boundary" while (true) { // find CRLF size_t crlf = std::string::npos; for (size_t k = i; k + 1 < req.size(); ++k) { if (req[k] == '\r' && req[k + 1] == '\n') { crlf = k; break; } } if (crlf == std::string::npos) return; size_t bpos = crlf + 2; if (!matchBoundaryAt(bpos)) { i = bpos; continue; } // After boundary: "--" (closing) or "\r\n" (next part) size_t p = bpos + B; if (p + 1 < req.size() && req[p] == '-' && req[p + 1] == '-') { // closing before any part → nothing to parse return; } if (p + 1 >= req.size() || req[p] != '\r' || req[p + 1] != '\n') return; // malformed i = p + 2; // start of first part headers partStart = i; break; } } // Now iterate over parts: each ends just before "\r\n--boundary" or "\r\n--boundary--" while (true) { // Find next delimiter line: look for "\r\n--boundary" from i onward size_t delimPos = std::string::npos; size_t closePos = std::string::npos; // scan for CRLF then boundary for (size_t k = i; k + 2 + B <= req.size(); ++k) { if (req[k] == '\r' && req[k + 1] == '\n' && matchBoundaryAt(k + 2)) { // is this closing? check for "--" after boundary size_t after = k + 2 + B; if (after + 1 < req.size() && req[after] == '-' && req[after + 1] == '-') { closePos = k; // boundary that closes the multipart break; // closing takes precedence } else { delimPos = k; // normal part delimiter break; } } } if (delimPos == std::string::npos && closePos == std::string::npos) { // No terminating boundary found → malformed return; } size_t partEnd = (closePos != std::string::npos) ? closePos : delimPos; // points to CR of the CRLF before boundary // Trim trailing CRLF from part body if present if (partEnd >= 2 && req[partEnd - 2] == '\r' && req[partEnd - 1] == '\n') { partEnd -= 2; } if (partStart != std::string::npos && partEnd >= partStart) { _parseMultiSection(req, partStart, partEnd); } if (closePos != std::string::npos) { // Reached closing boundary (“--boundary--”), done return; } // Move i to start of next part: skip CRLF + "--boundary" + CRLF size_t after = delimPos + 2 + B; // after "\r\n--boundary" if (after + 1 >= req.size() || req[after] != '\r' || req[after + 1] != '\n') { // malformed separator return; } i = after + 2; // start of next part headers partStart = i; } } void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t start, size_t end){ Loading