part II of rewriting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@635 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
359
core/request.cpp
359
core/request.cpp
@@ -7,18 +7,12 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#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"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +21,6 @@
|
||||
Request::Request() : char_empty(0)
|
||||
{
|
||||
id = 0;
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +30,6 @@ void Request::SetConfig(Config * pconfig)
|
||||
}
|
||||
|
||||
|
||||
void Request::Init()
|
||||
{
|
||||
compress.Init();
|
||||
}
|
||||
|
||||
|
||||
void Request::ClearPostFileTmp()
|
||||
@@ -67,7 +56,9 @@ void Request::Clear()
|
||||
if( ++id == 0 )
|
||||
++id;
|
||||
|
||||
get_tab.clear();
|
||||
ClearPostFileTmp();
|
||||
|
||||
get_tab.clear();
|
||||
post_tab.clear();
|
||||
post_file_tab.clear();
|
||||
cookie_tab.clear();
|
||||
@@ -78,17 +69,16 @@ void Request::Clear()
|
||||
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_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_fcgi_role = &char_empty;
|
||||
env_content_type = &char_empty;
|
||||
env_http_accept_encoding = &char_empty;
|
||||
env_fcgi_role = &char_empty;
|
||||
env_content_type = &char_empty;
|
||||
|
||||
|
||||
session = 0;
|
||||
@@ -107,8 +97,6 @@ void Request::Clear()
|
||||
redirect_to.clear();
|
||||
x_sendfile.clear();
|
||||
send_as_attachment = false;
|
||||
|
||||
plugin.Call(WINIX_REQUEST_CLEAR);
|
||||
}
|
||||
|
||||
|
||||
@@ -158,14 +146,14 @@ return true;
|
||||
|
||||
|
||||
|
||||
std::string * Request::PostVar(const char * var)
|
||||
const std::string & Request::PostVar(const char * var)
|
||||
{
|
||||
PostTab::iterator p = post_tab.find(var);
|
||||
|
||||
if( p == post_tab.end() )
|
||||
return 0;
|
||||
return str_empty;
|
||||
|
||||
return &(p->second);
|
||||
return p->second;
|
||||
}
|
||||
|
||||
|
||||
@@ -187,11 +175,6 @@ return true;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
|
||||
|
||||
void Request::PrintGetTab()
|
||||
{
|
||||
debug << "get_tab: " << get_tab.size() << "\n";
|
||||
@@ -215,106 +198,6 @@ char ** e;
|
||||
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()
|
||||
@@ -330,216 +213,6 @@ 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;
|
||||
|
Reference in New Issue
Block a user