Example #1
0
bool CWebserverRequest::SendFile(const std::string path,const std::string filename)
{
	if( (tmpint = OpenFile(path, filename) ) != -1 )
	{							// Wenn Datei geöffnet werden konnte
		if (!SocketWrite("HTTP/1.0 200 OK\r\n"))
		{
			close(tmpint);
			return false;
		}
		HttpStatus = 200;

		if (!SocketWrite("Content-Type: " + GetContentType(FileExt) + "\r\n\r\n"))
		{
			close(tmpint);
			return false;
		}
		if (Method == M_HEAD) {
			close(tmpint);
			return true;
		}
		off_t start = 0;
		off_t end = lseek(tmpint,0,SEEK_END);
		int written = 0;
		if((written = sendfile(Socket,tmpint,&start,end)) == -1)
			perror("sendfile failed");
		close(tmpint);
		return true;
	}
	else
	{
		Send404Error();
		return false;
	}
}
Example #2
0
//-------------------------------------------------------------------------
bool CWebserverRequest::SendResponse()
{
	RewriteURL();		// Erst mal die URL umschreiben

	if(Path.compare("/control/") == 0)						// api for external programs
	{
		return Parent->WebDbox->ControlAPI->Execute(this);
	}
	else if(Path.compare("/bouquetedit/") == 0)				// bouquetedit api
	{
		return Parent->WebDbox->BouqueteditAPI->Execute(this);
	}
	else if(Path.compare("/fb/") == 0)						// webbrowser api
	{
		return Parent->WebDbox->WebAPI->Execute(this);
	}
	else
	{
	// Normale Datei										//normal file
		if( (tmpint = OpenFile(Path,Filename) ) != -1 )		// Testen ob Datei auf Platte geöffnet werden kann
		{											// Wenn Datei geöffnet werden konnte
			if (!SocketWrite("HTTP/1.0 200 OK\r\n"))
			{
				close(tmpint);
				return false;
			}
			HttpStatus = 200;
			if( FileExt == "" )		// Anhand der Dateiendung den Content bestimmen
				ContentType = "text/html";
			else
			{
				if(  (FileExt.compare("html") == 0) || (FileExt.compare("htm") == 0) )
					ContentType = "text/html";
				else if(FileExt.compare("gif") == 0)
					ContentType = "image/gif";
				else if((FileExt.compare("png") == 0) || (FileExt.compare("PNG") == 0) )
					ContentType = "image/png";
				else if( (FileExt.compare("jpg") == 0) || (FileExt.compare("JPG") == 0) )
					ContentType = "image/jpeg";
				else if( (FileExt.compare("css") == 0) || (FileExt.compare("CSS") == 0) )
					ContentType = "text/css";
				else if(FileExt.compare("xml") == 0)
					ContentType = "text/xml";
				else
					ContentType = "text/plain";

			}
			if (!SocketWrite("Content-Type: " + ContentType + "\r\n\r\n"))
			{
				close(tmpint);
				return false;
			}
			if (Method != M_HEAD) {
				SendOpenFile(tmpint);
			}
			else {
				close(tmpint);
			}
		}
		else
		{											// Wenn Datei nicht geöffnet werden konnte
			Send404Error();							// 404 Error senden
		}
		return true;
	}
}