int main() { #ifdef PROFILE int i; #endif const char *config_url, *username, *password, *config_options; strtbl *options = NULL; int exit_status = 0; // install SIGUSR1, SIGPIPE, SIGTERM handler signal(SIGTERM, sig_handler); signal(SIGUSR1, sig_handler); signal(SIGPIPE, sig_handler); /* Provide a hook via an environment variable to define the config URL */ config_url = getenv(WO_CONFIG_URL); if (!config_url) { /* Flat file URL */ /* config_url = "file:///Local/Library/WebObjects/Configuration/WOConfig.xml"; */ /* Local wotaskd */ /* config_url = "http://localhost:1085"; */ /* Multicast URL */ config_url = CONFIG_URL; /* Actually "webobjects://239.128.14.2:1085"; */ } WOLog(WO_INFO,"<FastCGI> config url is %s", config_url); options = st_new(8); st_add(options, WOCONFIG, config_url, 0); /* * If your webserver is configured to pass these environment variables, we use them to * protect WOAdaptorInfo output. */ username = getenv(WO_ADAPTOR_INFO_USERNAME); if (username && strlen(username) != 0) { st_add(options, WOUSERNAME, username, 0); password = getenv(WO_ADAPTOR_INFO_PASSWORD); if(password && strlen(password) != 0) { st_add(options, WOPASSWORD, password, 0); } } config_options = getenv(WO_CONFIG_OPTIONS); if (config_options) st_add(options, WOOPTIONS, config_options, 0); /* * SECURITY ALERT * * To disable WOAdaptorInfo, uncomment the next line. * st_add(options, WOUSERNAME, "disabled", 0); * * To specify an WOAdaptorInfo username and password, uncomment the next two lines. * st_add(options, WOUSERNAME, "joe", 0); * st_add(options, WOPASSWORD, "secret", 0); * */ if (init_adaptor(options)) { WOLog( WO_ERR, "<FastCGI> Adaptor initialization failed."); exit(-1); } WOLog( WO_INFO,"<FastCGI> process started" ); while (!should_terminate) { HTTPRequest *req; HTTPResponse *resp = NULL; WOURLComponents wc = WOURLComponents_Initializer; const char *qs; unsigned int qs_len; char *url; const char *script_name, *path_info; const char *reqerr; WOURLError urlerr; FCGX_ParamArray hdrp_org; exit_status = FCGX_Accept(&in, &out, &err, &hdrp ); if ( exit_status < 0 ) { break; } #ifdef PROFILE for (i=0; i < 50000; i++) { #endif WOLog( WO_INFO,"<FastCGI> request accepted" ); #ifdef WIN32 _setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stdin), _O_BINAR1Y); #endif script_name = FCGX_GetParam( CGI_SCRIPT_NAME, hdrp); path_info = FCGX_GetParam( CGI_PATH_INFO, hdrp); WOLog( WO_INFO,"<FastCGI> CGI_SCRIPT_NAME = %s", script_name ); WOLog( WO_INFO,"<FastCGI> CGI_PATH_INFO = %s", path_info ); if (script_name == NULL) { prepareAndSendErrorResponse(INV_SCRIPT, HTTP_NOT_FOUND); break; } else if (path_info == NULL) { path_info = "/"; } /* * extract WebObjects application name from URI */ url = WOMALLOC(strlen(path_info) + strlen(script_name) + 1); strcpy(url, script_name); strcat(url, path_info); WOLog(WO_INFO,"<FastCGI> new request: %s",url); urlerr = WOParseApplicationName(&wc, url); if (urlerr != WOURLOK) { const char *_urlerr; _urlerr = WOURLstrerror(urlerr); WOLog(WO_INFO,"<FastCGI> URL Parsing Error: %s", _urlerr); if (urlerr == WOURLInvalidApplicationName) { if (ac_authorizeAppListing(&wc)) { resp = WOAdaptorInfo(NULL, &wc); sendErrorResponse(resp); } else { prepareAndSendErrorResponse(_urlerr, HTTP_NOT_FOUND); } WOFREE(url); break; } prepareAndSendErrorResponse(_urlerr, HTTP_BAD_REQUEST); WOFREE(url); break; } /* * build the request... */ req = req_new( FCGX_GetParam("REQUEST_METHOD", hdrp), NULL); /* * validate the method */ reqerr = req_validateMethod(req); if (reqerr) { prepareAndSendErrorResponse(reqerr, HTTP_BAD_REQUEST); WOFREE(url); break; } /* * copy the headers. This looks wierd... all we're doing is copying * *every* environment variable into our headers. It may be beyond * the spec, but more information probably won't hurt. */ hdrp_org=hdrp; while (hdrp && *hdrp) { char *key, *value; /* copy env. line. */ key = WOSTRDUP(*hdrp); for (value = key; *value && !isspace((int)*value) && (*value != '='); value++) {} if (*value) { *value++ = '\0'; /* null terminate 'key' */ } while (*value && (isspace((int)*value) || (*value == '='))) { value++; } /* BEGIN Support for getting the client's certificate. */ if (strcmp((const char *)key, "SSL_CLIENT_CERTIFICATE") == 0 || strcmp((const char *)key, "SSL_SERVER_CERTIFICATE") == 0 ) { value = 0; WOLog(WO_INFO,"<FastCGI> DROPPING ENV VAR (DUPLICATE) = %s", key); } if (strcmp((const char *)key, "SSL_CLIENT_CERT") == 0 || strcmp((const char *)key, "SSL_SERVER_CERT") == 0) { value = make_cert_one_line(value); //WOLog(WO_INFO,"<FastCGI> PASSING %s = %s", key, value); } /* END Support for getting the client's certificate */ if (key && *key && value && *value) { /* must specify copy key and value because key translation might replace this key, and value lives in the same buffer */ req_addHeader(req, key, value, STR_COPYKEY|STR_COPYVALUE); } /* BEGIN Support for getting the client's certificate */ if (freeValueNeeded ) { free(value); freeValueNeeded=0; } /* END Support for getting the client's certificate */ WOFREE(key); hdrp++; /* next env variable */ } hdrp=hdrp_org; /* * get form data if any * assume that POSTs with content length will be reformatted to GETs later */ WOLog ( WO_INFO, "Getting request data, length: %d",req->content_length ); if (req->content_length > 0) { req_allocateContent(req, req->content_length, 1); req->getMoreContent = (req_getMoreContentCallback)readContentData; WOLog ( WO_INFO, "content_buffer_size: %d",req->content_buffer_size ); if (req->content_buffer_size == 0) { prepareAndSendErrorResponse(ALLOCATION_FAILURE, HTTP_SERVER_ERROR); WOFREE(url); break; } if (readContentData(req, req->content, req->content_buffer_size, 1) == -1) { prepareAndSendErrorResponse(WOURLstrerror(WOURLInvalidPostData), HTTP_BAD_REQUEST); WOFREE(url); break; } } /* Always get the query string */ qs = FCGX_GetParam("QUERY_STRING", hdrp); if (qs) { qs_len = strlen(qs); } else { qs_len = 0; } if (qs_len > 0) { wc.queryString.start = qs; wc.queryString.length = qs_len; WOLog(WO_INFO,"<FastCGI> new request with Query String: %s", qs); } /* * message the application & collect the response */ resp = tr_handleRequest(req, url, &wc, FCGX_GetParam(CGI_SERVER_PROTOCOL, hdrp), documentRoot()); if (resp != NULL) { sendResponse(resp); resp_free(resp); /* dump the response */ } WOFREE(url); req_free(req); #if defined(FINDLEAKS) showleaks(); #endif } #ifdef PROFILE } #endif st_free(options); WOLog( WO_INFO,"<FastCGI> process exiting" ); return exit_status; }
void FileSystemRouteHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) { Poco::Path dataFolder(ofToDataPath("",true)); Poco::Path documentRoot(ofToDataPath(_parent.getSettings().getDocumentRoot(),true)); std::string dataFolderString = dataFolder.toString(); std::string documentRootString = documentRoot.toString(); // doc root validity check if(_parent.getSettings().getRequireDocumentRootInDataFolder() && (documentRootString.length() < dataFolderString.length() || documentRootString.substr(0,dataFolderString.length()) != dataFolderString)) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Document Root is not a sub directory of the data folder."; response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); return; } // check path Poco::URI uri(request.getURI()); std::string path = uri.getPath(); // just get the path // make paths absolute if(path.empty()) { path = "/"; } Poco::Path requestPath = documentRoot.append(path).makeAbsolute(); // add the default index if no filename is requested if(requestPath.getFileName().empty()) { requestPath.append(_parent.getSettings().getDefaultIndex()).makeAbsolute(); } std::string requestPathString = requestPath.toString(); // double check path safety (not needed?) if((requestPathString.length() < documentRootString.length() || requestPathString.substr(0,documentRootString.length()) != documentRootString)) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Requested document not inside DocumentFolder."; response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); _parent.handleRequest(request,response); return; } ofFile file(requestPathString); // use it to parse file name parts try { // ofx::Media::MediaTypeMap mediaMap; // Poco::Net::MediaType mediaType = mediaMap.getMediaTypeForSuffix(file.getExtension()); std::string mediaTypeString = "application/octet-stream"; std::string ext = file.getExtension(); if(ext == "json") { mediaTypeString = "application/json"; } else if(ext == "html") { mediaTypeString = "text/html"; } else if(ext == "jpg" || ext == "jpeg") { mediaTypeString = "image/jpeg"; } else if(ext == "png") { mediaTypeString = "image/png"; } else if(ext == "js") { mediaTypeString = "application/javascript"; } else if(ext == "css") { mediaTypeString = "text/css"; } else if(ext == "xml") { mediaTypeString = "application/xml"; } else if(ext == "ico") { mediaTypeString = "image/x-icon"; } response.sendFile(file.getAbsolutePath(), mediaTypeString); // will throw exceptions return; } catch (const Poco::FileNotFoundException& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << ex.displayText(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); _parent.handleRequest(request,response); } catch (const Poco::OpenFileException& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << ex.displayText(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); } catch (const exception& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Unknown server error: " << ex.what(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); } }
void FileSystemRoute::handleRequest(ServerEventArgs& evt) { Poco::Path dataFolder(ofToDataPath("", true)); Poco::Path documentRoot(ofToDataPath(getSettings().getDocumentRoot(), true)); std::string dataFolderString = dataFolder.toString(); std::string documentRootString = documentRoot.toString(); // Document root validity check. if (_settings.getRequireDocumentRootInDataFolder() && (documentRootString.length() < dataFolderString.length() || documentRootString.substr(0, dataFolderString.length()) != dataFolderString)) { ofLogError("FileSystemRoute::handleRequest") << "Document Root is not a sub directory of the data folder."; evt.getResponse() .setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } // check path Poco::URI uri(evt.getRequest().getURI()); std::string path = uri.getPath(); // just get the path // make paths absolute if (path.empty()) { path = "/"; } Poco::Path requestPath = documentRoot.append(path).makeAbsolute(); // add the default index if no filename is requested if (requestPath.getFileName().empty()) { requestPath.append(getSettings().getDefaultIndex()).makeAbsolute(); } std::string requestPathString = requestPath.toString(); // double check path safety (not needed?) if ((requestPathString.length() < documentRootString.length() || requestPathString.substr(0, documentRootString.length()) != documentRootString)) { ofLogError("FileSystemRoute::handleRequest") << "Requested document not inside DocumentFolder."; evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); handleErrorResponse(evt); return; } ofFile file(requestPathString); // use it to parse file name parts std::string mediaTypeString = MediaTypeMap::getDefault()->getMediaTypeForPath(file.path()).toString(); try { // TODO: this is where we would begin to work honoring /// Accept-Encoding:gzip, deflate, sdch evt.getResponse().sendFile(file.getAbsolutePath(), mediaTypeString); return; } catch (const Poco::FileNotFoundException& exc) { ofLogError("FileSystemRoute::handleRequest") << exc.displayText(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); handleErrorResponse(evt); return; } catch (const Poco::OpenFileException& exc) { ofLogError("FileSystemRoute::handleRequest") << exc.displayText(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } catch (const std::exception& exc) { ofLogError("FileSystemRoute::handleRequest") << "Unknown server error: " << exc.what(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } }
vector<LinkStatus*> SearchManager::children(LinkStatus* link) { vector<LinkStatus*> children; if(!link || link->absoluteUrl().hasRef()) return children; vector<Node*> const& nodes = link->childrenNodes(); int count = 0; for(uint i = 0; i != nodes.size(); ++i) { ++count; Node* node = nodes[i]; KURL url; if(node->url().isEmpty()) url = ""; else url = Url::normalizeUrl(node->url(), *link, documentRoot().path()); if( (node->isLink() && checkable(url, *link) && !Url::existUrl(url, children) && !node->url().isEmpty()) || node->malformed() ) { LinkStatus* ls = new LinkStatus(node, link); ls->setAbsoluteUrl(url); if(localDomain(ls->absoluteUrl())) ls->setExternalDomainDepth(-1); else ls->setExternalDomainDepth(link->externalDomainDepth() + 1); //ls->setIsLocalRestrict(localDomain(url)); ls->setIsLocalRestrict(ls->local()); // @todo clean this nonsense if(!validUrl(url)) { ls->setMalformed(true); ls->setErrorOccurred(true); } ls->setOnlyCheckHeader(onlyCheckHeader(ls)); if(link->externalDomainDepth() > external_domain_depth_) { kdDebug(23100) << "link->externalDomainDepth() > external_domain_depth_: " << link->externalDomainDepth() << endl; kdDebug(23100) << "link: " << endl << link->toString() << endl; kdDebug(23100) << "child: " << endl << ls->toString() << endl; } Q_ASSERT(link->externalDomainDepth() <= external_domain_depth_); children.push_back(ls); } if(count == 50) { kapp->processEvents(); count = 0; } } return children; }