Loading examples/httpclient.cpp +17 −0 Changes for examples/httpclient.cpp: 17 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -25,7 +25,24 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <netplus/socket.h> #include <netplus/exception.h> #include "http.h" int main(int argc, char** argv){ try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestVersion(HTTPVERSION(1.1)); req.send(cltsock,&srvsock); }catch(netplus::NetException &exp){ throw exp.what(); } } src/http.cpp +55 −2 Changes for src/http.cpp: 55 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -283,7 +283,7 @@ void libhttppp::HttpResponse::send(netplus::con* curconnection,const char* data, curconnection->sending(true); } void libhttppp::HttpResponse::parse(netplus::con* curconnection){ void libhttppp::HttpResponse::parse(const char *data,size_t len){ } Loading Loading @@ -347,7 +347,7 @@ void libhttppp::HttpRequest::parse(netplus::con* curconnection){ for(size_t cpos=pos; cpos<header.length(); ++cpos){ if(header[cpos]==' ' && (cpos-pos)<255){ _Version = header.substr(pos,cpos-pos); _RequestVersion = header.substr(pos,cpos-pos); ++pos; found=true; break; Loading Loading @@ -443,6 +443,26 @@ void libhttppp::HttpRequest::parse(netplus::con* curconnection){ } } void libhttppp::HttpRequest::printHeader(std::string &buffer){ if(_RequestType==GETREQUEST) buffer="GET "; else if(_RequestType==POSTREQUEST) buffer="POST "; buffer.append(_RequestURL); buffer.append(" "); buffer.append(_RequestVersion); buffer.append("\r\n"); for(HeaderData *curdat=getfirstHeaderData(); curdat; curdat=nextHeaderData(curdat)){ buffer.append(getKey(curdat)); buffer.append(": "); if(getValue(curdat)) buffer.append(getValue(curdat)); buffer.append("\r\n"); } } int libhttppp::HttpRequest::getRequestType(){ return _RequestType; } Loading @@ -459,12 +479,45 @@ size_t libhttppp::HttpRequest::getRequestLength() { return _Request.length(); } const char * libhttppp::HttpRequest::getRequestVersion(){ return _RequestVersion.c_str(); } void libhttppp::HttpRequest::setRequestData(const char* data, size_t len){ _Request.clear(); _Request.resize(len); _Request.insert(0,data); } void libhttppp::HttpRequest::setRequestVersion(const char* version){ if(version) _RequestVersion=version; else _RequestVersion.clear(); } void libhttppp::HttpRequest::send(netplus::socket* src,netplus::socket* dest){ std::string header; printHeader(header); try { src->sendData(dest,(void*)header.c_str(),header.length()); size_t send=0; while(send<_Request.length()){ send+=src->sendData(dest,(void*)_Request.substr(send,_Request.length()-send).c_str(), _Request.length()-send); } }catch(netplus::NetException &e){ HTTPException excep; excep[HTTPException::Error] << "RequestSend Socket Error: " << e.what(); throw excep; } } void libhttppp::HttpRequest::setRequestType(int req){ if(req == POSTREQUEST || req == GETREQUEST){ _RequestType=req; Loading src/http.h +7 −5 Changes for src/http.h: 7 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -98,7 +98,7 @@ namespace libhttppp { void send(netplus::con *curconnection,const char* data, unsigned long datalen); //only use as server /*client method*/ void parse(netplus::con *curconnection); void parse(const char *data,size_t len); private: std::string _State; std::string _Version; Loading @@ -114,23 +114,25 @@ namespace libhttppp { ~HttpRequest(); /*server methods*/ void parse(netplus::con *curconnection); //only use as server size_t printHeader(char **buffer); void printHeader(std::string &buffer); int getRequestType(); const char *getRequestURL(); const char *getRequest(); size_t getRequestLength(); const char *getRequestVersion(); /*Client methods*/ void setRequestType(int req); void setRequestURL(const char *url); void setRequestVersion(const char *version); /*only for post Reuquesttype*/ void setRequestData(const char *data,size_t len); void send(netplus::socket* src,netplus::socket* dest); private: std::string _Request; int _RequestType; std::string _RequestURL; std::string _Version; std::string _RequestVersion; HttpHeader *_HttpHeader; netplus::con *_Connection; Loading Loading
examples/httpclient.cpp +17 −0 Changes for examples/httpclient.cpp: 17 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -25,7 +25,24 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <netplus/socket.h> #include <netplus/exception.h> #include "http.h" int main(int argc, char** argv){ try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestVersion(HTTPVERSION(1.1)); req.send(cltsock,&srvsock); }catch(netplus::NetException &exp){ throw exp.what(); } }
src/http.cpp +55 −2 Changes for src/http.cpp: 55 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -283,7 +283,7 @@ void libhttppp::HttpResponse::send(netplus::con* curconnection,const char* data, curconnection->sending(true); } void libhttppp::HttpResponse::parse(netplus::con* curconnection){ void libhttppp::HttpResponse::parse(const char *data,size_t len){ } Loading Loading @@ -347,7 +347,7 @@ void libhttppp::HttpRequest::parse(netplus::con* curconnection){ for(size_t cpos=pos; cpos<header.length(); ++cpos){ if(header[cpos]==' ' && (cpos-pos)<255){ _Version = header.substr(pos,cpos-pos); _RequestVersion = header.substr(pos,cpos-pos); ++pos; found=true; break; Loading Loading @@ -443,6 +443,26 @@ void libhttppp::HttpRequest::parse(netplus::con* curconnection){ } } void libhttppp::HttpRequest::printHeader(std::string &buffer){ if(_RequestType==GETREQUEST) buffer="GET "; else if(_RequestType==POSTREQUEST) buffer="POST "; buffer.append(_RequestURL); buffer.append(" "); buffer.append(_RequestVersion); buffer.append("\r\n"); for(HeaderData *curdat=getfirstHeaderData(); curdat; curdat=nextHeaderData(curdat)){ buffer.append(getKey(curdat)); buffer.append(": "); if(getValue(curdat)) buffer.append(getValue(curdat)); buffer.append("\r\n"); } } int libhttppp::HttpRequest::getRequestType(){ return _RequestType; } Loading @@ -459,12 +479,45 @@ size_t libhttppp::HttpRequest::getRequestLength() { return _Request.length(); } const char * libhttppp::HttpRequest::getRequestVersion(){ return _RequestVersion.c_str(); } void libhttppp::HttpRequest::setRequestData(const char* data, size_t len){ _Request.clear(); _Request.resize(len); _Request.insert(0,data); } void libhttppp::HttpRequest::setRequestVersion(const char* version){ if(version) _RequestVersion=version; else _RequestVersion.clear(); } void libhttppp::HttpRequest::send(netplus::socket* src,netplus::socket* dest){ std::string header; printHeader(header); try { src->sendData(dest,(void*)header.c_str(),header.length()); size_t send=0; while(send<_Request.length()){ send+=src->sendData(dest,(void*)_Request.substr(send,_Request.length()-send).c_str(), _Request.length()-send); } }catch(netplus::NetException &e){ HTTPException excep; excep[HTTPException::Error] << "RequestSend Socket Error: " << e.what(); throw excep; } } void libhttppp::HttpRequest::setRequestType(int req){ if(req == POSTREQUEST || req == GETREQUEST){ _RequestType=req; Loading
src/http.h +7 −5 Changes for src/http.h: 7 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -98,7 +98,7 @@ namespace libhttppp { void send(netplus::con *curconnection,const char* data, unsigned long datalen); //only use as server /*client method*/ void parse(netplus::con *curconnection); void parse(const char *data,size_t len); private: std::string _State; std::string _Version; Loading @@ -114,23 +114,25 @@ namespace libhttppp { ~HttpRequest(); /*server methods*/ void parse(netplus::con *curconnection); //only use as server size_t printHeader(char **buffer); void printHeader(std::string &buffer); int getRequestType(); const char *getRequestURL(); const char *getRequest(); size_t getRequestLength(); const char *getRequestVersion(); /*Client methods*/ void setRequestType(int req); void setRequestURL(const char *url); void setRequestVersion(const char *version); /*only for post Reuquesttype*/ void setRequestData(const char *data,size_t len); void send(netplus::socket* src,netplus::socket* dest); private: std::string _Request; int _RequestType; std::string _RequestURL; std::string _Version; std::string _RequestVersion; HttpHeader *_HttpHeader; netplus::con *_Connection; Loading