Loading examples/httpclient.cpp +11 −11 Changes for examples/httpclient.cpp: 11 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -78,9 +78,9 @@ int main(int argc, char** argv){ #endif netplus::tcp cltsock(argv[1], atoi(argv[2]), 1, 0); netplus::tcp srvsock; char *data=new char[16384]; try{ srvsock.connect(&cltsock); try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); Loading @@ -98,9 +98,7 @@ int main(int argc, char** argv){ return -1; } std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); size_t recv= srvsock.recvData(data, 16384,0); size_t recv= srvsock.recvData(&data, 16384,0); std::vector<char> html; libhttppp::HttpResponse res; Loading @@ -108,7 +106,7 @@ int main(int argc, char** argv){ bool chunked=false; long long rlen=0; hsize=res.parse(data.get(), recv); hsize=res.parse(data, recv); try { for(libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); cval; cval=cval->nextvalue()){ Loading @@ -125,10 +123,10 @@ int main(int argc, char** argv){ if(!chunked){ do{ std::copy(data.get() + cpos, data.get()+recv, std::back_inserter(html)); std::copy(data + cpos, data+recv, std::back_inserter(html)); rlen-=((long)recv- cpos); if(rlen>0){ recv=(long)srvsock.recvData(data, 16384,0); recv=(long)srvsock.recvData(&data, 16384,0); cpos=0; } }while(rlen>0); Loading @@ -139,7 +137,7 @@ int main(int argc, char** argv){ if (recv - cpos > 0) { if (readed == chunklen) { if ((chunklen = readchunk(data.get(), recv, cpos)) == 0) { if ((chunklen = readchunk(data, recv, cpos)) == 0) { break; } readed = 0; Loading @@ -147,7 +145,7 @@ int main(int argc, char** argv){ size_t len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get() + cpos, data.get() + (cpos + len), std::back_inserter(html)); std::copy(data + cpos, data + (cpos + len), std::back_inserter(html)); cpos += len; readed += len; Loading @@ -157,7 +155,7 @@ int main(int argc, char** argv){ for (;;) { cpos = 0; try { recv = srvsock.recvData(data, 16384,0); recv = srvsock.recvData(&data, 16384,0); break; } catch (netplus::NetException& e) { Loading @@ -173,16 +171,18 @@ int main(int argc, char** argv){ ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::cout.write(html.data(),html.size())<<std::endl; delete[] data; return 0; }catch(netplus::NetException &exp){ std::cerr << exp.what() <<std::endl; delete[] data; return -1; } } src/http.cpp +39 −29 Changes for src/http.cpp: 39 added lines, 29 removed lines. Original line number Diff line number Diff line Loading @@ -190,6 +190,7 @@ void libhttppp::HttpClient::reconnect(){ } const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq){ char* data = new char[BLOCKSIZE]; try{ _ret.clear(); size_t hsize=0; Loading @@ -207,18 +208,17 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq nreq.send(_cltsock); std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); int recv,chunklen=0; try{ for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &re){ if(re.getErrorType()!=netplus::NetException::Note) if(re.getErrorType()!=netplus::NetException::Note){ throw re; } else continue; } Loading @@ -229,11 +229,11 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq throw he; } hsize=res.parse(data.get(),recv); hsize=res.parse(data,recv); recv-=++hsize; std::move(data.get()+hsize,data.get()+16384,data.get()); std::move(data+hsize,data+BLOCKSIZE,data); cpos=0; Loading @@ -251,13 +251,13 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq if(!chunked){ do{ try{ std::copy(data.get()+cpos,data.get()+recv,std::back_inserter(_ret)); std::copy(data+cpos,data+recv,std::back_inserter(_ret)); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ Loading @@ -283,7 +283,7 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ if( (chunklen=readchunk(data,recv,cpos)) == 0 ){ break; } readed=0; Loading @@ -291,14 +291,14 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get()+cpos,data.get()+(cpos+len),std::back_inserter(_ret)); std::copy(data+cpos,data+(cpos+len),std::back_inserter(_ret)); cpos+=len; readed+=len; }else{ cpos=0; for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()!=netplus::NetException::Note){ Loading @@ -314,14 +314,18 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq }; } }catch(netplus::NetException &e){ delete[] data; libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } delete[] data; return _ret; } const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq,const std::vector<char>& post){ char *data=new char[BLOCKSIZE]; try{ _ret.clear(); size_t hsize=0; Loading @@ -344,21 +348,18 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre try { while(sended!=post.size()){ std::shared_ptr<const char[]> buf(post.data() + sended); sended+=_cltsock->sendData(buf , post.size() - sended, 0); sended+=_cltsock->sendData(post.data() + sended , post.size() - sended, 0); } }catch(netplus::NetException &e){ throw HTTPException()[HTTPException::Error]<<e.what(); } std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); int recv,chunklen=0; try{ for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &re){ if(re.getErrorType()!=netplus::NetException::Note) Loading @@ -373,11 +374,11 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre throw he; } hsize=res.parse(data.get(),recv); hsize=res.parse(data,recv); recv-=++hsize; std::move(data.get()+hsize,data.get()+16384,data.get()); std::move(data+hsize,data+BLOCKSIZE,data); cpos=0; Loading @@ -395,13 +396,13 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre if(!chunked){ do{ try{ std::copy(data.get()+cpos,data.get()+recv,std::back_inserter(_ret)); std::copy(data+cpos,data+recv,std::back_inserter(_ret)); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ Loading @@ -427,7 +428,7 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ if( (chunklen=readchunk(data,recv,cpos)) == 0 ){ break; } readed=0; Loading @@ -435,14 +436,14 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get()+cpos,data.get()+(cpos+len),std::back_inserter(_ret)); std::copy(data+cpos,data+(cpos+len),std::back_inserter(_ret)); cpos+=len; readed+=len; }else{ cpos=0; for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()!=netplus::NetException::Note){ Loading @@ -460,8 +461,10 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); delete[] data; throw ee; } delete[] data; return _ret; } Loading Loading @@ -793,6 +796,15 @@ libhttppp::HttpResponse::HttpResponse() : HttpHeader(){ _TransferEncoding=nullptr; } libhttppp::HttpResponse::HttpResponse(const HttpResponse &src) : HttpResponse(){ for(HttpHeader::HeaderData *curdat=src._firstHeaderData; curdat; curdat=curdat->nextHeaderData()){ HttpHeader::HeaderData *newdat=setHeaderData(curdat->getkey()); for(HttpHeader::HeaderData::Values *curval=curdat->getfirstValue(); curval; curval=curval->nextvalue()){ newdat->push_back(curval->getvalue()); } } } void libhttppp::HttpResponse::setState(const char* httpstate){ HTTPException httpexception; if(httpstate==nullptr){ Loading Loading @@ -919,7 +931,7 @@ void libhttppp::HttpResponse::send(std::shared_ptr<netplus::con> curconnection,c curconnection->SendData.append(header.data(),header.size()); if (datalen > 0) std::copy(data.begin(), data.end(), std::back_inserter(curconnection->SendData)); curconnection->SendData.append(data.data(),data.size()); } size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ Loading Loading @@ -1070,7 +1082,7 @@ libhttppp::HttpRequest::HttpRequest() : HttpHeader(){ _lastHeaderData=nullptr; }; libhttppp::HttpRequest::HttpRequest(std::shared_ptr <netplus::eventapi> evapi) : netplus::con(evapi) { libhttppp::HttpRequest::HttpRequest(netplus::eventapi *evapi) : con(evapi){ _RequestType=PARSEREQUEST; _MaxUploadSize=DEFAULT_UPLOADSIZE; _firstHeaderData=nullptr; Loading Loading @@ -1315,12 +1327,10 @@ void libhttppp::HttpRequest::send(std::shared_ptr<netplus::socket> dest){ printHeader(header); try { std::shared_ptr<const char[]> hbuf(header.c_str()); size_t send=dest->sendData(hbuf,header.length()); size_t send=dest->sendData(header.c_str(),header.length()); while(send<_Request.length()){ std::shared_ptr<const char[]> sbuf(_Request.substr(send,_Request.length()-send).c_str()); send+=dest->sendData(sbuf, _Request.length()-send); send+=dest->sendData(_Request.substr(send,_Request.length()-send).c_str(), _Request.length()-send); } }catch(netplus::NetException &e){ HTTPException excep; Loading src/http.h +2 −1 Changes for src/http.h: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -169,6 +169,7 @@ namespace libhttppp { class HttpResponse : public HttpHeader,public netplus::con { public: HttpResponse(); HttpResponse(const HttpResponse &src); ~HttpResponse(); /*server methods*/ Loading Loading @@ -208,7 +209,7 @@ namespace libhttppp { class HttpRequest : public HttpHeader, public netplus::con, std::enable_shared_from_this<HttpRequest> { public: HttpRequest(); HttpRequest(std::shared_ptr <netplus::eventapi> evapi); HttpRequest(netplus::eventapi *evapi); ~HttpRequest(); void clear(); Loading src/httpd.cpp +15 −16 Changes for src/httpd.cpp: 15 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -49,25 +49,25 @@ libhttppp::HttpEvent::HttpEvent(std::shared_ptr<netplus::socket> &ssock, int tim } void libhttppp::HttpEvent::CreateConnection(std::shared_ptr <netplus::con> &curcon){ curcon=std::make_shared<HttpRequest>(std::enable_shared_from_this<netplus::event>::shared_from_this()); void libhttppp::HttpEvent::CreateConnection(void **res){ *res=new HttpRequest(this); } void libhttppp::HttpEvent::RequestEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::RequestEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::ResponseEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::ConnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::ConnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::DisconnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::DisconnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::RequestEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ std::shared_ptr <HttpRequest> cureq=std::static_pointer_cast<HttpRequest>(curcon); void libhttppp::HttpEvent::RequestEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ HttpRequest *cureq=static_cast<HttpRequest*>(curcon); try{ REQUESTHANDLING: switch(cureq->getRequestType()){ Loading Loading @@ -100,8 +100,8 @@ REQUESTHANDLING: } } void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr<netplus::con> curcon,const int tid,ULONG_PTR args){ std::shared_ptr <HttpRequest> cureq=std::static_pointer_cast<HttpRequest>(curcon); void libhttppp::HttpEvent::ResponseEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ HttpRequest *cureq=static_cast<HttpRequest*>(curcon); try{ ResponseEvent(cureq,tid,args); if(cureq->SendData.empty()){ Loading @@ -115,12 +115,12 @@ void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr<netplus::con> curcon,co } } void libhttppp::HttpEvent::ConnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ ConnectEvent(std::static_pointer_cast<HttpRequest>(curcon), tid, args); void libhttppp::HttpEvent::ConnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ ConnectEvent(static_cast<HttpRequest*>(curcon), tid, args); } void libhttppp::HttpEvent::DisconnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ DisconnectEvent(std::static_pointer_cast<HttpRequest>(curcon),tid,args); void libhttppp::HttpEvent::DisconnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ DisconnectEvent(static_cast<HttpRequest*>(curcon),tid,args); } libhttppp::HttpD::HttpD(int argc, char** argv) { Loading Loading @@ -274,8 +274,7 @@ libhttppp::HttpD::HttpD(const char *httpaddr, int port,int maxconnections,const } } std::shared_ptr<netplus::socket> libhttppp::HttpD::getServerSocket(){ const std::shared_ptr<netplus::socket> &libhttppp::HttpD::getServerSocket(){ return _ServerSocket; } Loading src/httpd.h +13 −11 Changes for src/httpd.h: 13 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -38,21 +38,22 @@ namespace cmdplus { } namespace libhttppp { class HttpEvent : public netplus::event, std::enable_shared_from_this<HttpEvent>{ class HttpEvent : public netplus::event { public: HttpEvent(std::shared_ptr<netplus::socket> &ssock,int timeout = -1); virtual void RequestEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void ResponseEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void ConnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void DisconnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void RequestEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void ResponseEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void ConnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void DisconnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); protected: void CreateConnection(std::shared_ptr <netplus::con> &curcon); void CreateConnection(void **res); private: void RequestEvent(std::shared_ptr <netplus::con> curcon, const int tid, ULONG_PTR args); void ResponseEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void ConnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void DisconnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void RequestEvent(netplus::con *curcon, const int tid, ULONG_PTR args); void ResponseEvent(netplus::con *curcon,const int tid,ULONG_PTR args); void ConnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args); void DisconnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args); }; Loading @@ -61,10 +62,11 @@ namespace libhttppp { HttpD(int argc, char** argv); HttpD(const char *httpaddr, int port,int maxconnections,const char *sslcertpath,const char *sslkeypath); ~HttpD(); std::shared_ptr<netplus::socket> getServerSocket(); const std::shared_ptr<netplus::socket> &getServerSocket(); protected: void FileServer(); private: bool _fileServer; std::shared_ptr<netplus::socket> _ServerSocket; HTTPException _httpexception; Loading Loading
examples/httpclient.cpp +11 −11 Changes for examples/httpclient.cpp: 11 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -78,9 +78,9 @@ int main(int argc, char** argv){ #endif netplus::tcp cltsock(argv[1], atoi(argv[2]), 1, 0); netplus::tcp srvsock; char *data=new char[16384]; try{ srvsock.connect(&cltsock); try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); Loading @@ -98,9 +98,7 @@ int main(int argc, char** argv){ return -1; } std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); size_t recv= srvsock.recvData(data, 16384,0); size_t recv= srvsock.recvData(&data, 16384,0); std::vector<char> html; libhttppp::HttpResponse res; Loading @@ -108,7 +106,7 @@ int main(int argc, char** argv){ bool chunked=false; long long rlen=0; hsize=res.parse(data.get(), recv); hsize=res.parse(data, recv); try { for(libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); cval; cval=cval->nextvalue()){ Loading @@ -125,10 +123,10 @@ int main(int argc, char** argv){ if(!chunked){ do{ std::copy(data.get() + cpos, data.get()+recv, std::back_inserter(html)); std::copy(data + cpos, data+recv, std::back_inserter(html)); rlen-=((long)recv- cpos); if(rlen>0){ recv=(long)srvsock.recvData(data, 16384,0); recv=(long)srvsock.recvData(&data, 16384,0); cpos=0; } }while(rlen>0); Loading @@ -139,7 +137,7 @@ int main(int argc, char** argv){ if (recv - cpos > 0) { if (readed == chunklen) { if ((chunklen = readchunk(data.get(), recv, cpos)) == 0) { if ((chunklen = readchunk(data, recv, cpos)) == 0) { break; } readed = 0; Loading @@ -147,7 +145,7 @@ int main(int argc, char** argv){ size_t len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get() + cpos, data.get() + (cpos + len), std::back_inserter(html)); std::copy(data + cpos, data + (cpos + len), std::back_inserter(html)); cpos += len; readed += len; Loading @@ -157,7 +155,7 @@ int main(int argc, char** argv){ for (;;) { cpos = 0; try { recv = srvsock.recvData(data, 16384,0); recv = srvsock.recvData(&data, 16384,0); break; } catch (netplus::NetException& e) { Loading @@ -173,16 +171,18 @@ int main(int argc, char** argv){ ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::cout.write(html.data(),html.size())<<std::endl; delete[] data; return 0; }catch(netplus::NetException &exp){ std::cerr << exp.what() <<std::endl; delete[] data; return -1; } }
src/http.cpp +39 −29 Changes for src/http.cpp: 39 added lines, 29 removed lines. Original line number Diff line number Diff line Loading @@ -190,6 +190,7 @@ void libhttppp::HttpClient::reconnect(){ } const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq){ char* data = new char[BLOCKSIZE]; try{ _ret.clear(); size_t hsize=0; Loading @@ -207,18 +208,17 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq nreq.send(_cltsock); std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); int recv,chunklen=0; try{ for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &re){ if(re.getErrorType()!=netplus::NetException::Note) if(re.getErrorType()!=netplus::NetException::Note){ throw re; } else continue; } Loading @@ -229,11 +229,11 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq throw he; } hsize=res.parse(data.get(),recv); hsize=res.parse(data,recv); recv-=++hsize; std::move(data.get()+hsize,data.get()+16384,data.get()); std::move(data+hsize,data+BLOCKSIZE,data); cpos=0; Loading @@ -251,13 +251,13 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq if(!chunked){ do{ try{ std::copy(data.get()+cpos,data.get()+recv,std::back_inserter(_ret)); std::copy(data+cpos,data+recv,std::back_inserter(_ret)); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ Loading @@ -283,7 +283,7 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ if( (chunklen=readchunk(data,recv,cpos)) == 0 ){ break; } readed=0; Loading @@ -291,14 +291,14 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get()+cpos,data.get()+(cpos+len),std::back_inserter(_ret)); std::copy(data+cpos,data+(cpos+len),std::back_inserter(_ret)); cpos+=len; readed+=len; }else{ cpos=0; for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()!=netplus::NetException::Note){ Loading @@ -314,14 +314,18 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq }; } }catch(netplus::NetException &e){ delete[] data; libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } delete[] data; return _ret; } const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nreq,const std::vector<char>& post){ char *data=new char[BLOCKSIZE]; try{ _ret.clear(); size_t hsize=0; Loading @@ -344,21 +348,18 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre try { while(sended!=post.size()){ std::shared_ptr<const char[]> buf(post.data() + sended); sended+=_cltsock->sendData(buf , post.size() - sended, 0); sended+=_cltsock->sendData(post.data() + sended , post.size() - sended, 0); } }catch(netplus::NetException &e){ throw HTTPException()[HTTPException::Error]<<e.what(); } std::shared_ptr<char[]> data(new char[16384], std::default_delete<char[]>()); int recv,chunklen=0; try{ for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &re){ if(re.getErrorType()!=netplus::NetException::Note) Loading @@ -373,11 +374,11 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre throw he; } hsize=res.parse(data.get(),recv); hsize=res.parse(data,recv); recv-=++hsize; std::move(data.get()+hsize,data.get()+16384,data.get()); std::move(data+hsize,data+BLOCKSIZE,data); cpos=0; Loading @@ -395,13 +396,13 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre if(!chunked){ do{ try{ std::copy(data.get()+cpos,data.get()+recv,std::back_inserter(_ret)); std::copy(data+cpos,data+recv,std::back_inserter(_ret)); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ Loading @@ -427,7 +428,7 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ if( (chunklen=readchunk(data,recv,cpos)) == 0 ){ break; } readed=0; Loading @@ -435,14 +436,14 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.get()+cpos,data.get()+(cpos+len),std::back_inserter(_ret)); std::copy(data+cpos,data+(cpos+len),std::back_inserter(_ret)); cpos+=len; readed+=len; }else{ cpos=0; for(;;){ try{ recv=_cltsock->recvData(data,16384); recv=_cltsock->recvData(&data,BLOCKSIZE); break; }catch(netplus::NetException &e){ if(e.getErrorType()!=netplus::NetException::Note){ Loading @@ -460,8 +461,10 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); delete[] data; throw ee; } delete[] data; return _ret; } Loading Loading @@ -793,6 +796,15 @@ libhttppp::HttpResponse::HttpResponse() : HttpHeader(){ _TransferEncoding=nullptr; } libhttppp::HttpResponse::HttpResponse(const HttpResponse &src) : HttpResponse(){ for(HttpHeader::HeaderData *curdat=src._firstHeaderData; curdat; curdat=curdat->nextHeaderData()){ HttpHeader::HeaderData *newdat=setHeaderData(curdat->getkey()); for(HttpHeader::HeaderData::Values *curval=curdat->getfirstValue(); curval; curval=curval->nextvalue()){ newdat->push_back(curval->getvalue()); } } } void libhttppp::HttpResponse::setState(const char* httpstate){ HTTPException httpexception; if(httpstate==nullptr){ Loading Loading @@ -919,7 +931,7 @@ void libhttppp::HttpResponse::send(std::shared_ptr<netplus::con> curconnection,c curconnection->SendData.append(header.data(),header.size()); if (datalen > 0) std::copy(data.begin(), data.end(), std::back_inserter(curconnection->SendData)); curconnection->SendData.append(data.data(),data.size()); } size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ Loading Loading @@ -1070,7 +1082,7 @@ libhttppp::HttpRequest::HttpRequest() : HttpHeader(){ _lastHeaderData=nullptr; }; libhttppp::HttpRequest::HttpRequest(std::shared_ptr <netplus::eventapi> evapi) : netplus::con(evapi) { libhttppp::HttpRequest::HttpRequest(netplus::eventapi *evapi) : con(evapi){ _RequestType=PARSEREQUEST; _MaxUploadSize=DEFAULT_UPLOADSIZE; _firstHeaderData=nullptr; Loading Loading @@ -1315,12 +1327,10 @@ void libhttppp::HttpRequest::send(std::shared_ptr<netplus::socket> dest){ printHeader(header); try { std::shared_ptr<const char[]> hbuf(header.c_str()); size_t send=dest->sendData(hbuf,header.length()); size_t send=dest->sendData(header.c_str(),header.length()); while(send<_Request.length()){ std::shared_ptr<const char[]> sbuf(_Request.substr(send,_Request.length()-send).c_str()); send+=dest->sendData(sbuf, _Request.length()-send); send+=dest->sendData(_Request.substr(send,_Request.length()-send).c_str(), _Request.length()-send); } }catch(netplus::NetException &e){ HTTPException excep; Loading
src/http.h +2 −1 Changes for src/http.h: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -169,6 +169,7 @@ namespace libhttppp { class HttpResponse : public HttpHeader,public netplus::con { public: HttpResponse(); HttpResponse(const HttpResponse &src); ~HttpResponse(); /*server methods*/ Loading Loading @@ -208,7 +209,7 @@ namespace libhttppp { class HttpRequest : public HttpHeader, public netplus::con, std::enable_shared_from_this<HttpRequest> { public: HttpRequest(); HttpRequest(std::shared_ptr <netplus::eventapi> evapi); HttpRequest(netplus::eventapi *evapi); ~HttpRequest(); void clear(); Loading
src/httpd.cpp +15 −16 Changes for src/httpd.cpp: 15 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -49,25 +49,25 @@ libhttppp::HttpEvent::HttpEvent(std::shared_ptr<netplus::socket> &ssock, int tim } void libhttppp::HttpEvent::CreateConnection(std::shared_ptr <netplus::con> &curcon){ curcon=std::make_shared<HttpRequest>(std::enable_shared_from_this<netplus::event>::shared_from_this()); void libhttppp::HttpEvent::CreateConnection(void **res){ *res=new HttpRequest(this); } void libhttppp::HttpEvent::RequestEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::RequestEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::ResponseEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::ConnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::ConnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::DisconnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args){ void libhttppp::HttpEvent::DisconnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args){ } void libhttppp::HttpEvent::RequestEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ std::shared_ptr <HttpRequest> cureq=std::static_pointer_cast<HttpRequest>(curcon); void libhttppp::HttpEvent::RequestEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ HttpRequest *cureq=static_cast<HttpRequest*>(curcon); try{ REQUESTHANDLING: switch(cureq->getRequestType()){ Loading Loading @@ -100,8 +100,8 @@ REQUESTHANDLING: } } void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr<netplus::con> curcon,const int tid,ULONG_PTR args){ std::shared_ptr <HttpRequest> cureq=std::static_pointer_cast<HttpRequest>(curcon); void libhttppp::HttpEvent::ResponseEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ HttpRequest *cureq=static_cast<HttpRequest*>(curcon); try{ ResponseEvent(cureq,tid,args); if(cureq->SendData.empty()){ Loading @@ -115,12 +115,12 @@ void libhttppp::HttpEvent::ResponseEvent(std::shared_ptr<netplus::con> curcon,co } } void libhttppp::HttpEvent::ConnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ ConnectEvent(std::static_pointer_cast<HttpRequest>(curcon), tid, args); void libhttppp::HttpEvent::ConnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ ConnectEvent(static_cast<HttpRequest*>(curcon), tid, args); } void libhttppp::HttpEvent::DisconnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args){ DisconnectEvent(std::static_pointer_cast<HttpRequest>(curcon),tid,args); void libhttppp::HttpEvent::DisconnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args){ DisconnectEvent(static_cast<HttpRequest*>(curcon),tid,args); } libhttppp::HttpD::HttpD(int argc, char** argv) { Loading Loading @@ -274,8 +274,7 @@ libhttppp::HttpD::HttpD(const char *httpaddr, int port,int maxconnections,const } } std::shared_ptr<netplus::socket> libhttppp::HttpD::getServerSocket(){ const std::shared_ptr<netplus::socket> &libhttppp::HttpD::getServerSocket(){ return _ServerSocket; } Loading
src/httpd.h +13 −11 Changes for src/httpd.h: 13 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -38,21 +38,22 @@ namespace cmdplus { } namespace libhttppp { class HttpEvent : public netplus::event, std::enable_shared_from_this<HttpEvent>{ class HttpEvent : public netplus::event { public: HttpEvent(std::shared_ptr<netplus::socket> &ssock,int timeout = -1); virtual void RequestEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void ResponseEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void ConnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void DisconnectEvent(std::shared_ptr <HttpRequest> curreq,const int tid,ULONG_PTR args); virtual void RequestEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void ResponseEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void ConnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); virtual void DisconnectEvent(HttpRequest *curreq,const int tid,ULONG_PTR args); protected: void CreateConnection(std::shared_ptr <netplus::con> &curcon); void CreateConnection(void **res); private: void RequestEvent(std::shared_ptr <netplus::con> curcon, const int tid, ULONG_PTR args); void ResponseEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void ConnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void DisconnectEvent(std::shared_ptr <netplus::con> curcon,const int tid,ULONG_PTR args); void RequestEvent(netplus::con *curcon, const int tid, ULONG_PTR args); void ResponseEvent(netplus::con *curcon,const int tid,ULONG_PTR args); void ConnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args); void DisconnectEvent(netplus::con *curcon,const int tid,ULONG_PTR args); }; Loading @@ -61,10 +62,11 @@ namespace libhttppp { HttpD(int argc, char** argv); HttpD(const char *httpaddr, int port,int maxconnections,const char *sslcertpath,const char *sslkeypath); ~HttpD(); std::shared_ptr<netplus::socket> getServerSocket(); const std::shared_ptr<netplus::socket> &getServerSocket(); protected: void FileServer(); private: bool _fileServer; std::shared_ptr<netplus::socket> _ServerSocket; HTTPException _httpexception; Loading