Loading examples/httpclient.cpp +174 −61 Changes for examples/httpclient.cpp: 174 added lines, 61 removed lines. Original line number Diff line number Diff line Loading @@ -40,64 +40,98 @@ #include <cstring> int readchunk(const char* data, size_t datasize, size_t& pos) { int start = pos; // returns: // >=0 : chunk length parsed successfully, and pos advanced past CRLF // -1 : need more data static long long readChunkSizeLine(const char* data, size_t datasize, size_t& pos) { size_t start = pos; char value[512]; // find CR while (pos < datasize && data[pos] != '\r') pos++; while ((pos++ < datasize) && data[pos] != '\r') {}; if (pos >= datasize) { pos = start; return -1; } // need more if (pos + 1 >= datasize || data[pos + 1] != '\n') { pos = start; return -1; } // need more int len = pos - start; // line is [start, pos) size_t len = pos - start; if (len == 0) { pos = start; return -1; } // shouldn't happen // handle chunk extensions: "1a;ext=foo" size_t end = start; while (end < pos && data[end] != ';') end++; std::string hex(data + start, data + end); // consume CRLF pos += 2; if (len > 511) { // parse hex char* ep = nullptr; long long v = std::strtoll(hex.c_str(), &ep, 16); if (ep == hex.c_str()) { // invalid hex, treat as protocol error libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << "nginxfiler: chunck size: " << len << " to big aborting !"; ee[libhttppp::HTTPException::Error] << "Invalid chunk size line: " << hex; throw ee; } memcpy(value, data + start, len); value[len] = '\0'; if (len < 1) { return 0; } int result = strtol(value,nullptr, 16); return result; return v; } int main(int argc, char** argv){ libhttppp::HttpUrl url(argv[1]); #ifndef Windows signal(SIGPIPE, SIG_IGN); #endif std::unique_ptr<netplus::socket> cltsock=std::make_unique<netplus::tcp>(argv[1], atoi(argv[2]), 1, 0); netplus::tcp srvsock; std::unique_ptr<netplus::socket> cltsock; std::unique_ptr<netplus::socket> srvsock; netplus::x509cert x509; if(url.getProtocol()==libhttppp::HttpUrl::HTTP){ cltsock=std::make_unique<netplus::tcp>(url.getHost(), url.getPort(), 1, 0); srvsock=std::make_unique<netplus::tcp>(-1); }else if((url.getProtocol()==libhttppp::HttpUrl::HTTPS)){ cltsock=std::make_unique<netplus::ssl>(url.getHost(), url.getPort(), 1, 0,x509); srvsock=std::make_unique<netplus::ssl>(x509,-1); } netplus::buffer data(16384); try{ srvsock.connect(cltsock); srvsock->connect(cltsock); try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestURL(url.getPath()); req.setRequestVersion(HTTPVERSION(1.1)); req.setHeaderData("connection")->push_back("keep-alive"); std::stringstream shost; shost << argv[1] << ':' << argv[2]; req.setHeaderData("host")->push_back(shost.str()); req.setHeaderData("host")->push_back(url.getHost()); req.setHeaderData("accept")->push_back("text/json"); req.setHeaderData("user-agent")->push_back("libhttppp/1.0 (Alpha Version 0.1)"); req.send(srvsock); req.send(*srvsock); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } size_t recv= srvsock.recvData(data,0); size_t recv=0; for (;;) { try { recv = srvsock->recvData(data,0); if (recv == 0) { continue; } break; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } std::cerr << e.what() << std::endl; return 1; } } std::vector<char> html; libhttppp::HttpResponse res; Loading @@ -105,77 +139,156 @@ int main(int argc, char** argv){ bool chunked=false; long long rlen=0; try{ hsize=res.parse(data.data.buf, recv); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; } try { for(libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); cval; cval=cval->nextvalue()){ auto skipCRLF = [&](size_t& cpos) { if (recv - cpos >= 2 && data.data.buf[cpos] == '\r' && data.data.buf[cpos+1] == '\n') { cpos += 2; return true; } return false; }; libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); if(cval){ while(cval){ if(cval->getvalue()=="chunked"){ chunked=true; } cval=cval->nextvalue(); } } }catch(...){ if(chunked==false){ chunked=false; rlen=(long)res.getContentLength(); }; } size_t cpos = hsize; auto recvMore = [&]() { for (;;) { try { recv = srvsock->recvData(data, 0); cpos = 0; if (recv == 0) { libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << "Connection closed while reading chunked body"; throw ee; } return; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; if(!chunked){ do{ std::copy(data.data.buf + cpos, data.data.buf+recv, std::back_inserter(html)); rlen-=((long)recv- cpos); if(rlen>0){ recv=(long)srvsock.recvData(data,0); recv=(long)srvsock->recvData(data,0); cpos=0; } }while(rlen>0); } else { size_t chunklen = 0; size_t readed = 0; enum State { READ_SIZE, READ_DATA, READ_CRLF, DONE }; State st = READ_SIZE; for (;;) { if (recv - cpos > 0) { if (cpos >= recv) { recvMore(); continue; } if (readed == chunklen) { if ((chunklen = readchunk(data.data.buf, recv, cpos)) == 0) { break; if (st == READ_SIZE) { long long cl = readChunkSizeLine(data.data.buf, recv, cpos); if (cl < 0) { // need more data recvMore(); continue; } if (cl == 0) { st = DONE; // RFC: after 0-size chunk there may be trailers terminated by CRLF. // We'll just drain until we see a blank line. continue; } chunklen = (size_t)cl; readed = 0; st = READ_DATA; continue; } size_t len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.data.buf + cpos, data.data.buf + (cpos + len), std::back_inserter(html)); if (st == READ_DATA) { size_t avail = recv - cpos; size_t take = std::min(chunklen - readed, avail); html.insert(html.end(), data.data.buf + cpos, data.data.buf + cpos + take); cpos += take; readed += take; cpos += len; readed += len; } else { try { for (;;) { cpos = 0; try { recv = srvsock.recvData(data,0); break; if (readed == chunklen) { st = READ_CRLF; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } throw e; } if (st == READ_CRLF) { // need exactly \r\n after chunk data if (recv - cpos < 2) { // boundary recvMore(); continue; } } catch (netplus::NetException& e) { if (data.data.buf[cpos] != '\r' || data.data.buf[cpos + 1] != '\n') { libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); ee[libhttppp::HTTPException::Error] << "Malformed chunk: missing CRLF after data"; throw ee; } cpos += 2; st = READ_SIZE; continue; } }; } if (st == DONE) { // Drain trailers until blank line: "\r\n" // If we're already at end of buffer, read more. if (cpos >= recv) { recvMore(); continue; } // Look for CRLFCRLF across boundaries in a simple way: // read line by line similar to size line. size_t lineStart = cpos; while (cpos < recv && data.data.buf[cpos] != '\r') cpos++; if (cpos >= recv) { cpos = lineStart; recvMore(); continue; } if (cpos + 1 >= recv || data.data.buf[cpos + 1] != '\n') { cpos = lineStart; recvMore(); continue; } size_t lineLen = cpos - lineStart; cpos += 2; // consume CRLF if (lineLen == 0) { // blank line => end of trailers break; } // else: ignore trailer line content and continue continue; } } } /* for (size_t i = 0; i < std::min<size_t>(html.size(), 8); ++i) printf("%02x ", (unsigned char)html[i]); printf("\n");*/ std::cout.write(html.data(),html.size())<<std::endl; return 0; }catch(netplus::NetException &exp){ Loading src/httpd.cpp +2 −2 Changes for src/httpd.cpp: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -52,8 +52,8 @@ struct nop void operator() (T const &) const noexcept { } }; void libhttppp::HttpEvent::CreateConnection(std::unique_ptr<netplus::con> &res){ res=std::make_unique<HttpRequest>(std::move(this)); void libhttppp::HttpEvent::CreateConnection(std::shared_ptr<netplus::con> &res){ res=std::make_shared<HttpRequest>(std::move(this)); } void libhttppp::HttpEvent::RequestEvent(HttpRequest &curreq,const int tid,ULONG_PTR args){ Loading src/httpd.h +1 −1 Changes for src/httpd.h: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ namespace libhttppp { virtual void DisconnectEvent(HttpRequest &curreq,const int tid,ULONG_PTR args); protected: void CreateConnection(std::unique_ptr<netplus::con> &res); void CreateConnection(std::shared_ptr<netplus::con> &res); private: void RequestEvent(netplus::con &curcon, const int tid, ULONG_PTR args); void ResponseEvent(netplus::con &curcon,const int tid,ULONG_PTR args); Loading Loading
examples/httpclient.cpp +174 −61 Changes for examples/httpclient.cpp: 174 added lines, 61 removed lines. Original line number Diff line number Diff line Loading @@ -40,64 +40,98 @@ #include <cstring> int readchunk(const char* data, size_t datasize, size_t& pos) { int start = pos; // returns: // >=0 : chunk length parsed successfully, and pos advanced past CRLF // -1 : need more data static long long readChunkSizeLine(const char* data, size_t datasize, size_t& pos) { size_t start = pos; char value[512]; // find CR while (pos < datasize && data[pos] != '\r') pos++; while ((pos++ < datasize) && data[pos] != '\r') {}; if (pos >= datasize) { pos = start; return -1; } // need more if (pos + 1 >= datasize || data[pos + 1] != '\n') { pos = start; return -1; } // need more int len = pos - start; // line is [start, pos) size_t len = pos - start; if (len == 0) { pos = start; return -1; } // shouldn't happen // handle chunk extensions: "1a;ext=foo" size_t end = start; while (end < pos && data[end] != ';') end++; std::string hex(data + start, data + end); // consume CRLF pos += 2; if (len > 511) { // parse hex char* ep = nullptr; long long v = std::strtoll(hex.c_str(), &ep, 16); if (ep == hex.c_str()) { // invalid hex, treat as protocol error libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << "nginxfiler: chunck size: " << len << " to big aborting !"; ee[libhttppp::HTTPException::Error] << "Invalid chunk size line: " << hex; throw ee; } memcpy(value, data + start, len); value[len] = '\0'; if (len < 1) { return 0; } int result = strtol(value,nullptr, 16); return result; return v; } int main(int argc, char** argv){ libhttppp::HttpUrl url(argv[1]); #ifndef Windows signal(SIGPIPE, SIG_IGN); #endif std::unique_ptr<netplus::socket> cltsock=std::make_unique<netplus::tcp>(argv[1], atoi(argv[2]), 1, 0); netplus::tcp srvsock; std::unique_ptr<netplus::socket> cltsock; std::unique_ptr<netplus::socket> srvsock; netplus::x509cert x509; if(url.getProtocol()==libhttppp::HttpUrl::HTTP){ cltsock=std::make_unique<netplus::tcp>(url.getHost(), url.getPort(), 1, 0); srvsock=std::make_unique<netplus::tcp>(-1); }else if((url.getProtocol()==libhttppp::HttpUrl::HTTPS)){ cltsock=std::make_unique<netplus::ssl>(url.getHost(), url.getPort(), 1, 0,x509); srvsock=std::make_unique<netplus::ssl>(x509,-1); } netplus::buffer data(16384); try{ srvsock.connect(cltsock); srvsock->connect(cltsock); try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); req.setRequestURL(argv[3]); req.setRequestURL(url.getPath()); req.setRequestVersion(HTTPVERSION(1.1)); req.setHeaderData("connection")->push_back("keep-alive"); std::stringstream shost; shost << argv[1] << ':' << argv[2]; req.setHeaderData("host")->push_back(shost.str()); req.setHeaderData("host")->push_back(url.getHost()); req.setHeaderData("accept")->push_back("text/json"); req.setHeaderData("user-agent")->push_back("libhttppp/1.0 (Alpha Version 0.1)"); req.send(srvsock); req.send(*srvsock); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } size_t recv= srvsock.recvData(data,0); size_t recv=0; for (;;) { try { recv = srvsock->recvData(data,0); if (recv == 0) { continue; } break; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } std::cerr << e.what() << std::endl; return 1; } } std::vector<char> html; libhttppp::HttpResponse res; Loading @@ -105,77 +139,156 @@ int main(int argc, char** argv){ bool chunked=false; long long rlen=0; try{ hsize=res.parse(data.data.buf, recv); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; } try { for(libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); cval; cval=cval->nextvalue()){ auto skipCRLF = [&](size_t& cpos) { if (recv - cpos >= 2 && data.data.buf[cpos] == '\r' && data.data.buf[cpos+1] == '\n') { cpos += 2; return true; } return false; }; libhttppp::HttpHeader::HeaderData::Values *cval=res.getTransferEncoding(); if(cval){ while(cval){ if(cval->getvalue()=="chunked"){ chunked=true; } cval=cval->nextvalue(); } } }catch(...){ if(chunked==false){ chunked=false; rlen=(long)res.getContentLength(); }; } size_t cpos = hsize; auto recvMore = [&]() { for (;;) { try { recv = srvsock->recvData(data, 0); cpos = 0; if (recv == 0) { libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << "Connection closed while reading chunked body"; throw ee; } return; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; if(!chunked){ do{ std::copy(data.data.buf + cpos, data.data.buf+recv, std::back_inserter(html)); rlen-=((long)recv- cpos); if(rlen>0){ recv=(long)srvsock.recvData(data,0); recv=(long)srvsock->recvData(data,0); cpos=0; } }while(rlen>0); } else { size_t chunklen = 0; size_t readed = 0; enum State { READ_SIZE, READ_DATA, READ_CRLF, DONE }; State st = READ_SIZE; for (;;) { if (recv - cpos > 0) { if (cpos >= recv) { recvMore(); continue; } if (readed == chunklen) { if ((chunklen = readchunk(data.data.buf, recv, cpos)) == 0) { break; if (st == READ_SIZE) { long long cl = readChunkSizeLine(data.data.buf, recv, cpos); if (cl < 0) { // need more data recvMore(); continue; } if (cl == 0) { st = DONE; // RFC: after 0-size chunk there may be trailers terminated by CRLF. // We'll just drain until we see a blank line. continue; } chunklen = (size_t)cl; readed = 0; st = READ_DATA; continue; } size_t len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); std::copy(data.data.buf + cpos, data.data.buf + (cpos + len), std::back_inserter(html)); if (st == READ_DATA) { size_t avail = recv - cpos; size_t take = std::min(chunklen - readed, avail); html.insert(html.end(), data.data.buf + cpos, data.data.buf + cpos + take); cpos += take; readed += take; cpos += len; readed += len; } else { try { for (;;) { cpos = 0; try { recv = srvsock.recvData(data,0); break; if (readed == chunklen) { st = READ_CRLF; } catch (netplus::NetException& e) { if (e.getErrorType() == netplus::NetException::Note) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } throw e; } if (st == READ_CRLF) { // need exactly \r\n after chunk data if (recv - cpos < 2) { // boundary recvMore(); continue; } } catch (netplus::NetException& e) { if (data.data.buf[cpos] != '\r' || data.data.buf[cpos + 1] != '\n') { libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); ee[libhttppp::HTTPException::Error] << "Malformed chunk: missing CRLF after data"; throw ee; } cpos += 2; st = READ_SIZE; continue; } }; } if (st == DONE) { // Drain trailers until blank line: "\r\n" // If we're already at end of buffer, read more. if (cpos >= recv) { recvMore(); continue; } // Look for CRLFCRLF across boundaries in a simple way: // read line by line similar to size line. size_t lineStart = cpos; while (cpos < recv && data.data.buf[cpos] != '\r') cpos++; if (cpos >= recv) { cpos = lineStart; recvMore(); continue; } if (cpos + 1 >= recv || data.data.buf[cpos + 1] != '\n') { cpos = lineStart; recvMore(); continue; } size_t lineLen = cpos - lineStart; cpos += 2; // consume CRLF if (lineLen == 0) { // blank line => end of trailers break; } // else: ignore trailer line content and continue continue; } } } /* for (size_t i = 0; i < std::min<size_t>(html.size(), 8); ++i) printf("%02x ", (unsigned char)html[i]); printf("\n");*/ std::cout.write(html.data(),html.size())<<std::endl; return 0; }catch(netplus::NetException &exp){ Loading
src/httpd.cpp +2 −2 Changes for src/httpd.cpp: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -52,8 +52,8 @@ struct nop void operator() (T const &) const noexcept { } }; void libhttppp::HttpEvent::CreateConnection(std::unique_ptr<netplus::con> &res){ res=std::make_unique<HttpRequest>(std::move(this)); void libhttppp::HttpEvent::CreateConnection(std::shared_ptr<netplus::con> &res){ res=std::make_shared<HttpRequest>(std::move(this)); } void libhttppp::HttpEvent::RequestEvent(HttpRequest &curreq,const int tid,ULONG_PTR args){ Loading
src/httpd.h +1 −1 Changes for src/httpd.h: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ namespace libhttppp { virtual void DisconnectEvent(HttpRequest &curreq,const int tid,ULONG_PTR args); protected: void CreateConnection(std::unique_ptr<netplus::con> &res); void CreateConnection(std::shared_ptr<netplus::con> &res); private: void RequestEvent(netplus::con &curcon, const int tid, ULONG_PTR args); void ResponseEvent(netplus::con &curcon,const int tid,ULONG_PTR args); Loading