void spider::PageDownloader::download() const { using std::back_inserter; using std::string; using std::vector; try { Url const& url = getUrl(); std::cerr << "Downloading page: " << url << std::endl; HttpResponse response = getResponse(); int statusCode = response.getStatusCode(); if (statusCode < 200 || statusCode >= 400) { return; // ignore errors } if (statusCode >= 300 && statusCode < 400) { handleRedirect(response); return; } string content = getContent(response); vector<Url> urls; m_finder.getUrls(url, content, back_inserter(urls)); m_manager.download(url, urls.begin(), urls.end()); } catch (ConnectionException const& exception) { std::cerr << exception.what() << std::endl; } }
void WebClientApp::onReadComplete() { mText.push_back( "Read complete" ); console() << "HTTP version: "; switch ( mHttpResponse.getHttpVersion() ) { case HttpVersion::HTTP_0_9: console() << "0.9"; break; case HttpVersion::HTTP_1_0: console() << "1.0"; break; case HttpVersion::HTTP_1_1: console() << "1.1"; break; case HttpVersion::HTTP_2_0: console() << "2.0"; break; } console() << endl; console() << "Status code: " << mHttpResponse.getStatusCode() << endl; console() << "Reason: " << mHttpResponse.getReason() << endl; console() << "Header fields: " << endl; for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { console() << ">> " << kvp.first << ": " << kvp.second << endl; } console() << endl; console() << "Body:" << endl; console() << HttpResponse::bufferToString( mHttpResponse.getBody() ) << endl; mSession->close(); }
void HttpProtocol::geneHttpResponse(const HttpResponse& http,string& response) { HttpHeader header; http.getHeader(header); char t[10]; sprintf(t, "%d", http.getStatusCode()); string code = t; response = "" + http.getVersion() + " " + code + " " + http.getReason() + "\n"; geneHeader(response,header); response += http.getContent(); }
bool Http11Handler::writeResponse(void* req, void* res, void* context) { HttpRequest* request = (HttpRequest*)req; HttpResponse* response = (HttpResponse*)res; if(isClosed()) { if(request!=NULL) { delete request; } delete response; return true; } if(!response->isDone()) { response->updateContent(request, chunkSize); } if(response->getHeader(HttpRequest::Connection)=="") { if(StringUtil::toLowerCopy(request->getHeader(HttpRequest::Connection))!="keep-alive" || CastUtil::lexical_cast<int>(response->getStatusCode())>307 || request->getHttpVers()<1.1) { response->addHeaderValue(HttpResponse::Connection, "close"); } else { response->addHeaderValue(HttpResponse::Connection, "keep-alive"); } } std::string data = response->generateOnlyHeaderResponse(request); if(!write(data)) { bool isFirst = true; while(response->hasContent && (data = response->getRemainingContent(request->getUrl(), isFirst)) != "") { isFirst = false; if(write(data)) { break; } } } if(request!=NULL) { delete request; } delete response; return true; }
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, map<string, string> urlpattMap, map<string, string> mappattMap, void* dlib, string ext, resFuncMap rstCntMap, map<string, string> mapMap, map<string, string> urlMap, string pthwofile) { Logger logger = Logger::getLogger("ControllerHandler"); string claz; bool isContrl = false; if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!="")) { //logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl; if(urlpattMap[req->getCntxt_name()+"*.*"]!="") claz = "getReflectionCIFor" + urlpattMap[req->getCntxt_name()+"*.*"]; else claz = "getReflectionCIFor" + urlMap[req->getCntxt_name()+ext]; string libName = Constants::INTER_LIB_FILE; if(dlib == NULL) { cerr << dlerror() << endl; exit(-1); } void *mkr = dlsym(dlib, claz.c_str()); if(mkr!=NULL) { FunPtr f = (FunPtr)mkr; ClassInfo srv = f(); args argus; Constructor ctor = srv.getConstructor(argus); Reflector ref; void *_temp = ref.newInstanceGVP(ctor); Controller *thrd = (Controller *)_temp; try{ logger << "Controller called" << endl; res = thrd->service(*req); logger << res.getStatusCode() << endl; logger << res.getContent_type() << endl; logger << res.getContent_len() << endl; if(res.getStatusCode()!="") isContrl = true; ext = AuthHandler::getFileExtension(req->getUrl()); //delete mkr; }catch(...){ logger << "Controller exception" << endl;} logger << "Controller called\n" << flush; } } else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!="")) { string file = req->getFile(); string fili = file.substr(0,file.find_last_of(".")); if(mappattMap[req->getCntxt_name()+"*.*"]!="") { req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]); logger << "URL mapped from * to " << mappattMap[req->getCntxt_name()+"*.*"] << endl; } else { req->setFile(fili+mapMap[req->getCntxt_name()+ext]); logger << "URL mapped from " << ext << " to " << mapMap[req->getCntxt_name()+ext] << endl; } } else { resFuncMap::iterator it; RestFunction rft; bool flag = false; int prsiz = 0; vector<string> valss; //logger << pthwofile << endl; for (it=rstCntMap.begin();it!=rstCntMap.end();it++) { valss.clear(); //logger << it->first << endl; //if(pthwofile.find(it->first)!=string::npos) { RestFunction ft = it->second; prsiz = ft.params.size(); string pthwofiletemp(pthwofile); if(ft.baseUrl=="") { logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz << ", against url: " << it->first << endl; for (int var = 0; var < prsiz; var++) { //logger << "loop - " << pthwofiletemp << endl; string valsvv(pthwofiletemp.substr(pthwofiletemp.find_last_of("/")+1)); pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.find_last_of("/")); valss.push_back(valsvv); } reverse(valss.begin(),valss.end()); //logger << "after - " << pthwofiletemp << endl; /*if(pthwofiletemp.at(pthwofiletemp.length()-1)=='/') { pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.length()-1); }*/ //logger << "after - " << pthwofiletemp << endl; logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz << ",vals: " << valss.size() << ", against url: " << it->first << endl; if(it->first==pthwofiletemp) { string lhs = StringUtil::toUpperCopy(ft.meth); string rhs = StringUtil::toUpperCopy(req->getMethod()); logger << lhs << " <> " << rhs << endl; if(prsiz==(int)valss.size() && lhs==rhs) { logger << "got correct url -- restcontroller " << endl; rft = ft; flag = true; } else { res.setStatusCode("404"); res.setStatusMsg("Not Found"); //res.setContent_type("text/plain"); /*if(prsiz==valss.size()) res.setContent_str("Invalid number of arguments"); else res.setContent_str("Invalid HTTPMethod used");*/ logger << "Rest Controller Param/Method Error" << endl; } break; } } else { string baseUrl(req->getCntxt_name()+ft.baseUrl); logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz << ", against url: " << baseUrl << endl; for (int var = 1; var <= prsiz; var++) { strVec vemp; stringstream ss; ss << "{"; ss << var; ss << "}"; string param; ss >> param; StringUtil::split(vemp, baseUrl, (param)); if(vemp.size()==2 && pthwofiletemp.find(vemp.at(0))!=string::npos) { string temp = pthwofiletemp; StringUtil::replaceFirst(temp, vemp.at(0), ""); if(temp.find("/")!=string::npos) { pthwofiletemp = temp.substr(temp.find("/")); temp = temp.substr(0, temp.find("/")); } valss.push_back(temp); baseUrl = vemp.at(1); logger << "variable at " << param << " mapped to " << temp << " from URL" << endl; logger << baseUrl << endl; logger << pthwofiletemp << endl; } else { flag = false; break; } } string lhs = StringUtil::toUpperCopy(ft.meth); string rhs = StringUtil::toUpperCopy(req->getMethod()); logger << lhs << " <> " << rhs << endl; if(prsiz==(int)valss.size() && lhs==rhs) { logger << "got correct url -- restcontroller " << endl; rft = ft; flag = true; break; } else { res.setStatusCode("404"); res.setStatusMsg("Not Found"); //res.setContent_type("text/plain"); /*if(prsiz==valss.size()) res.setContent_str("Invalid number of arguments"); else res.setContent_str("Invalid HTTPMethod used");*/ logger << "Rest Controller Param/Method Error" << endl; } } } } if(flag) { //logger << "inside restcontroller logic ..." << endl; string libName = Constants::INTER_LIB_FILE; if(dlib == NULL) { cerr << dlerror() << endl; exit(-1); } string clasnam("getReflectionCIFor"+rft.clas); void *mkr = dlsym(dlib, clasnam.c_str()); logger << mkr << endl; if(mkr!=NULL) { FunPtr f = (FunPtr)mkr; ClassInfo srv = f(); args argus; Constructor ctor = srv.getConstructor(argus); Reflector ref; void *_temp = ref.newInstanceGVP(ctor); RestController* rstcnt = (RestController*)_temp; rstcnt->request = req; rstcnt->response = &res; vals valus; bool invValue = false; for (int var = 0; var < prsiz; var++) { try { argus.push_back(rft.params.at(var).type); if(rft.params.at(var).type=="int") { int* ival = new int(CastUtil::lexical_cast<int>(valss.at(var))); valus.push_back(ival); } else if(rft.params.at(var).type=="long") { long* ival = new long(CastUtil::lexical_cast<long>(valss.at(var))); valus.push_back(ival); } else if(rft.params.at(var).type=="double") { double* ival = new double(CastUtil::lexical_cast<double>(valss.at(var))); valus.push_back(ival); } else if(rft.params.at(var).type=="float") { float* ival = new float(CastUtil::lexical_cast<float>(valss.at(var))); valus.push_back(ival); } else if(rft.params.at(var).type=="bool") { bool* ival = new bool(CastUtil::lexical_cast<bool>(valss.at(var))); valus.push_back(ival); } else if(rft.params.at(var).type=="string") { string* sval = new string(valss.at(var)); valus.push_back(sval); } } catch (...) { invValue= true; break; } } Method meth = srv.getMethod(rft.name, argus); if(meth.getMethodName()!="" && !invValue) { ref.invokeMethodUnknownReturn(_temp,meth,valus); logger << "successfully called restcontroller" << endl; //return; } else { res.setStatusCode("404"); res.setStatusMsg("Not Found"); //res.setContent_type("text/plain"); /*if(invValue) res.setContent_str("Invalid value passed as URL param"); else res.setContent_str("Rest Controller Method Not Found");*/ logger << "Rest Controller Method Not Found" << endl; //return; } } } } return isContrl; }
int ClientService::handle_input(ACE_HANDLE fd /* = ACE_INVALID_HANDLE */) { if ( fd != get_handle() ) { ND_DEBUG("[HTTP][Conn %07d] Received input for unmanaged handle.\n"); return 0; } ssize_t recv_cnt; // ACE_Time_Value zeroTime(ACE_Time_Value::zero); /* while ( (recv_cnt = sock_.recv(buffer_, buflen_)) > 0 ) { ND_DEBUG("[HTTP][Conn %07d] Received %d octets of request data.\n", _connectionNumber, recv_cnt); req_.addData(buffer_, recv_cnt); } int closeErr = errno; if (recv_cnt < 0 && closeErr != EWOULDBLOCK) { ND_DEBUG("[HTTP][Conn %07d] Connection closed to %s. %p.\n", _connectionNumber, peer_name_, "Reason"); req_.clear(); return -1; } */ if ( (recv_cnt = sock_.recv(buffer_, buflen_)) < 1 ) { ND_DEBUG("[HTTP][Conn %07d] Connection closed to %s. %p.\n", _connectionNumber, peer_name_, "Reason"); req_.clear(); return -1; } ND_DEBUG("[HTTP][Conn %07d] Received %d octets of request data.\n", _connectionNumber, recv_cnt); req_.addData(buffer_, recv_cnt); // std::string reqStr(req_.getAsString()); try { req_.parse(); } catch (const nd_error& e) { ND_CRITICAL("[HTTP][Conn %07d] %s\n", _connectionNumber, e.what()); req_.clear(); return 0; } // ND_DEBUG("[HTTP][Conn %07d] Received %d-octet request:\n%s\n", _connectionNumber, req_.getRequestLength(), reqStr.c_str()); if (req_.isComplete()) { HttpResponse response; httpManager::instance()->handleRequest(req_, response); string responseStr = response.makeClientStr((req_.getMethod() != HttpRequest::MethodHEAD)); ND_DEBUG("[HTTP][Conn %07d] Sending response (%d %s, %s, %d octet contents).\n", _connectionNumber, response.getStatusCode(), response.getStatusStr().c_str(), response.getContentType().c_str(), response.getContent().length()); sock_.send(responseStr.c_str(), responseStr.length()); double ver = req_.getProtocolVersion(); req_.clear(); if ( ver == 1.0 ) { ND_DEBUG("[HTTP][Conn %07d] Closing HTTP 1.0 connection.\n", _connectionNumber); return -1; } } else { if ( req_.hasContent() ) ND_DEBUG("[HTTP][Conn %07d] Received %d octets of %d expected, waiting for more.\n", _connectionNumber, req_.getContentLength(), req_.getReportedContentLength()); } return 0; }
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, ConfigurationData configData, string ext, string pthwofile) { map<string, string> urlpattMap = configData.urlpattMap; map<string, string> mappattMap = configData.mappattMap; resFuncMap rstCntMap = configData.rstCntMap; map<string, string> mapMap = configData.mapMap; map<string, string> urlMap = configData.urlMap; Logger logger = LoggerFactory::getLogger("ControllerHandler"); bool isContrl = false; if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!="")) { //logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl; string controller; if(urlpattMap[req->getCntxt_name()+"*.*"]!="") controller = urlpattMap[req->getCntxt_name()+"*.*"]; else controller = urlMap[req->getCntxt_name()+ext]; void *_temp = configData.ffeadContext->getBean("controller_"+req->getCntxt_name()+controller, req->getCntxt_name()); Controller* thrd = static_cast<Controller*>(_temp); if(thrd!=NULL) { try{ logger << ("Controller " + controller + " called") << endl; res = thrd->service(*req); if(res.getStatusCode()!="") isContrl = true; ext = AuthHandler::getFileExtension(req->getUrl()); //delete mkr; }catch(...){ logger << "Controller Exception occurred" << endl; } logger << "Controller call complete" << endl; } else { logger << "Invalid Controller" << endl; res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError); isContrl = true; } } else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!="")) { string file = req->getFile(); string fili = file.substr(0,file.find_last_of(".")); if(mappattMap[req->getCntxt_name()+"*.*"]!="") { req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]); logger << ("URL mapped from * to " + mappattMap[req->getCntxt_name()+"*.*"]) << endl; } else { req->setFile(fili+mapMap[req->getCntxt_name()+ext]); logger << ("URL mapped from " + ext + " to " + mapMap[req->getCntxt_name()+ext]) << endl; } } else { resFuncMap::iterator it; RestFunction rft; bool flag = false; int prsiz = 0; vector<string> valss; map<string, string> mapOfValues; //logger << pthwofile << endl; for (it=rstCntMap.begin();it!=rstCntMap.end();it++) { valss.clear(); //logger << it->first << endl; //if(pthwofile.find(it->first)!=string::npos) { RestFunction ft = it->second; prsiz = ft.params.size(); string pthwofiletemp(pthwofile); string baseUrl(it->first); strVec resturlparts; StringUtil::split(resturlparts, baseUrl, "/"); strVec urlparts; StringUtil::split(urlparts, pthwofiletemp, "/"); if(urlparts.size()!=resturlparts.size()) { flag = false; //break; } else { flag = true; } if(flag) { bool fflag = true; for (int var = 0; var < (int)resturlparts.size(); var++) { //logger << "resturlparts.at(var) = " << resturlparts.at(var) << endl; if(resturlparts.at(var).find("{")!=string::npos && resturlparts.at(var).find("}")!=string::npos && resturlparts.at(var).length()>2) { string paramname = resturlparts.at(var); string pref, suff; int st = paramname.find("{")+1; pref = paramname.substr(0, st-1); int len = paramname.find("}") - st; suff = paramname.substr(paramname.find("}")+1); paramname = paramname.substr(st, len); string paramvalue = urlparts.at(var); if(st>1) { int stpre = paramvalue.find(pref) + pref.length(); int len = paramvalue.length() - pref.length() - suff.length(); paramvalue = paramvalue.substr(stpre, len); } mapOfValues[paramname] = paramvalue; //logger << "mapOfValues(" << paramname << ") = "<< paramvalue << endl; logger << ("Restcontroller matched url : " + pthwofiletemp + ",param size: " + CastUtil::lexical_cast<string>(prsiz) + ", against url: " + baseUrl) << endl; } else if(urlparts.at(var)!=resturlparts.at(var)) { fflag = false; break; } } flag = fflag; } string lhs = StringUtil::toUpperCopy(ft.meth); string rhs = StringUtil::toUpperCopy(req->getMethod()); //if(prsiz==(int)valss.size() && lhs==rhs) if(flag && lhs==rhs) { logger << "Encountered rest controller url/method match" << endl; rft = ft; flag = true; break; } else if(flag) { res.setHTTPResponseStatus(HTTPResponseStatus::InvalidMethod); return true; } else { res.setHTTPResponseStatus(HTTPResponseStatus::NotFound); //res.addHeaderValue(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_PLAIN); /*if(prsiz==valss.size()) res.setContent("Invalid number of arguments"); else res.setContent("Invalid HTTPMethod used");*/ //logger << "Rest Controller Param/Method Error" << endl; } } } if(flag) { //logger << "inside restcontroller logic ..." << endl; Reflector ref; ClassInfo srv = ref.getClassInfo(rft.clas, req->getCntxt_name()); void *_temp = configData.ffeadContext->getBean("restcontroller_"+req->getCntxt_name()+rft.clas, req->getCntxt_name()); RestController* rstcnt = (RestController*)_temp; if(rstcnt==NULL) { logger << "Invalid Rest Controller" << endl; res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError); return true; } rstcnt->request = req; rstcnt->response = &res; args argus; vals valus; bool invValue = false; vector<ifstream*> allStreams; map<string, vector<ifstream*>* > mpvecstreams; for (int var = 0; var < prsiz; var++) { try { string icont = rft.icontentType; string ocont = rft.ocontentType; if(icont=="") icont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON; else if(icont!=req->getHeader(HttpRequest::ContentType) && req->getHeader(HttpRequest::ContentType).find(icont)!=0) { res.setHTTPResponseStatus(HTTPResponseStatus::UnsupportedMedia); return true; } if(ocont=="") ocont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON; req->addHeaderValue(HttpRequest::ContentType, icont); res.addHeaderValue(HttpResponse::ContentType, ocont); string pmvalue; if(rft.params.at(var).from=="path") pmvalue = mapOfValues[rft.params.at(var).name]; else if(rft.params.at(var).from=="reqparam") pmvalue = req->getQueryParam(rft.params.at(var).name); else if(rft.params.at(var).from=="postparam") pmvalue = req->getRequestParam(rft.params.at(var).name); else if(rft.params.at(var).from=="header") pmvalue = req->getHeader(rft.params.at(var).name); else if(rft.params.at(var).from=="multipart-content") { MultipartContent mcont = req->getMultipartContent(rft.params.at(var).name); if(mcont.isValid()) { if(mcont.isAFile()) { if(rft.params.at(var).type=="filestream") { pmvalue = rft.params.at(var).name; } else if(rft.params.at(var).type!="vector-of-filestream") { logger << "File can only be mapped to ifstream" << endl; res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError); return true; } } else { pmvalue = mcont.getContent(); } } else if(rft.params.at(var).type!="vector-of-filestream") { logger << "Invalid mapping specified in config, no multipart content found with name " + rft.params.at(var).name << endl; res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError); return true; } } else { if(prsiz>1) { logger << "Request Body cannot be mapped to more than one argument..." << endl; res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest); return true; } pmvalue = req->getContent(); } logger << ("Restcontroller parameter type/value = " + rft.params.at(var).type + "/" + pmvalue) << endl; logger << ("Restcontroller content types input/output = " + icont + "/" + ocont) << endl; if(rft.params.at(var).type=="int") { argus.push_back(rft.params.at(var).type); int* ival = new int(CastUtil::lexical_cast<int>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="short") { argus.push_back(rft.params.at(var).type); short* ival = new short(CastUtil::lexical_cast<short>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="long") { argus.push_back(rft.params.at(var).type); long* ival = new long(CastUtil::lexical_cast<long>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="double") { argus.push_back(rft.params.at(var).type); double* ival = new double(CastUtil::lexical_cast<double>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="float") { argus.push_back(rft.params.at(var).type); float* ival = new float(CastUtil::lexical_cast<float>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="bool") { argus.push_back(rft.params.at(var).type); bool* ival = new bool(CastUtil::lexical_cast<bool>(pmvalue)); valus.push_back(ival); } else if(rft.params.at(var).type=="string" || rft.params.at(var).type=="std::string") { argus.push_back(rft.params.at(var).type); string* sval = new string(pmvalue); valus.push_back(sval); } else if(rft.params.at(var).type=="filestream") { argus.push_back("ifstream*"); MultipartContent mcont = req->getMultipartContent(pmvalue); if(mcont.isValid() && mcont.isAFile()) { ifstream* ifs = new ifstream; ifs->open(mcont.getTempFileName().c_str()); valus.push_back(ifs); allStreams.push_back(ifs); } } else if(rft.params.at(var).type=="vector-of-filestream") { vector<ifstream*> *vifs = NULL; if(mpvecstreams.find(rft.params.at(var).name)==mpvecstreams.end()) { argus.push_back("vector<ifstream*>"); vifs = new vector<ifstream*>; vector<MultipartContent> mcontvec = req->getMultiPartFileList(rft.params.at(var).name); for(int mci=0;mci<(int)mcontvec.size();mci++) { MultipartContent mcont = mcontvec.at(mci); if(mcont.isValid() && mcont.isAFile()) { ifstream* ifs = new ifstream; ifs->open(mcont.getTempFileName().c_str()); vifs->push_back(ifs); allStreams.push_back(ifs); } } mpvecstreams[rft.params.at(var).name] = vifs; } else { vifs = mpvecstreams[rft.params.at(var).name]; } valus.push_back(vifs); } else if(rft.params.at(var).type.find("vector-of-")==0 || rft.params.at(var).type.find("list-of-")==0 || rft.params.at(var).type.find("deque-of-")==0 || rft.params.at(var).type.find("set-of-")==0 || rft.params.at(var).type.find("multiset-of-")==0 || rft.params.at(var).type.find("queue-of-")==0) { string stlcnt = rft.params.at(var).type; string stltype; string typp; if(rft.params.at(var).type.find("vector-of-")==0) { StringUtil::replaceFirst(stlcnt,"vector-of-",""); stltype = "std::vector"; typp = "vector<" + stlcnt + ">"; } else if(rft.params.at(var).type.find("list-of-")==0) { StringUtil::replaceFirst(stlcnt,"list-of-",""); stltype = "std::list"; typp = "list<" + stlcnt + ">"; } else if(rft.params.at(var).type.find("deque-of-")==0) { StringUtil::replaceFirst(stlcnt,"deque-of-",""); stltype = "std::deque"; typp = "deque<" + stlcnt + ">"; } else if(rft.params.at(var).type.find("set-of-")==0) { StringUtil::replaceFirst(stlcnt,"set-of-",""); stltype = "std::set"; typp = "set<" + stlcnt + ">"; } else if(rft.params.at(var).type.find("multiset-of-")==0) { StringUtil::replaceFirst(stlcnt,"multiset-of-",""); stltype = "std::multiset"; typp = "multiset<" + stlcnt + ">"; } else if(rft.params.at(var).type.find("queue-of-")==0) { StringUtil::replaceFirst(stlcnt,"queue-of-",""); stltype = "std::queue"; typp = "queue<" + stlcnt + ">"; } StringUtil::replaceFirst(stlcnt," ",""); logger << ("Restcontroller param body holds "+stltype+" of type " + stlcnt) << endl; argus.push_back(typp); void* voidPvect = NULL; if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON) { voidPvect = JSONSerialize::unSerializeUnknown(pmvalue, stltype+"<"+stlcnt+">",req->getCntxt_name()); } #ifdef INC_XMLSER else { voidPvect = XMLSerialize::unSerializeUnknown(pmvalue, stltype+"<"+stlcnt+",",req->getCntxt_name()); } #endif if(voidPvect==NULL) { res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest); return true; } valus.push_back(voidPvect); } else { argus.push_back(rft.params.at(var).type); void* voidPvect = NULL; if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON) { voidPvect = JSONSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type,req->getCntxt_name()); } #ifdef INC_XMLSER else { voidPvect = XMLSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type,req->getCntxt_name()); } #endif if(voidPvect==NULL) { res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest); return true; } valus.push_back(voidPvect); } } catch (const char* ex) { logger << "Restcontroller exception occurred" << endl; logger << ex << endl; invValue= true; res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest); return true; } catch (...) { logger << "Restcontroller exception occurred" << endl; invValue= true; res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest); return true; } } Method meth = srv.getMethod(rft.name, argus); if(meth.getMethodName()!="" && !invValue) { ref.invokeMethodUnknownReturn(_temp,meth,valus); logger << "Successfully called restcontroller" << endl; //return; for(int i=0;i<(int)allStreams.size();++i) { if(allStreams.at(i)!=NULL) { if(allStreams.at(i)->is_open()) { allStreams.at(i)->close(); allStreams.at(i)->clear(); } delete allStreams.at(i); } } map<string, vector<ifstream*>* >::iterator it; for(it=mpvecstreams.begin();it!=mpvecstreams.end();++it) { delete it->second; } } else { res.setHTTPResponseStatus(HTTPResponseStatus::NotFound); //res.addHeaderValue(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_PLAIN); logger << "Rest Controller Method Not Found" << endl; //return; } } } return isContrl; }
static ngx_int_t ngx_http_ffeadcpp_module_handler_post_read(ngx_http_request_t *r) { string cntpath = ""; cntpath.append(ffeadcpp_path.data, ffeadcpp_path.len); cntpath += "/web/"; HttpRequest* req = new HttpRequest(cntpath); //cerr << "FFEAD in ngx_http_ffeadcpp_module_handler " << cntpath << r->uri.data << endl; ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; ngx_list_part_t *part; ngx_table_elt_t *h; ngx_uint_t i; /* Get the first part of the list. There is usual only one part. */ part = &r->headers_in.headers.part; h = part->elts; /* Headers list array may consist of more than one part, so loop through all of it */ for (i = 0; /* void */ ; i++) { if (i >= part->nelts) { if (part->next == NULL) { /* The last part, search is done. */ break; } part = part->next; h = part->elts; i = 0; } // cerr << "header -> " << string(h[i].key.data, h[i].key.len) << " = " << string(h[i].value.data, h[i].value.len) << endl; req->buildRequest(string(h[i].key.data, h[i].key.len), string(h[i].value.data, h[i].value.len)); } string content; ngx_http_read_input_data(r, content); //cerr << "Input Request Data\n " << content << "\n======================\n" << endl; //cerr << "URL -> " << string(r->uri.data,r->uri.len) << endl; //cerr << "Method -> " << string(r->main->method_name.data, r->main->method_name.len) << endl; if(content!="") { req->buildRequest("Content", content.c_str()); } req->buildRequest("URL", string(r->uri.data,r->uri.len)); req->buildRequest("Method", string(r->main->method_name.data, r->main->method_name.len)); if(r->args.len > 0) { req->buildRequest("GetArguments", string(r->args.data,r->args.len)); } req->buildRequest("HttpVersion", CastUtil::lexical_cast<string>(r->http_version)); HttpResponse* respo = new HttpResponse; ServiceTask* task = new ServiceTask; task->handle(req, respo); delete task; cerr << req->toString() << endl; if(respo->isDone()) { for (int var = 0; var < (int)respo->getCookies().size(); var++) { set_custom_header_in_headers_out(r, "Set-Cookie", respo->getCookies().at(var)); } /* allocate a buffer for your response body */ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } /* attach this buffer to the buffer chain */ out.buf = b; out.next = NULL; string data = respo->generateResponse(false); //cerr << "OUT -> " << data << endl; map<string,string>::const_iterator it; for(it=respo->getHeaders().begin();it!=respo->getHeaders().end();it++) { if(StringUtil::toLowerCopy(it->first)==StringUtil::toLowerCopy(HttpResponse::ContentLength)) { r->headers_out.content_length_n = CastUtil::lexical_cast<int>(it->second); } else { set_custom_header_in_headers_out(r, it->first, it->second); } } //cerr << "done writing headers" << endl; /* adjust the pointers of the buffer */ b->pos = data.c_str(); b->last = data.length(); b->memory = 1; /* this buffer is in memory */ b->last_buf = 1; /* this is the last buffer in the buffer chain */ /* set the status line */ r->headers_out.status = CastUtil::lexical_cast<int>(respo->getStatusCode()); delete respo; delete req; /* send the headers of your response */ rc = ngx_http_send_header(r); //cerr << "done sending headers " << rc << " " << NGX_OK <<" "<< r->header_only <<" " << NGX_ERROR << endl; if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } /* send the buffer chain of your response */ return ngx_http_output_filter(r, &out); } else { u_char *last, *location; size_t len; ngx_str_t path; ngx_uint_t level; ngx_log_t *log; ngx_open_file_info_t of; ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); log = r->connection->log; ngx_memzero(&of, sizeof(ngx_open_file_info_t)); of.read_ahead = clcf->read_ahead; of.directio = clcf->directio; of.valid = clcf->open_file_cache_valid; of.min_uses = clcf->open_file_cache_min_uses; of.errors = clcf->open_file_cache_errors; of.events = clcf->open_file_cache_events; path.data = req->getUrl().c_str(); path.len = ngx_strlen(path.data); delete respo; delete req; rc = NGX_OK; if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } cerr << string(path.data,path.len) << endl; if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) != NGX_OK) { switch (of.err) { case 0: rc = NGX_HTTP_INTERNAL_SERVER_ERROR; break; case NGX_ENOENT: case NGX_ENOTDIR: case NGX_ENAMETOOLONG: level = NGX_LOG_ERR; rc = NGX_HTTP_NOT_FOUND; break; case NGX_EACCES: #if (NGX_HAVE_OPENAT) case NGX_EMLINK: case NGX_ELOOP: #endif level = NGX_LOG_ERR; rc = NGX_HTTP_FORBIDDEN; break; default: level = NGX_LOG_CRIT; rc = NGX_HTTP_INTERNAL_SERVER_ERROR; break; } if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { ngx_log_error(level, log, of.err, "%s \"%s\" faileddddddddddddddd", of.failed, path.data); } //return rc; } r->root_tested = !r->error_page; cerr << "found file" << endl; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd); /*if (of.is_dir) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir"); ngx_http_clear_location(r); r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); if (r->headers_out.location == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } len = r->uri.len + 1; if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) { location = path.data + clcf->root.len; *last = '/'; } else { if (r->args.len) { len += r->args.len + 1; } location = ngx_pnalloc(r->pool, len); if (location == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } last = ngx_copy(location, r->uri.data, r->uri.len); *last = '/'; if (r->args.len) { *++last = '?'; ngx_memcpy(++last, r->args.data, r->args.len); } } */ /* * we do not need to set the r->headers_out.location->hash and * r->headers_out.location->key fields */ /*r->headers_out.location->value.len = len; r->headers_out.location->value.data = location; return NGX_HTTP_MOVED_PERMANENTLY; }*/ #if !(NGX_WIN32) /* the not regular files are probably Unix specific */ if (!of.is_file) { ngx_log_error(NGX_LOG_CRIT, log, 0, "\"%s\" is not a regular file", path.data); rc = NGX_HTTP_NOT_FOUND; } #endif /* if (r->method == NGX_HTTP_POST) { return NGX_HTTP_NOT_ALLOWED; }*/ if (rc != NGX_OK) { r->headers_out.status = rc; ngx_http_send_header(r); return ngx_http_send_special (r, NGX_HTTP_LAST); } /*rc = ngx_http_discard_request_body(r); if (rc != NGX_OK) { return rc; }*/ log->action = "sending response to client"; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = of.size; r->headers_out.last_modified_time = of.mtime; /*if (ngx_http_set_etag(r) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; }*/ if (ngx_http_set_content_type(r) != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (r != r->main && of.size == 0) { return ngx_http_send_header(r); } r->allow_ranges = 1; /* we need to allocate all before the header would be sent */ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); if (b->file == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } b->file_pos = 0; b->file_last = of.size; b->in_file = b->file_last ? 1: 0; b->last_buf = (r == r->main) ? 1: 0; b->last_in_chain = 1; b->file->fd = of.fd; b->file->name = path; b->file->log = log; b->file->directio = of.is_directio; out.buf = b; out.next = NULL; //return ngx_http_output_filter(r, &out); ngx_http_finalize_request(r, ngx_http_output_filter(r, &out));return NGX_DONE; /*cerr << req->getUrl() << " " << cntpath << endl; cntpath = cntpath.substr(0, cntpath.length()-1); string rurl = req->getUrl(); StringUtil::replaceAll(cntpath, "//", "/"); StringUtil::replaceFirst(rurl, cntpath, ""); cerr << req->getUrl() << " " << cntpath << " " << rurl << endl; ngx_str_t nuri; nuri.data = rurl.c_str(); nuri.len = ngx_strlen(nuri.data); return ngx_http_internal_redirect(r, &nuri, NULL);*/ } }
void HttpClientApp::onRead( ci::BufferRef buffer ) { size_t sz = buffer->getSize(); mBytesRead += sz; mText.push_back( toString( sz ) + " bytes read" ); if ( !mHttpResponse.hasHeader() ) { // Parse header mHttpResponse.parseHeader( HttpResponse::bufferToString( buffer ) ); buffer = HttpResponse::removeHeader( buffer ); // Get content-length for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { if ( kvp.first == "Content-Length" ) { mContentLength = fromString<size_t>( kvp.second ); break; } } } // Append buffer to body mHttpResponse.append( buffer ); if ( mBytesRead < mContentLength ) { // Keep reading until we hit the content length mSession->read(); } else { mText.push_back( "Read complete" ); mText.push_back( toString( mHttpResponse.getStatusCode() ) + " " + mHttpResponse.getReason() ); if ( mHttpResponse.getStatusCode() == 200 ) { for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { // Choose file extension based on MIME type if ( kvp.first == "Content-Type" ) { string mime = kvp.second; if ( mime == "audio/mp3" ) { mFilename += ".mp3"; } else if ( mime == "image/jpeg" ) { mFilename += ".jpg"; } else if ( mime == "image/png" ) { mFilename += ".png"; } } else if ( kvp.first == "Connection" ) { // Close connection if requested by server if ( kvp.second == "close" ) { mSession->close(); } } } // Save the file fs::path path = getAppPath(); #if !defined ( CINDER_MSW ) path = path.parent_path(); #endif path = path / mFilename; OStreamFileRef file = writeFileStream( path ); file->write( mHttpResponse.getBody() ); mText.push_back( mFilename + " downloaded" ); } else { // Write error mText.push_back( "Response: " + HttpResponse::bufferToString( mHttpResponse.getBody() ) ); mSession->close(); } } }
int main() { #ifdef OS_MINGW // startup WinSock in Windows WSADATA wsa_data; WSAStartup(MAKEWORD(1,1), &wsa_data); #endif PropFileReader propFileReader; propMap props = propFileReader.getProperties("testValues.prop"); CsvFileReader csvFileReader; strVecVec testCases = csvFileReader.getRows("test.csv"); Timer timer, timerc; std::string cookies, result; int total, skipped = 0, passed = 0, failed = 0, header = 0, counter = 0; total = (int)testCases.size(); std::string ip = props["SERVER_IP_ADDRESS"]; if(ip=="") ip = "localhost"; int port = 8080; if(props["SERVER_PORT"]!="") { try{ port = CastUtil::lexical_cast<int>(props["SERVER_PORT"]); } catch(...) { } } bool sslEnabled = false; if(props["SERVER_SSL_ENABLED"]!="") { try{ sslEnabled = CastUtil::lexical_cast<bool>(props["SERVER_SSL_ENABLED"]); } catch(...) { } } std::string sslFile = props["SERVER_SSL_FILE"]; bool isDebug = false; if(StringUtil::toLowerCopy(props["DEBUG"])=="true") { isDebug = true; } std::cout << "Server IP - " << ip <<std::endl; std::cout << "Server Port - " << port <<std::endl; std::cout << "Debug Mode - " << isDebug <<std::endl; std::cout << "Server SSL Enabled - " << CastUtil::lexical_cast<std::string>(sslEnabled) <<std::endl; if(sslEnabled) { std::cout << "Server SSL File - " << sslFile <<std::endl; } timerc.start(); for (int var = 0; var < total; ++var) { ClientInterface* client; if(sslEnabled) { if(sslFile!="") client = new SSLClient(sslFile); else client = new SSLClient; } else client = new Client; if(testCases[var].size()>=4) { if(testCases[var][0]=="ENABLED") { header++; continue; } counter = var; std::string request = testCases[var][2]; if(testCases[var][0]=="N" || testCases[var][0]=="n") { std::cout << "Request " << counter << " " << request << " was Skipped" << std::endl; skipped++; continue; } bool debugCont = false; std::string debugContStr = testCases[var][1]; if(debugContStr=="Y" || debugContStr=="y") debugCont = true; std::string responseCode = testCases[var][3]; std::string file; if(testCases[var].size()>4) file = testCases[var][4]; std::string fileCntlen; if(testCases[var].size()>5) fileCntlen = testCases[var][5]; std::string reqContTyp, content, headers, respCntType; if(testCases[var].size()>6) { reqContTyp = testCases[var][6]; } if(testCases[var].size()>7) { content = testCases[var][7]; } if(testCases[var].size()>8) { headers = testCases[var][8]; if(headers!="" && headers.find("HEADERVALS_")!=std::string::npos) { headers = props[headers]; } else { std::vector<std::string> headerVec; StringUtil::split(headerVec, headers, ";"); headers = ""; for (int var = 0; var < (int)headerVec.size(); ++var) { std::vector<std::string> param; StringUtil::split(param, headerVec.at(var), "="); if(param.size()==2) { headers += param.at(0) + ": " + param.at(1) + "\r\n"; } } } } if(testCases[var].size()>9) { respCntType = testCases[var][9]; } std::string data = request; data += " HTTP/1.1\r\nHost: "+ip+":"+CastUtil::lexical_cast<std::string>(port)+"\r\nUser-Agent: Program\r\n"; if(content!="" && content.find("TSTVALUES_")!=std::string::npos) content = props[content]; if(reqContTyp!="") { data += "Content-Type: " + reqContTyp + "\r\n"; } if(content.length()>0) { data += "Content-Length: " + CastUtil::lexical_cast<std::string>((int)content.length()) + "\r\n"; } if(cookies!="") { data += "Cookie: " + cookies + "\r\n"; } if(headers!="") { data += headers; } data += "\r\n"; if(content!="") { data += content; } timer.start(); if(isDebug) { std::cout << "HTTP Request Is=>\n" << data << "\n\n" << std::endl; } client->connectionUnresolv(ip,port); int bytes = client->sendData(data); std::string tot = client->getTextData("\r\n","content-length"); long long millis = timer.elapsedMilliSeconds(); if(isDebug) { std::cout << "HTTP Response Is=>\n" << tot << "\n\n" << std::endl; } HttpResponse res; HttpResponseParser parser(tot, res); if(res.getHeader("Set-Cookie")!="") { cookies = res.getHeader("Set-Cookie"); cookies = cookies.substr(0, cookies.find(";")); } std::string debugContentValue; if(debugCont) { debugContentValue = ", Content => " + parser.getContent(); } std::string ss; bool passedFlag = false, done = false; if(res.getStatusCode()==responseCode) { if(respCntType!="") { if(res.getHeader("Content-Type")==respCntType) { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " was Successfull, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue; passedFlag = true; } else { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " Failed, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + ", Expected ContentType = " + respCntType + ", Actual ContentType = " + res.getHeader("Content-Type"); passedFlag = false; } done = true; } if(!done) { std::string cntlen = res.getHeader("Content-Length"); if(file!="") { std::ifstream myfile (&file[0], std::ios::binary | std::ios::ate); if (myfile.is_open() && cntlen!="" && myfile.tellg()==CastUtil::lexical_cast<int>(cntlen)) { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " was Successfull, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue; passedFlag = true; } else { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + ", Invalid Content Length, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue; passedFlag = false; } } else if((file=="" && fileCntlen=="") || (fileCntlen!="" && fileCntlen==cntlen)) { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " was Successfull, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue; passedFlag = true; } else { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + ", Invalid Content Length, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue; passedFlag = false; } } } else { ss.clear(); ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " Failed, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + ", Expected Status = " + responseCode + ", Actual Status = " + res.getStatusCode(); passedFlag = false; } std::cout << ss << std::endl; if(passedFlag) passed++; else failed++; } else { skipped++; } if(client!=NULL) { client->closeConnection(); delete client; } } std::cout << "Total Tests = " << total-1 << ", Passed = " << passed << ", Failed = " << failed << ", Skipped = " << skipped << ", Time taken = " << timerc.elapsedMilliSeconds() << "ms" << std::endl; #ifdef OS_MINGW WSACleanup(); #endif return 0; }