winix/core/request.cpp

579 lines
10 KiB
C++
Raw Normal View History

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#include <ctime>
#include <sys/stat.h>
#include <unistd.h>
#include "request.h"
#include "getparser.h"
#include "postparser.h"
#include "cookieparser.h"
#include "log.h"
#include "plugin.h"
#include "misc.h"
#include "db.h"
#include "functions/functionbase.h"
Request::Request() : char_empty(0)
{
id = 0;
Clear();
}
void Request::SetConfig(Config * pconfig)
{
config = pconfig;
}
void Request::Init()
{
compress.Init();
}
void Request::ClearPostFileTmp()
{
// deleting temporary files (if exists)
while( !post_file_tab.empty() )
{
const std::string & tmp_filename = post_file_tab.begin()->second.tmp_filename;
if( unlink(tmp_filename.c_str()) == 0 )
log << log3 << "Request: deleted tmp file: " << tmp_filename << logend;
post_file_tab.erase(post_file_tab.begin());
}
}
void Request::Clear()
{
// warning: don't clear: in, out, err, env
// id is never 0
if( ++id == 0 )
++id;
get_tab.clear();
post_tab.clear();
post_file_tab.clear();
cookie_tab.clear();
method = none;
role = responder;
headers.str("");
page.str("");
debug.str("");
//notify.str("");
env_request_method = &char_empty;
env_request_uri = &char_empty;
env_http_cookie = &char_empty;
env_remote_addr = &char_empty;
env_http_host = &char_empty;
env_http_user_agent = &char_empty;
env_http_accept_encoding = &char_empty;
env_fcgi_role = &char_empty;
env_content_type = &char_empty;
session = 0;
item_tab.clear();
item.Clear();
dir_tab.clear();
is_item = false;
function = 0;
param_tab.clear();
status = WINIX_ERR_OK;
notify_code = 0;
browser_msie = false;
redirect_to.clear();
x_sendfile.clear();
send_as_attachment = false;
plugin.Call(WINIX_REQUEST_CLEAR);
}
// value can be null
void Request::SetCookie(const char * name, const char * value, tm * expires)
{
headers << "Set-Cookie: " << name << "=";
if( value && value[0]!=0 )
headers << value;
else
headers << "\"\"";
if( expires )
headers << "; expires=" << DateToStrCookie(expires) << " GMT";
headers << "; path=/; domain=." << config->base_server << "\r\n";
}
void Request::SetCookie(const char * name, long value, tm * expires)
{
headers << "Set-Cookie: " << name << "=" << value;
if( expires )
headers << "; expires=" << DateToStrCookie(expires) << " GMT";
headers << "; path=/; domain=." << config->base_server << "\r\n";
}
bool Request::IsPostVar(const char * var)
{
PostTab::iterator p;
p = post_tab.find(var);
if( p == post_tab.end() )
return false;
return true;
}
std::string * Request::PostVar(const char * var)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
return 0;
return &(p->second);
}
bool Request::PostVar(const char * var, std::string & result)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
{
result.clear();
return false;
}
result = p->second;
return true;
}
//
void Request::PrintGetTab()
{
debug << "get_tab: " << get_tab.size() << "\n";
for(GetTab::iterator i = get_tab.begin() ; i != get_tab.end() ; ++i)
debug << " \"" << *i << "\"\n";
debug << std::endl;
}
void Request::PrintEnv()
{
char ** e;
debug << "environment variables:\n";
for( e = env ; *e ; ++e )
debug << ' ' << *e << "\n";
debug << std::endl;
}
void Request::PrintIn()
{
char buf[100];
int buf_len = sizeof(buf) / sizeof(char);
int len;
debug << "fcgi input:\n";
do
{
len = FCGX_GetStr(buf, buf_len - 1, in);
if( len != 0 )
{
buf[len] = 0;
debug << buf;
}
}
while( len == buf_len - 1 );
debug << std::endl;
}
const char * Request::SetEnvVar(const char * var)
{
const char * v = FCGX_GetParam(var, env);
if( v )
return v;
// char_empty contains '\0'
return &char_empty;
}
void Request::ReadEnvVariables()
{
// we store that values because FCGX_GetParam has O(n) complexity
// with this variables (env_*) we have O(1)
env_request_method = SetEnvVar("REQUEST_METHOD");
env_request_uri = SetEnvVar("REQUEST_URI");
env_http_cookie = SetEnvVar("HTTP_COOKIE");
env_remote_addr = SetEnvVar("REMOTE_ADDR");
env_http_host = SetEnvVar("HTTP_HOST");
env_http_user_agent = SetEnvVar("HTTP_USER_AGENT");
env_http_accept_encoding = SetEnvVar("HTTP_ACCEPT_ENCODING");
env_fcgi_role = SetEnvVar("FCGI_ROLE");
env_content_type = SetEnvVar("CONTENT_TYPE");
}
void Request::CheckIE()
{
char * msie = strstr(env_http_user_agent, "MSIE");
if( msie )
browser_msie = true;
else
browser_msie = false;
}
void Request::CheckKonqueror()
{
char * kon = strstr(env_http_user_agent, "Konqueror");
if( kon )
browser_konqueror = true;
else
browser_konqueror = false;
}
void Request::CheckMethod()
{
method = none;
if( ToSmall(env_request_method[0]) == 'g' )
method = get;
else
if( ToSmall(env_request_method[0]) == 'p' )
method = post;
// default we assume 'responder'
role = responder;
if( ToSmall(env_fcgi_role[0]) == 'a' )
role = authorizer;
}
bool Request::AllPostVarEmpty()
{
PostTab::iterator i;
for(i=post_tab.begin() ; i!=post_tab.end() ; ++i)
if( !i->second.empty() )
return false;
return true;
}
// !! czy te parsery powinny byc skladowymi Request?
void Request::ReadParameters()
{
// !! wrzucic jako skladowa klasy
GetParser get_parser(env_request_uri, get_tab);
get_parser.Parse();
if( method == post )
{
if( IsSubStringNoCase("multipart/form-data", env_content_type) )
{
log << log3 << "Request: post content type: multipart/form-data" << logend;
post_multi_parser.SetConfig(config);
post_multi_parser.Parse(in, post_tab, post_file_tab);
}
else
{
// !! wrzucic jako skladowa klasy
PostParser post_parser(in, post_tab);
post_parser.Parse();
}
}
CookieParser cookie_parser(env_http_cookie, cookie_tab);
cookie_parser.Parse();
accept_encoding_parser.Parse(env_http_accept_encoding);
}
void Request::StandardLog()
{
log.PutDate(log1);
log << env_remote_addr << ' ' << env_request_method << ' ';
log << env_http_host << env_request_uri << ' ' << env_http_user_agent << logend;
}
// reading everything
void Request::Read()
{
ReadEnvVariables();
CheckMethod();
StandardLog();
ReadParameters();
CheckIE();
if( role == authorizer )
log << log3 << "Request: fast cgi role: authorizer" << logend;
CheckKonqueror();
}
void Request::SendSessionCookie()
{
if( !session || session->id==0 )
return;
if( !session->puser || !session->remember_me )
{
SetCookie(config->http_session_id_name.c_str(), session->id);
return;
}
time_t t = time(0) + config->session_remember_max_idle;
tm * expires = localtime(&t);
if( !expires )
{
// oops, something wrong
SetCookie(config->http_session_id_name.c_str(), session->id);
return;
}
SetCookie(config->http_session_id_name.c_str(), session->id, expires);
}
void Request::SendHeaders(bool compressing, Header header)
{
if( send_as_attachment )
FCGX_PutS("Content-Disposition: attachment\r\n", out);
if( !redirect_to.empty() )
{
FCGX_PutS("Status: 301 Moved Permanently\r\n", out);
FCGX_FPrintF(out, "Location: %s\r\n", redirect_to.c_str());
log << log2 << "Redirect to: " << redirect_to << logend;
}
else
if( !x_sendfile.empty() )
{
FCGX_FPrintF(out, "X-LIGHTTPD-send-file: %s\r\n", x_sendfile.c_str());
FCGX_PutS("Status: 200 OK\r\n", out);
log << log2 << "Sending file: " << x_sendfile << logend;
}
else
{
switch(header)
{
case h_404:
FCGX_PutS("Status: 404 Not Found\r\n", out);
FCGX_PutS("Content-Type: text/html\r\n", out);
log << log2 << "Request: response: 404 Not Found" << logend;
break;
case h_403:
FCGX_PutS("Status: 403 Forbidden\r\n", out);
FCGX_PutS("Content-Type: text/html\r\n", out);
log << log2 << "Request: response: 403 Forbidden" << logend;
break;
default:
FCGX_PutS("Status: 200 OK\r\n", out);
if( role != authorizer )
FCGX_PutS("Content-Type: text/html\r\n", out);
}
}
if( compressing )
FCGX_PutS("Content-Encoding: deflate\r\n", out);
FCGX_PutS(headers.str().c_str(), out);
FCGX_PutS("\r\n", out);
}
void Request::AddDebugInfo()
{
const std::string & d = debug.str();
if( !d.empty() )
{
page << "\n<!--\n";
page << d;
page << "\n-->\n";
}
}
// !! to powinno isc do kontrolera app
void Request::SendPage(bool compressing, const std::string & source_ref)
{
const std::string * source = &source_ref;
bool raw = is_item && item.content_type == Item::ct_raw && status == WINIX_ERR_OK &&
function && (function->fun.url == "cat" || function->fun.url == "run");
if( config->html_filter && !raw )
{
html_filter.TrimWhite(true);
html_filter.BreakLines(60);
html_filter.InsertTabs(2);
html_filter.CheckOrphans(HTMLFilter::lang_pl, HTMLFilter::orphan_160space);
html_filter.Filter(*source, clean_html);
source = &clean_html;
}
if( compressing )
compress.CompressAndPut(source->c_str(), source->length(), out);
else
FCGX_PutS(source->c_str(), out);
}
void Request::SendAll()
{
const std::string & source = page.str();
Header header = h_200;
bool compressing = config->compression && role == responder && redirect_to.empty() && x_sendfile.empty() &&
!browser_msie && !browser_konqueror &&
accept_encoding_parser.AcceptDeflate() && source.size() >= 512;
if( status == WINIX_ERR_NO_ITEM || status == WINIX_ERR_NO_FUNCTION || status == WINIX_ERR_UNKNOWN_PARAM )
header = h_404;
if( status == WINIX_ERR_PERMISSION_DENIED || status == WINIX_ERR_CANT_CHANGE_USER || status == WINIX_ERR_CANT_CHANGE_GROUP )
header = h_403;
SendSessionCookie();
SendHeaders(compressing, header);
if( !redirect_to.empty() || !x_sendfile.empty() )
// if there is a redirect or a file to send then we do not send a content
return;
if( header == h_200 && role == authorizer && is_item && item.auth != Item::auth_none )
// if there is an item and the item has 'file' storage we do not send a content
return;
// adding debug info if exists
AddDebugInfo();
// sending content
SendPage(compressing, source);
}
bool Request::IsParam(const char * param_name)
{
ParamTab::iterator i;
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
{
// !! make sure that exists std::string::operator==(const char*)
// (optimization)
if( i->name == param_name )
return true;
}
return false;
}
const std::string & Request::ParamValue(const char * param_name)
{
ParamTab::iterator i;
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
{
if( i->name == param_name )
{
return i->value;
}
}
return str_empty;
}