mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-09-13 00:13:43 +02:00
yWeb 2.8.a.4
- updated nhttpd - Code clean up - changes to nhttpd.conf - some changes for logo display git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@416 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
@@ -29,14 +29,12 @@
|
||||
//=============================================================================
|
||||
// Constructor & Destructor
|
||||
//=============================================================================
|
||||
CWebserverResponse::CWebserverResponse(CWebserver *pWebserver)
|
||||
{
|
||||
CWebserverResponse::CWebserverResponse(CWebserver *pWebserver) {
|
||||
Webserver = pWebserver;
|
||||
CWebserverResponse();
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
CWebserverResponse::CWebserverResponse()
|
||||
{
|
||||
CWebserverResponse::CWebserverResponse() {
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
@@ -54,198 +52,177 @@ CWebserverResponse::CWebserverResponse()
|
||||
// CRLF ; generated by SendHeader
|
||||
// [ message-body ] ; by HOOK Handling Loop or Sendfile
|
||||
//=============================================================================
|
||||
bool CWebserverResponse::SendResponse()
|
||||
{
|
||||
bool CWebserverResponse::SendResponse() {
|
||||
// Init Hookhandler
|
||||
Connection->HookHandler.session_init(Connection->Request.ParameterList, Connection->Request.UrlData,
|
||||
(Connection->Request.HeaderList), (Cyhttpd::ConfigList), Connection->Method, Connection->keep_alive);
|
||||
Connection->HookHandler.session_init(Connection->Request.ParameterList,
|
||||
Connection->Request.UrlData, (Connection->Request.HeaderList),
|
||||
(Cyhttpd::ConfigList), Connection->Method, Connection->keep_alive);
|
||||
//--------------------------------------------------------------
|
||||
// HOOK Handling Loop [ PREPARE response hook ]
|
||||
// Checking and Preperation: Auth, static, cache, ...
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// move to mod_sendfile ???
|
||||
// move to mod_sendfile ???
|
||||
#ifdef Y_CONFIG_USE_HOSTEDWEB
|
||||
// for hosted webs: rewrite URL
|
||||
std::string _hosted="/hosted/";
|
||||
if((Connection->Request.UrlData["path"]).compare(0,_hosted.length(),"/hosted/") == 0) // hosted Web ?
|
||||
Connection->Request.UrlData["path"]=Cyhttpd::ConfigList["HostedDocumentRoot"]
|
||||
+(Connection->Request.UrlData["path"]).substr(_hosted.length()-1);
|
||||
if((Connection->Request.UrlData["path"]).compare(0,_hosted.length(),"/hosted/") == 0) // hosted Web ?
|
||||
Connection->Request.UrlData["path"]=Cyhttpd::ConfigList["WebsiteMain.hosted_directory"]
|
||||
+(Connection->Request.UrlData["path"]).substr(_hosted.length()-1);
|
||||
#endif //Y_CONFIG_USE_HOSTEDWEB
|
||||
|
||||
do
|
||||
{
|
||||
if(Connection->RequestCanceled)
|
||||
do {
|
||||
if (Connection->RequestCanceled)
|
||||
return false;
|
||||
|
||||
Connection->HookHandler.Hooks_PrepareResponse();
|
||||
if(Connection->HookHandler.status == HANDLED_ERROR || Connection->HookHandler.status == HANDLED_ABORT)
|
||||
{
|
||||
log_level_printf(2,"Response Prepare Hook found but Error\n");
|
||||
if (Connection->HookHandler.status == HANDLED_ERROR
|
||||
|| Connection->HookHandler.status == HANDLED_ABORT) {
|
||||
log_level_printf(2, "Response Prepare Hook found but Error\n");
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
Write(Connection->HookHandler.yresult);
|
||||
return false;
|
||||
}
|
||||
// URL has new value. Analyze new URL for SendFile
|
||||
else if(Connection->HookHandler.status == HANDLED_SENDFILE ||
|
||||
Connection->HookHandler.status == HANDLED_REWRITE)
|
||||
{
|
||||
else if (Connection->HookHandler.status == HANDLED_SENDFILE
|
||||
|| Connection->HookHandler.status == HANDLED_REWRITE) {
|
||||
Connection->Request.analyzeURL(Connection->HookHandler.NewURL);
|
||||
Connection->HookHandler.UrlData = Connection->Request.UrlData;
|
||||
}
|
||||
if(Connection->HookHandler.status == HANDLED_REDIRECTION)
|
||||
{
|
||||
if (Connection->HookHandler.status == HANDLED_REDIRECTION) {
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while(Connection->HookHandler.status == HANDLED_REWRITE);
|
||||
|
||||
} while (Connection->HookHandler.status == HANDLED_REWRITE);
|
||||
|
||||
// Prepare = NOT_MODIFIED ?
|
||||
if(Connection->HookHandler.httpStatus == HTTP_NOT_MODIFIED)
|
||||
{
|
||||
if (Connection->HookHandler.httpStatus == HTTP_NOT_MODIFIED) {
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------
|
||||
// HOOK Handling Loop [ response hook ]
|
||||
// Production
|
||||
//--------------------------------------------------------------
|
||||
if(Connection->HookHandler.status != HANDLED_SENDFILE)
|
||||
do
|
||||
{
|
||||
if(Connection->RequestCanceled)
|
||||
return false;
|
||||
if (Connection->HookHandler.status != HANDLED_SENDFILE)
|
||||
do {
|
||||
if (Connection->RequestCanceled)
|
||||
return false;
|
||||
|
||||
Connection->HookHandler.Hooks_SendResponse();
|
||||
if((Connection->HookHandler.status == HANDLED_READY)||(Connection->HookHandler.status == HANDLED_CONTINUE))
|
||||
{
|
||||
log_level_printf(2,"Response Hook Output. Status:%d\n", Connection->HookHandler.status);
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
if(Connection->Method != M_HEAD)
|
||||
Write(Connection->HookHandler.yresult);
|
||||
if(Connection->HookHandler.status != HANDLED_CONTINUE)
|
||||
return true;
|
||||
}
|
||||
else if(Connection->HookHandler.status == HANDLED_ERROR)
|
||||
{
|
||||
log_level_printf(2,"Response Hook found but Error\n");
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
if(Connection->Method != M_HEAD)
|
||||
Write(Connection->HookHandler.yresult);
|
||||
return false;
|
||||
}
|
||||
else if(Connection->HookHandler.status == HANDLED_ABORT)
|
||||
return false;
|
||||
// URL has new value. Analyze new URL for SendFile
|
||||
else if(Connection->HookHandler.status == HANDLED_SENDFILE ||
|
||||
Connection->HookHandler.status == HANDLED_REWRITE)
|
||||
{
|
||||
Connection->Request.analyzeURL(Connection->HookHandler.NewURL);
|
||||
Connection->HookHandler.UrlData = Connection->Request.UrlData;
|
||||
}
|
||||
if(Connection->HookHandler.status == HANDLED_REDIRECTION)
|
||||
{
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while(Connection->HookHandler.status == HANDLED_REWRITE);
|
||||
Connection->HookHandler.Hooks_SendResponse();
|
||||
if ((Connection->HookHandler.status == HANDLED_READY)
|
||||
|| (Connection->HookHandler.status == HANDLED_CONTINUE)) {
|
||||
log_level_printf(2, "Response Hook Output. Status:%d\n", Connection->HookHandler.status);
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
if (Connection->Method != M_HEAD)
|
||||
Write(Connection->HookHandler.yresult);
|
||||
if (Connection->HookHandler.status != HANDLED_CONTINUE)
|
||||
return true;
|
||||
} else if (Connection->HookHandler.status == HANDLED_ERROR) {
|
||||
log_level_printf(2, "Response Hook found but Error\n");
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
if (Connection->Method != M_HEAD)
|
||||
Write(Connection->HookHandler.yresult);
|
||||
return false;
|
||||
} else if (Connection->HookHandler.status == HANDLED_ABORT)
|
||||
return false;
|
||||
// URL has new value. Analyze new URL for SendFile
|
||||
else if (Connection->HookHandler.status == HANDLED_SENDFILE
|
||||
|| Connection->HookHandler.status == HANDLED_REWRITE) {
|
||||
Connection->Request.analyzeURL(Connection->HookHandler.NewURL);
|
||||
Connection->HookHandler.UrlData = Connection->Request.UrlData;
|
||||
}
|
||||
if (Connection->HookHandler.status == HANDLED_REDIRECTION) {
|
||||
Write(Connection->HookHandler.BuildHeader());
|
||||
return false;
|
||||
}
|
||||
} while (Connection->HookHandler.status == HANDLED_REWRITE);
|
||||
|
||||
// Send static file
|
||||
if(Connection->HookHandler.status == HANDLED_SENDFILE && !Connection->RequestCanceled)
|
||||
{
|
||||
if (Connection->HookHandler.status == HANDLED_SENDFILE
|
||||
&& !Connection->RequestCanceled) {
|
||||
bool cache = true;
|
||||
// if(Connection->HookHandler.UrlData["path"] == "/tmp/")//TODO: un-cachable dirs
|
||||
// cache = false;
|
||||
// if(Connection->HookHandler.UrlData["path"] == "/tmp/")//TODO: un-cachable dirs
|
||||
// cache = false;
|
||||
Write(Connection->HookHandler.BuildHeader(cache));
|
||||
if(Connection->Method != M_HEAD)
|
||||
if (Connection->Method != M_HEAD)
|
||||
Sendfile(Connection->Request.UrlData["url"]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// arrived here? = error!
|
||||
SendError(HTTP_NOT_FOUND);
|
||||
SendError( HTTP_NOT_FOUND);
|
||||
return false;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// Output
|
||||
//=============================================================================
|
||||
void CWebserverResponse::SendHeader(HttpResponseType responseType, bool cache, std::string ContentType)
|
||||
{
|
||||
Connection->HookHandler.SetHeader(responseType, ContentType);
|
||||
void CWebserverResponse::SendHeader(HttpResponseType responseType, bool cache,
|
||||
std::string ContentType) {
|
||||
Connection->HookHandler.SetHeader(responseType, ContentType);
|
||||
Write(Connection->HookHandler.BuildHeader(cache));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// BASIC Send over Socket for Strings (char*)
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CWebserverResponse::WriteData(char const * data, long length)
|
||||
{
|
||||
if(Connection->RequestCanceled)
|
||||
bool CWebserverResponse::WriteData(char const * data, long length) {
|
||||
if (Connection->RequestCanceled)
|
||||
return false;
|
||||
if(Connection->sock->Send(data, length) == -1)
|
||||
{
|
||||
log_level_printf(1,"response canceled: %s\n", strerror(errno));
|
||||
if (Connection->sock->Send(data, length) == -1) {
|
||||
log_level_printf(1, "response canceled: %s\n", strerror(errno));
|
||||
Connection->RequestCanceled = true;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
#define bufferlen 4*1024
|
||||
void CWebserverResponse::printf ( const char *fmt, ... )
|
||||
{
|
||||
void CWebserverResponse::printf(const char *fmt, ...) {
|
||||
char buffer[bufferlen];
|
||||
va_list arglist;
|
||||
va_start( arglist, fmt );
|
||||
vsnprintf( buffer, bufferlen, fmt, arglist );
|
||||
va_start(arglist, fmt);
|
||||
vsnprintf(buffer, bufferlen, fmt, arglist);
|
||||
va_end(arglist);
|
||||
Write(buffer);
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CWebserverResponse::Write(char const *text)
|
||||
{
|
||||
bool CWebserverResponse::Write(char const *text) {
|
||||
return WriteData(text, strlen(text));
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CWebserverResponse::WriteLn(char const *text)
|
||||
{
|
||||
if(!WriteData(text, strlen(text)))
|
||||
bool CWebserverResponse::WriteLn(char const *text) {
|
||||
if (!WriteData(text, strlen(text)))
|
||||
return false;
|
||||
return WriteData("\r\n",2);
|
||||
return WriteData("\r\n", 2);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CWebserverResponse::Sendfile(std::string filename)
|
||||
{
|
||||
if(Connection->RequestCanceled)
|
||||
bool CWebserverResponse::Sendfile(std::string filename) {
|
||||
if (Connection->RequestCanceled)
|
||||
return false;
|
||||
int filed = open( filename.c_str(), O_RDONLY );
|
||||
if(filed != -1 ) //can access file?
|
||||
int filed = open(filename.c_str(), O_RDONLY);
|
||||
if (filed != -1) //can access file?
|
||||
{
|
||||
if(!Connection->sock->SendFile(filed))
|
||||
if (!Connection->sock->SendFile(filed))
|
||||
Connection->RequestCanceled = true;
|
||||
close(filed);
|
||||
}
|
||||
return (filed != -1 );
|
||||
return (filed != -1);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Send File: Determine MIME-Type fro File-Extention
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string CWebserverResponse::GetContentType(std::string ext)
|
||||
{
|
||||
std::string CWebserverResponse::GetContentType(std::string ext) {
|
||||
std::string ctype = "text/plain";
|
||||
ext = string_tolower(ext);
|
||||
for (unsigned int i = 0;i < (sizeof(MimeFileExtensions)/sizeof(MimeFileExtensions[0])); i++)
|
||||
if (MimeFileExtensions[i].fileext == ext)
|
||||
{
|
||||
for (unsigned int i = 0; i < (sizeof(MimeFileExtensions)
|
||||
/ sizeof(MimeFileExtensions[0])); i++)
|
||||
if (MimeFileExtensions[i].fileext == ext) {
|
||||
ctype = MimeFileExtensions[i].mime;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ctype;
|
||||
}
|
||||
|
Reference in New Issue
Block a user