replace bare pointers with shared_ptr in HttpReponse static methods;fix a delete bug

This commit is contained in:
an-tao 2018-05-30 21:23:46 +08:00
parent 7b7a426f85
commit 1aaa65fff7
13 changed files with 32 additions and 29 deletions

View File

@ -150,7 +150,7 @@ void create_view::newViewHeaderFile(std::ofstream &file,const std::string &class
file<<"using namespace drogon;\n";
file<<"class "<<className<<":public HttpView<"<<className<<">\n";
file<<"{\npublic:\n\t"<<className<<"(){};\n\tvirtual ~"<<className<<"(){};\nprotected:\n\t"
"virtual HttpResponse* genHttpResponse(const HttpViewData&) override;\n};";
"virtual HttpResponsePtr genHttpResponse(const HttpViewData&) override;\n};";
}
void create_view::newViewSourceFile(std::ofstream &file,const std::string &className,std::ifstream &infile)
@ -227,7 +227,7 @@ void create_view::newViewSourceFile(std::ofstream &file,const std::string &class
//std::cout<<"file pos:"<<infile.tellg()<<std::endl;
std::string viewDataName=className+"_view_data";
file <<"HttpResponse* "<<className<<"::genHttpResponse(const HttpViewData& "<<viewDataName<<")\n{\n";
file <<"HttpResponsePtr "<<className<<"::genHttpResponse(const HttpViewData& "<<viewDataName<<")\n{\n";
//std::string bodyName=className+"_bodystr";
std::string streamName=className+"_tmp_stream";

View File

@ -14,6 +14,6 @@ void JsonTestController::asyncHandleHttpRequest(const HttpRequest& req,std::func
array.append(user);
}
json["rows"]=array;
auto resp=std::unique_ptr<HttpResponse>(HttpResponse::newHttpJsonResponse(json));
auto resp=HttpResponse::newHttpJsonResponse(json);
callback(*resp);
}

View File

@ -5,6 +5,6 @@ void ListParaCtl::asyncHandleHttpRequest(const HttpRequest& req,std::function<vo
HttpViewData data;
data.insert("title",std::string("list parameters"));
data.insert("parameters",req.getPremeter());
auto res=std::unique_ptr<drogon::HttpResponse>(drogon::HttpResponse::newHttpViewResponse("ListParaView.csp",data));
auto res=drogon::HttpResponse::newHttpViewResponse("ListParaView.csp",data);
callback(*res);
}

View File

@ -3,7 +3,7 @@ using namespace example;
void TestController::asyncHandleHttpRequest(const HttpRequest& req,std::function<void (HttpResponse &)>callback)
{
//write your application logic here
std::unique_ptr<HttpResponse> resp=std::unique_ptr<HttpResponse>(HttpResponse::newHttpResponse());
auto resp=HttpResponse::newHttpResponse();
LOG_DEBUG<<"!!!!!!!!!!1";
resp->setStatusCode(HttpResponse::k200Ok);
resp->setContentTypeCode(CT_TEXT_HTML);

View File

@ -4,6 +4,6 @@ void TestViewCtl::asyncHandleHttpRequest(const HttpRequest& req,std::function<vo
//write your application logic here
drogon::HttpViewData data;
data.insert("title",std::string("TestView"));
std::unique_ptr<HttpResponse> res=std::unique_ptr<HttpResponse>(drogon::HttpResponse::newHttpViewResponse("TestView",data));
auto res=drogon::HttpResponse::newHttpViewResponse("TestView",data);
callback(*res);
}

View File

@ -21,8 +21,9 @@
#pragma once
#include <drogon/HttpViewData.h>
#include <string>
#include <json/json.h>
#include <string>
#include <memory>
using std::string;
#define CT_APPLICATION_JSON 1
@ -49,6 +50,8 @@ using std::string;
namespace drogon
{
class HttpResponse;
typedef std::shared_ptr<HttpResponse> HttpResponsePtr;
class HttpResponse
{
public:
@ -100,11 +103,11 @@ namespace drogon
virtual std::string getBody() const=0;
static HttpResponse* newHttpResponse();
static HttpResponse* notFoundResponse();
static HttpResponse* newHttpJsonResponse(const Json::Value &data);
static HttpResponse* newHttpViewResponse(const std::string &viewName,const HttpViewData& data);
static HttpResponse* locationRedirectResponse(std::string path);
static HttpResponsePtr newHttpResponse();
static HttpResponsePtr notFoundResponse();
static HttpResponsePtr newHttpJsonResponse(const Json::Value &data);
static HttpResponsePtr newHttpViewResponse(const std::string &viewName,const HttpViewData& data);
static HttpResponsePtr locationRedirectResponse(std::string path);
};
}

View File

@ -18,13 +18,13 @@ namespace drogon
{
public:
static HttpResponse* genHttpResponse(std::string viewName,const HttpViewData &data);
static HttpResponsePtr genHttpResponse(std::string viewName,const HttpViewData &data);
virtual ~HttpViewBase(){};
HttpViewBase(){};
protected:
virtual HttpResponse* genHttpResponse(const HttpViewData&)=0;
virtual HttpResponsePtr genHttpResponse(const HttpViewData&)=0;
};
}

View File

@ -6,6 +6,6 @@ class NotFound:public HttpView<NotFound>
public:
NotFound(){};
virtual ~NotFound(){};
protected:
virtual HttpResponse* genHttpResponse(const HttpViewData&)override;
protected:
HttpResponsePtr genHttpResponse(const HttpViewData&) override;
};

View File

@ -234,7 +234,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::function<v
}
}
auto res=std::unique_ptr<drogon::HttpResponse>(drogon::HttpResponse::notFoundResponse());
auto res=drogon::HttpResponse::notFoundResponse();
if(needSetJsessionid)
res->addCookie("JSESSIONID",session_id);
@ -298,7 +298,7 @@ void HttpAppFrameworkImpl::readSendFile(const std::string& filePath,const HttpRe
resp->setStatusCode(HttpResponse::k200Ok);
LOG_INFO << "file len:" << str.length();
resp->setBody(str);
delete contents;
delete [] contents;
}
std::string HttpAppFrameworkImpl::getUuid()

View File

@ -187,7 +187,7 @@ namespace drogon
headers_[field] = value;
transform(field.begin(), field.end(), field.begin(), ::tolower);
if(field == "cookie") {
LOG_INFO<<"cookies!!!:"<<value;
LOG_TRACE<<"cookies!!!:"<<value;
std::string::size_type pos;
while((pos = value.find(";")) != std::string::npos) {
std::string coo = value.substr(0, pos);

View File

@ -25,17 +25,17 @@
using namespace trantor;
using namespace drogon;
HttpResponse* HttpResponse::newHttpResponse()
HttpResponsePtr HttpResponse::newHttpResponse()
{
auto res = new HttpResponseImpl;
auto res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(HttpResponse::k200Ok);
res->setContentTypeCode(CT_TEXT_HTML);
return res;
}
HttpResponse* HttpResponse::newHttpJsonResponse(const Json::Value &data)
HttpResponsePtr HttpResponse::newHttpJsonResponse(const Json::Value &data)
{
auto res=new HttpResponseImpl;
auto res=std::make_shared<HttpResponseImpl>();
res->setStatusCode(HttpResponse::k200Ok);
res->setContentTypeCode(CT_APPLICATION_JSON);
Json::StreamWriterBuilder builder;
@ -44,7 +44,7 @@ HttpResponse* HttpResponse::newHttpJsonResponse(const Json::Value &data)
res->setBody(writeString(builder, data));
return res;
}
HttpResponse* HttpResponse::notFoundResponse()
HttpResponsePtr HttpResponse::notFoundResponse()
{
HttpViewData data;
data.insert("version",getVersion());
@ -54,15 +54,15 @@ HttpResponse* HttpResponse::notFoundResponse()
return res;
}
HttpResponse* HttpResponse::locationRedirectResponse(std::string path)
HttpResponsePtr HttpResponse::locationRedirectResponse(std::string path)
{
auto res=new HttpResponseImpl;
auto res=std::make_shared<HttpResponseImpl>();
res->setStatusCode(HttpResponse::k302Found);
res->redirect(path.c_str());
return res;
}
HttpResponse* HttpResponse::newHttpViewResponse(const std::string &viewName,const HttpViewData& data)
HttpResponsePtr HttpResponse::newHttpViewResponse(const std::string &viewName,const HttpViewData& data)
{
return HttpViewBase::genHttpResponse(viewName,data);
}

View File

@ -9,7 +9,7 @@
#include <trantor/utils/Logger.h>
#include <memory>
using namespace drogon;
HttpResponse* HttpViewBase::genHttpResponse(std::string viewName,const HttpViewData &data)
HttpResponsePtr HttpViewBase::genHttpResponse(std::string viewName,const HttpViewData &data)
{
LOG_TRACE<<"http view name="<<viewName;
auto pos=viewName.find(".csp");

View File

@ -6,7 +6,7 @@
#include <vector>
#include <set>
using namespace std;
HttpResponse* NotFound::genHttpResponse(const HttpViewData& NotFound_view_data)
HttpResponsePtr NotFound::genHttpResponse(const HttpViewData& NotFound_view_data)
{
std::stringstream NotFound_tmp_stream;
NotFound_tmp_stream << "<html>\n";