Loading plugins/nginxfiler/filer.cpp +24 −157 Changes for plugins/nginxfiler/filer.cpp: 24 added lines, 157 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ *******************************************************************************/ #include <iostream> #include <sstream> #include <chrono> #include <cstring> #include <cmath> Loading Loading @@ -86,6 +87,12 @@ namespace blogi { } void RenderUI(std::string path,struct json_object *jobj,libhtmlpp::HtmlString &out){ if (json_object_get_type(jobj) != json_type_array) { libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << "<p>Error: Invalid data format for directory listing.</p>"; throw he; } int fcount = json_object_array_length(jobj); out<<"<div id=\"nginxfiler\"><ul>"; for(int i =0; i<fcount; ++i) { Loading Loading @@ -163,179 +170,44 @@ namespace blogi { path+="/"; netplus::tcp srvsock(_NHost.c_str(),_NPort,2,0); netplus::tcp cltsock; try{ try{ srvsock.setTimeout(_Timeout); cltsock.connect(&srvsock); }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); throw he; } std::string nurl=_NPrefix+path; libhttppp::HttpRequest nreq; nreq.setRequestType(GETREQUEST); nreq.setRequestURL(nurl.c_str()); nreq.setRequestVersion(HTTPVERSION(1.1)); nreq.setHeaderData("connection")->push_back("keep-alive"); std::string cohost = _NHost; cohost += ':'; cohost += _NPort; nreq.setHeaderData("host")->push_back(cohost); nreq.setHeaderData("accept")->push_back("text/json"); nreq.setHeaderData("user-agent")->push_back("blogi/1.0 (Alpha Version 0.1)"); nreq.send(cltsock); netplus::buffer data(BLOCKSIZE); int recv,chunklen=0; try{ for(;;){ try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); throw he; } std::vector<char> json; libhttppp::HttpResponse res; int hsize=0,cpos; bool chunked=false; int rlen=0; hsize=res.parse(data.data.buf,recv); recv-=hsize; std::move(data.data.buf+hsize,data.data.buf+recv,data.data.buf); data.size=recv-hsize; cpos=0; try{ for(libhttppp::HttpHeader::HeaderData::Values *chdat=res.getTransferEncoding(); chdat; chdat=chdat->nextvalue()){ if(chdat->getvalue()=="chunked") chunked=true; } if(!chunked) throw "e"; }catch(...){ rlen=res.getContentLength(); json.resize(rlen); } if(!chunked){ do{ try{ json.insert(json.end(),data.data.buf+cpos,data.data.buf+recv); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } } }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } }while(rlen>0); }else{ std::stringstream ss; if(_NSSL) ss << "https://" << _NHost << ":" << _NPort << _NPrefix; else ss << "http://" << _NHost << ":" << _NPort << _NPrefix; int readed=0; std::cout << ss.str() << std::endl; for(;;){ if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.data.buf,recv,cpos)) == 0 ){ break; } readed=0; } libhttppp::HttpUrl url(ss.str()); int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); libhttppp::HttpRequest nreq; libhttppp::HttpClient client(url); std::copy(data.data.buf+cpos,data.data.buf+(cpos+len),std::back_inserter(json)); cpos+=len; readed+=len; }else{ try{ for(;;){ cpos=0; try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::vector<char> json = client.Get(nreq); json.push_back('\0'); std::cout << json.data() << std::endl; struct json_object *ndir; enum json_tokener_error jerrno; ndir = json_tokener_parse_verbose(json.data(),&jerrno); std::cout << json.data() << std::endl; if(jerrno != json_tokener_success){ libhttppp::HTTPException e; e[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json !"; std::cerr << json_tokener_error_desc(jerrno) <<std::endl; throw e; } std::cout << "test2" << std::endl; libhtmlpp::HtmlString fileHtml,out; try{ RenderUI(path,ndir,fileHtml); libhtmlpp::HtmlElement *fel=(libhtmlpp::HtmlElement*)fileHtml.parse(); Loading @@ -343,12 +215,6 @@ namespace blogi { if(page->getElementbyID("main") && fel) page->getElementbyID("main")->appendChild(fel); }catch(libhtmlpp::HTMLException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } Args->theme->printSite(tid,out,page,req.getRequestURL(),Args->auth->isLoggedIn(tid,req,sessionid)); libhttppp::HttpResponse curres; Loading Loading @@ -432,3 +298,4 @@ extern "C" blogi::PluginApi* create() { extern "C" void destroy(blogi::PluginApi* p) { delete p; } Loading
plugins/nginxfiler/filer.cpp +24 −157 Changes for plugins/nginxfiler/filer.cpp: 24 added lines, 157 removed lines. Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ *******************************************************************************/ #include <iostream> #include <sstream> #include <chrono> #include <cstring> #include <cmath> Loading Loading @@ -86,6 +87,12 @@ namespace blogi { } void RenderUI(std::string path,struct json_object *jobj,libhtmlpp::HtmlString &out){ if (json_object_get_type(jobj) != json_type_array) { libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << "<p>Error: Invalid data format for directory listing.</p>"; throw he; } int fcount = json_object_array_length(jobj); out<<"<div id=\"nginxfiler\"><ul>"; for(int i =0; i<fcount; ++i) { Loading Loading @@ -163,179 +170,44 @@ namespace blogi { path+="/"; netplus::tcp srvsock(_NHost.c_str(),_NPort,2,0); netplus::tcp cltsock; try{ try{ srvsock.setTimeout(_Timeout); cltsock.connect(&srvsock); }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); throw he; } std::string nurl=_NPrefix+path; libhttppp::HttpRequest nreq; nreq.setRequestType(GETREQUEST); nreq.setRequestURL(nurl.c_str()); nreq.setRequestVersion(HTTPVERSION(1.1)); nreq.setHeaderData("connection")->push_back("keep-alive"); std::string cohost = _NHost; cohost += ':'; cohost += _NPort; nreq.setHeaderData("host")->push_back(cohost); nreq.setHeaderData("accept")->push_back("text/json"); nreq.setHeaderData("user-agent")->push_back("blogi/1.0 (Alpha Version 0.1)"); nreq.send(cltsock); netplus::buffer data(BLOCKSIZE); int recv,chunklen=0; try{ for(;;){ try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); throw he; } std::vector<char> json; libhttppp::HttpResponse res; int hsize=0,cpos; bool chunked=false; int rlen=0; hsize=res.parse(data.data.buf,recv); recv-=hsize; std::move(data.data.buf+hsize,data.data.buf+recv,data.data.buf); data.size=recv-hsize; cpos=0; try{ for(libhttppp::HttpHeader::HeaderData::Values *chdat=res.getTransferEncoding(); chdat; chdat=chdat->nextvalue()){ if(chdat->getvalue()=="chunked") chunked=true; } if(!chunked) throw "e"; }catch(...){ rlen=res.getContentLength(); json.resize(rlen); } if(!chunked){ do{ try{ json.insert(json.end(),data.data.buf+cpos,data.data.buf+recv); rlen-=recv; if(rlen>0){ for(;;){ cpos=0; try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } } }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } }while(rlen>0); }else{ std::stringstream ss; if(_NSSL) ss << "https://" << _NHost << ":" << _NPort << _NPrefix; else ss << "http://" << _NHost << ":" << _NPort << _NPrefix; int readed=0; std::cout << ss.str() << std::endl; for(;;){ if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.data.buf,recv,cpos)) == 0 ){ break; } readed=0; } libhttppp::HttpUrl url(ss.str()); int len = (chunklen - readed) < (recv - cpos) ? (chunklen - readed) : (recv - cpos); libhttppp::HttpRequest nreq; libhttppp::HttpClient client(url); std::copy(data.data.buf+cpos,data.data.buf+(cpos+len),std::back_inserter(json)); cpos+=len; readed+=len; }else{ try{ for(;;){ cpos=0; try{ recv=cltsock.recvData(data,0); break; }catch(netplus::NetException &e){ if(e.getErrorType()==netplus::NetException::Note){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } e[netplus::NetException::Error] << "nginxfiler: can't reach nginx server !"; throw e; } } }catch(netplus::NetException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::vector<char> json = client.Get(nreq); json.push_back('\0'); std::cout << json.data() << std::endl; struct json_object *ndir; enum json_tokener_error jerrno; ndir = json_tokener_parse_verbose(json.data(),&jerrno); std::cout << json.data() << std::endl; if(jerrno != json_tokener_success){ libhttppp::HTTPException e; e[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json !"; std::cerr << json_tokener_error_desc(jerrno) <<std::endl; throw e; } std::cout << "test2" << std::endl; libhtmlpp::HtmlString fileHtml,out; try{ RenderUI(path,ndir,fileHtml); libhtmlpp::HtmlElement *fel=(libhtmlpp::HtmlElement*)fileHtml.parse(); Loading @@ -343,12 +215,6 @@ namespace blogi { if(page->getElementbyID("main") && fel) page->getElementbyID("main")->appendChild(fel); }catch(libhtmlpp::HTMLException &e){ libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } Args->theme->printSite(tid,out,page,req.getRequestURL(),Args->auth->isLoggedIn(tid,req,sessionid)); libhttppp::HttpResponse curres; Loading Loading @@ -432,3 +298,4 @@ extern "C" blogi::PluginApi* create() { extern "C" void destroy(blogi::PluginApi* p) { delete p; }