Skip to content

Commit

Permalink
Add the getJsonError method (drogonframework#507)
Browse files Browse the repository at this point in the history
* Add the getJsonError method to the HttpRequest and the HttpResponse classes
  • Loading branch information
an-tao authored Jul 9, 2020
1 parent 9d3efea commit c864070
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 14 deletions.
16 changes: 13 additions & 3 deletions lib/inc/drogon/HttpRequest.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* HttpRequest.h
* @file HttpRequest.h
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
Expand Down Expand Up @@ -280,14 +280,24 @@ class HttpRequest
* string (the part after the question mark in the URI) must be empty,
* otherwise the method returns an empty shared_ptr object.
*/
virtual const std::shared_ptr<Json::Value> jsonObject() const = 0;
virtual const std::shared_ptr<Json::Value> &jsonObject() const = 0;

/// Get the Json object of the request
const std::shared_ptr<Json::Value> getJsonObject() const
const std::shared_ptr<Json::Value> &getJsonObject() const
{
return jsonObject();
}

/**
* @brief Get the error message of parsing the JSON body received from peer.
* This method usually is called after getting a empty shared_ptr object
* by the getJsonObject() method.
*
* @return const std::string& The error message. An empty string is returned
* when no error occurs.
*/
virtual const std::string &getJsonError() const = 0;

/// Get the content type
virtual ContentType contentType() const = 0;
ContentType getContentType() const
Expand Down
14 changes: 12 additions & 2 deletions lib/inc/drogon/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,22 @@ class HttpResponse
/// Get the json object from the server response.
/// If the response is not in json format, then a empty shared_ptr is
/// retured.
virtual const std::shared_ptr<Json::Value> jsonObject() const = 0;
const std::shared_ptr<Json::Value> getJsonObject() const
virtual const std::shared_ptr<Json::Value> &jsonObject() const = 0;
const std::shared_ptr<Json::Value> &getJsonObject() const
{
return jsonObject();
}

/**
* @brief Get the error message of parsing the JSON body received from peer.
* This method usually is called after getting a empty shared_ptr object
* by the getJsonObject() method.
*
* @return const std::string& The error message. An empty string is returned
* when no error occurs.
*/
virtual const std::string &getJsonError() const = 0;

/**
* @brief Set the reponse object to the pass-through mode or not. It's not
* by default when a new response object is created.
Expand Down
15 changes: 12 additions & 3 deletions lib/src/HttpRequestImpl.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* HttpRequestImpl.cc
* @file HttpRequestImpl.cc
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
Expand Down Expand Up @@ -46,11 +46,19 @@ void HttpRequestImpl::parseJson() const
{
LOG_ERROR << errs;
jsonPtr_.reset();
jsonParsingErrorPtr_ =
std::make_unique<std::string>(std::move(errs));
}
else
{
jsonParsingErrorPtr_.reset();
}
}
else
{
jsonPtr_.reset();
jsonParsingErrorPtr_ =
std::make_unique<std::string>("content type error");
}
}
void HttpRequestImpl::parseParameters() const
Expand Down Expand Up @@ -412,7 +420,7 @@ void HttpRequestImpl::addHeader(const char *start,
case 6:
if (field == "expect")
{
expect_ = value;
expectPtr_ = std::make_unique<std::string>(value);
}
break;
case 10:
Expand Down Expand Up @@ -506,12 +514,13 @@ void HttpRequestImpl::swap(HttpRequestImpl &that) noexcept
swap(local_, that.local_);
swap(creationDate_, that.creationDate_);
swap(content_, that.content_);
swap(expect_, that.expect_);
swap(expectPtr_, that.expectPtr_);
swap(contentType_, that.contentType_);
swap(contentTypeString_, that.contentTypeString_);
swap(keepAlive_, that.keepAlive_);
swap(loop_, that.loop_);
swap(flagForParsingContentType_, that.flagForParsingContentType_);
swap(jsonParsingErrorPtr_, that.jsonParsingErrorPtr_);
}

const char *HttpRequestImpl::methodString() const
Expand Down
22 changes: 17 additions & 5 deletions lib/src/HttpRequestImpl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* HttpRequestImpl.h
* @file HttpRequestImpl.h
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
Expand Down Expand Up @@ -58,12 +58,13 @@ class HttpRequestImpl : public HttpRequest
sessionPtr_.reset();
attributesPtr_.reset();
cacheFilePtr_.reset();
expect_.clear();
expectPtr_.reset();
content_.clear();
contentType_ = CT_TEXT_PLAIN;
flagForParsingContentType_ = false;
contentTypeString_.clear();
keepAlive_ = true;
jsonParsingErrorPtr_.reset();
}
trantor::EventLoop *getLoop()
{
Expand Down Expand Up @@ -351,7 +352,7 @@ class HttpRequestImpl : public HttpRequest
return attributesPtr_;
}

virtual const std::shared_ptr<Json::Value> jsonObject() const override
virtual const std::shared_ptr<Json::Value> &jsonObject() const override
{
// Not multi-thread safe but good, because we basically call this
// function in a single thread
Expand Down Expand Up @@ -427,7 +428,10 @@ class HttpRequestImpl : public HttpRequest
}
const std::string &expect() const
{
return expect_;
const static std::string none{""};
if (expectPtr_)
return *expectPtr_;
return none;
}
bool keepAlive() const
{
Expand All @@ -437,6 +441,13 @@ class HttpRequestImpl : public HttpRequest
{
return isOnSecureConnection_;
}
virtual const std::string &getJsonError() const override
{
const static std::string none{""};
if (jsonParsingErrorPtr_)
return *jsonParsingErrorPtr_;
return none;
}

~HttpRequestImpl();

Expand Down Expand Up @@ -482,7 +493,8 @@ class HttpRequestImpl : public HttpRequest
trantor::InetAddress local_;
trantor::Date creationDate_;
std::unique_ptr<CacheFile> cacheFilePtr_;
std::string expect_;
mutable std::unique_ptr<std::string> jsonParsingErrorPtr_;
std::unique_ptr<std::string> expectPtr_;
bool keepAlive_{true};
bool isOnSecureConnection_{false};
bool passThrough_{false};
Expand Down
10 changes: 10 additions & 0 deletions lib/src/HttpResponseImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ void HttpResponseImpl::swap(HttpResponseImpl &that) noexcept
fullHeaderString_.swap(that.fullHeaderString_);
httpString_.swap(that.httpString_);
swap(datePos_, that.datePos_);
swap(jsonParsingErrorPtr_, that.jsonParsingErrorPtr_);
}

void HttpResponseImpl::clear()
Expand All @@ -599,6 +600,7 @@ void HttpResponseImpl::clear()
version_ = Version::kHttp11;
statusMessage_ = string_view{};
fullHeaderString_.reset();
jsonParsingErrorPtr_.reset();
sendfileName_.clear();
headers_.clear();
cookies_.clear();
Expand Down Expand Up @@ -628,11 +630,19 @@ void HttpResponseImpl::parseJson() const
LOG_ERROR << errs;
LOG_ERROR << "body: " << bodyPtr_->getString();
jsonPtr_.reset();
jsonParsingErrorPtr_ =
std::make_shared<std::string>(std::move(errs));
}
else
{
jsonParsingErrorPtr_.reset();
}
}
else
{
jsonPtr_.reset();
jsonParsingErrorPtr_ =
std::make_shared<std::string>("empty response body");
}
}

Expand Down
10 changes: 9 additions & 1 deletion lib/src/HttpResponseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class HttpResponseImpl : public HttpResponse

void swap(HttpResponseImpl &that) noexcept;
void parseJson() const;
virtual const std::shared_ptr<Json::Value> jsonObject() const override
virtual const std::shared_ptr<Json::Value> &jsonObject() const override
{
// Not multi-thread safe but good, because we basically call this
// function in a single thread
Expand All @@ -302,6 +302,13 @@ class HttpResponseImpl : public HttpResponse
}
return jsonPtr_;
}
virtual const std::string &getJsonError() const override
{
const static std::string none{""};
if (jsonParsingErrorPtr_)
return *jsonParsingErrorPtr_;
return none;
}
void setJsonObject(const Json::Value &pJson)
{
flagForParsingJson_ = true;
Expand Down Expand Up @@ -396,6 +403,7 @@ class HttpResponseImpl : public HttpResponse
mutable bool flagForSerializingJson_{true};
mutable ContentType contentType_{CT_TEXT_PLAIN};
mutable bool flagForParsingContentType_{false};
mutable std::shared_ptr<std::string> jsonParsingErrorPtr_;
string_view contentTypeString_{
"Content-Type: text/html; charset=utf-8\r\n"};
bool passThrough_{false};
Expand Down

0 comments on commit c864070

Please sign in to comment.