@ -176,8 +176,9 @@ static int accessHandlerCallback(void *cls,
/* Prepare the variables */
struct MHD_Response * response ;
string content = " " ;
string mimeType = " " ;
std : : string content ;
std : : string mimeType ;
std : : string httpRedirection ;
unsigned int contentLength = 0 ;
bool found = true ;
int httpResponseCode = MHD_HTTP_OK ;
@ -202,6 +203,10 @@ static int accessHandlerCallback(void *cls,
searchers . find ( humanReadableBookId ) - > second : NULL ;
kiwix : : Reader * reader = readers . find ( humanReadableBookId ) ! = readers . end ( ) ?
readers . find ( humanReadableBookId ) - > second : NULL ;
if ( reader = = NULL ) {
humanReadableBookId = " " ;
}
pthread_mutex_unlock ( & mapLock ) ;
/* Get suggestions */
@ -224,7 +229,6 @@ static int accessHandlerCallback(void *cls,
}
content + = ( content = = " [ " ? " " : " , " ) ;
content + = " { \" value \" : \" " + std : : string ( term ) + " \" , \" label \" : \" containing ' " + std : : string ( term ) + " '... \" }] " ;
std : : cout < < content < < std : : endl ;
mimeType = " text/x-json; charset=utf-8 " ;
}
@ -234,8 +238,26 @@ static int accessHandlerCallback(void *cls,
}
/* Display the search restults */
else if ( ! strcmp ( url , " /search " ) & & searcher ! = NULL ) {
else if ( ! strcmp ( url , " /search " ) ) {
/* Retrieve the pattern to search */
const char * pattern = MHD_lookup_connection_value ( connection , MHD_GET_ARGUMENT_KIND , " pattern " ) ;
if ( pattern = = NULL )
pattern = " " ;
/* Try first to load directly the article if exactly matching the pattern */
std : : string patternCorrespondingUrl ;
if ( reader ! = NULL ) {
pthread_mutex_lock ( & readerLock ) ;
reader - > getPageUrlFromTitle ( pattern , patternCorrespondingUrl ) ;
pthread_mutex_unlock ( & readerLock ) ;
if ( ! patternCorrespondingUrl . empty ( ) ) {
std : : cout < < " Search url: " < < patternCorrespondingUrl < < std : : endl ;
httpRedirection = " / " + humanReadableBookId + " / " + patternCorrespondingUrl ;
}
}
/* Make the search */
if ( patternCorrespondingUrl . empty ( ) & & searcher ! = NULL ) {
const char * start = MHD_lookup_connection_value ( connection , MHD_GET_ARGUMENT_KIND , " start " ) ;
const char * end = MHD_lookup_connection_value ( connection , MHD_GET_ARGUMENT_KIND , " end " ) ;
unsigned int startNumber = 0 ;
@ -247,9 +269,6 @@ static int accessHandlerCallback(void *cls,
if ( end ! = NULL )
endNumber = atoi ( end ) ;
if ( pattern = = NULL )
pattern = " " ;
/* Get the results */
pthread_mutex_lock ( & searcherLock ) ;
try {
@ -262,6 +281,11 @@ static int accessHandlerCallback(void *cls,
pthread_mutex_unlock ( & searcherLock ) ;
mimeType = " text/html; charset=utf-8 " ;
} else {
content = " <html><head><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b> \" " + string ( pattern ) + " \" </b> and the fulltext search engine is not available for this content.</p></body></html> " ;
mimeType = " text/html " ;
httpResponseCode = MHD_HTTP_NOT_FOUND ;
}
}
/* Display the content of a ZIM article */
@ -335,6 +359,11 @@ static int accessHandlerCallback(void *cls,
MHD_NO ,
MHD_YES ) ;
/* Make a redirection if necessary otherwise send the content */
if ( ! httpRedirection . empty ( ) ) {
MHD_add_response_header ( response , " Location " , httpRedirection . c_str ( ) ) ;
httpResponseCode = MHD_HTTP_FOUND ;
} else {
/* Add if necessary the content-encoding */
if ( acceptEncodingDeflate & & mimeType . find ( " text/html " ) ! = string : : npos ) {
MHD_add_response_header ( response , " Content-encoding " , " deflate " ) ;
@ -342,6 +371,7 @@ static int accessHandlerCallback(void *cls,
/* Specify the mime type */
MHD_add_response_header ( response , " Content-Type " , mimeType . c_str ( ) ) ;
}
/* clear context pointer */
* ptr = NULL ;