Loading examples/httpcookie.cpp +11 −10 Original line number Diff line number Diff line Loading @@ -69,9 +69,7 @@ public: _Curres.send(_Curreq,html.c_str(),html.size()); }; const char *getData(const char *key){ libhttppp::HttpForm curform; curform.parse(_Curreq); const char *getData(const char *key,libhttppp::HttpForm &curform){ for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData() ; cururlform; cururlform=cururlform->nextData()){ if(strcmp(key,cururlform->getKey())==0) Loading @@ -81,9 +79,7 @@ public: return nullptr; } int getDataInt(const char *key){ libhttppp::HttpForm curform; curform.parse(_Curreq); int getDataInt(const char *key,libhttppp::HttpForm &curform){ for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData(); cururlform; cururlform=cururlform->nextData()){ if(strcmp(key,cururlform->getKey())==0){ Loading @@ -97,7 +93,7 @@ public: void setCookieForm(){ _HTMLDat << "<div style=\"border: thin solid black\"><span>Set Httpcookie</span></br>" << "<form action=\"/\" method=\"post\">" << "<form action=\"/\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">" << "Key:<br> <input type=\"text\" name=\"key\" value=\"key\"><br>" << "Value:<br> <input type=\"text\" name=\"value\" value=\"value\"><br>" << "Comment:<br> <input type=\"text\" name=\"comment\" value=\"\"><br>" Loading @@ -109,8 +105,13 @@ public: << "<button type=\"submit\">Submit</button>" << "</form>"; _HTMLDat << "</div></br>"; if(getData("key")!=nullptr) _Cookie.setcookie(&_Curres,getData("key"),getData("value"),getData("comment"),getData("domain"),getDataInt("max-age"),getData("path"),false,getData("version")); libhttppp::HttpForm curform; curform.parse(_Curreq); if(getData("key",curform)!=nullptr){ _Cookie.setcookie(&_Curres,getData("key",curform),getData("value",curform),getData("comment",curform),getData("domain",curform), getDataInt("max-age",curform),getData("path",curform),false,getData("version",curform)); } }; void parseCookie() { Loading src/http.cpp +54 −61 Original line number Diff line number Diff line Loading @@ -573,6 +573,10 @@ HEADERENDFOUND: while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(++iv==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; Loading Loading @@ -745,6 +749,10 @@ size_t libhttppp::HttpRequest::parse(){ while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(++iv==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; Loading Loading @@ -913,16 +921,28 @@ void libhttppp::HttpForm::parse(libhttppp::HttpRequest* request){ throw excep; } for(HttpHeader::HeaderData::Values *cval=ctype->getfirstValue(); cval; cval=cval->nextvalue()){ if(request->getContentLength() <= request->RecvData.size()){ if(ctype && ctype->at(0).getvalue().find("multipart/form-data",17)){ // _parseBoundary(ctype); if(cval->getvalue().find("multipart/form-data") !=std::string::npos){ for(HttpHeader::HeaderData::Values *cbod=ctype->getfirstValue(); cbod; cbod=cbod->nextvalue()){ size_t fcbod=std::string::npos; if( (fcbod=cbod->getvalue().find("boundary="))!=std::string::npos ) { fcbod+=9; _Boundary.clear(); _Boundary.resize(cbod->getvalue().length()-fcbod); _Boundary.insert(_Boundary.begin(),std::next(cbod->getvalue().begin(),fcbod),cbod->getvalue().end()); _Boundary.push_back('\0'); break; } } _parseMulitpart(request->RecvData); } if(ctype && ctype->at(0).getvalue().find("application/x-www-form-urlencoded",34)){ if(cval->getvalue().find("application/x-www-form-urlencoded")!=std::string::npos){ _parseUrlDecode(request->RecvData,request->getContentLength()); } } } } netplus::condata<char> urldat; const char *rurl=request->getRequest(); size_t rurlsize=strlen(rurl); Loading Loading @@ -970,6 +990,8 @@ long libhttppp::HttpForm::urlDecode(const std::vector<char> in,std::vector<char> } const char *libhttppp::HttpForm::getBoundary(){ if(_Boundary.empty()) return nullptr; return _Boundary.data(); } Loading @@ -977,39 +999,7 @@ size_t libhttppp::HttpForm::getBoundarySize(){ return _Boundary.size(); } void libhttppp::HttpForm::_parseBoundary(const char* contenttype){ size_t ctstartpos=0; size_t ctendpos=0; const char* boundary="boundary="; size_t bdpos=0; for(size_t cpos=0; cpos<strlen(contenttype); cpos++){ if(bdpos==(strlen(boundary)-1)){ break; }else if(contenttype[cpos]==boundary[bdpos]){ if(ctstartpos==0) ctstartpos=cpos; bdpos++; }else{ bdpos=0; ctstartpos=0; } } for(size_t cpos=ctstartpos; cpos<strlen(contenttype); cpos++){ if(contenttype[cpos]==';'){ ctendpos=cpos; } } if(ctendpos==0) ctendpos=strlen(contenttype); /*cut boundary=*/ ctstartpos+=strlen(boundary); std::copy(contenttype+ctstartpos,contenttype+ctendpos, std::inserter<std::vector<char>>(_Boundary,_Boundary.begin())); _Boundary.push_back('\0'); } #include <iostream> void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ std::vector<char> realboundary; realboundary.resize(_Boundary.size()+2); Loading @@ -1017,6 +1007,8 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ realboundary[0]='-'; realboundary[1]='-'; std::cout.write(realboundary.data(),realboundary.size()) << std::endl; netplus::condata<char> req; std::copy(data.begin(),data.end(), Loading @@ -1029,7 +1021,7 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ //check if boundary if((char)tolower(req[cr])==realboundary[realboundarypos++]){ //check if boundary completed if(realboundarypos==realboundary.size()-1){ if(realboundary[realboundarypos]=='\0'){ //ceck if boundary before found set data end if(oldpos!=std::string::npos) _parseMultiSection(req,oldpos,cr-realboundarypos); Loading @@ -1046,6 +1038,7 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ realboundarypos=0; } } } void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t start, size_t end){ Loading Loading @@ -1358,12 +1351,12 @@ void libhttppp::HttpForm::_parseUrlDecode(const netplus::condata<char> &data,siz size_t vlstpos=keyendpos+1; std::vector<char> value,uvalue; std::copy(data.begin()+fdatstpos,data.begin()+keyendpos,std::inserter<std::vector<char>>(key,key.begin())); key.push_back('\0'); urlDecode(key,ukey); ukey.push_back('\0'); if(vlstpos<=data.size()){ std::copy(data.begin()+vlstpos,data.begin()+fdatpos,std::inserter<std::vector<char>>(value,value.begin())); value.push_back('\0'); urlDecode(value,uvalue); uvalue.push_back('\0'); } UrlcodedForm::Data urldat(ukey.data(),uvalue.data()); UrlFormData.addFormData(urldat); Loading Loading @@ -1405,7 +1398,7 @@ libhttppp::HttpForm::UrlcodedForm::Data *libhttppp::HttpForm::UrlcodedForm::Dat return _next; } libhttppp::HttpForm::UrlcodedForm::Data::Data(Data& fdat){ libhttppp::HttpForm::UrlcodedForm::Data::Data(const Data& fdat){ _Key=fdat._Key; _Value=fdat._Value; _next = nullptr; Loading Loading @@ -1495,41 +1488,46 @@ void libhttppp::HttpCookie::setcookie(HttpResponse *curresp, } HttpHeader::HeaderData *dat=curresp->setHeaderData("set-cookie"); std::string buf=key; buf+="="; buf+=value; dat->push_back(buf); if(comment){ std::string buf="comment="; buf="comment="; buf+=comment; dat->push_back(buf); } if(domain){ std::string buf="domain="; buf="domain="; buf+=domain; dat->push_back(buf); } if(maxage>=0){ std::string buf="max-age="; buf="max-age="; buf+=maxage; dat->push_back(buf); } if(path){ std::string buf="path="; buf="path="; buf+=path; dat->push_back(buf); } if(samesite){ std::string buf="sameSite="; buf="sameSite="; buf+=samesite; dat->push_back(buf); }else{ buf="sameSite=Lax"; dat->push_back(buf); } if(secure){ std::string buf="secure"; buf="secure"; buf+=secure; dat->push_back(buf); } if(version){ std::string buf="version="; buf="version="; buf+=version; dat->push_back(buf); } Loading @@ -1544,19 +1542,14 @@ void libhttppp::HttpCookie::parse(libhttppp::HttpRequest* curreq){ throw httpexception; } size_t fend=0; for (HttpHeader::HeaderData::Values *cval=hc->getfirstValue(); cval; cval=cval->nextvalue() ) { std::string cpair=cval->getvalue(); size_t fdel = cpair.find("=",fdel); size_t fdel = cpair.find("="); if(fdel!=std::string::npos){ std::string key=cpair.substr(fend,fdel); fend=cpair.find(";",++fdel); if(fend==std::string::npos) fend=cpair.length(); std::string value=cpair.substr(fdel,fend); std::string key=cpair.substr(0,fdel++); std::string value=cpair.substr(fdel,cpair.length()-fdel); if(!key.empty()){ CookieData *cdat=addCookieData(); Loading src/http.h +1 −2 Original line number Diff line number Diff line Loading @@ -284,7 +284,7 @@ namespace libhttppp { public: class Data { public: Data(Data &fdat); Data(const Data &fdat); Data(const char *key,const char *value); ~Data(); const char *getKey(); Loading Loading @@ -329,7 +329,6 @@ namespace libhttppp { /*multiform*/ void _parseMulitpart(const netplus::condata<char> &data); void _parseMultiSection(netplus::condata<char> &data,size_t start, size_t end); void _parseBoundary(const char *contenttype); std::vector<char> _Boundary; /*both methods*/ Loading Loading
examples/httpcookie.cpp +11 −10 Original line number Diff line number Diff line Loading @@ -69,9 +69,7 @@ public: _Curres.send(_Curreq,html.c_str(),html.size()); }; const char *getData(const char *key){ libhttppp::HttpForm curform; curform.parse(_Curreq); const char *getData(const char *key,libhttppp::HttpForm &curform){ for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData() ; cururlform; cururlform=cururlform->nextData()){ if(strcmp(key,cururlform->getKey())==0) Loading @@ -81,9 +79,7 @@ public: return nullptr; } int getDataInt(const char *key){ libhttppp::HttpForm curform; curform.parse(_Curreq); int getDataInt(const char *key,libhttppp::HttpForm &curform){ for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData(); cururlform; cururlform=cururlform->nextData()){ if(strcmp(key,cururlform->getKey())==0){ Loading @@ -97,7 +93,7 @@ public: void setCookieForm(){ _HTMLDat << "<div style=\"border: thin solid black\"><span>Set Httpcookie</span></br>" << "<form action=\"/\" method=\"post\">" << "<form action=\"/\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">" << "Key:<br> <input type=\"text\" name=\"key\" value=\"key\"><br>" << "Value:<br> <input type=\"text\" name=\"value\" value=\"value\"><br>" << "Comment:<br> <input type=\"text\" name=\"comment\" value=\"\"><br>" Loading @@ -109,8 +105,13 @@ public: << "<button type=\"submit\">Submit</button>" << "</form>"; _HTMLDat << "</div></br>"; if(getData("key")!=nullptr) _Cookie.setcookie(&_Curres,getData("key"),getData("value"),getData("comment"),getData("domain"),getDataInt("max-age"),getData("path"),false,getData("version")); libhttppp::HttpForm curform; curform.parse(_Curreq); if(getData("key",curform)!=nullptr){ _Cookie.setcookie(&_Curres,getData("key",curform),getData("value",curform),getData("comment",curform),getData("domain",curform), getDataInt("max-age",curform),getData("path",curform),false,getData("version",curform)); } }; void parseCookie() { Loading
src/http.cpp +54 −61 Original line number Diff line number Diff line Loading @@ -573,6 +573,10 @@ HEADERENDFOUND: while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(++iv==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; Loading Loading @@ -745,6 +749,10 @@ size_t libhttppp::HttpRequest::parse(){ while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(++iv==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; Loading Loading @@ -913,16 +921,28 @@ void libhttppp::HttpForm::parse(libhttppp::HttpRequest* request){ throw excep; } for(HttpHeader::HeaderData::Values *cval=ctype->getfirstValue(); cval; cval=cval->nextvalue()){ if(request->getContentLength() <= request->RecvData.size()){ if(ctype && ctype->at(0).getvalue().find("multipart/form-data",17)){ // _parseBoundary(ctype); if(cval->getvalue().find("multipart/form-data") !=std::string::npos){ for(HttpHeader::HeaderData::Values *cbod=ctype->getfirstValue(); cbod; cbod=cbod->nextvalue()){ size_t fcbod=std::string::npos; if( (fcbod=cbod->getvalue().find("boundary="))!=std::string::npos ) { fcbod+=9; _Boundary.clear(); _Boundary.resize(cbod->getvalue().length()-fcbod); _Boundary.insert(_Boundary.begin(),std::next(cbod->getvalue().begin(),fcbod),cbod->getvalue().end()); _Boundary.push_back('\0'); break; } } _parseMulitpart(request->RecvData); } if(ctype && ctype->at(0).getvalue().find("application/x-www-form-urlencoded",34)){ if(cval->getvalue().find("application/x-www-form-urlencoded")!=std::string::npos){ _parseUrlDecode(request->RecvData,request->getContentLength()); } } } } netplus::condata<char> urldat; const char *rurl=request->getRequest(); size_t rurlsize=strlen(rurl); Loading Loading @@ -970,6 +990,8 @@ long libhttppp::HttpForm::urlDecode(const std::vector<char> in,std::vector<char> } const char *libhttppp::HttpForm::getBoundary(){ if(_Boundary.empty()) return nullptr; return _Boundary.data(); } Loading @@ -977,39 +999,7 @@ size_t libhttppp::HttpForm::getBoundarySize(){ return _Boundary.size(); } void libhttppp::HttpForm::_parseBoundary(const char* contenttype){ size_t ctstartpos=0; size_t ctendpos=0; const char* boundary="boundary="; size_t bdpos=0; for(size_t cpos=0; cpos<strlen(contenttype); cpos++){ if(bdpos==(strlen(boundary)-1)){ break; }else if(contenttype[cpos]==boundary[bdpos]){ if(ctstartpos==0) ctstartpos=cpos; bdpos++; }else{ bdpos=0; ctstartpos=0; } } for(size_t cpos=ctstartpos; cpos<strlen(contenttype); cpos++){ if(contenttype[cpos]==';'){ ctendpos=cpos; } } if(ctendpos==0) ctendpos=strlen(contenttype); /*cut boundary=*/ ctstartpos+=strlen(boundary); std::copy(contenttype+ctstartpos,contenttype+ctendpos, std::inserter<std::vector<char>>(_Boundary,_Boundary.begin())); _Boundary.push_back('\0'); } #include <iostream> void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ std::vector<char> realboundary; realboundary.resize(_Boundary.size()+2); Loading @@ -1017,6 +1007,8 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ realboundary[0]='-'; realboundary[1]='-'; std::cout.write(realboundary.data(),realboundary.size()) << std::endl; netplus::condata<char> req; std::copy(data.begin(),data.end(), Loading @@ -1029,7 +1021,7 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ //check if boundary if((char)tolower(req[cr])==realboundary[realboundarypos++]){ //check if boundary completed if(realboundarypos==realboundary.size()-1){ if(realboundary[realboundarypos]=='\0'){ //ceck if boundary before found set data end if(oldpos!=std::string::npos) _parseMultiSection(req,oldpos,cr-realboundarypos); Loading @@ -1046,6 +1038,7 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data){ realboundarypos=0; } } } void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t start, size_t end){ Loading Loading @@ -1358,12 +1351,12 @@ void libhttppp::HttpForm::_parseUrlDecode(const netplus::condata<char> &data,siz size_t vlstpos=keyendpos+1; std::vector<char> value,uvalue; std::copy(data.begin()+fdatstpos,data.begin()+keyendpos,std::inserter<std::vector<char>>(key,key.begin())); key.push_back('\0'); urlDecode(key,ukey); ukey.push_back('\0'); if(vlstpos<=data.size()){ std::copy(data.begin()+vlstpos,data.begin()+fdatpos,std::inserter<std::vector<char>>(value,value.begin())); value.push_back('\0'); urlDecode(value,uvalue); uvalue.push_back('\0'); } UrlcodedForm::Data urldat(ukey.data(),uvalue.data()); UrlFormData.addFormData(urldat); Loading Loading @@ -1405,7 +1398,7 @@ libhttppp::HttpForm::UrlcodedForm::Data *libhttppp::HttpForm::UrlcodedForm::Dat return _next; } libhttppp::HttpForm::UrlcodedForm::Data::Data(Data& fdat){ libhttppp::HttpForm::UrlcodedForm::Data::Data(const Data& fdat){ _Key=fdat._Key; _Value=fdat._Value; _next = nullptr; Loading Loading @@ -1495,41 +1488,46 @@ void libhttppp::HttpCookie::setcookie(HttpResponse *curresp, } HttpHeader::HeaderData *dat=curresp->setHeaderData("set-cookie"); std::string buf=key; buf+="="; buf+=value; dat->push_back(buf); if(comment){ std::string buf="comment="; buf="comment="; buf+=comment; dat->push_back(buf); } if(domain){ std::string buf="domain="; buf="domain="; buf+=domain; dat->push_back(buf); } if(maxage>=0){ std::string buf="max-age="; buf="max-age="; buf+=maxage; dat->push_back(buf); } if(path){ std::string buf="path="; buf="path="; buf+=path; dat->push_back(buf); } if(samesite){ std::string buf="sameSite="; buf="sameSite="; buf+=samesite; dat->push_back(buf); }else{ buf="sameSite=Lax"; dat->push_back(buf); } if(secure){ std::string buf="secure"; buf="secure"; buf+=secure; dat->push_back(buf); } if(version){ std::string buf="version="; buf="version="; buf+=version; dat->push_back(buf); } Loading @@ -1544,19 +1542,14 @@ void libhttppp::HttpCookie::parse(libhttppp::HttpRequest* curreq){ throw httpexception; } size_t fend=0; for (HttpHeader::HeaderData::Values *cval=hc->getfirstValue(); cval; cval=cval->nextvalue() ) { std::string cpair=cval->getvalue(); size_t fdel = cpair.find("=",fdel); size_t fdel = cpair.find("="); if(fdel!=std::string::npos){ std::string key=cpair.substr(fend,fdel); fend=cpair.find(";",++fdel); if(fend==std::string::npos) fend=cpair.length(); std::string value=cpair.substr(fdel,fend); std::string key=cpair.substr(0,fdel++); std::string value=cpair.substr(fdel,cpair.length()-fdel); if(!key.empty()){ CookieData *cdat=addCookieData(); Loading
src/http.h +1 −2 Original line number Diff line number Diff line Loading @@ -284,7 +284,7 @@ namespace libhttppp { public: class Data { public: Data(Data &fdat); Data(const Data &fdat); Data(const char *key,const char *value); ~Data(); const char *getKey(); Loading Loading @@ -329,7 +329,6 @@ namespace libhttppp { /*multiform*/ void _parseMulitpart(const netplus::condata<char> &data); void _parseMultiSection(netplus::condata<char> &data,size_t start, size_t end); void _parseBoundary(const char *contenttype); std::vector<char> _Boundary; /*both methods*/ Loading