void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) { response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_OK); if (request.getContentType() == "application/soap+xml") { assert(false); //std::string req(std::istreambuf_iterator<char>(request.stream()), std::istreambuf_iterator<char>()); //tinyxml2::XMLDocument reqDoc; //reqDoc.Parse(req.c_str()); //tinyxml2::XMLDocument respDoc; //respDoc.LoadFile(m_wsdl.c_str()); //cout << "Sending wsdl via soap" << endl << flush; //m_outbuf = ""; //m_soapProtocol.SendResponse(reqDoc, respDoc, respDoc.FirstChildElement(), "http://www.w3.org/2005/08/addressing/anonymous"); //response.sendBuffer(m_outbuf.c_str(), m_outbuf.size()); //m_outbuf = ""; } else { cout << "Sending wsdl via http" << endl << flush; response.sendFile(m_wsdl.c_str(), "text/xml"); } }
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) { const char *ext = s_mime_types[0].ext; const char *mime = s_mime_types[0].type; size_t epos = m_path.find_last_of("."); if(epos!=std::string::npos) { ext = &m_path[epos]; for(size_t i=0; i<_countof(s_mime_types); ++i) { if(strcmp(ext, s_mime_types[i].ext)==0) { mime = s_mime_types[i].type; } } } response.sendFile(m_path, mime); }
void FileSystemRoute::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) { // see if we have an html file with that error code ofFile errorFile(_settings.getDocumentRoot() + "/" + ofToString(response.getStatus()) + ".html"); if(errorFile.exists()) { try { response.sendFile(errorFile.getAbsolutePath(),"text/html"); return; } catch (const Poco::FileNotFoundException& exc) { ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Poco::FileNotFoundException: " << exc.code() << " " << exc.displayText(); } catch (const Poco::OpenFileException& exc) { ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Poco::OpenFileException: " << exc.code() << " " << exc.displayText(); } catch (const Poco::Exception& exc) { ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Exception: " << exc.code() << " " << exc.displayText(); } catch (const std::exception& exc) { ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "exception: " << exc.what(); } catch ( ... ) { ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "... Unknown exception."; } } // if nothing is returned, then base route will get it BaseRoute::handleRequest(request,response); }
void AppRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) { // Check for the favicon.ico request, we don't have one for now, // so set status code to HTTP_NOT_FOUND if ( request.getURI().compare("/favicon.ico") == 0 ) { response.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); response.send(); return; } std::string lastModifiedHeader = request.get("If-Modified-Since", ""); Poco::URI uri(request.getURI()); Poco::Util::Application& app = Poco::Util::Application::instance(); std::string staticPathname = app.config().getString("mq.web.app", ""); if ( staticPathname.empty() ) { Poco::Logger& logger = Poco::Logger::get("mq.web"); logger.error("mq.web.app property not defined. Check your configuration."); response.setStatus(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); response.send(); return; } Poco::Path staticPath(staticPathname); staticPath.makeDirectory(); std::vector<std::string> uriPathSegments; uri.getPathSegments(uriPathSegments); std::vector<std::string>::iterator it = uriPathSegments.begin(); it++; for(; it != uriPathSegments.end(); ++it) { staticPath.append(*it); } if (staticPath.isDirectory()) { staticPath.append("index.html"); } Poco::File staticFile(staticPath); Poco::Logger& logger = Poco::Logger::get("mq.web.access"); if ( staticFile.exists() ) { if ( !lastModifiedHeader.empty() ) { Poco::DateTime lastModifiedDate; int timeZoneDifferential = 0; if ( Poco::DateTimeParser::tryParse(Poco::DateTimeFormat::HTTP_FORMAT, lastModifiedHeader, lastModifiedDate, timeZoneDifferential) ) { if ( staticFile.getLastModified() <= lastModifiedDate.timestamp() ) { logger.information(Poco::Logger::format("$0 : HTTP_NOT_MODIFIED", staticPath.toString())); response.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_MODIFIED); response.send(); return; } } } logger.information(Poco::Logger::format("$0 : HTTP_OK", staticPath.toString())); std::string mimeType; if ( staticPath.getExtension().compare("gif") == 0 ) { mimeType = "image/gif"; } else if ( staticPath.getExtension().compare("css") == 0 ) { mimeType = "text/css"; } else if ( staticPath.getExtension().compare("html") == 0 || staticPath.getExtension().compare("htm") == 0) { mimeType = "text/html"; } else if ( staticPath.getExtension().compare("js") == 0 ) { mimeType = "text/javascript"; } else if ( staticPath.getExtension().compare("png") == 0 ) { mimeType = "image/png"; } else if ( staticPath.getExtension().compare("jpg") == 0 || staticPath.getExtension().compare("jpeg") == 0) { mimeType = "image/jpeg"; } try { response.sendFile(staticPath.toString(), mimeType); } catch(Poco::FileNotFoundException&) { // We can't get here normally ... but you never know :) response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND, Poco::Logger::format("Can't find file $0", staticPath.toString())); } catch(Poco::OpenFileException&) { response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR, Poco::Logger::format("Can't open file $0", staticPath.toString())); } return; } logger.error(Poco::Logger::format("$0 : HTTP_NOT_FOUND", staticFile.path())); response.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); response.send(); }
void ShowFilePage::handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) { m_log->trace("ShowFilePage::handleRequest from "+request.clientAddress().toString()); m_log->trace("ShowFilePage::handleRequest ContentType="+request.getContentType()); m_log->trace("ShowFilePage::handleRequest TransferEncoding="+request.getTransferEncoding()); m_log->trace("ShowFilePage::handleRequest URI="+request.getURI()); std::map<std::string,QueryVar> queryvars; CreateQueryVarMap(request,queryvars); std::string content=""; if(queryvars.find("file")!=queryvars.end() && m_filewhitelist.find((*queryvars.find("file")).second.GetData())!=m_filewhitelist.end()) { try { response.sendFile(global::basepath+(*queryvars.find("file")).second.GetData(),m_filewhitelist[(*queryvars.find("file")).second.GetData()]); } catch(Poco::FileNotFoundException &fnf) { m_log->error("ShowFilePage::handleRequest caught FileNotFound exception - "+fnf.message()); } catch(Poco::OpenFileException &of) { m_log->error("ShowFilePage::handleRequest caught OpenFile exception - "+of.message()); } catch(...) { m_log->error("ShowFilePage::handleRequest caught other exception"); } } else if(request.getURI().size()>0 && request.getURI()[0]=='/' && m_filewhitelist.find(request.getURI().substr(1))!=m_filewhitelist.end()) { try { response.sendFile(global::basepath+request.getURI().substr(1),m_filewhitelist[request.getURI().substr(1)]); } catch(Poco::FileNotFoundException &fnf) { m_log->error("ShowFilePage::handleRequest caught FileNotFound exception - "+fnf.message()); } catch(Poco::OpenFileException &of) { m_log->error("ShowFilePage::handleRequest caught OpenFile exception - "+of.message()); } catch(...) { m_log->error("ShowFilePage::handleRequest caught other exception"); } } else if(request.getURI().size()>0 && m_filewhitelist.find(request.getURI())!=m_filewhitelist.end()) { try { response.sendFile(global::basepath+request.getURI(),m_filewhitelist[request.getURI()]); } catch(Poco::FileNotFoundException &fnf) { m_log->error("ShowFilePage::handleRequest caught FileNotFound exception - "+fnf.message()); } catch(Poco::OpenFileException &of) { m_log->error("ShowFilePage::handleRequest caught OpenFile exception - "+of.message()); } catch(...) { m_log->error("ShowFilePage::handleRequest caught other exception"); } } }
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 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 std::string mediaTypeString = Media::MediaTypeMap::getDefault()->getMediaTypeForPath(file.path()).toString(); try { // TODO: this is where we would begin to work honoring /// Accept-Encoding:gzip, deflate, sdch response.sendFile(file.getAbsolutePath(), mediaTypeString); 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); } }