open-source-search-engine/PageDirectory.cpp

155 lines
4.6 KiB
C++
Raw Normal View History

2013-08-03 00:12:24 +04:00
#include "gb-include.h"
//#include "CollectionRec.h"
2013-08-03 00:12:24 +04:00
#include "Pages.h"
#include "Categories.h"
#include "PageResults.h" // printDMOZSubtopics()
2013-08-03 00:12:24 +04:00
// function is in PageRoot.cpp:
bool printDirHomePage ( SafeBuf &sb , HttpRequest *r ) ;
2013-08-03 00:12:24 +04:00
// . returns false if blocked, true otherwise
// . sets g_errno on error
bool sendPageDirectory ( TcpSocket *s , HttpRequest *r ) {
// get the collection
long collLen = 0;
char *coll = r->getString("c", &collLen, NULL);
if (!coll || collLen <= 0) {
coll = g_conf.m_dirColl;
collLen = gbstrlen(coll);
}
// get the category
char *path = r->getPath();
long pathLen = r->getPathLen();
// get the category id from the path
char decodedPath[MAX_HTTP_FILENAME_LEN+2];
// do not breach the buffer
if ( pathLen > MAX_HTTP_FILENAME_LEN ) pathLen = MAX_HTTP_FILENAME_LEN;
long decodedPathLen = urlDecode(decodedPath, path, pathLen);
decodedPath[decodedPathLen] = '\0';
// sanity check
if ( decodedPathLen > MAX_HTTP_FILENAME_LEN ) { char*xx=NULL;*xx=0;}
// remove cgi
long cgiPos = 0;
long cgiLen = 0;
for (long i = 0; i < decodedPathLen; i++) {
if (decodedPath[i] == '?') {
cgiPos = i+1;
cgiLen = decodedPathLen - cgiPos;
decodedPathLen = i;
break;
}
}
2013-10-14 01:24:41 +04:00
// look it up. returns catId <= 0 if dmoz not setup yet.
2013-08-03 00:12:24 +04:00
long catId = g_categories->getIdFromPath(decodedPath, decodedPathLen);
SafeBuf sb;
long xml = r->getLong("xml",0);
// if /Top print the directory homepage
2013-10-14 01:24:41 +04:00
if ( catId == 1 || catId <= 0 ) {
// this is in PageRoot.cpp
2014-07-06 09:20:15 +04:00
if ( ! printDirHomePage(sb,r) )
// this will be an error if dmoz not set up and
// it and xml or json reply format requested
return g_httpServer.sendErrorReply(s,500,
mstrerror(g_errno));
}
//
// try printing this shit out not as search results right now
// but just verbatim from dmoz files
//
2013-10-14 02:45:12 +04:00
else {
// search box
printLogoAndSearchBox(&sb,r,catId,NULL);
2013-10-14 03:06:38 +04:00
// radio buttons for search dmoz. no, this is printed
// from call to printLogoAndSearchBox()
//printDmozRadioButtons(sb,catId);
2013-10-14 02:45:12 +04:00
// the dmoz breadcrumb
printDMOZCrumb ( &sb,catId,xml);
2013-10-14 02:45:12 +04:00
// print the subtopcis in this topic. show as links above
// the search results
printDMOZSubTopics ( &sb, catId , xml );
2013-10-14 02:45:12 +04:00
// ok, for now just print the dmoz topics since our search
// results will be empty... until populated!
g_categories->printUrlsInTopic ( &sb , catId );
}
return g_httpServer.sendDynamicPage ( s,
(char*) sb.getBufStart(),
sb.length(),
// 120 seconds cachetime
// don't cache anymore
// since
// we have the login bar
// @ the top of the page
0,//120, // cachetime
false,// post?
"text/html",
200,
NULL, // cookie
"UTF-8",
r);
2013-08-03 00:12:24 +04:00
// . make a new request for PageResults
//Url dirUrl;
char requestBuf[1024+MAX_COLL_LEN+128];
long requestBufSize = 1024+MAX_COLL_LEN+128;
//g_categories.createDirectorySearchUrl ( &dirUrl,
log("dmoz: creating search request");
2013-08-03 00:12:24 +04:00
long requestBufLen = g_categories->createDirSearchRequest(
requestBuf,
requestBufSize,
catId,
r->getHost(),
r->getHostLen(),
coll,
collLen,
&decodedPath[cgiPos],
cgiLen,
true ,
r );
if ( requestBufLen > 1024+MAX_COLL_LEN+128 ) { char*xx=NULL;*xx=0; }
if ( requestBufLen == 0 ) {
g_errno = EBADREQUEST;
log ( "directory: Unable to generate request for Directory: %s",
decodedPath );
return g_httpServer.sendErrorReply(s,500, mstrerror(g_errno));
}
// set the Request
//if (!r->set(&dirUrl))
// return g_httpServer.sendErrorReply(s,500, mstrerror(g_errno));
// copy into s->m_readBuf, make sure there room
//if (r->m_bufLen+1 > s->m_readBufSize) {
if (requestBufLen+1 > s->m_readBufSize) {
char *reBuf = (char*)mrealloc ( s->m_readBuf,
s->m_readBufSize,
//r->m_bufLen+1,
requestBufLen+1,
"PageDirectory" );
if (!reBuf) {
log("directory: Could not reallocate %li bytes for"
//" m_readBuf", r->m_bufLen+1);
" m_readBuf", requestBufLen+1);
return g_httpServer.sendErrorReply(s,500,
mstrerror(g_errno));
}
s->m_readBuf = reBuf;
//s->m_readBufSize = r->m_bufLen+1;
s->m_readBufSize = requestBufLen+1;
}
//memcpy(s->m_readBuf, r->m_buf, r->m_bufLen);
//s->m_readBuf[r->m_bufLen] = '\0';
memcpy(s->m_readBuf, requestBuf, requestBufLen);
s->m_readBuf[requestBufLen] = '\0';
// create the new search request
//if (!r->set(s->m_readBuf, r->m_bufLen, s))
if (!r->set(s->m_readBuf, requestBufLen, s))
return g_httpServer.sendErrorReply(s,500, mstrerror(g_errno));
// send the new request to PageResults
return sendPageResults(s, r);
}