Loading examples/httpclient.cpp +16 −7 Changes for examples/httpclient.cpp: 16 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <signal.h> #include <netplus/socket.h> #include <netplus/exception.h> Loading @@ -34,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "exception.h" int main(int argc, char** argv){ signal(SIGPIPE, SIG_IGN); try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); Loading @@ -43,38 +45,45 @@ int main(int argc, char** argv){ req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestVersion(HTTPVERSION(1.1)); *req.setData("Connection") << "closed"; *req.setData("Host") << argv[1]; *req.setData("connection") << "keep-alive"; *req.setData("host") << argv[1] << ":" << argv[2]; *req.setData("user-agent") << "libhttppp (0.1)"; req.send(cltsock,&srvsock); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } char data[512]; int len=cltsock->recvData(&srvsock,data,512); char data[16384]; int len=cltsock->recvData(&srvsock,data,16384); libhttppp::HttpResponse res; std::string html; try { libhttppp::HttpResponse res; size_t hsize=res.parse(data,len); size_t amount = len-hsize; if(amount>0) html.assign(data+hsize,amount); std::cout << res.getContentLength() << std::endl; if(res.getContentLength()==std::string::npos) return 0; while(amount < res.getContentLength()){ size_t recv=cltsock->recvData(&srvsock,data,512); try{ size_t recv=cltsock->recvData(&srvsock,data,16384); amount+=recv; html.append(data,recv); }catch(netplus::NetException &e){ std::cerr << e.what() << std::endl; } } }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; }; if(!html.empty()) std::cout << html << std::endl; delete cltsock; }catch(netplus::NetException &exp){ std::cerr << exp.what() <<std::endl; return -1; Loading src/http.cpp +18 −27 Changes for src/http.cpp: 18 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -113,8 +113,6 @@ size_t libhttppp::HttpHeader::getDataSizet(HttpHeader::HeaderData *pos){ int libhttppp::HttpHeader::getDataInt(HttpHeader::HeaderData *pos){ const char *val=getData(pos); if(strlen(val)<255) return 0; return atoi(val); } Loading @@ -129,20 +127,6 @@ libhttppp::HttpHeader::HeaderData *libhttppp::HttpHeader::setData(const char* ke return _lastHeaderData; } libhttppp::HttpHeader::HeaderData *libhttppp::HttpHeader::setData(const char* key,libhttppp::HttpHeader::HeaderData *pos){ HTTPException httpexception; if(!key){ httpexception[HTTPException::Error] << "no headerdata key set can't do this"; throw httpexception; } if(pos){ pos->_Key = key; return pos; }else{ return setData(key); } } void libhttppp::HttpHeader::deldata(const char* key){ for(HeaderData *curdat=_firstHeaderData; curdat; curdat=curdat->_nextHeaderData){ if(curdat->_Key==key){ Loading Loading @@ -192,11 +176,11 @@ size_t libhttppp::HttpHeader::getHeaderSize(){ libhttppp::HttpHeader::HeaderData::HeaderData(const char *key){ HTTPException excep; _nextHeaderData=nullptr; if(!key){ excep[HTTPException::Error] << "no headerdata key set can't do this"; throw excep; } _nextHeaderData=nullptr; _Key = key; } Loading Loading @@ -232,17 +216,20 @@ void libhttppp::HttpResponse::setState(const char* httpstate){ } void libhttppp::HttpResponse::setContentLength(size_t len){ _ContentLength=setData("content-length",_ContentLength); if(!getData("content-length")) _ContentLength=setData("content-length"); *_ContentLength<<len; } void libhttppp::HttpResponse::setContentType(const char* type){ _ContentType=setData("content-type",_ContentType); if(!getData("content-type")) _ContentType=setData("content-type"); *_ContentType<<type; } void libhttppp::HttpResponse::setConnection(const char* type){ _Connection=setData("connection",_Connection); if(!getData("connection")) _Connection=setData("connection"); *_Connection<<type; } Loading Loading @@ -301,8 +288,6 @@ void libhttppp::HttpResponse::send(netplus::con* curconnection,const char* data, curconnection->sending(true); } #include <iostream> size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ if(inlen <9){ HTTPException excep; Loading Loading @@ -373,7 +358,6 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ for (size_t it = 0; it < valuelen; ++it) { value[it] = (char)tolower(value[it]); } std::cout << key << ":" << value << std::endl; *setData(key.c_str())<<value.c_str(); } } Loading Loading @@ -663,8 +647,11 @@ libhttppp::HttpForm::~HttpForm(){ void libhttppp::HttpForm::parse(libhttppp::HttpRequest* request){ HttpHeader::HeaderData *ctype=request->getData("content-type"); if(!ctype) return; int rtype = request->getRequestType(); _ContentType = request->getData(request->getData("content-type")); _ContentType = request->getData(ctype); switch(rtype){ case GETREQUEST:{ _parseUrlDecode(request); Loading Loading @@ -1296,7 +1283,8 @@ void libhttppp::HttpCookie::setcookie(HttpResponse *curresp, void libhttppp::HttpCookie::parse(libhttppp::HttpRequest* curreq){ HttpHeader::HeaderData *hc = curreq->getData("cookie"); if(!curreq->getData(hc)) if(!hc) return; std::string cdat; Loading Loading @@ -1363,7 +1351,10 @@ libhttppp::HttpAuth::~HttpAuth(){ void libhttppp::HttpAuth::parse(libhttppp::HttpRequest* curreq){ const char *authstr=curreq->getData(curreq->getData("authorization")); HttpHeader::HeaderData *hau=curreq->getData("authorization"); if(!hau) return; const char *authstr=curreq->getData(hau); if(!authstr) return; if(strcmp(authstr,"basic")==0) Loading Loading @@ -1408,7 +1399,7 @@ void libhttppp::HttpAuth::parse(libhttppp::HttpRequest* curreq){ } void libhttppp::HttpAuth::setAuth(libhttppp::HttpResponse* curresp){ HttpHeader::HeaderData *dat=curresp->setData("www-authenticate",nullptr); HttpHeader::HeaderData *dat=curresp->setData("www-authenticate"); switch(_Authtype){ case BASICAUTH:{ if(_Realm){ Loading src/http.h +1 −2 Changes for src/http.h: 1 added line, 2 removed lines. Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ namespace libhttppp { HeaderData &operator<<(int value); protected: HeaderData(const char *key); virtual ~HeaderData(); ~HeaderData(); private: std::string _Key; std::string _Value; Loading @@ -63,7 +63,6 @@ namespace libhttppp { const char *getValue(HeaderData *pos); HeaderData *setData(const char *key); HeaderData *setData(const char* key,HeaderData *pos); HeaderData *getData(const char *key); Loading Loading
examples/httpclient.cpp +16 −7 Changes for examples/httpclient.cpp: 16 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <signal.h> #include <netplus/socket.h> #include <netplus/exception.h> Loading @@ -34,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "exception.h" int main(int argc, char** argv){ signal(SIGPIPE, SIG_IGN); try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); Loading @@ -43,38 +45,45 @@ int main(int argc, char** argv){ req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestVersion(HTTPVERSION(1.1)); *req.setData("Connection") << "closed"; *req.setData("Host") << argv[1]; *req.setData("connection") << "keep-alive"; *req.setData("host") << argv[1] << ":" << argv[2]; *req.setData("user-agent") << "libhttppp (0.1)"; req.send(cltsock,&srvsock); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } char data[512]; int len=cltsock->recvData(&srvsock,data,512); char data[16384]; int len=cltsock->recvData(&srvsock,data,16384); libhttppp::HttpResponse res; std::string html; try { libhttppp::HttpResponse res; size_t hsize=res.parse(data,len); size_t amount = len-hsize; if(amount>0) html.assign(data+hsize,amount); std::cout << res.getContentLength() << std::endl; if(res.getContentLength()==std::string::npos) return 0; while(amount < res.getContentLength()){ size_t recv=cltsock->recvData(&srvsock,data,512); try{ size_t recv=cltsock->recvData(&srvsock,data,16384); amount+=recv; html.append(data,recv); }catch(netplus::NetException &e){ std::cerr << e.what() << std::endl; } } }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; }; if(!html.empty()) std::cout << html << std::endl; delete cltsock; }catch(netplus::NetException &exp){ std::cerr << exp.what() <<std::endl; return -1; Loading
src/http.cpp +18 −27 Changes for src/http.cpp: 18 added lines, 27 removed lines. Original line number Diff line number Diff line Loading @@ -113,8 +113,6 @@ size_t libhttppp::HttpHeader::getDataSizet(HttpHeader::HeaderData *pos){ int libhttppp::HttpHeader::getDataInt(HttpHeader::HeaderData *pos){ const char *val=getData(pos); if(strlen(val)<255) return 0; return atoi(val); } Loading @@ -129,20 +127,6 @@ libhttppp::HttpHeader::HeaderData *libhttppp::HttpHeader::setData(const char* ke return _lastHeaderData; } libhttppp::HttpHeader::HeaderData *libhttppp::HttpHeader::setData(const char* key,libhttppp::HttpHeader::HeaderData *pos){ HTTPException httpexception; if(!key){ httpexception[HTTPException::Error] << "no headerdata key set can't do this"; throw httpexception; } if(pos){ pos->_Key = key; return pos; }else{ return setData(key); } } void libhttppp::HttpHeader::deldata(const char* key){ for(HeaderData *curdat=_firstHeaderData; curdat; curdat=curdat->_nextHeaderData){ if(curdat->_Key==key){ Loading Loading @@ -192,11 +176,11 @@ size_t libhttppp::HttpHeader::getHeaderSize(){ libhttppp::HttpHeader::HeaderData::HeaderData(const char *key){ HTTPException excep; _nextHeaderData=nullptr; if(!key){ excep[HTTPException::Error] << "no headerdata key set can't do this"; throw excep; } _nextHeaderData=nullptr; _Key = key; } Loading Loading @@ -232,17 +216,20 @@ void libhttppp::HttpResponse::setState(const char* httpstate){ } void libhttppp::HttpResponse::setContentLength(size_t len){ _ContentLength=setData("content-length",_ContentLength); if(!getData("content-length")) _ContentLength=setData("content-length"); *_ContentLength<<len; } void libhttppp::HttpResponse::setContentType(const char* type){ _ContentType=setData("content-type",_ContentType); if(!getData("content-type")) _ContentType=setData("content-type"); *_ContentType<<type; } void libhttppp::HttpResponse::setConnection(const char* type){ _Connection=setData("connection",_Connection); if(!getData("connection")) _Connection=setData("connection"); *_Connection<<type; } Loading Loading @@ -301,8 +288,6 @@ void libhttppp::HttpResponse::send(netplus::con* curconnection,const char* data, curconnection->sending(true); } #include <iostream> size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ if(inlen <9){ HTTPException excep; Loading Loading @@ -373,7 +358,6 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ for (size_t it = 0; it < valuelen; ++it) { value[it] = (char)tolower(value[it]); } std::cout << key << ":" << value << std::endl; *setData(key.c_str())<<value.c_str(); } } Loading Loading @@ -663,8 +647,11 @@ libhttppp::HttpForm::~HttpForm(){ void libhttppp::HttpForm::parse(libhttppp::HttpRequest* request){ HttpHeader::HeaderData *ctype=request->getData("content-type"); if(!ctype) return; int rtype = request->getRequestType(); _ContentType = request->getData(request->getData("content-type")); _ContentType = request->getData(ctype); switch(rtype){ case GETREQUEST:{ _parseUrlDecode(request); Loading Loading @@ -1296,7 +1283,8 @@ void libhttppp::HttpCookie::setcookie(HttpResponse *curresp, void libhttppp::HttpCookie::parse(libhttppp::HttpRequest* curreq){ HttpHeader::HeaderData *hc = curreq->getData("cookie"); if(!curreq->getData(hc)) if(!hc) return; std::string cdat; Loading Loading @@ -1363,7 +1351,10 @@ libhttppp::HttpAuth::~HttpAuth(){ void libhttppp::HttpAuth::parse(libhttppp::HttpRequest* curreq){ const char *authstr=curreq->getData(curreq->getData("authorization")); HttpHeader::HeaderData *hau=curreq->getData("authorization"); if(!hau) return; const char *authstr=curreq->getData(hau); if(!authstr) return; if(strcmp(authstr,"basic")==0) Loading Loading @@ -1408,7 +1399,7 @@ void libhttppp::HttpAuth::parse(libhttppp::HttpRequest* curreq){ } void libhttppp::HttpAuth::setAuth(libhttppp::HttpResponse* curresp){ HttpHeader::HeaderData *dat=curresp->setData("www-authenticate",nullptr); HttpHeader::HeaderData *dat=curresp->setData("www-authenticate"); switch(_Authtype){ case BASICAUTH:{ if(_Realm){ Loading
src/http.h +1 −2 Changes for src/http.h: 1 added line, 2 removed lines. Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ namespace libhttppp { HeaderData &operator<<(int value); protected: HeaderData(const char *key); virtual ~HeaderData(); ~HeaderData(); private: std::string _Key; std::string _Value; Loading @@ -63,7 +63,6 @@ namespace libhttppp { const char *getValue(HeaderData *pos); HeaderData *setData(const char *key); HeaderData *setData(const char* key,HeaderData *pos); HeaderData *getData(const char *key); Loading