Beispiel #1
0
	void HttpServer::setDefaultResponseHeaders(HttpResponseHeader & hdr,const QString & content_type,bool with_session_info)
	{
		hdr.setValue("Server","KTorrent/" KT_VERSION_MACRO);
		hdr.setValue("Date",DateTimeToString(QDateTime::currentDateTime(Qt::UTC),false));
		hdr.setValue("Content-Type",content_type);
		hdr.setValue("Connection","keep-alive");
		if (with_session_info && session.sessionId && session.logged_in)
		{
			hdr.setValue("Set-Cookie",QString("KT_SESSID=%1").arg(session.sessionId));
		}
	}
Beispiel #2
0
	void HttpClientHandler::executePHPScript(
			PhpInterface* php_iface,
			HttpResponseHeader & hdr,
			const QString & php_exe,
			const QString & php_file,
			const QMap<QString,QString> & args)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Launching PHP script " << php_file << endl;
		php = new PhpHandler(php_exe,php_iface);
		if (!php->executeScript(php_file,args))
		{
			QString data = QString(HTTP_500_ERROR).arg("Failed to launch PHP executable !");
			hdr.setResponseCode(500);
			hdr.setValue("Content-Length",QString::number(data.utf8().length()));
			
			QTextStream os(client);
			os.setEncoding( QTextStream::UnicodeUTF8 );
			os << hdr.toString();
			os << data;
			state = WAITING_FOR_REQUEST;
		}
		else
		{
			php_response_hdr = hdr;
			connect(php,SIGNAL(finished()),this,SLOT(onPHPFinished()));
			state = PROCESSING_PHP;
		}
	}
	bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl;
		setResponseHeaders(hdr);
		// first look in cache
		MMapFile* c = srv->cacheLookup(full_path);
		
		if (!c)
		{
			// not in cache so load it
			c = new MMapFile();
			if (!c->open(full_path,QIODevice::ReadOnly))
			{
				delete c;
				Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl;
				return false;
			}
			srv->insertIntoCache(full_path,c);
		}
		
	//	Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl;
	//	Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl;
				
		QByteArray data((const char*)c->getDataPointer(),c->getSize());	
		hdr.setValue("Content-Length",QString::number(data.size()));
		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data);
		
		sendOutputBuffer();
	//	Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl;
		return true;
	}
	void HttpClientHandler::setResponseHeaders(HttpResponseHeader& hdr)
	{
		if (shouldClose())
		{
			if (header.majorVersion() == 1 && header.minorVersion() == 0)
				return;
			else
				hdr.setValue("Connection","close");
		}
		else
		{
			if (header.majorVersion() == 1 && header.minorVersion() == 0)
				hdr.setValue("Connection","Keep-Alive");
			else
				return;
		}
	}
	void HttpClientHandler::send(HttpResponseHeader & hdr,const QByteArray & data)
	{
		setResponseHeaders(hdr);
		hdr.setValue("Content-Length",QString::number(data.length()));
		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data);
		sendOutputBuffer();
	}
Beispiel #6
0
	void HttpClientHandler::send500(HttpResponseHeader & hdr)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 500 " << endl;
		QString data = QString(HTTP_500_ERROR).arg("An internal server error occured !");
		hdr.setValue("Content-Length",QString::number(data.length()));

		QTextStream os(client);
		os.setEncoding( QTextStream::UnicodeUTF8 );
		os << hdr.toString();
		os << data;
	}
Beispiel #7
0
	void HttpClientHandler::send404(HttpResponseHeader & hdr,const QString & path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 404 " << path << endl;
		QString data = HTTP_404_ERROR;
		hdr.setValue("Content-Length",QString::number(data.length()));

		QTextStream os(client);
		os.setEncoding( QTextStream::UnicodeUTF8 );
		os << hdr.toString();
		os << data;
	}
	void HttpClientHandler::send404(HttpResponseHeader & hdr,const QString & path)
	{
		setResponseHeaders(hdr);
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 404 " << path << endl;
		QString data = QString(HTTP_404_ERROR).arg(path);
		hdr.setValue("Content-Length",QString::number(data.length()));

		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data.toUtf8());
		sendOutputBuffer();
	}
	void HttpClientHandler::send500(HttpResponseHeader & hdr,const QString & error)
	{
		setResponseHeaders(hdr);
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending 500 " << endl;
		QString err = i18n("An internal server error occurred: %1",error);
		QString data = QString(HTTP_500_ERROR).arg(err);
		hdr.setValue("Content-Length",QString::number(data.length()));

		output_buffer.append(hdr.toString().toUtf8());
		output_buffer.append(data.toUtf8());
		sendOutputBuffer();
	}
Beispiel #10
0
	bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path)
	{
	//	Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl;
		// first look in cache
		MMapFile* c = srv->cacheLookup(full_path);
		
		if (!c)
		{
			// not in cache so load it
			c = new MMapFile();
			if (!c->open(full_path,MMapFile::READ))
			{
				delete c;
				Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl;
				return false;
			}
			srv->insertIntoCache(full_path,c);
		}
		
		hdr.setValue("Content-Length",QString::number(c->getSize()));
		
	//	Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl;
	//	Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl;
				
		QCString d = hdr.toString().utf8();
		client->writeBlock(d.data(),d.length());

		Uint32 written = 0;
		Uint32 total = c->getSize();
		const char* data = (const char*)c->getDataPointer();
		while (written < total)
		{
			Uint32 w = client->writeBlock(data + written,total - written);
			written += w;
		}
		client->flush();
	//	Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl;
		return true;
	}