Loading examples/httpclient.cpp +39 −38 Changes for examples/httpclient.cpp: 39 added lines, 38 removed lines. Original line number Diff line number Diff line /******************************************************************************* Copyright (c) 2014, Jan Koester jan.koester@gmx.net All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Copyright (c) 2014, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> Loading @@ -34,11 +34,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "http.h" #include "exception.h" #include <cstring> int main(int argc, char** argv){ signal(SIGPIPE, SIG_IGN); netplus::socket *cltsock=nullptr; try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); cltsock=srvsock.connect(); libhttppp::HttpRequest req; try{ Loading @@ -47,7 +50,8 @@ int main(int argc, char** argv){ req.setRequestVersion(HTTPVERSION(1.1)); *req.setData("connection") << "keep-alive"; *req.setData("host") << argv[1] << ":" << argv[2]; *req.setData("Accept") << "text/html"; *req.setData("accept") << "text/html"; *req.setData("scheme") << "http"; *req.setData("user-agent") << "libhttppp/1.0 (Alpha Version 0.1)"; req.send(cltsock,&srvsock); }catch(libhttppp::HTTPException &e){ Loading @@ -56,41 +60,38 @@ int main(int argc, char** argv){ } char data[16384]; int len=cltsock->recvData(&srvsock,data,16384); int recv=cltsock->recvData(&srvsock,data,16384); std::string html; try { libhttppp::HttpResponse res; size_t amount = 0,rlen=0,len=recv; try { size_t hsize=res.parse(data,len); size_t amount = len-hsize; amount = len-hsize; if(amount>0) html.assign(data+hsize,amount); size_t rlen; try{ rlen=res.getContentLength(); }catch(...){ rlen=amount; rlen=0; } }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; }; while(amount<rlen){ try{ size_t recv=cltsock->recvData(&srvsock,data,16384); 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; }; delete cltsock; if(!html.empty()) std::cout << html << std::endl; delete cltsock; }catch(netplus::NetException &exp){ delete cltsock; std::cerr << exp.what() <<std::endl; return -1; } Loading src/http.cpp +15 −16 Changes for src/http.cpp: 15 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -322,16 +322,15 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ std::copy(data+v,data+ve,std::begin(_State)); while(ve<inlen){ if(data[ve]!='\r' || data[ve]!='\n') if(data[ve]!='\n') break; ++ve; } ++ve; size_t lrow=0,delimeter=0,startkeypos=0,pos=ve; while(pos< inlen){ if(delimeter==0 && data[pos]==':'){ delimeter=pos; } if(data[pos]=='\r' || (data[pos-1]!='\r' && data[pos]=='\n')) { if(delimeter>lrow && delimeter!=0){ size_t keylen=delimeter-startkeypos; Loading @@ -346,34 +345,34 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ if(pos > 0 && valuelen <= inlen){ std::string value; value.resize(valuelen); std::copy(data+(delimeter+1),data+pos,std::begin(value)); std::copy(data+(delimeter+1),data+(pos-1),std::begin(value)); for (size_t it = 0; it < valuelen; ++it) { value[it] = (char)tolower(value[it]); } *setData(key.c_str())<<value.c_str(); delimeter=0; } } } if(pos+2 < inlen && data[pos+2]=='\r'){ ++pos; break; }else if(pos+1 < inlen && data[pos+1]=='\n'){ break; } delimeter=0; lrow=pos; startkeypos=lrow+2; } if(delimeter==0 && data[pos]==':'){ delimeter=pos; } if(data[pos]=='\r' && data[pos+3]=='\n'){ pos+=3; break; }else if(data[pos]=='\n' && data[pos+1]=='\n'){ ++pos; break; } ++pos; } if(getData("content-length")) _ContentLength=getData("content-length"); if(getData("content-type")) _ContentType=getData("content-type"); if(getData("connection")) _Connection=getData("connection"); _ContentType=getData("content-type"); return ++pos; } Loading src/http.h +1 −0 Changes for src/http.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ namespace libhttppp { private: std::string _State; std::string _Version; bool _Chuncked; HeaderData *_Connection; HeaderData *_ContentType; HeaderData *_ContentLength; Loading Loading
examples/httpclient.cpp +39 −38 Changes for examples/httpclient.cpp: 39 added lines, 38 removed lines. Original line number Diff line number Diff line /******************************************************************************* Copyright (c) 2014, Jan Koester jan.koester@gmx.net All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Copyright (c) 2014, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> Loading @@ -34,11 +34,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "http.h" #include "exception.h" #include <cstring> int main(int argc, char** argv){ signal(SIGPIPE, SIG_IGN); netplus::socket *cltsock=nullptr; try{ netplus::tcp srvsock(argv[1],atoi(argv[2]),1,0); netplus::socket *cltsock=srvsock.connect(); cltsock=srvsock.connect(); libhttppp::HttpRequest req; try{ Loading @@ -47,7 +50,8 @@ int main(int argc, char** argv){ req.setRequestVersion(HTTPVERSION(1.1)); *req.setData("connection") << "keep-alive"; *req.setData("host") << argv[1] << ":" << argv[2]; *req.setData("Accept") << "text/html"; *req.setData("accept") << "text/html"; *req.setData("scheme") << "http"; *req.setData("user-agent") << "libhttppp/1.0 (Alpha Version 0.1)"; req.send(cltsock,&srvsock); }catch(libhttppp::HTTPException &e){ Loading @@ -56,41 +60,38 @@ int main(int argc, char** argv){ } char data[16384]; int len=cltsock->recvData(&srvsock,data,16384); int recv=cltsock->recvData(&srvsock,data,16384); std::string html; try { libhttppp::HttpResponse res; size_t amount = 0,rlen=0,len=recv; try { size_t hsize=res.parse(data,len); size_t amount = len-hsize; amount = len-hsize; if(amount>0) html.assign(data+hsize,amount); size_t rlen; try{ rlen=res.getContentLength(); }catch(...){ rlen=amount; rlen=0; } }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; }; while(amount<rlen){ try{ size_t recv=cltsock->recvData(&srvsock,data,16384); 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; }; delete cltsock; if(!html.empty()) std::cout << html << std::endl; delete cltsock; }catch(netplus::NetException &exp){ delete cltsock; std::cerr << exp.what() <<std::endl; return -1; } Loading
src/http.cpp +15 −16 Changes for src/http.cpp: 15 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -322,16 +322,15 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ std::copy(data+v,data+ve,std::begin(_State)); while(ve<inlen){ if(data[ve]!='\r' || data[ve]!='\n') if(data[ve]!='\n') break; ++ve; } ++ve; size_t lrow=0,delimeter=0,startkeypos=0,pos=ve; while(pos< inlen){ if(delimeter==0 && data[pos]==':'){ delimeter=pos; } if(data[pos]=='\r' || (data[pos-1]!='\r' && data[pos]=='\n')) { if(delimeter>lrow && delimeter!=0){ size_t keylen=delimeter-startkeypos; Loading @@ -346,34 +345,34 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ if(pos > 0 && valuelen <= inlen){ std::string value; value.resize(valuelen); std::copy(data+(delimeter+1),data+pos,std::begin(value)); std::copy(data+(delimeter+1),data+(pos-1),std::begin(value)); for (size_t it = 0; it < valuelen; ++it) { value[it] = (char)tolower(value[it]); } *setData(key.c_str())<<value.c_str(); delimeter=0; } } } if(pos+2 < inlen && data[pos+2]=='\r'){ ++pos; break; }else if(pos+1 < inlen && data[pos+1]=='\n'){ break; } delimeter=0; lrow=pos; startkeypos=lrow+2; } if(delimeter==0 && data[pos]==':'){ delimeter=pos; } if(data[pos]=='\r' && data[pos+3]=='\n'){ pos+=3; break; }else if(data[pos]=='\n' && data[pos+1]=='\n'){ ++pos; break; } ++pos; } if(getData("content-length")) _ContentLength=getData("content-length"); if(getData("content-type")) _ContentType=getData("content-type"); if(getData("connection")) _Connection=getData("connection"); _ContentType=getData("content-type"); return ++pos; } Loading
src/http.h +1 −0 Changes for src/http.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ namespace libhttppp { private: std::string _State; std::string _Version; bool _Chuncked; HeaderData *_Connection; HeaderData *_ContentType; HeaderData *_ContentLength; Loading