Skip to content

Commit 4648686

Browse files
fxdupontRazvan Becheriu
authored and
Razvan Becheriu
committed
[#3490] Use a parser on response
1 parent 6244dd4 commit 4648686

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

src/lib/http/testutils/test_http_client.h

+48-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cc/data.h>
1111
#include <http/client.h>
1212
#include <http/http_types.h>
13+
#include <http/response_parser.h>
1314

1415
#include <boost/asio/read.hpp>
1516
#include <boost/asio/buffer.hpp>
@@ -18,6 +19,7 @@
1819

1920
using namespace boost::asio::ip;
2021
using namespace isc::asiolink;
22+
using namespace isc::http;
2123

2224
/// @brief Common base for test HTTP/HTTPS clients.
2325
class BaseTestHttpClient : public boost::noncopyable {
@@ -210,12 +212,29 @@ class TestHttpClient : public BaseTestHttpClient {
210212
buf_.data() + bytes_transferred);
211213
}
212214

213-
// Two consecutive new lines end headers, " } ]" sequence
214-
// end large response we're expecting.
215-
if ((response_.find("\r\n\r\n", 0) != std::string::npos) &&
216-
((response_.size() < 1000) ||
217-
(response_.rfind(" } ]") == (response_.size() - 4)))) {
218-
receive_done_ = true;
215+
// Two consecutive new lines end the part of the response we're
216+
// expecting.
217+
bool need_data(true);
218+
if (response_.find("\r\n\r\n", 0) != std::string::npos) {
219+
// Try to parse the response.
220+
try {
221+
HttpResponse hr;
222+
HttpResponseParser parser(hr);
223+
parser.initModel();
224+
parser.postBuffer(&response_[0], response_.size());
225+
parser.poll();
226+
if (!parser.needData()) {
227+
need_data = false;
228+
if (parser.httpParseOk()) {
229+
receive_done_ = true;
230+
}
231+
}
232+
} catch (const std::exception& ex) {
233+
need_data = false;
234+
ADD_FAILURE() << "error parsing response: " << ex.what();
235+
}
236+
}
237+
if (!need_data) {
219238
io_service_->stop();
220239
} else {
221240
receivePartialResponse();
@@ -474,12 +493,29 @@ class TestHttpsClient : public BaseTestHttpClient {
474493
buf_.data() + bytes_transferred);
475494
}
476495

477-
// Two consecutive new lines end headers, " } ]" sequence
478-
// end large response we're expecting.
479-
if ((response_.find("\r\n\r\n", 0) != std::string::npos) &&
480-
((response_.size() < 1000) ||
481-
(response_.rfind(" } ]") == (response_.size() - 4)))) {
482-
receive_done_ = true;
496+
// Two consecutive new lines end the part of the response we're
497+
// expecting.
498+
bool need_data(true);
499+
if (response_.find("\r\n\r\n", 0) != std::string::npos) {
500+
// Try to parse the response.
501+
try {
502+
HttpResponse hr;
503+
HttpResponseParser parser(hr);
504+
parser.initModel();
505+
parser.postBuffer(&response_[0], response_.size());
506+
parser.poll();
507+
if (!parser.needData()) {
508+
need_data = false;
509+
if (parser.httpParseOk()) {
510+
receive_done_ = true;
511+
}
512+
}
513+
} catch (const std::exception& ex) {
514+
need_data = false;
515+
ADD_FAILURE() << "error parsing response: " << ex.what();
516+
}
517+
}
518+
if (!need_data) {
483519
io_service_->stop();
484520
} else {
485521
receivePartialResponse();

0 commit comments

Comments
 (0)