@@ -543,53 +543,54 @@ drogon::HttpResponsePtr errorHttpResponse(const std::string &error,
543
543
return resp;
544
544
}
545
545
546
- DrogonHttp::DrogonHttp (const drogon::HttpRequestPtr &req,
547
- DrogonCallback callback) {
548
- this ->req_ = req;
549
- this ->callback_ = std::move (callback);
550
- this ->type_ = InterfaceType::kDrogonHttp ;
551
- this ->json_ = nullptr ;
552
- }
553
-
554
- void DrogonHttp::setJson () {
546
+ std::shared_ptr<Json::Value> parseJson (const drogon::HttpRequest *req) {
555
547
#ifdef PROTEUS_ENABLE_LOGGING
556
- const auto & logger = this -> getLogger () ;
548
+ Logger logger{Loggers:: kServer } ;
557
549
#endif
558
- const auto &json_raw = this ->req_ ->getJsonObject ();
559
-
560
- // if we fail to get the JSON object, return
561
- if (json_raw == nullptr ) {
562
- auto root = std::make_shared<Json::Value>();
563
- std::string errors;
564
- Json::CharReaderBuilder builder;
565
- Json::CharReader *reader = builder.newCharReader ();
566
- auto body = this ->req_ ->getBody ();
567
- auto body_parsed = z_decompress (body.data (), body.length ());
568
- if (body_parsed.empty ()) {
569
- this ->callback_ (errorHttpResponse (" Failed attempt to inflate request" ,
570
- HttpStatusCode::k400BadRequest));
571
- return ;
572
- }
573
- bool parsingSuccessful =
574
- reader->parse (body_parsed.data (), body_parsed.data () + body_parsed.size (),
575
- root.get (), &errors);
576
- if (!parsingSuccessful) {
577
- this ->callback_ (errorHttpResponse (" Failed to parse JSON request" ,
578
- HttpStatusCode::k400BadRequest));
579
- return ;
580
- }
581
- PROTEUS_LOG_INFO (logger, " Successfully inflated request" );
582
- this ->json_ = std::move (root);
583
- } else {
584
- this ->json_ = json_raw;
550
+
551
+ // attempt to get the JSON object directly first
552
+ const auto &json_obj = req->getJsonObject ();
553
+ if (json_obj != nullptr ) {
554
+ return json_obj;
585
555
}
586
- }
587
556
588
- size_t DrogonHttp::getInputSize () {
589
- if (this ->json_ == nullptr ) {
590
- this ->setJson ();
557
+ PROTEUS_LOG_DEBUG (logger, " Failed to get JSON data directly" );
558
+
559
+ // if it's not valid, then we need to attempt to parse the body
560
+ auto root = std::make_shared<Json::Value>();
561
+
562
+ std::string errors;
563
+ Json::CharReaderBuilder builder;
564
+ Json::CharReader *reader = builder.newCharReader ();
565
+ auto body = req->getBody ();
566
+ bool success =
567
+ reader->parse (body.data (), body.data () + body.size (), root.get (), &errors);
568
+ if (success) {
569
+ return root;
570
+ }
571
+
572
+ PROTEUS_LOG_DEBUG (logger, " Failed to interpret body as JSON data" );
573
+
574
+ // if it's still not valid, attempt to uncompress the body and convert to JSON
575
+ auto body_decompress = z_decompress (body.data (), body.length ());
576
+ success = reader->parse (body_decompress.data (),
577
+ body_decompress.data () + body_decompress.size (),
578
+ root.get (), &errors);
579
+ if (success) {
580
+ return root;
591
581
}
592
582
583
+ throw std::invalid_argument (" Failed to interpret request body as JSON" );
584
+ }
585
+
586
+ DrogonHttp::DrogonHttp (const drogon::HttpRequestPtr &req,
587
+ DrogonCallback callback)
588
+ : req_(req), callback_(std::move(callback)) {
589
+ this ->type_ = InterfaceType::kDrogonHttp ;
590
+ this ->json_ = parseJson (req.get ());
591
+ }
592
+
593
+ size_t DrogonHttp::getInputSize () {
593
594
if (!this ->json_ ->isMember (" inputs" )) {
594
595
throw std::invalid_argument (" No 'inputs' key present in request" );
595
596
}
0 commit comments