void HttpServer::redirectToLoginPage(HttpClientHandler* hdlr) { HttpResponseHeader rhdr(301); setDefaultResponseHeaders(rhdr,"text/html",false); rhdr.setValue("Location","/login.html"); QString path = rootDir + bt::DirSeparator() + WebInterfacePluginSettings::skin() + "/login.html"; if (!hdlr->sendFile(rhdr,path)) { HttpResponseHeader nhdr(404); setDefaultResponseHeaders(nhdr,"text/html",false); hdlr->send404(nhdr,path); } Out(SYS_WEB|LOG_NOTICE) << "Redirecting to /login.html" << endl; }
void CComSuite::test_empty() { std::ofstream empty(".rawfile", std::ios::binary); empty.close(); std::ofstream nhdr(".nhdr", std::ios::app); nhdr << "sizes: 0 0 0\n"; nhdr.close(); ccom(".config"); std::ifstream outraw(".outraw", std::ios::binary); CPPUNIT_ASSERT(outraw); uint8_t v; outraw.read(reinterpret_cast<char*>(&v), sizeof(uint8_t)); CPPUNIT_ASSERT(outraw.eof()); }
void CComSuite::setUp() { std::ofstream cfg(".config"); cfg << "in: .nhdr\n" << "outraw: .outraw\n" << "outnhdr: .outnhdr\n" << "component: { range 1 20 }\n"; cfg.close(); std::ofstream nhdr(".nhdr"); nhdr << "NRRD0002\n" << "dimension: 3\n" << "type: uint8\n" << "encoding: raw\n" << "data file: .rawfile\n"; nhdr.close(); }
// two components but the range provided means they should be merged. void CComSuite::test_twovalues_merged() { writearray<6,uint8_t>(".rawfile", {{6,6, 19,19,19,19}}); std::ofstream nhdr(".nhdr", std::ios::app); nhdr << "sizes: 6 1 1\n"; nhdr.close(); ccom(".config"); std::ifstream outraw(".outraw", std::ios::binary); // make sure we get all the same value CPPUNIT_ASSERT(match<6>({{1,1,1,1,1,1}}, outraw)); // .. and also that the output is not too large. uint8_t v; outraw.read(reinterpret_cast<char*>(&v), sizeof(uint8_t)); CPPUNIT_ASSERT(outraw.eof()); }
// just a single value void CComSuite::test_single() { writearray<6,uint8_t>(".rawfile", {{19, 19, 19, 19, 19, 19}}); std::ofstream nhdr(".nhdr", std::ios::app); nhdr << "sizes: 6 1 1\n"; nhdr.close(); ccom(".config"); std::ifstream outraw(".outraw", std::ios::binary); // make sure we get all the same value uint8_t v; for(size_t i=0; i < 6; ++i) { outraw.read(reinterpret_cast<char*>(&v), sizeof(uint8_t)); CPPUNIT_ASSERT(v == 1); } // .. and also that the output is not too large. outraw.read(reinterpret_cast<char*>(&v), sizeof(uint8_t)); CPPUNIT_ASSERT(outraw.eof()); }
void HttpServer::handleGet(HttpClientHandler* hdlr,const QHttpRequestHeader & hdr,bool do_not_check_session) { QString file = hdr.path(); if (file == "/") file = "/login.html"; //Out(SYS_WEB|LOG_DEBUG) << "GET " << hdr.path() << endl; KURL url; url.setEncodedPathAndQuery(file); QString path = rootDir + bt::DirSeparator() + WebInterfacePluginSettings::skin() + url.path(); // first check if the file exists (if not send 404) if (!bt::Exists(path)) { HttpResponseHeader rhdr(404); setDefaultResponseHeaders(rhdr,"text/html",false); hdlr->send404(rhdr,path); return; } QFileInfo fi(path); QString ext = fi.extension(); // if it is the login page send that if (file == "/login.html" || file == "/") { session.logged_in = false; ext = "html"; path = rootDir + bt::DirSeparator() + WebInterfacePluginSettings::skin() + "/login.html"; } else if (!session.logged_in && (ext == "html" || ext == "php")) { // for any html or php page, a login is necessary redirectToLoginPage(hdlr); return; } else if (session.logged_in && !do_not_check_session && (ext == "html" || ext == "php")) { // if we are logged in and it's a html or php page, check the session id if (!checkSession(hdr)) { session.logged_in = false; // redirect to login page redirectToLoginPage(hdlr); return; } } if (ext == "html") { HttpResponseHeader rhdr(200); setDefaultResponseHeaders(rhdr,"text/html",true); if (path.endsWith("login.html")) { // clear cookie in case of login page QDateTime dt = QDateTime::currentDateTime().addDays(-1); QString cookie = QString("KT_SESSID=666; expires=%1 +0000").arg(DateTimeToString(dt,true)); rhdr.setValue("Set-Cookie",cookie); } if (!hdlr->sendFile(rhdr,path)) { HttpResponseHeader nhdr(404); setDefaultResponseHeaders(nhdr,"text/html",false); hdlr->send404(nhdr,path); } } else if (ext == "css" || ext == "js" || ext == "png" || ext == "ico" || ext == "gif" || ext == "jpg") { if (hdr.hasKey("If-Modified-Since")) { QDateTime dt = parseDate(hdr.value("If-Modified-Since")); if (dt.isValid() && dt < fi.lastModified()) { HttpResponseHeader rhdr(304); setDefaultResponseHeaders(rhdr,"text/html",true); rhdr.setValue("Cache-Control","max-age=0"); rhdr.setValue("Last-Modified",DateTimeToString(fi.lastModified(),false)); rhdr.setValue("Expires",DateTimeToString(QDateTime::currentDateTime(Qt::UTC).addSecs(3600),false)); hdlr->sendResponse(rhdr); return; } } HttpResponseHeader rhdr(200); setDefaultResponseHeaders(rhdr,ExtensionToContentType(ext),true); rhdr.setValue("Last-Modified",DateTimeToString(fi.lastModified(),false)); rhdr.setValue("Expires",DateTimeToString(QDateTime::currentDateTime(Qt::UTC).addSecs(3600),false)); rhdr.setValue("Cache-Control","private"); if (!hdlr->sendFile(rhdr,path)) { HttpResponseHeader nhdr(404); setDefaultResponseHeaders(nhdr,"text/html",false); hdlr->send404(nhdr,path); } } else if (ext == "php") { bool redirect = false; bool shutdown = false; if (url.queryItems().count() > 0 && session.logged_in) redirect = php_i->exec(url,shutdown); if (shutdown) { // first send back login page redirectToLoginPage(hdlr); QTimer::singleShot(1000,kapp,SLOT(quit())); } else if (redirect) { HttpResponseHeader rhdr(301); setDefaultResponseHeaders(rhdr,"text/html",true); rhdr.setValue("Location",url.encodedPathAndQuery()); hdlr->executePHPScript(php_i,rhdr,WebInterfacePluginSettings::phpExecutablePath(), path,url.queryItems()); } else { HttpResponseHeader rhdr(200); setDefaultResponseHeaders(rhdr,"text/html",true); hdlr->executePHPScript(php_i,rhdr,WebInterfacePluginSettings::phpExecutablePath(), path,url.queryItems()); } } else { HttpResponseHeader rhdr(404); setDefaultResponseHeaders(rhdr,"text/html",false); hdlr->send404(rhdr,path); } }