@@ -58,7 +58,6 @@ void mochios::helpers::client::buildResponse(mochios::messages::Response &res,
5858 res.set (key, value);
5959 }
6060
61- // Body
6261 std::stringstream body;
6362 while (std::getline (response, line) && line != " \r " ) {
6463 body << line;
@@ -68,48 +67,126 @@ void mochios::helpers::client::buildResponse(mochios::messages::Response &res,
6867 return ;
6968}
7069
70+ void mochios::helpers::client::buildDefaultResponse (
71+ mochios::messages::Response &res, int statusCode) {
72+ res.statusCode = statusCode;
73+ json::object responseBody;
74+
75+ switch (statusCode) {
76+ case 400 :
77+ responseBody[" error" ] = " Bad Request" ;
78+ responseBody[" message" ] =
79+ " The request could not be understood by the server." ;
80+ break ;
81+ case 404 :
82+ responseBody[" error" ] = " Not Found" ;
83+ responseBody[" message" ] = " The requested resource could not be found." ;
84+ break ;
85+ case 408 :
86+ responseBody[" error" ] = " Request Timeout" ;
87+ responseBody[" message" ] = " The server timed out waiting for the request." ;
88+ break ;
89+ case 500 :
90+ responseBody[" error" ] = " Internal Server Error" ;
91+ responseBody[" message" ] = " An unexpected error occurred on the server." ;
92+ break ;
93+ default :
94+ responseBody[" error" ] = " Unknown Error" ;
95+ responseBody[" message" ] = " An unknown error occurred." ;
96+ break ;
97+ }
98+
99+ res.body = responseBody.dumps (0 );
100+ res.set (" Content-Type" , " application/json" );
101+ res.set (" Connection" , " close" );
102+ res.set (" Content-Length" , std::to_string (res.body .size ()));
103+ return ;
104+ }
105+
71106mochios::messages::Response
72107mochios::helpers::client::send (mochios::messages::Request &request,
73108 const int &socket) {
74- std::pair<std::string, std::string> requestString =
75- mochios::helpers::client::buildRequest (request);
76- if (brewtils::sys::send (socket, requestString.first .c_str (),
77- requestString.first .size (), 0 ) < 0 ) {
78- logger::error (
79- " Error sending request headers" ,
80- " mochios::messages::Response "
81- " mochios::helpers::client::send(mochios::messages::Request &request, "
82- " const mochios::enums::method &method, const int &socket)" );
83- }
84- if (requestString.second .size () > 0 ) {
85- if (brewtils::sys::send (socket, requestString.second .c_str (),
86- requestString.second .size (), 0 ) < 0 ) {
87- logger::error (
88- " Error sending request body" ,
89- " mochios::messages::Response "
90- " mochios::helpers::client::send(mochios::messages::Request &request, "
91- " const mochios::enums::method &method, const int &socket)" );
109+ mochios::messages::Response res;
110+ bool socketClosed = false ;
111+ try {
112+ std::pair<std::string, std::string> requestString =
113+ mochios::helpers::client::buildRequest (request);
114+ if (brewtils::sys::send (socket, requestString.first .c_str (),
115+ requestString.first .size (), 0 ) < 0 ) {
116+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
117+ mochios::helpers::client::buildDefaultResponse (res, 408 );
118+ throw res;
119+ } else {
120+ logger::error (" Error sending request headers" ,
121+ " mochios::messages::Response "
122+ " mochios::helpers::client::send(mochios::messages::"
123+ " Request &request, const mochios::enums::method &method, "
124+ " const int &socket)" );
125+ }
126+ }
127+ if (requestString.second .size () > 0 ) {
128+ if (brewtils::sys::send (socket, requestString.second .c_str (),
129+ requestString.second .size (), 0 ) < 0 ) {
130+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
131+ mochios::helpers::client::buildDefaultResponse (res, 408 );
132+ throw res;
133+ } else {
134+ if (brewtils::sys::send (socket, requestString.second .c_str (),
135+ requestString.second .size (), 0 ) < 0 ) {
136+ logger::error (" Error sending request body" ,
137+ " mochios::messages::Response "
138+ " mochios::helpers::client::send(mochios::messages::"
139+ " Request &request, const mochios::enums::method "
140+ " &method, const int &socket)" );
141+ }
142+ }
143+ }
92144 }
93- }
94145
95- std::stringstream oss;
96- char buffer[4096 ];
97- int bytesRead;
98- while ((bytesRead = brewtils::sys::recv (socket, buffer, 4096 , 0 )) > 0 ) {
99- oss << buffer;
100- memset (buffer, 0 , 4096 );
101- }
102- close (socket);
146+ std::stringstream oss;
147+ char buffer[4096 ];
148+ int bytesRead;
149+ while ((bytesRead = brewtils::sys::recv (socket, buffer, 4096 , 0 )) > 0 ) {
150+ oss.write (buffer, bytesRead);
151+ memset (buffer, 0 , 4096 );
152+ }
153+ close (socket);
154+ socketClosed = true ;
103155
104- mochios::messages::Response res;
105- std::string line;
106- std::getline (oss, line);
107- std::vector<std::string> parts = brewtils::string::split (line, " " );
108- res.statusCode = std::stoi (parts[1 ]);
109- if (parts.size () > 2 ) {
110- res.statusText = parts[2 ];
156+ if (bytesRead < 0 ) {
157+ if (oss.str ().empty () || errno == EAGAIN || errno == EWOULDBLOCK) {
158+ mochios::helpers::client::buildDefaultResponse (res, 408 );
159+ throw res;
160+ } else {
161+ logger::error (" Receive failed: " + std::string (strerror (errno)),
162+ " mochios::messages::Response "
163+ " mochios::helpers::client::send(mochios::messages::"
164+ " Request &request, const mochios::enums::method &method, "
165+ " const int &socket)" );
166+ }
167+ }
168+
169+ std::string line;
170+ std::getline (oss, line);
171+ std::vector<std::string> parts = brewtils::string::split (line, " " );
172+ res.statusCode = std::stoi (brewtils::string::trim (parts[1 ]));
173+ if (parts.size () > 2 ) {
174+ res.statusText = brewtils::string::trim (parts[2 ]);
175+ }
176+ mochios::helpers::client::buildResponse (res, oss);
177+ } catch (const mochios::messages::Response &e) {
178+ } catch (const std::exception &e) {
179+ logger::error (" Exception occurred: " + std::string (e.what ()));
180+ if (!socketClosed) {
181+ close (socket);
182+ socketClosed = true ;
183+ }
184+ mochios::helpers::client::buildDefaultResponse (res, 500 );
111185 }
112186
113- mochios::helpers::client::buildResponse (res, oss);
114- return res;
187+ if (res.statusCode >= 200 && res.statusCode < 300 ) {
188+ return res;
189+ } else {
190+ throw res;
191+ }
115192}
0 commit comments