Cleaned up InternalServer::handle_suggest()

As a result of this clean-up the /suggest endpoint too stopped
generating confusing 404 Not Found errors (which, like in /meta's case
is not that important). Another functional change is that the "term"
parameter became optional.
This commit is contained in:
Veloman Yunkan 2021-12-11 22:04:13 +04:00 committed by Matthieu Gautier
parent 20b5a2b971
commit 872ddd9cb3
2 changed files with 12 additions and 24 deletions

View File

@ -440,38 +440,26 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
printf("** running handle_suggest\n"); printf("** running handle_suggest\n");
} }
std::string content;
std::string mimeType;
std::string bookName; std::string bookName;
std::string bookId;
std::string queryString;
std::shared_ptr<zim::Archive> archive; std::shared_ptr<zim::Archive> archive;
try { try {
bookName = request.get_argument("content"); bookName = request.get_argument("content");
bookId = mp_nameMapper->getIdForName(bookName); const std::string bookId = mp_nameMapper->getIdForName(bookName);
queryString = request.get_argument("term");
archive = mp_library->getArchiveById(bookId); archive = mp_library->getArchiveById(bookId);
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
return Response::build_404(*this, "", bookName, ""); // error handled by the archive == nullptr check below
}
auto start = 0;
try {
start = request.get_argument<unsigned int>("start");
} catch (const std::exception&) {}
unsigned int count = 10;
try {
count = request.get_argument<unsigned int>("count");
} catch (const std::exception&) {}
if (count == 0) {
count = 10;
} }
if (archive == nullptr) { if (archive == nullptr) {
return Response::build_404(*this, "", bookName, ""); const std::string error_details = "No such book: " + bookName;
return Response::build_404(*this, "", bookName, "", error_details);
}
const auto queryString = request.get_optional_param("term", std::string());
const auto start = request.get_optional_param<unsigned int>("start", 0);
unsigned int count = request.get_optional_param<unsigned int>("count", 10);
if (count == 0) {
count = 10;
} }
if (m_verbose.load()) { if (m_verbose.load()) {

View File

@ -179,6 +179,7 @@ const ResourceCollection resources200Compressible{
{ NO_ETAG, "/search?content=zimfile&pattern=a" }, { NO_ETAG, "/search?content=zimfile&pattern=a" },
{ NO_ETAG, "/suggest?content=zimfile" },
{ NO_ETAG, "/suggest?content=zimfile&term=ray" }, { NO_ETAG, "/suggest?content=zimfile&term=ray" },
{ NO_ETAG, "/catch/external?source=www.example.com" }, { NO_ETAG, "/catch/external?source=www.example.com" },
@ -282,7 +283,6 @@ const char* urls404[] = {
"/random?content=non-existent-book", "/random?content=non-existent-book",
"/search", "/search",
"/suggest", "/suggest",
"/suggest?content=zimfile",
"/suggest?content=non-existent-book&term=abcd", "/suggest?content=non-existent-book&term=abcd",
"/catch/external", "/catch/external",
"/zimfile/A/non-existent-article", "/zimfile/A/non-existent-article",