#include "ReportCommand.h" #include "Reports.h" #include "InputOutput.h" #include "WebServer.h" using namespace std; const char *ReportCommand::newRequest(const char *command, InputOutput *io, const char *query) { Report *r = 0; char *fBuf = 0; //---------------------------------------------- command switch if (!_STRCMP(command, "Shutdown")) { //shutdown the webserver io->sendHandshake(WebServer::html); io->send("shutting down..."); io->shutdown(); //shutdown the IO io->send("done."); } else if (!_STRCMP(command, "Stop")) { //stop Spider(s) io->sendHandshake(WebServer::html); io->send("stopping spider(s)..."); m_sm->stop(); io->send("done."); } else if (!_STRCMP(command, "Start")) { //start Spider(s) io->sendHandshake(WebServer::html); io->send("starting spider(s)..."); m_sm->run(); io->send("done."); } //---------------------------------------------- reports //format = Report_ //special command asking for a report - xml with an xslt directive //the command is equal to the C++ class name and also the xsl filename //thus, for Report_Full the Report_Full class will be used to create the xml //and the http xml response will include a directive to use Report_Full.xsl //note that Reports can be parameterised, thus the use of classes rather than //many functions else if (!_STRCMP(command, "")) r = new Report_Full(m_sm); //default report (Report_Full): custom un-registered xsl files can be used here else if (!_STRCMP(command, "Report_Full")) r = new Report_Full(m_sm); else if (!_STRCMP(command, "Report_Summary")) r = new Report_Summary(m_sm); else if (!_STRCMP(command, "Report_Index")) r = new Report_Index(m_sm); else if (!_STRCMP(command, "Report_DomainSummary")) { char *idStart, *idEnd, *pageIdsStart; //?domainId=&pageIds=x1,x2,x3,x4 //page ids vector pageIds; if (pageIdsStart = (char*)strstr(command+30, "&pageIds=")) { *pageIdsStart = 0; idStart = pageIdsStart + 9; while (idEnd = strchr(idStart, ',')) { *idEnd = 0; pageIds.push_back(atoi(idStart)); idStart = idEnd + 1; } if (*idStart) pageIds.push_back(atoi(idStart)); } //domain id int domainId = atoi(command+30); r = new Report_DomainSummary(m_sm, domainId, &pageIds); } else { //serve an xsl file (just the XSL handshake) io->sendHandshake(WebServer::xsl); FILE *f = fopen(command, "r"); if (f) { //filesize fseek(f, 0, SEEK_END); long lSize = ftell(f); rewind(f); //read fBuf = (char*)mallocCheck((size_t)lSize+1); size_t numRead = fread(fBuf, 1, lSize, f); if (numRead) { fBuf[numRead] = 0; //zero terminate buffer io->send(fBuf); } free(fBuf); fclose(f); //close } } //send any generated reports (all XML with an XSLT) if (r) { io->sendHandshake(WebServer::xml, command+7); //send headers (xml handshake, xml decleration and XSLT directive) io->send(r->xml()); //xml is static malloc Report space, not copied, no need to free delete r; //destructor does nothing } return 0; }