Loading examples/httpclient.cpp +3 −221 Changes for examples/httpclient.cpp: 3 added lines, 221 removed lines. Original line number Diff line number Diff line Loading @@ -40,42 +40,7 @@ #include <cstring> // 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; // find CR while (pos < datasize && data[pos] != '\r') pos++; if (pos >= datasize) { pos = start; return -1; } // need more if (pos + 1 >= datasize || data[pos + 1] != '\n') { pos = start; return -1; } // need more // 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; // 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] << "Invalid chunk size line: " << hex; throw ee; } return v; } int main(int argc, char** argv){ libhttppp::HttpUrl url(argv[1]); Loading @@ -85,10 +50,6 @@ int main(int argc, char** argv){ #endif std::unique_ptr<netplus::socket> cltsock; netplus::x509cert x509; netplus::buffer data(16384); try{ try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); Loading @@ -98,191 +59,12 @@ int main(int argc, char** argv){ 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(url,cltsock); libhttppp::HttpClient clt(url); std::vector<char> back = clt.Get(req); std::cout.write(back.data(),back.size()); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } size_t recv=0; for (;;) { try { recv = cltsock->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; size_t len=recv,chunklen=0,hsize=0; 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; } 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(); } } if(chunked==false){ chunked=false; rlen=(long)res.getContentLength(); } size_t cpos = hsize; auto recvMore = [&]() { for (;;) { try { recv = cltsock->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)cltsock->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 (cpos >= recv) { recvMore(); continue; } 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; } 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; if (readed == chunklen) { st = READ_CRLF; } continue; } if (st == READ_CRLF) { // need exactly \r\n after chunk data if (recv - cpos < 2) { // boundary recvMore(); continue; } if (data.data.buf[cpos] != '\r' || data.data.buf[cpos + 1] != '\n') { libhttppp::HTTPException ee; 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){ std::cerr << exp.what() <<std::endl; return -1; } } src/http.cpp +372 −127 File changed.Preview size limit exceeded, changes collapsed. Show changes 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 @@ -89,6 +89,7 @@ namespace libhttppp { private: HttpUrl _url; std::unique_ptr<netplus::socket> _cltsock; netplus::x509cert _cert; }; Loading Loading
examples/httpclient.cpp +3 −221 Changes for examples/httpclient.cpp: 3 added lines, 221 removed lines. Original line number Diff line number Diff line Loading @@ -40,42 +40,7 @@ #include <cstring> // 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; // find CR while (pos < datasize && data[pos] != '\r') pos++; if (pos >= datasize) { pos = start; return -1; } // need more if (pos + 1 >= datasize || data[pos + 1] != '\n') { pos = start; return -1; } // need more // 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; // 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] << "Invalid chunk size line: " << hex; throw ee; } return v; } int main(int argc, char** argv){ libhttppp::HttpUrl url(argv[1]); Loading @@ -85,10 +50,6 @@ int main(int argc, char** argv){ #endif std::unique_ptr<netplus::socket> cltsock; netplus::x509cert x509; netplus::buffer data(16384); try{ try{ libhttppp::HttpRequest req; req.setRequestType(GETREQUEST); Loading @@ -98,191 +59,12 @@ int main(int argc, char** argv){ 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(url,cltsock); libhttppp::HttpClient clt(url); std::vector<char> back = clt.Get(req); std::cout.write(back.data(),back.size()); }catch(libhttppp::HTTPException &e){ std::cerr << e.what() << std::endl; return -1; } size_t recv=0; for (;;) { try { recv = cltsock->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; size_t len=recv,chunklen=0,hsize=0; 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; } 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(); } } if(chunked==false){ chunked=false; rlen=(long)res.getContentLength(); } size_t cpos = hsize; auto recvMore = [&]() { for (;;) { try { recv = cltsock->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)cltsock->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 (cpos >= recv) { recvMore(); continue; } 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; } 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; if (readed == chunklen) { st = READ_CRLF; } continue; } if (st == READ_CRLF) { // need exactly \r\n after chunk data if (recv - cpos < 2) { // boundary recvMore(); continue; } if (data.data.buf[cpos] != '\r' || data.data.buf[cpos + 1] != '\n') { libhttppp::HTTPException ee; 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){ std::cerr << exp.what() <<std::endl; return -1; } }
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 @@ -89,6 +89,7 @@ namespace libhttppp { private: HttpUrl _url; std::unique_ptr<netplus::socket> _cltsock; netplus::x509cert _cert; }; Loading