예제 #1
0
    void TorrentPostHandler::post(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr, const QByteArray& data)
    {
        const char* ptr = data.data();
        int len = data.size();
        int pos = QString(data).indexOf("\r\n\r\n");

        if (pos == -1 || pos + 4 >= len)
        {
            HttpResponseHeader rhdr(500);
            server->setDefaultResponseHeaders(rhdr, "text/html", false);
            hdlr->send500(rhdr, i18n("Invalid data received"));
            return;
        }

        // save torrent to a temporary file
        QString save_file = kt::DataDir() + "webgui_load_torrent";
        QFile tmp_file(save_file);

        if (!tmp_file.open(QIODevice::WriteOnly))
        {
            HttpResponseHeader rhdr(500);
            server->setDefaultResponseHeaders(rhdr, "text/html", false);
            hdlr->send500(rhdr, i18n("Failed to open temporary file"));
            return;
        }

        QDataStream out(&tmp_file);
        out.writeRawData(ptr + (pos + 4), len - (pos + 4));
        tmp_file.close();

        Out(SYS_WEB | LOG_NOTICE) << "Loading file " << save_file << endl;
        core->loadSilently(KUrl(save_file), QString());

        KUrl url;
        url.setEncodedPathAndQuery(hdr.path());
        QString page = url.queryItem("page");
        // there needs to be a page to send back
        if (page.isEmpty())
        {
            server->redirectToLoginPage(hdlr);
        }
        else
        {
            // redirect to page mentioned in page parameter
            HttpResponseHeader rhdr(301);
            server->setDefaultResponseHeaders(rhdr, "text/html", true);
            rhdr.setValue("Location", "/" + page);
            hdlr->send(rhdr, QByteArray());
        }
    }
예제 #2
0
 void TorrentPostHandler::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
 {
     Q_UNUSED(hdr);
     // Send internal server error
     HttpResponseHeader rhdr(500);
     server->setDefaultResponseHeaders(rhdr, "text/html", false);
     hdlr->send500(rhdr, i18n("HTTP Get not supported when uploading a torrent"));
 }
예제 #3
0
	void HttpServer::handleTorrentPost(HttpClientHandler* hdlr,const QHttpRequestHeader & hdr,const QByteArray & data)
	{
		const char* ptr = data.data();
		Uint32 len = data.size();
		int pos = QString(data).find("\r\n\r\n");

		if (!session.logged_in || !checkSession(hdr))
		{
			// You can't post torrents if you are not logged in
			// or the session is not OK
			redirectToLoginPage(hdlr);
			return;
		}
		
		if (pos == -1 || pos + 4 >= len || ptr[pos + 4] != 'd')
		{
			HttpResponseHeader rhdr(500);
			setDefaultResponseHeaders(rhdr,"text/html",false);
			hdlr->send500(rhdr);
			return;
		}
		
		// save torrent to a temporary file
		KTempFile tmp_file(locateLocal("tmp", "ktwebgui-"), ".torrent");
		QDataStream* out = tmp_file.dataStream();
		if (!out)
		{
			HttpResponseHeader rhdr(500);
			setDefaultResponseHeaders(rhdr,"text/html",false);
			hdlr->send500(rhdr);
			return;
		}
		
		out->writeRawBytes(ptr + (pos + 4),len - (pos + 4));
		tmp_file.sync();
		tmp_file.setAutoDelete(true);
		
		Out(SYS_WEB|LOG_NOTICE) << "Loading file " << tmp_file.name() << endl;
		core->loadSilently(KURL::fromPathOrURL(tmp_file.name()));
		
		handleGet(hdlr,hdr);
	}
예제 #4
0
	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 ChallengeGenerator::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
{
    Q_UNUSED(hdr);
    HttpResponseHeader rhdr(200);
    server->setDefaultResponseHeaders(rhdr,"text/xml",false);
    QByteArray output_data;
    QXmlStreamWriter out(&output_data);
    out.setAutoFormatting(true);
    out.writeStartDocument();
    out.writeStartElement("challenge");
    out.writeCharacters(server->challengeString());
    out.writeEndElement();
    out.writeEndDocument();
    hdlr->send(rhdr,output_data);
}
	void TorrentListGenerator::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
	{
		Q_UNUSED(hdr);
		HttpResponseHeader rhdr(200);
		server->setDefaultResponseHeaders(rhdr,"text/xml",true);
		
		QByteArray output_data;
		QXmlStreamWriter out(&output_data);
		out.setAutoFormatting(true);
		out.writeStartDocument();
		out.writeStartElement("torrents");
		kt::QueueManager* qman = core->getQueueManager();
		kt::QueueManager::iterator i = qman->begin();
		while (i != qman->end())
		{
			bt::TorrentInterface* ti = *i;
			const bt::TorrentStats & s = ti->getStats();
			out.writeStartElement("torrent");
			writeElement(out,"name",ti->getDisplayName());
			writeElement(out,"info_hash",ti->getInfoHash().toString());
			writeElement(out,"status",s.statusToString());
			writeElement(out,"bytes_downloaded",BytesToString(s.bytes_downloaded));
			writeElement(out,"bytes_uploaded",BytesToString(s.bytes_uploaded));
			writeElement(out,"total_bytes",BytesToString(s.total_bytes));
			writeElement(out,"total_bytes_to_download",BytesToString(s.total_bytes_to_download));
			writeElement(out,"download_rate",BytesPerSecToString(s.download_rate));
			writeElement(out,"upload_rate",BytesPerSecToString(s.upload_rate));
			writeElement(out,"num_peers",QString::number(s.num_peers));
			writeElement(out,"seeders",QString::number(s.seeders_connected_to));
			writeElement(out,"seeders_total",QString::number(s.seeders_total));
			writeElement(out,"leechers",QString::number(s.leechers_connected_to));
			writeElement(out,"leechers_total",QString::number(s.leechers_total));
			writeElement(out,"running",s.running ? "1" : "0");
			writeElement(out,"percentage",QString::number(Percentage(s),'f',2));
			writeElement(out,"num_files",QString::number(ti->getNumFiles()));
			out.writeEndElement();
			i++;
		}
		out.writeEndElement();
		out.writeEndDocument();
		hdlr->send(rhdr,output_data);
	}
예제 #7
0
    void GlobalDataGenerator::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
    {
        Q_UNUSED(hdr);
        HttpResponseHeader rhdr(200);
        server->setDefaultResponseHeaders(rhdr, "text/xml", true);

        CurrentStats s = core->getStats();
        QByteArray output_data;
        QXmlStreamWriter out(&output_data);
        out.setAutoFormatting(true);
        out.writeStartDocument();
        out.writeStartElement("global_data");
        writeElement(out, "transferred_down", BytesToString(s.bytes_downloaded));
        writeElement(out, "transferred_up", BytesToString(s.bytes_uploaded));
        writeElement(out, "speed_down", BytesPerSecToString(s.download_speed));
        writeElement(out, "speed_up", BytesPerSecToString(s.upload_speed));
        writeElement(out, "dht", Settings::dhtSupport() ? "1" : "0");
        writeElement(out, "encryption", Settings::useEncryption() ? "1" : "0");
        out.writeEndElement();
        out.writeEndDocument();
        hdlr->send(rhdr, output_data);
    }
예제 #8
0
	void HttpServer::handleUnsupportedMethod(HttpClientHandler* hdlr)
	{
		HttpResponseHeader rhdr(500);
		setDefaultResponseHeaders(rhdr,"text/html",false);
		hdlr->send500(rhdr);
	}
예제 #9
0
	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);
		}
	}