diff --git a/CMakeLists.txt b/CMakeLists.txt index ac65825..a4f5624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,4 +7,4 @@ add_compile_options(-DBOOST_LOG_DYN_LINK) add_subdirectory(MINWebServer) add_executable(demo test_minweb_server_library.cpp) -target_link_libraries(demo ndn-cxx boost_system pthread boost_thread boost_log_setup boost_log MINWebServer jsoncpp) +target_link_libraries(demo ndn-cxx boost_system pthread boost_thread boost_log_setup boost_log MINWebServer jsoncpp) \ No newline at end of file diff --git a/MINWebServer/CMakeLists.txt b/MINWebServer/CMakeLists.txt index f904333..9caec33 100644 --- a/MINWebServer/CMakeLists.txt +++ b/MINWebServer/CMakeLists.txt @@ -18,8 +18,7 @@ set(CURRENT_SOURCES http/MINHttpServer.cpp http/MINHttpServerImpl.cpp http/MINHttpSession.cpp - http/MINWebRequest.cpp - http/MINWebResponse.cpp + http/MINHttpSession.h server/FileCache.cpp base/MBase.cpp ) diff --git a/MINWebServer/http/MINHttpServer.h b/MINWebServer/http/MINHttpServer.h index 4e618ac..bdce722 100644 --- a/MINWebServer/http/MINHttpServer.h +++ b/MINWebServer/http/MINHttpServer.h @@ -24,6 +24,12 @@ namespace MIN { */ virtual void replyResponse(std::shared_ptr response) = 0; + /** + * 返回response的body长度 + * @param response + */ + //virtual void returnResponseLength(std::shared_ptr response) = 0; + /** * 启动MINHttpServer服务 * @param wait 是否阻塞运行 diff --git a/MINWebServer/http/MINHttpSession.cpp b/MINWebServer/http/MINHttpSession.cpp index 4a8fc2b..72398d8 100644 --- a/MINWebServer/http/MINHttpSession.cpp +++ b/MINWebServer/http/MINHttpSession.cpp @@ -19,316 +19,608 @@ using namespace std ; #include #include #include -#include "MINWebRequest.h" -#include "MINHttpServerImpl.h" +#include +#include + +#include "MINHttpSession.h" #include using namespace ndn ; using namespace std ; -class Sessionpro + + + +void Sessionpro::run(){ + + cout << "Service build listen start " << endl; + Name namePrefix="/MIN/Nginx/test2"; + + m_face.registerPrefix(namePrefix, + bind(&Sessionpro::onSuccess, this, _1), + RegisterPrefixFailureCallback()); + + m_face.processEvents(); +} + +vector Sessionpro::split(const string& s, vector& tokens, char delim = '-') { + tokens.clear(); + auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t { + for (size_t i = pos; i < s.size(); i++) { + if (s[i] != delim) return i; + } + return string::npos; + }; + size_t lastPos = string_find_first_not(0); + size_t pos = s.find(delim, lastPos); + while (lastPos != string::npos) { + tokens.emplace_back(s.substr(lastPos, pos - lastPos)); + lastPos = string_find_first_not(pos); + pos = s.find(delim, lastPos); + } + return tokens; +} +string readFileIntoString(string filename) { -public: - - - void run(){ - cout << "Service build listen start " << endl; - Name namePrefix="/MIN/Nginx/test2"; - m_face.registerPrefix(namePrefix, - bind(&Sessionpro::onSuccess, this, _1), - RegisterPrefixFailureCallback()); - m_face.processEvents(); - } - -private: - - vector split(const string& s, vector& tokens, char delim = '-') { - tokens.clear(); - auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t { - for (size_t i = pos; i < s.size(); i++) { - if (s[i] != delim) return i; - } - return string::npos; - }; - size_t lastPos = string_find_first_not(0); - size_t pos = s.find(delim, lastPos); - while (lastPos != string::npos) { - tokens.emplace_back(s.substr(lastPos, pos - lastPos)); - lastPos = string_find_first_not(pos); - pos = s.find(delim, lastPos); + ifstream ifile(filename); + //将文件读入到ostringstream对象buf中 + ostringstream buf; + char ch; + while(buf&&ifile.get(ch)) + buf.put(ch); + //返回与流对象buf关联的字符串 + return buf.str(); +} +shared_ptr getSignatureInfo(const Interest &interest) { + auto &name = interest.getName(); + if (name.size() > ndn::signed_interest::MIN_SIZE) { + try { + return make_shared(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue()); + } catch (...) { + return nullptr; } - return tokens; } + return nullptr; +} - void onSuccess(const Name& name){ +void Sessionpro::onSuccess(const Name& name){ - m_face.setInterestFilter("/MIN/Nginx/test2/index.html/MIN_SERVICE_DETECT", - bind(&Sessionpro::onInterestDetect, this, _1, _2)); + //TODO: index.html - m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION", - bind(&Sessionpro::onInterestBuild, this, _1, _2)); + cout << " onsucess " << endl; - m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION/sessionId", - bind(&Sessionpro::onInterest, this, _1, _2)); - } - - void onInterestBuild(const InterestFilter& filter, const Interest& interest) - { - if(interest.getName()[-3].toUri()=="MIN_HTTP_SESSION") { - std::cout << ">> Session I: " << interest << std::endl; - uu = boost::uuids::random_generator()(); - uuid_string = boost::uuids::to_string(uu); - std::cout << uuid_string << std::endl; - sessionIdMap[uuid_string]=true; - if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx - string intParaStr((char *) - interest.getApplicationParameters().value(), - interest.getApplicationParameters().value_size()); - } - - Name dataName(interest.getName()); - Json::Reader reader; - Json::Value root1; - - root1["sessionId"] = uuid_string; - cout << root1["sessionId"].asString() << endl; - cout << root1.toStyledString() << endl; - - std::string content = root1.toStyledString(); - - shared_ptr data = make_shared(); - data->setName(dataName); - //data->setFreshnessPeriod(0_s); // 10 seconds - lp::CachePolicy noCachePolicy; - noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); - data->setTag(make_shared(noCachePolicy)); - // 数据缓存在节点中,立即变旧 - - data->setContent(reinterpret_cast(content.data()), content.size()); - - m_keyChain.sign(*data); - std::cout << "<< D: " << *data << std::endl; - m_face.put(*data); + m_face.setInterestFilter("/MIN/Nginx/test2", [this](const InterestFilter& filter, const Interest& interest) { + auto name = interest.getName(); + auto signatureInfoPtr = getSignatureInfo(interest); + int commandIndex = -1; + if (signatureInfoPtr) { + commandIndex -= 2; } - else if(sessionIdMap.find(interest.getName()[4].toUri())->second){//interest.getName()[4].toUri()==uuid_string&&(!uuid_string.empty() - /* - * assume GR request packet only once - */ - if(interest.getName()[-3].toUri().substr(0,2)=="GR"){ - //TODO:loopnum requestid duoci - std::cout << ">> request I: " << interest << std::endl; - string requestId="webtest"; - loopNum=0; - vector want; - //cout << "parse name: " << split(interest.getName()[-3].toUri(),want)[1] <onInterestDetect(filter, interest); + } else { + this->onInterestBuild(filter, interest); + } + }); + m_face.setInterestFilter("/MIN/Nginx/test2/index.html/MIN_SERVICE_DETECT", + bind(&Sessionpro::onInterestDetect, this, _1, _2)); - Name dataName(interest.getName()); - Json::Reader reader; - Json::Value root1; + m_face.setInterestFilter("/MIN/Nginx/test2/getUserInfo/MIN_SERVICE_DETECT", + bind(&Sessionpro::onInterestUserDetect, this, _1, _2)); - root1["requestId"] = finalId; - //seqMap[requestMap[split(interest.getName()[-3].toUri(),want)[1]]]=atoi(split(interest.getName()[-3].toUri(),want)[2].c_str()); - cout << root1["requestId"].asString() << endl; - cout << root1.toStyledString() << endl; + m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION", + bind(&Sessionpro::onInterestBuild, this, _1, _2)); +} - std::string content = root1.toStyledString(); +void Sessionpro::onInterestBuild(const InterestFilter& filter, const Interest& interest) +{ + cout << "fuck " << endl; + if(interest.getName()[-3].toUri()=="MIN_HTTP_SESSION" && interest.getName()[4].toUri()!="MIN_HTTP_RESPONSE") { + std::cout << ">> Session I: " << interest << std::endl; + uu = boost::uuids::random_generator()(); + uuid_string = boost::uuids::to_string(uu); + std::cout << uuid_string << std::endl; + sessionIdMap[uuid_string]=true; + if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx + string intParaStr((char *) + interest.getApplicationParameters().value(), + interest.getApplicationParameters().value_size()); + } - shared_ptr data = make_shared(); - data->setName(dataName); - data->setFreshnessPeriod(0_s); // 10 seconds - lp::CachePolicy noCachePolicy; - noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); - data->setTag(make_shared(noCachePolicy)); - // 数据缓存在节点中,立即变旧 + Name dataName(interest.getName()); + Json::Reader reader; + Json::Value root1; - data->setContent(reinterpret_cast(content.data()), content.size()); + root1["sessionId"] = uuid_string; + cout << root1["sessionId"].asString() << endl; + cout << root1.toStyledString() << endl; - m_keyChain.sign(*data); - std::cout << "<< D: " << *data << std::endl; - m_face.put(*data); - cout << requestMap[finalId] << endl; + std::string content = root1.toStyledString(); + + shared_ptr data = make_shared(); + data->setName(dataName); + //data->setFreshnessPeriod(0_s); // 10 seconds + lp::CachePolicy noCachePolicy; + noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); + data->setTag(make_shared(noCachePolicy)); + // 数据缓存在节点中,立即变旧 + + data->setContent(reinterpret_cast(content.data()), content.size()); + + m_keyChain.sign(*data); + std::cout << "<< D: " << *data << std::endl; + m_face.put(*data); + } + else if(sessionIdMap.find(interest.getName()[4].toUri())->second){//interest.getName()[4].toUri()==uuid_string&&(!uuid_string.empty() + /* + * assume GR request packet only once + */ + if(interest.getName()[-3].toUri().substr(0,2)=="GR"){ + //TODO:loopnum requestid duoci + std::cout << ">> request I: " << interest << std::endl; + string requestId="webtest"; + requestIdSeq++; + loopNum=0; + vector want; + //cout << "parse name: " << split(interest.getName()[-3].toUri(),want)[1] <>==========================" << endl; - vector ackSeq; - split(interest.getName()[-3].toUri(),ackSeq); - cout << ackSeq[1] << endl; - //makeAck makeack; - //TODO:sessionidmap judge + Name dataName(interest.getName()); + Json::Reader reader; + Json::Value root1; - string storeInterest((char *) - interest.getApplicationParameters().value(), - interest.getApplicationParameters().value_size()); - localMap[stoi(ackSeq[1])]=storeInterest; - storeRequestIdSeq[interest.getName()[-4].toUri()]=localMap; - Json::Reader reader ; - Json::Value root1 ; - string ack="ACK-"; - root1["ACK"]=ack.append(ackSeq[1]); - cout << root1["ACK"].asString() << endl ; - cout << root1.toStyledString() << endl ; + root1["requestId"] = finalId; + //seqMap[requestMap[split(interest.getName()[-3].toUri(),want)[1]]]=atoi(split(interest.getName()[-3].toUri(),want)[2].c_str()); + cout << root1["requestId"].asString() << endl; + cout << root1.toStyledString() << endl; std::string content = root1.toStyledString(); + shared_ptr data = make_shared(); - //data->setName(dataName.append(root1["ACK"].asString())); - data->setName(interest.getName()); + data->setName(dataName); data->setFreshnessPeriod(0_s); // 10 seconds lp::CachePolicy noCachePolicy; noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); data->setTag(make_shared(noCachePolicy)); // 数据缓存在节点中,立即变旧 - data->setContent(reinterpret_cast(content.data()), content.size()); + data->setContent(reinterpret_cast(content.data()), content.size()); m_keyChain.sign(*data); - std::cout << ">> D: " << *data << std::endl; + std::cout << "<< D: " << *data << std::endl; m_face.put(*data); - cout << "output of seq content" << localMap[stoi(ackSeq[1])] << endl; - if(storeRequestIdSeq[interest.getName()[-4].toUri()].size() == requestMap[interest.getName()[-4].toUri()]){ - //TODO:feed to minhttp parser - parseStr=""; - auto it = storeRequestIdSeq[interest.getName()[-4].toUri()].begin(); - while(it != storeRequestIdSeq[interest.getName()[-4].toUri()].end()) { - cout<second<second); - it++; - } - MIN::MINWebRequest minWebRequest; - MIN::MINWebMessage minWebMessage; - minWebRequest.ybParse(minWebMessage,minWebRequest,parseStr,interest); - //MIN::MINWebServer::OnRequestReceiveListener(minWebRequest); - } + cout << requestMap[finalId] << endl; + } + else{ + cerr << "GR request duplicate" << endl; } } - } + else if(interest.getName()[-3].toUri().substr(0,3)=="SEQ" && requestMap.count(interest.getName()[-4].toUri())){ + cout << "SEQ I>>==========================" << endl; + vector ackSeq; + split(interest.getName()[-3].toUri(),ackSeq); + cout << ackSeq[1] << endl; - void onInterestDetect(const InterestFilter& filter, const Interest& interest) - { - std::cout << "<< I: " << interest << std::endl; - if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx - string intParaStr((char *) - interest.getApplicationParameters().value(), - interest.getApplicationParameters().value_size()); + //makeAck makeack; + //TODO:sessionidmap judge + + string storeInterest((char *) + interest.getApplicationParameters().value(), + interest.getApplicationParameters().value_size()); + localMap[stoi(ackSeq[1])]=storeInterest; + storeRequestIdSeq[interest.getName()[-4].toUri()]=localMap; + Json::Reader reader ; + Json::Value root1 ; + string ack="ACK-"; + root1["ACK"]=ack.append(ackSeq[1]); + cout << root1["ACK"].asString() << endl ; + cout << root1.toStyledString() << endl ; + + std::string content = root1.toStyledString(); + shared_ptr data = make_shared(); + //data->setName(dataName.append(root1["ACK"].asString())); + data->setName(interest.getName()); + data->setFreshnessPeriod(0_s); // 10 seconds + lp::CachePolicy noCachePolicy; + noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); + data->setTag(make_shared(noCachePolicy)); + // 数据缓存在节点中,立即变旧 + + data->setContent(reinterpret_cast(content.data()), content.size()); + + m_keyChain.sign(*data); + std::cout << "<< D: " << *data << std::endl; + m_face.put(*data); + cout << "output of seq content" << localMap[stoi(ackSeq[1])] << endl; + cout << "output of seq content" << storeRequestIdSeq[interest.getName()[-4].toUri()].size() << endl; + cout << "output of seq content" << requestMap[interest.getName()[-4].toUri()] << endl; + if(storeRequestIdSeq[interest.getName()[-4].toUri()].size() == requestMap[interest.getName()[-4].toUri()]){ + //TODO:feed to minhttp parser + parseStr=""; + auto it = storeRequestIdSeq[interest.getName()[-4].toUri()].begin(); + while(it != storeRequestIdSeq[interest.getName()[-4].toUri()].end()) { + cout<second<second); + it++; + } + minWebRequest->ybParse(minWebMessage,minWebRequest,parseStr,interest); + //MIN::MINWebServer::OnRequestReceiveListener(minWebRequest); + pair localPair(interest.getName()[-5].toUri(),interest.getName()[-4].toUri()); + //localPair[interest.getName()[4].toUri()]=interest.getName()[5].toUri(); + requestPacketId[localPair]=minWebRequest; + responsePacketState[requestPacketId[localPair]]=true; + //this->listener(minWebRequest); + + } } - - Name dataName(interest.getName()); - - Json::Reader reader ; - Json::Value root ; - - string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ; - reader.parse(str,root) ; - cout << root["serverPrefix"].asString() << endl ; - cout << root.toStyledString() << endl ; - - static const std::string content = root.toStyledString(); - - shared_ptr data = make_shared(); - data->setName(dataName); - data->setFreshnessPeriod(0_s); // 10 seconds - // 数据缓存在节点中,立即变旧 - - data->setContent(reinterpret_cast(content.data()), content.size()); - - - m_keyChain.sign(*data); - std::cout << ">> D: " << *data << std::endl; - m_face.put(*data); + //TODO: /MIN/Nginx/test2/MIN_HTTP_SESSION/abcdxxx/RES-ccc } - - void onInterest(const InterestFilter& filter, const Interest& interest) - { - std::cout << "<< I: " << interest << std::endl; -// uuid_t uu; - boost::uuids::uuid uu = boost::uuids::random_generator()(); - string uuid_string = boost::uuids::to_string(uu); - std::cout << uu << std::endl; - - if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx - string intParaStr((char *) - interest.getApplicationParameters().value(), - interest.getApplicationParameters().value_size()); + else if(interest.getName()[4].toUri()=="MIN_HTTP_RESPONSE"){ + minWebRequest->context["feature"]=interest.getName()[-3].toUri(); + minWebRequest->context["prefix"]=interest.getName().toUri(); + if(interest.getName()[-3].toUri()=="pull"){ + minWebRequest->context["number"]=stoi(interest.getName()[-4].toUri()); } + this->listener(minWebRequest); - Name dataName(interest.getName()); + // Get Response from blocking queue - Json::Reader reader ; - Json::Value root1 ; - - - root1["sessionId"]=uuid_string; - cout << root1["sessionId"].asString() << endl ; - cout << root1.toStyledString() << endl ; - - static const std::string content = root1.toStyledString(); - - shared_ptr data = make_shared(); - data->setName(dataName); - data->setFreshnessPeriod(0_s); // 10 seconds - // 数据缓存在节点中,立即变旧 - - data->setContent(reinterpret_cast(content.data()), content.size()); - - - m_keyChain.sign(*data); - std::cout << ">> D: " << *data << std::endl; - m_face.put(*data); +// shared_ptr response=make_shared(); +// int length=0; +// std::string responseStr; +// std::vector responseSeq; +// ///MIN/Nginx/test2/MIN_HTTP_SESSION/MIN_HTTP_RESPONSE/sessionId/RES-requstId/ask +// //response chunk data +// if(interest.getName()[-3].toUri()=="ask"){ +// minWebRequest->context["feature"]=interest.getName()[-3].toUri(); +// minWebRequest->context["prefix"]=interest.getName().toUri(); +// vector responseRequestId; +// split(interest.getName()[6].toUri(),responseRequestId); +// pair localPair2(interest.getName()[5].toUri(),responseRequestId[1]); +// cout << "responsestate" << responsePacketState[requestPacketId[localPair2]] << endl; +// //cout << responseRequestId[0] << responseRequestId[1] << endl; +// if(responsePacketState[requestPacketId[localPair2]]) { +// // TODO 生成一个回复 +// Json::Value rootChunk ; +// response->status = MIN::MINWebServer::MIN_HTTP_STATUS_OK; +// response->contentType = MIN::MINWebServer::TEXT_HTML; +// response->contentLength = 15; +// response->body = R"({\"name\":\"wgh\", \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100 +//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100 +//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100 +//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100 +//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100 +//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100})"; +// // TODO deal response +//// ifstream infile; +//// string buffstr; +//// infile.open("/home/yb/yb/index/index.html", ios::in); +////// read line and line +//// while(getline(infile, buffstr)) +//// { +//// cout << buffstr << endl;// red line string +//// break; +//// } +//// infile.close(); +//// if(minWebRequest->path=="/index.html") +//// response->body="fuckbad"; +//// else +//// response->body="fuckbad"; +// cout << "fuck bad " << endl; +// responseStr.append("MIN-HTTP/1.0").append(" ").append("200").append(" ").append( +// MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code; +// responseStr.append("\r\n"); +// responseStr.append("Content-Type").append(" ").append( +// MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType)); +// responseStr.append("\r\n"); +// responseStr.append("Content-Length").append(" ").append("15"); +// responseStr.append("\r\n\r\n"); +// responseStr.append(response->body); +// storeResponseStr[localPair2]=responseStr; +// cout << " fuck bad bda " << endl; +// length=responseStr.length(); +// if(length<=7000){ +//// responseSeq[0]=storeResponseStr[localPair3]; +// storeResponseSeq[localPair2].push_back(responseStr); +// cout << "fuck test" << endl; +// } +// else { +// size_t sendSeq = responseStr.length() / 7000; +// for (int i = 0; i <= sendSeq; i++) { +// if (i == 0) +// storeResponseSeq[localPair2].push_back(responseStr.substr(0, 6996)); +// else if (i==sendSeq-1) +// storeResponseSeq[localPair2].push_back(responseStr.substr((7000 * i) , responseStr.length())); +// else +// storeResponseSeq[localPair2].push_back(responseStr.substr((7000 * i) + 1, 7000*(i+1))); +// } +// } +// cout << " test split" << storeResponseSeq[localPair2][1] << endl; +// /* +// * SEND RESPONSE TO BROWSER CLIENT +// */ +// Name dataName(interest.getName()); //TODO: need requestId +// shared_ptr resData = make_shared(); +// resData->setName(dataName); +// resData->setFreshnessPeriod(0_s); // 10 seconds +// lp::CachePolicy noCachePolicy; +// noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); +// resData->setTag(make_shared(noCachePolicy)); +// string urlStr="/MIN_HTTP_RESPONSE/"; +// rootChunk["type"]="0"; +// rootChunk["url"]=urlStr.append(responseRequestId[1]); +// rootChunk["length"]=to_string(length); +// //length=responseStr.length(); +// rootChunk["chunkSize"]="7000"; +// //string contentStr =rootChunk.asString(); +// cout << responseStr << endl; +// resData->setContent(reinterpret_cast(rootChunk.toStyledString().data()), rootChunk.toStyledString().size()); +// m_keyChain.sign(*resData); +// std::cout << ">> D: " << "chunkdata packet :" << " " << *resData << std::endl; +// m_face.put(*resData); +// } +// } +// else if(interest.getName()[-3].toUri()=="pull"){ +// pair localPair3(interest.getName()[5].toUri(),interest.getName()[6].toUri()); +// cout << storeResponseStr[localPair3].length() << endl; +// cout << storeResponseSeq[localPair3][0] << endl; +// Name dataName(interest.getName()); //TODO: need requestId +// shared_ptr resData = make_shared(); +// resData->setName(dataName); +// resData->setFreshnessPeriod(0_s); // 10 seconds +// lp::CachePolicy noCachePolicy; +// noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); +// resData->setTag(make_shared(noCachePolicy)); +// cout << storeResponseSeq[localPair3][stoi(interest.getName()[-4].toUri())].length() << endl; +// string contentStr1=storeResponseSeq[localPair3][stoi(interest.getName()[-4].toUri())]; +// resData->setContent(reinterpret_cast(contentStr1.data()), contentStr1.size()); +// m_keyChain.sign(*resData); +// std::cout << "<< D: " << "data packet :" << stoi(interest.getName()[-4].toUri()) << "th" << " " << *resData << std::endl; +// m_face.put(*resData); +// sessionIdMap[interest.getName()[5].toUri()]= false; +// } } - void onRegisterFailed(const Name& prefix, const std::string& reason) - { - std::cerr << "ERROR: Failed to register prefix \"" - << prefix << "\" in local hub's daemon (" << reason << ")" - << std::endl; - m_face.shutdown(); - } - -private: - Face m_face; - KeyChain m_keyChain; - boost::uuids::uuid uu; - string uuid_string; - string parseStr; - int seq=0; - map requestMap; //GR-0->requestId0 - map seqMap; //requestId0->loopNum(seq-i) - int requestSeq=-1; - map> storeRequestIdSeq; - map localMap; - map storeSeq; - map sessionIdMap; - int loopNum;//TODO: loopnummap - int seqNum; -}; - - - -int main(int argc, char** argv) -{ - Sessionpro producer; - try { - producer.run(); - } - catch (const std::exception& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } - return 0; } + +void Sessionpro::onInterestUserDetect(const InterestFilter& filter, const Interest& interest) +{ + std::cout << ">> I: " << interest << std::endl; + if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx + string intParaStr((char *) + interest.getApplicationParameters().value(), + interest.getApplicationParameters().value_size()); + } + + Name dataName(interest.getName()); + + Json::Reader reader ; + Json::Value root ; + + string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ; + reader.parse(str,root) ; + cout << root["serverPrefix"].asString() << endl ; + cout << root.toStyledString() << endl ; + + static const std::string content = root.toStyledString(); + + shared_ptr data = make_shared(); + data->setName(dataName); + data->setFreshnessPeriod(0_s); // 10 seconds + // 数据缓存在节点中,立即变旧 + + data->setContent(reinterpret_cast(content.data()), content.size()); + + + m_keyChain.sign(*data); + std::cout << ">> D: " << *data << std::endl; + m_face.put(*data); +} + +void Sessionpro::onInterestDetect(const InterestFilter& filter, const Interest& interest) +{ + std::cout << ">> I: " << interest << std::endl; + if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx + string intParaStr((char *) + interest.getApplicationParameters().value(), + interest.getApplicationParameters().value_size()); + } + + Name dataName(interest.getName()); + + Json::Reader reader ; + Json::Value root ; + + string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ; + reader.parse(str,root) ; + cout << root["serverPrefix"].asString() << endl ; + cout << root.toStyledString() << endl ; + + static const std::string content = root.toStyledString(); + + shared_ptr data = make_shared(); + data->setName(dataName); + data->setFreshnessPeriod(0_s); // 10 seconds + // 数据缓存在节点中,立即变旧 + + data->setContent(reinterpret_cast(content.data()), content.size()); + + + m_keyChain.sign(*data); + std::cout << ">> D: " << *data << std::endl; + m_face.put(*data); +} + +void Sessionpro::onInterest(const InterestFilter& filter, const Interest& interest) +{ + std::cout << "<< I: " << interest << std::endl; +// uuid_t uu; + boost::uuids::uuid uu = boost::uuids::random_generator()(); + string uuid_string = boost::uuids::to_string(uu); + std::cout << uu << std::endl; + + if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx + string intParaStr((char *) + interest.getApplicationParameters().value(), + interest.getApplicationParameters().value_size()); + } + + Name dataName(interest.getName()); + + Json::Reader reader ; + Json::Value root1 ; + + + root1["sessionId"]=uuid_string; + cout << root1["sessionId"].asString() << endl ; + cout << root1.toStyledString() << endl ; + + static const std::string content = root1.toStyledString(); + + shared_ptr data = make_shared(); + data->setName(dataName); + data->setFreshnessPeriod(0_s); // 10 seconds + // 数据缓存在节点中,立即变旧 + + data->setContent(reinterpret_cast(content.data()), content.size()); + + + m_keyChain.sign(*data); + std::cout << ">> D: " << *data << std::endl; + m_face.put(*data); +} +void Sessionpro::onRegisterFailed(const Name& prefix, const std::string& reason) +{ + std::cerr << "ERROR: Failed to register prefix \"" + << prefix << "\" in local hub's daemon (" << reason << ")" + << std::endl; + m_face.shutdown(); +} + +void Sessionpro::setOnRequestListener(const MIN::MINWebServer::OnRequestReceiveListener &onRequestReceiveListener) { + this->listener = onRequestReceiveListener; +} + +void Sessionpro::replyResponse(std::shared_ptr response) { + cout << response->context["feature"] << endl; + cout << response->contentType < responseSeq; + std::string responseStr; + responseStr.append("MIN-HTTP/1.0").append(" ").append("200").append(" ").append( + MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code; + responseStr.append("\r\n"); + responseStr.append("Content-Type").append(" ").append( + MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType)); + responseStr.append("\r\n"); + responseStr.append("Content-Length").append(" ").append(to_string(response->contentLength)); + responseStr.append("\r\n\r\n"); + std::cout << "-----------------------------------reply" << endl; + std::cout << std::string((char *)response->content, response->contentLength) << std::endl; + if(response->body.empty()) + responseStr.append(std::string((char *)response->content, response->contentLength)); + else + responseStr.append(response->body); +// std::cout << response->body << std::endl; + cout << "this is str: " << responseStr << endl; + int length=responseStr.length(); + cout << response->contentLength << endl; + cout << "this is length " << length << endl; + if(length<=7000){ +// responseSeq[0]=storeResponseStr[localPair3]; + responseSeq.push_back(responseStr); + cout << "fuck test" << endl; + cout << responseStr << endl; + cout << responseSeq[0] <context["feature"]=="ask"){ + + + /* + * SEND RESPONSE TO BROWSER CLIENT + */ + Name dataName(Name(response->context["prefix"])); //TODO: need requestId + shared_ptr resData = make_shared(); + Json::Value rootChunk ; + resData->setName(dataName); + resData->setFreshnessPeriod(0_s); // 10 seconds + lp::CachePolicy noCachePolicy; + noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); + resData->setTag(make_shared(noCachePolicy)); + string urlStr=""; + rootChunk["type"]="0"; + rootChunk["url"]=urlStr.append(response->context["prefix"]); + rootChunk["length"]=to_string(length); + //length=responseStr.length(); + rootChunk["chunkSize"]="7000"; + //string contentStr =rootChunk.asString(); + cout << rootChunk.toStyledString().size()<< endl; + resData->setContent(reinterpret_cast(rootChunk.toStyledString().data()), rootChunk.toStyledString().size()); + m_keyChain.sign(*resData); + std::cout << "<< D: " << "chunkdata packet :" << " " << *resData << std::endl; + m_face.put(*resData); + } + else if(response->context["feature"]=="pull"){ +// pair localPair3(interest.getName()[5].toUri(),interest.getName()[6].toUri()); +// cout << storeResponseStr[localPair3].length() << endl; +// cout << storeResponseSeq[localPair3][0] << endl; + Name dataName(Name(response->context["prefix"])); //TODO: need requestId + shared_ptr resData = make_shared(); + resData->setName(dataName); + resData->setFreshnessPeriod(0_s); // 10 seconds + lp::CachePolicy noCachePolicy; + noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE); + resData->setTag(make_shared(noCachePolicy)); + cout << response->context["number"] <context["number"]]; + resData->setContent(reinterpret_cast(contentStr1.data()), contentStr1.size()); + m_keyChain.sign(*resData); + std::cout << "<< D: " << "data packet :" << response->context["number"] << "th" << " " << *resData << std::endl; + m_face.put(*resData); + //sessionIdMap[interest.getName()[5].toUri()]= false; + } +} + + + + +// +// +//int main(int argc, char** argv) +//{ +// Sessionpro producer; +// try { +// producer.run(); +// } +// catch (const std::exception& e) { +// std::cerr << "ERROR: " << e.what() << std::endl; +// } +// return 0; +//} //if(!storeSeq.count(stoi(ackSeq[1]))){ // seqNum++; // std::cout << "SEQ >> I: " << interest << std::endl; diff --git a/MINWebServer/http/MINHttpSession.h b/MINWebServer/http/MINHttpSession.h new file mode 100644 index 0000000..0689655 --- /dev/null +++ b/MINWebServer/http/MINHttpSession.h @@ -0,0 +1,69 @@ +// +// Created by yb on 11/27/20. +// + +#ifndef MIN_NGINX_MINHTTPSESSION_H +#define MIN_NGINX_MINHTTPSESSION_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../message/MINWebRequest.h" +#include "../message/MINWebResponse.h" +#include "MINHttpServer.h" + +#include +using namespace ndn ; +using namespace std ; + +class Sessionpro { +public: + void run(); + shared_ptr minWebRequest= make_shared(); + MIN::MINWebMessage minWebMessage; + //typedef std::function &request)> OnRequestReceiveListener; + + void setOnRequestListener(const MIN::MINWebServer::OnRequestReceiveListener &onRequestReceiveListener); + void replyResponse(std::shared_ptr response); + +private: + vector split(const string& s, vector& tokens, char delim ); + void onSuccess(const Name& name); + void onInterestBuild(const InterestFilter& filter, const Interest& interest); + void onInterestUserDetect(const InterestFilter& filter, const Interest& interest); + void onInterestDetect(const InterestFilter& filter, const Interest& interest); + void onInterest(const InterestFilter& filter, const Interest& interest); + void onRegisterFailed(const Name& prefix, const std::string& reason); +private: + Face m_face; + KeyChain m_keyChain; + boost::uuids::uuid uu; + string uuid_string; + string parseStr; + int seq=0; + map requestMap; //GR-0->requestId0 + map seqMap; //requestId0->loopNum(seq-i) + int requestSeq=-1; + map> storeRequestIdSeq; + map localMap; + map storeSeq; + map sessionIdMap; + int loopNum;//TODO: loopnummap + int seqNum; + int requestIdSeq=0; + map,bool> responsePacketState; + map,shared_ptr> requestPacketId; + map,string> storeResponseStr; + map,vector> storeResponseSeq; + MIN::MINWebServer::OnRequestReceiveListener listener; +}; + + +#endif //MIN_NGINX_MINHTTPSESSION_H diff --git a/MINWebServer/http/MINWebMessage.h b/MINWebServer/http/MINWebMessage.h deleted file mode 100644 index b517e80..0000000 --- a/MINWebServer/http/MINWebMessage.h +++ /dev/null @@ -1,78 +0,0 @@ -// -// Created by SunnyQjm on 2020/10/14 上午10:26. -// - -#ifndef MIN_NGINX_MINWEBMESSAGE_H -#define MIN_NGINX_MINWEBMESSAGE_H - -#include -#include -#include -#include "../utils/StringUtils.h" -#include "../common/MINHttpDef.h" -#include "../utils/json.h" - -namespace MIN { -#define MIN_MAP std::map - - enum class MessageParserState : uint8_t - { - NONE, - PARSING_START_LINE, - START_LINE_REQUEST, - START_LINE_RESPONSE, - HEADER_KEY, - HEADER_VALUE, - PARSING_BODY, - }; - - enum MINParserType { - MIN_WEB_REQUEST = 0, - MIN_WEB_RESPONSE = 1, - }; - - typedef std::map MINHttpHeaders; - typedef std::string MINHttpBody; - - class MINWebMessage { - public: - // /MIN/Nginx/test/getData?size=1&page=2 - // /MIN/Nginx/test/getPost/:filedId => /MIN/Nginx/test/getPost/2 - std::string url; - int type; - - MessageParserState state; - - // SignatureInfo - std::shared_ptr signatureInfo; - - // headers - MINHttpHeaders headers; - - // body - MINHttpBody body; - - // structured content - void *content; // DATA_NO_COPY - int contentLength; - MINWebServer::MINHttpContentType contentType; - nlohmann::json json; // APPLICATION_JSON - - virtual MINWebMessage& SetHeader(const std::string& name, const std::string& value) - { - headers[name] = value; - - return *this; - } - - - virtual void reset() { - type = MIN_WEB_REQUEST; - state= MessageParserState::NONE; - url = ""; - signatureInfo = std::make_shared(); - } - }; -} - -#endif //MIN_NGINX_MINWEBMESSAGE_H diff --git a/MINWebServer/http/MINWebRequest.cpp b/MINWebServer/http/MINWebRequest.cpp deleted file mode 100644 index ae700f1..0000000 --- a/MINWebServer/http/MINWebRequest.cpp +++ /dev/null @@ -1,192 +0,0 @@ -// -// Created by SunnyQjm on 2020/10/14 上午10:21. -// - -#include -#include -#include "MINWebRequest.h" -#include -#include -#include -#include "../common/logger.h" - -namespace MIN { - using namespace ndn; - using namespace std; - MIN_LOG_INIT(MINWebRequest); - -// MINWebMessage minWebMessage; -// MINWebRequest request; -// MINWebServer::MINHttpMethod httpMethod; - shared_ptr getSignatureInfo(const Interest &interest) { - auto &name = interest.getName(); - if (name.size() > ndn::signed_interest::MIN_SIZE) { - try { - return make_shared(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue()); - } catch (...) { - return nullptr; - } - } - return nullptr; - } - - void ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string& buf,const ndn::Interest& interest){ - minWebMessage.reset(); - std::vector buffer = std::vector(buf.begin(), buf.end()); - minWebMessage.state=MessageParserState::PARSING_START_LINE; - std::string temp=""; - bool skipNext=false; - std::string headerKey = ""; - MIN_LOG_DEBUG("??????????????????"); - // whether or not a message body is present - bool hasMessageBody = false; - - Name name = interest.getName(); - // 判断有没有签名 - auto signatureInfoPtr = getSignatureInfo(interest); - if (signatureInfoPtr) { - // 有签名则去掉后两个Component - name = interest.getName().getSubName(0, interest.getName().size() - 2); - } - request.url = name.toUri(); - auto lastComponentStr = name[-1].toUri(); - auto position = lastComponentStr.find(std::string(MIN_WEB_CHUNK_KEY) + "%3A"); - if (position != std::basic_string::npos) { - // 最后一个Component表示块号 - name = name.getSubName(0, name.size() - 1); - try { - request.queryParams[MIN_WEB_CHUNK_KEY] = lastComponentStr.substr(strlen(MIN_WEB_CHUNK_KEY) + 3); - } catch (...) { - std::cerr << "Interest Parser error: " << interest.toUri(); - } - } - cout << " this is url :" << request.url; - request.requestType=MIN_WEB_REQUEST_TYPE_STATIC; - request.service="/MIN/Nginx/test2"; - - // the index at which the message body begins - size_t bodyStartIndex = 0; - for(size_t index=0;indexSetStatusCode(static_cast(code)); - temp = ""; - continue; - } - // if we are at a carriage return start, then set the status message for - // the response, this can be blank in which it will use a generated status - // - // this will also set the state of the parser to move on to headers - else if (character == '\r') - { - //minWebMessage->SetStatusMessage(temp); - temp = ""; - minWebMessage.state = MessageParserState::HEADER_KEY; - skipNext = true; - continue; - } - } - else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == ':') - { - headerKey = temp; - - temp = ""; - - minWebMessage.state = MessageParserState::HEADER_VALUE; - - // HTTP defines that the next character in a header should be a space - // so skip that for parsing the value of the header - skipNext = true; - - continue; - } - // if we are parsing header values and hit the beginning of a carriage return then - // it is time to add the header to the message with the key and value, and move the - // state back to parsing keys - else if (minWebMessage.state == MessageParserState::HEADER_VALUE && character == '\r') - { - request.SetHeader(headerKey, temp); - - headerKey = ""; - temp = ""; - - minWebMessage.state = MessageParserState::HEADER_KEY; - - // skip the next character as it will just be a newline - skipNext = true; - - continue; - } - // if we are parsing header keys and we hit a carriage return, then we should assume - // that the headers have ended, and that we are now parsing a message body. - else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == '\r') - { - temp = ""; - - minWebMessage.state = MessageParserState::PARSING_BODY; - - // skip the next character as it'll be a newline - skipNext = true; - - continue; - } - temp+=character; - } - } -// std::shared_ptr MINWebRequest::Parse(MINWebMessage MINwebmessage,const std::string &buffer,const Interest& interest){ -// return Parser(MINwebmessage, std::vector(buffer.begin(), buffer.end()),interest); -// } - - void MINWebRequest::ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string &buffer, const Interest &interest) { - std::cout << "111" << std::endl; - } -} \ No newline at end of file diff --git a/MINWebServer/http/MINWebRequest.h b/MINWebServer/http/MINWebRequest.h deleted file mode 100644 index 153dba2..0000000 --- a/MINWebServer/http/MINWebRequest.h +++ /dev/null @@ -1,79 +0,0 @@ -// -// Created by SunnyQjm on 2020/10/14 上午10:21. -// - -#ifndef MIN_NGINX_MINWEBREQUEST_H -#define MIN_NGINX_MINWEBREQUEST_H - -#include "MINWebMessage.h" -#include -#include "../common/MINHttpDef.h" - -#define MIN_WEB_CHUNK_KEY "MIN_WEB_CHUNK" - -namespace MIN { - - using namespace ndn; - - enum MINWebRequestType { - MIN_WEB_REQUEST_TYPE_STATIC = 0, // 静态请求 - }; - - class MINWebRequest : public MINWebMessage { - public: - - // /MIN/Nginx/test - std::string service; - - // /index.html - std::string path; - - // 0 - MINWebRequestType requestType; - - // method - MINWebServer::MINHttpMethod method; - - //string method - std::string strmethod; - //version - //MINWebServer::MINHttpVersion version; - std::string version; - - // queryParams - MIN_MAP queryParams; - - MINWebRequest& SetPath(std::string &mpath){ - this->path=mpath; - return *this; - } - - - MINWebRequest& SetHeader(const std::string& name, const std::string& value) - { - headers[name] = value; - - return *this; - } - - void init() { - type = MIN_WEB_REQUEST; - } - - void reset() override { - MINWebMessage::reset(); - service = ""; - path = ""; - requestType = MIN_WEB_REQUEST_TYPE_STATIC; - queryParams.clear(); - } - - void ybParse(MINWebMessage minWebMessage,MINWebRequest request, const std::string &buf,const Interest& interest); -// { -// renturn std::shared_ptr Parser(MINWebMessage MINwebmessage,const std::vector& buffer,const Interest& interest); -// } - }; -} - - -#endif //MIN_NGINX_MINWEBREQUEST_H diff --git a/MINWebServer/http/MINWebResponse.cpp b/MINWebServer/http/MINWebResponse.cpp deleted file mode 100644 index 8953648..0000000 --- a/MINWebServer/http/MINWebResponse.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by SunnyQjm on 2020/10/14 上午10:21. -// - -#include "MINWebResponse.h" - -namespace MIN { -} \ No newline at end of file diff --git a/MINWebServer/http/MINWebResponse.h b/MINWebServer/http/MINWebResponse.h deleted file mode 100644 index 83acff4..0000000 --- a/MINWebServer/http/MINWebResponse.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// Created by SunnyQjm on 2020/10/14 上午10:21. -// - -#ifndef MIN_NGINX_MINWEBRESPONSE_H -#define MIN_NGINX_MINWEBRESPONSE_H - -#include "MINWebMessage.h" -#include "../common/MINHttpDef.h" - -namespace MIN { - - class MINWebResponse : public MINWebMessage { - public: - MINWebServer::MINHttpStatus status = MINWebServer::MIN_HTTP_STATUS_NOT_FOUND; - - void init() { - type = MIN_WEB_RESPONSE; - } - }; -} - - -#endif //MIN_NGINX_MINWEBRESPONSE_H diff --git a/MINWebServer/message/MINWebRequest.cpp b/MINWebServer/message/MINWebRequest.cpp index c957485..d851e26 100644 --- a/MINWebServer/message/MINWebRequest.cpp +++ b/MINWebServer/message/MINWebRequest.cpp @@ -1,16 +1,214 @@ // -// Created by SunnyQjm on 2020/10/14 上午10:21. +// Created by yb on 2020/11/14 // -#include -#include +#include +#include #include "MINWebRequest.h" #include #include #include "../common/logger.h" +#include namespace MIN { using namespace ndn; using namespace std; +// MINWebMessage minWebMessage; +// MINWebRequest request; +// MINWebServer::MINHttpMethod httpMethod; + shared_ptr getSignatureInfo(const Interest &interest) { + auto &name = interest.getName(); + if (name.size() > ndn::signed_interest::MIN_SIZE) { + try { + return make_shared(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue()); + } catch (...) { + return nullptr; + } + } + return nullptr; + } + + void MINWebRequest::ybParse(MINWebMessage minWebMessage,std::shared_ptr request,const std::string& buf,const ndn::Interest& interest){ + minWebMessage.reset(); + std::vector buffer = std::vector(buf.begin(), buf.end()); + cout << " ?????????????????" << endl; + minWebMessage.state=MessageParserState::PARSING_START_LINE; + std::string temp=""; + bool skipNext=false; + std::string headerKey = ""; + + // whether or not a message body is present + bool hasMessageBody = false; + + Name name = interest.getName(); + // 判断有没有签名 + cout << name.toUri() << endl; + auto signatureInfoPtr = getSignatureInfo(interest); + if (signatureInfoPtr) { + // 有签名则去掉后两个Component + name = interest.getName().getSubName(0, interest.getName().size() - 2); + } + string serviceprefix="/MIN/Web/test"; + request->url = ""; + cout << request->url << endl; + auto lastComponentStr = name[-1].toUri(); + auto position = lastComponentStr.find(std::string(MIN_WEB_CHUNK_KEY) + "%3A"); + cout << position << endl; + if (position != std::basic_string::npos) { + // 最后一个Component表示块号 + name = name.getSubName(0, name.size() - 1); + try { + request->queryParams[MIN_WEB_CHUNK_KEY] = lastComponentStr.substr(strlen(MIN_WEB_CHUNK_KEY) + 3); + } catch (...) { + std::cerr << "Interest Parser error: " << interest.toUri(); + } + } + cout << " this is url :" << request->url << endl; + request->requestType=MIN_WEB_REQUEST_TYPE_STATIC; + request->service="/MIN/Web/test"; + cout << " fuck :" << request->url << endl; + + // the index at which the message body begins + size_t bodyStartIndex = 0; + for(size_t index=0;indexheaders["Content-Type"]=="application/json"){ + //request->body = request->json.parse(temp); + cout << "this is body " << temp; + Json::Reader reader ; + Json::Value root ; + reader.parse(temp,root) ; + cout << root.toStyledString() << endl ; + request->body=root.toStyledString(); + } + else{ + request->body=temp; + } +// request->url=request->service.append(request->path); + cout << " this is url :" << request->url << endl; + cout << " this is headers :" << request->body << endl; + cout << " this is method :" << request->strmethod << endl; + break; + } + if(minWebMessage.state==MessageParserState::PARSING_START_LINE){ + cout << "character" << character << endl; + if(character==' '){ + request->method=MINWebServer::getMINHttpMethodEnumFromStr(temp); + request->strmethod=temp; + minWebMessage.state = MessageParserState::START_LINE_REQUEST; + temp = ""; + cout << "this is method" << request->strmethod << endl; + continue; + } + } + else if(minWebMessage.state==MessageParserState::START_LINE_REQUEST){ + if(character == ' '){ + request->path=temp; + temp=""; + cout << "this is path " << request->path << endl; + request->url=request->service.append(request->path); + continue; + } + else if(character== '\r'){ + request->version=temp; + cout << " this is version" << request->version << endl; + temp=""; + minWebMessage.state=MessageParserState::HEADER_KEY; + skipNext=true; + continue; + } + } + else if (minWebMessage.state == MessageParserState::START_LINE_RESPONSE) + { + // if we are at a space, then we have hit the status code for the response + if (character == ' ') + { + int code = std::stoi(temp); + //minWebMessage->SetStatusCode(static_cast(code)); + temp = ""; + continue; + } + // if we are at a carriage return start, then set the status message for + // the response, this can be blank in which it will use a generated status + // + // this will also set the state of the parser to move on to headers + else if (character == '\r') + { + //minWebMessage->SetStatusMessage(temp); + temp = ""; + minWebMessage.state = MessageParserState::HEADER_KEY; + skipNext = true; + continue; + } + } + else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == ':') + { + headerKey = temp; + + temp = ""; + + minWebMessage.state = MessageParserState::HEADER_VALUE; + + // HTTP defines that the next character in a header should be a space + // so skip that for parsing the value of the header + skipNext = true; + + continue; + } + // if we are parsing header values and hit the beginning of a carriage return then + // it is time to add the header to the message with the key and value, and move the + // state back to parsing keys + else if (minWebMessage.state == MessageParserState::HEADER_VALUE && character == '\r') + { + request->SetHeader(headerKey, temp); + cout << "this is header" << request->headers[headerKey] << endl; + headerKey = ""; + temp = ""; + + minWebMessage.state = MessageParserState::HEADER_KEY; + + // skip the next character as it will just be a newline + skipNext = true; + + continue; + } + // if we are parsing header keys and we hit a carriage return, then we should assume + // that the headers have ended, and that we are now parsing a message body. + else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == '\r') + { + temp = ""; + + minWebMessage.state = MessageParserState::PARSING_BODY; + + // skip the next character as it'll be a newline + skipNext = true; + + continue; + } + temp+=character; + cout << temp << endl; + } + } +// std::shared_ptr MINWebRequest::Parse(MINWebMessage MINwebmessage,const std::string &buffer,const Interest& interest){ +// return Parser(MINwebmessage, std::vector(buffer.begin(), buffer.end()),interest); +// } + +// void MINWebRequest::ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string &buffer, const Interest &interest) { +// std::cout << "111" << std::endl; +// } } \ No newline at end of file diff --git a/MINWebServer/message/MINWebRequest.h b/MINWebServer/message/MINWebRequest.h index 05af794..638378a 100644 --- a/MINWebServer/message/MINWebRequest.h +++ b/MINWebServer/message/MINWebRequest.h @@ -47,6 +47,20 @@ namespace MIN { // 可以再 preprocessor -> controller -> postprocessor 这条处理链的任意缓解向上下文中存取信息 nlohmann::json context; + + MINWebRequest& SetPath(std::string &mpath){ + this->path=mpath; + return *this; + } + + + MINWebRequest& SetHeader(const std::string& name, const std::string& value) + { + headers[name] = value; + + return *this; + } + void init() { type = MIN_WEB_REQUEST; } @@ -59,8 +73,11 @@ namespace MIN { queryParams.clear(); } + void ybParse(MINWebMessage minWebMessage,std::shared_ptr request, const std::string &buf,const Interest& interest); +// { +// renturn std::shared_ptr Parser(MINWebMessage MINwebmessage,const std::vector& buffer,const Interest& interest); +// } }; } - -#endif //MIN_NGINX_MINWEBREQUEST_H +#endif diff --git a/MINWebServer/message/MINWebResponse.h b/MINWebServer/message/MINWebResponse.h index 06494ca..50d9565 100644 --- a/MINWebServer/message/MINWebResponse.h +++ b/MINWebServer/message/MINWebResponse.h @@ -51,4 +51,4 @@ namespace MIN { } -#endif //MIN_NGINX_MINWEBRESPONSE_H +#endif //MIN_NGINX_MINWEBRESPONSE_H \ No newline at end of file diff --git a/MINWebServer/server/FileCache.h b/MINWebServer/server/FileCache.h index d21e8c2..6d862b7 100644 --- a/MINWebServer/server/FileCache.h +++ b/MINWebServer/server/FileCache.h @@ -8,7 +8,7 @@ #include #include #include -#include +//#include #include #include "../base/MBuf.h" diff --git a/test_minweb_server_library.cpp b/test_minweb_server_library.cpp index 5ad6a05..30de8e7 100644 --- a/test_minweb_server_library.cpp +++ b/test_minweb_server_library.cpp @@ -7,34 +7,84 @@ #include "MINWebServer/server/Server.h" #include "MINWebServer/message/MINWebRequest.h" #include "MINWebServer/message/MINWebResponse.h" +#include "MINWebServer/http/MINHttpSession.h" +#include "MINWebServer/http/MINHttpServerImpl.h" using namespace MIN; using namespace MIN::MINWebServer; -class MINHttpServerImpl : public MINHttpServer { +class MINHttpServerImpl1 : public MINHttpServer { public: void setOnRequestListener(const OnRequestReceiveListener &onRequestReceiveListener) override { this->listener = onRequestReceiveListener; } - void replyResponse(std::shared_ptr response) override { - std::cout << std::string((char *)response->content, response->contentLength) << std::endl; - std::cout << response->body << std::endl; + void replyResponse(std::shared_ptr response) override{ + // TODO deal response + sessionpro.replyResponse(response); +// std::string responseStr; +// responseStr.append("MIN-HTTP/1.0").append(" ").append(MIN::MINWebServer::getMINHttpStatusStr(response->status)).append(" ").append(MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code; +// responseStr.append("\r\n"); +// responseStr.append("Content-Type").append(" ").append(MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType)); +// responseStr.append("Content-Length").append(" ").append(" "); +// responseStr.append("\r\n\r\n"); +// responseStr.append(response->body); +// +// /* +// * SEND RESPONSE TO BROWSER CLIENT +// */ +// if(responseStr.length()<=7000){ +// ndn::KeyChain m_keyChain; +// ndn::Face m_face; +// //Name dataName(interest.getName()); //TODO: need requestId +// shared_ptr data = make_shared(); +// //data->setName(dataName); +// data->setFreshnessPeriod(0_s); // 10 seconds +// // 数据缓存在节点中,立即变旧 +// data->setContent(reinterpret_cast(responseStr.data()), responseStr.size()); +// m_keyChain.sign(*data); +// std::cout << ">> D: " << "data packet :" << " " << *data << std::endl; +// m_face.put(*data); +// } +// else { +// size_t sendSeq = responseStr.length() / 7000; +// std::vector responseSeq; +// for (int i = 0; i < sendSeq; i++) { +// if (i == 0) +// responseSeq[i] = responseStr.substr(0, 7000); +// else +// responseSeq[i] = responseStr.substr((7000 * i) + 1, 7000 * (i + 1)); +// } +// ndn::KeyChain m_keyChain; +// ndn::Face m_face; +// //Name dataName(interest.getName()); //TODO: need requestId +// shared_ptr data = make_shared(); +// //data->setName(dataName); +// data->setFreshnessPeriod(0_s); // 10 seconds +// // 数据缓存在节点中,立即变旧 +// for (int i = 0; i < sendSeq; i++) { +// data->setContent(reinterpret_cast(responseSeq[i].data()), responseSeq[i].size()); +// m_keyChain.sign(*data); +// std::cout << ">> D: " << "data packet :" << i << " " << *data << std::endl; +// m_face.put(*data); +// } +// } } +// void replyResponse(std::shared_ptr response) override { +// std::cout << "-----------------------------------reply" << endl; +// std::cout << std::string((char *)response->content, response->contentLength) << std::endl; +// std::cout << response->body << std::endl; +// } + void start(bool wait) override { - MINWebRequest request; - request.service = "/MIN/Web/test"; - request.url = "/MIN/Web/test/getUserInfo?id=1"; - request.path = "/getUserInfo?id=1"; -// request.path = "/test.cpp"; - request.method = MIN_HTTP_GET; - auto requestPtr = make_shared(request); - this->listener(requestPtr); + sessionpro.setOnRequestListener(this->listener); + sessionpro.run(); } private: + Sessionpro sessionpro; OnRequestReceiveListener listener; }; @@ -45,6 +95,8 @@ int main () { service.homePage = "index.html"; service.baseUrl = "/MIN/Web/test"; + std::cout << getMINHttpMethodEnumFromStr("GET") << std::endl; + std::cout << getMINHttpStatusEnumFromStr("Continue") << std::endl; service.GET("/getUserInfo", [](std::shared_ptr &request, std::shared_ptr &response) { nlohmann::json result = { @@ -56,7 +108,7 @@ int main () { return 0; }); - MINHttpServerImpl minHttpServer; + MINHttpServerImpl1 minHttpServer; Server::ServerConfig serverConfig; serverConfig.service = &service; @@ -64,4 +116,14 @@ int main () { Server server(serverConfig); server.run(); -} \ No newline at end of file +} + + +// MINWebRequest request; +// request.service = "/MIN/Web/test"; +// request.url = "/MIN/Web/test/getUserInfo"; +//// request.path = "/sunnyqjm.github.io/2017/"; +// request.path = "/getUserInfo"; +// request.method = MIN_HTTP_GET; +// auto requestPtr = make_shared(request); +// this->listener(requestPtr); \ No newline at end of file