winix/core/request.cpp

375 lines
5.2 KiB
C++
Executable File

/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008, Tomasz Sowa
* All rights reserved.
*
*/
#include "request.h"
Request::Request()
{
Clear();
}
void Request::Clear()
{
// warning: don't clear in, out, err, env
get_table.clear();
post_table.clear();
cookie_table.clear();
method = none;
headers.str("");
page.str("");
debug.str("");
env_request_method = 0;
env_request_uri = 0;
env_http_cookie = 0;
session = 0;
result = err404; // tutaj moze cos lepszego, cos w stylu 'not implemented'
dir = -1;
cur_dir_table.clear();
item_table.clear();
dir_table.clear();
item.Clear();
str.clear();
}
void Request::CopyFirstItem()
{
if( !request.item_table.empty() )
request.item = request.item_table[0];
else
{
request.item.Clear();
log << log1 << "Request::CopyFirstItem: item_table is empty" << logend;
request.result = err_internal;
}
}
// value can be null
void Request::SetCookie(const char * name, const char * value)
{
request.headers << "Set-Cookie: " << name << "=";
if( value && value[0]!=0 )
request.headers << value;
else
request.headers << "\"\"";
request.headers << "; path=/\r\n";
}
void Request::SetCookie(const char * name, long value)
{
request.headers << "Set-Cookie: " << name << "=" << value << "; path=/\r\n";
}
bool Request::IsPostVar(const char * var)
{
PostTable::iterator p;
p = post_table.find(var);
if( p == post_table.end() )
return false;
return true;
}
std::string & Request::PostVar(const char * var)
{
PostTable::iterator p;
p = post_table.find(var);
if( p == post_table.end() )
throw Error();
return p->second;
}
//
void Request::PrintGetTable()
{
debug << "get_table: " << get_table.size() << "\n";
for(GetTable::iterator i = get_table.begin() ; i != get_table.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;
}
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 = FCGX_GetParam("REQUEST_METHOD", env);
env_request_uri = FCGX_GetParam("REQUEST_URI", env);
env_http_cookie = FCGX_GetParam("HTTP_COOKIE", env);
}
void Request::CheckMethod()
{
method = none;
if( !env_request_method )
return;
if( env_request_method[0] == 'G' )
method = get;
else
if( env_request_method[0] == 'P' )
method = post;
}
void Request::ReadParameters()
{
// some parameters (get) we have always
// if( method == get )
{
GetParser get_parser(env_request_uri, get_table);
get_parser.Parse();
}
if( method == post )
{
PostParser post_parser(in, post_table);
post_parser.Parse();
}
CookieParser cookie_parser(env_http_cookie, cookie_table);
cookie_parser.Parse();
}
// reading everything
void Request::Read()
{
ReadEnvVariables();
CheckMethod();
ReadParameters();
}
void Request::SendAll()
{
if( result == redirect )
{
FCGX_PutS("Status: 301 Moved Permanently\r\n", out);
FCGX_FPrintF(out, "Location: %s\r\n", str.c_str());
log << log2 << "Redirect into: " << str << logend;
}
else
{
FCGX_PutS("Status: 200 OK\r\n", out);
FCGX_PutS("Content-Type: Text/Html\r\n", out);
}
FCGX_PutS(headers.str().c_str(), out);
FCGX_PutS("\r\n", out);
if( result == redirect )
// if there is a redirect we do not send a content
return;
FCGX_PutS(page.str().c_str(), out);
const std::string & d = debug.str();
if( !d.empty() )
{
FCGX_PutS("\n<!--\n", out);
FCGX_PutS(d.c_str(), out);
FCGX_PutS("\n-->\n", out);
}
}
// ----------------
/*
unsigned char Request::tohex(unsigned char polowa)
{
// polowa z zakresu 0-15
polowa = polowa & 0xf;
if(polowa<10)
polowa += '0';
else
polowa = polowa - 10 + 'A';
return polowa;
}
// cala wartosc zapisywana jest w postaci hex
string Request::ToHex(const string & dane)
{
string wynik;
unsigned int i;
for(i=0 ; i<dane.length() ; ++i)
{
wynik += tohex(dane[i] >> 4);
wynik += tohex(dane[i] & 0xf);
}
return wynik;
}
string Request::HexToString(const string & dane)
{
string wynik;
unsigned int i;
unsigned char znak,znak1,znak2;
if(dane.length()==0)
return wynik;
for(i=0 ; i<dane.length()-1 ; i+=2)
{
znak1 = parsujhex(dane[i]);
znak2 = parsujhex(dane[i+1]);
znak = (znak1 << 4) + znak2;
wynik += znak;
}
return wynik;
}
string Request::ToUrl(const string & dane)
{
string wynik;
unsigned int i;
for(i=0 ; i<dane.length() ; ++i)
{
if( (dane[i]>='a' && dane[i]<='z') ||
(dane[i]>='A' && dane[i]<='Z') ||
(dane[i]>='0' && dane[i]<='9') ||
dane[i]=='_' )
{
wynik += dane[i];
}
else
{
wynik += '%';
wynik += tohex(dane[i] >> 4);
wynik += tohex(dane[i] & 0xf);
}
}
return wynik;
}
*/