Loading plugins/nginxfiler/filer.cpp +64 −35 Original line number Diff line number Diff line Loading @@ -147,8 +147,6 @@ namespace blogi { bool Controller(const int tid,libhttppp::HttpRequest *req,libhtmlpp::HtmlElement *page){ char url[512]; struct json_object *ndir=nullptr; if(strncmp(req->getRequestURL(),Args->config->buildurl("nginxfiler",url,512),strlen(Args->config->buildurl("nginxfiler",url,512)))!=0){ return false; } Loading @@ -165,9 +163,11 @@ namespace blogi { try{ try{ srvsock=std::make_shared<netplus::tcp>(); // srvsock->setnonblocking(); cltsock=std::make_shared<netplus::tcp>(_NHost.c_str(),_NPort,1,0); cltsock->setTimeout(1); // cltsock->setnonblocking(); srvsock->connect(cltsock.get()); }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); Loading @@ -186,18 +186,36 @@ namespace blogi { *nreq.setData("user-agent") = "blogi/1.0 (Alpha Version 0.1)"; nreq.send(cltsock.get(),srvsock.get()); std::shared_ptr<char[16384]> data(new char[16384]); std::string test; nreq.printHeader(test); std::cerr << test <<std::endl; std::shared_ptr<char> data(new char[16384], std::default_delete<char[]>()); int recv,tries=0,chunklen=0; try{ for(;;){ try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); 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::string json; libhttppp::HttpResponse res; size_t hsize=0,cpos; Loading @@ -216,7 +234,7 @@ namespace blogi { try{ if(strcmp(res.getTransferEncoding(),"chunked")==0){ chunklen=readchunk(data.get(),recv,++cpos); chunklen=readchunk(data.get(),recv,cpos); chunked=true; }else{ throw; Loading Loading @@ -260,7 +278,7 @@ namespace blogi { size_t readed=0; for(;;){ if(recv - cpos < 0){ if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ Loading @@ -276,38 +294,54 @@ namespace blogi { readed+=len; }else{ try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); for(;;){ cpos=0; try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); 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 eh; eh[netplus::NetException::Error] << "nginxfiler: can't reach nginx server: " << eh.what(); throw eh; libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::cout << "\n"<< json << std::endl; enum json_tokener_error jerr = json_tokener_success; ndir = json_tokener_parse_verbose(json.c_str(),&jerr); struct json_object *ndir; ndir = json_tokener_parse(json.c_str()); if(jerr != json_tokener_success){ libhttppp::HTTPException ej; ej[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json :" << json_tokener_error_desc(jerr); throw ej; if(!ndir){ libhttppp::HTTPException e; e[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json !"; throw e; } libhtmlpp::HtmlString fileHtml,out; std::string sid; try{ RenderUI(path,ndir,fileHtml); libhtmlpp::Element *fel=fileHtml.parse(); libhtmlpp::HtmlElement *fel=(libhtmlpp::HtmlElement*)fileHtml.parse(); 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,sid)); Loading @@ -323,7 +357,6 @@ namespace blogi { curres.setState(HTTP500); curres.send(req, e.what(),strlen(e.what())); } while (json_object_put(ndir) != 1){}; return true; } private: Loading @@ -332,14 +365,7 @@ namespace blogi { char value[512]; while( (pos < datasize) && data[pos]!='\r'){ if (!isxdigit(data[pos])){ libhttppp::HTTPException ex; ex[libhttppp::HTTPException::Error] << "nginxfiler: wrong chuncklen at sign: " << pos; throw ex; } ++pos; }; while( (pos < datasize) && data[++pos]!='\r'){}; int len=pos-start; Loading @@ -353,6 +379,10 @@ namespace blogi { value[len]='\0'; if (len < 1) { return 0; } int result=strtol(value, NULL, 16); pos+=2; Loading @@ -374,4 +404,3 @@ extern "C" blogi::PluginApi* create() { extern "C" void destroy(blogi::PluginApi* p) { delete p; } Loading
plugins/nginxfiler/filer.cpp +64 −35 Original line number Diff line number Diff line Loading @@ -147,8 +147,6 @@ namespace blogi { bool Controller(const int tid,libhttppp::HttpRequest *req,libhtmlpp::HtmlElement *page){ char url[512]; struct json_object *ndir=nullptr; if(strncmp(req->getRequestURL(),Args->config->buildurl("nginxfiler",url,512),strlen(Args->config->buildurl("nginxfiler",url,512)))!=0){ return false; } Loading @@ -165,9 +163,11 @@ namespace blogi { try{ try{ srvsock=std::make_shared<netplus::tcp>(); // srvsock->setnonblocking(); cltsock=std::make_shared<netplus::tcp>(_NHost.c_str(),_NPort,1,0); cltsock->setTimeout(1); // cltsock->setnonblocking(); srvsock->connect(cltsock.get()); }catch(netplus::NetException &e){ libhttppp::HTTPException he; he[libhttppp::HTTPException::Error] << e.what(); Loading @@ -186,18 +186,36 @@ namespace blogi { *nreq.setData("user-agent") = "blogi/1.0 (Alpha Version 0.1)"; nreq.send(cltsock.get(),srvsock.get()); std::shared_ptr<char[16384]> data(new char[16384]); std::string test; nreq.printHeader(test); std::cerr << test <<std::endl; std::shared_ptr<char> data(new char[16384], std::default_delete<char[]>()); int recv,tries=0,chunklen=0; try{ for(;;){ try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); 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::string json; libhttppp::HttpResponse res; size_t hsize=0,cpos; Loading @@ -216,7 +234,7 @@ namespace blogi { try{ if(strcmp(res.getTransferEncoding(),"chunked")==0){ chunklen=readchunk(data.get(),recv,++cpos); chunklen=readchunk(data.get(),recv,cpos); chunked=true; }else{ throw; Loading Loading @@ -260,7 +278,7 @@ namespace blogi { size_t readed=0; for(;;){ if(recv - cpos < 0){ if(recv - cpos > 0){ if(readed==chunklen){ if( (chunklen=readchunk(data.get(),recv,cpos)) == 0 ){ Loading @@ -276,38 +294,54 @@ namespace blogi { readed+=len; }else{ try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); for(;;){ cpos=0; try{ recv=srvsock->recvData(cltsock.get(),data.get(),16384); 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 eh; eh[netplus::NetException::Error] << "nginxfiler: can't reach nginx server: " << eh.what(); throw eh; libhttppp::HTTPException ee; ee[libhttppp::HTTPException::Error] << e.what(); throw ee; } } }; } std::cout << "\n"<< json << std::endl; enum json_tokener_error jerr = json_tokener_success; ndir = json_tokener_parse_verbose(json.c_str(),&jerr); struct json_object *ndir; ndir = json_tokener_parse(json.c_str()); if(jerr != json_tokener_success){ libhttppp::HTTPException ej; ej[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json :" << json_tokener_error_desc(jerr); throw ej; if(!ndir){ libhttppp::HTTPException e; e[libhttppp::HTTPException::Error] << "nginxfiler: counld't read json !"; throw e; } libhtmlpp::HtmlString fileHtml,out; std::string sid; try{ RenderUI(path,ndir,fileHtml); libhtmlpp::Element *fel=fileHtml.parse(); libhtmlpp::HtmlElement *fel=(libhtmlpp::HtmlElement*)fileHtml.parse(); 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,sid)); Loading @@ -323,7 +357,6 @@ namespace blogi { curres.setState(HTTP500); curres.send(req, e.what(),strlen(e.what())); } while (json_object_put(ndir) != 1){}; return true; } private: Loading @@ -332,14 +365,7 @@ namespace blogi { char value[512]; while( (pos < datasize) && data[pos]!='\r'){ if (!isxdigit(data[pos])){ libhttppp::HTTPException ex; ex[libhttppp::HTTPException::Error] << "nginxfiler: wrong chuncklen at sign: " << pos; throw ex; } ++pos; }; while( (pos < datasize) && data[++pos]!='\r'){}; int len=pos-start; Loading @@ -353,6 +379,10 @@ namespace blogi { value[len]='\0'; if (len < 1) { return 0; } int result=strtol(value, NULL, 16); pos+=2; Loading @@ -374,4 +404,3 @@ extern "C" blogi::PluginApi* create() { extern "C" void destroy(blogi::PluginApi* p) { delete p; }