added: to misc:

UrlEncode() for char->wstring
         UrlEncode() for wstring->wstring
removed: Request::redirect_url_encoded flag
         the Request::redirect_to string should always be url-encoded
changed: in UrnEncode()
         now characters like '#' and '/' are not allowed in an url
         (will be url-encoded)
         


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@807 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2012-02-19 00:59:08 +00:00
parent 1a51b1adc7
commit 97c7edafd6
7 changed files with 130 additions and 50 deletions

View File

@ -178,8 +178,7 @@ void App::BaseUrlRedirect(int code)
system.PutUrlProto(config.use_ssl, cur.request->redirect_to);
cur.request->redirect_to += config.base_url;
AssignString(cur.request->env_request_uri, cur.request->redirect_to, false);
// cur.request->env_request_uri should not be UrlEncoded
cur.request->redirect_url_encoded = true;
// cur.request->env_request_uri should not be UrlEncoded because it contains slashes
cur.request->redirect_type = code;
}
@ -727,11 +726,7 @@ void App::SendHeadersRedirect()
break;
}
if( !cur.request->redirect_url_encoded )
UrlEncode(cur.request->redirect_to, cur.request->aredirect_to);
else
PT::WideToUTF8(cur.request->redirect_to, cur.request->aredirect_to);
FCGX_FPrintF(fcgi_request.out, "Location: %s\r\n", cur.request->aredirect_to.c_str());
log << log2 << "App: redirect to: " << cur.request->aredirect_to << logend;
}

View File

@ -991,49 +991,66 @@ return res;
void UrlEncode(const std::string & in, std::string & out, bool clear_out)
void UrlEncode(const char * in, std::string & out, bool clear_out)
{
char buffer[10];
if( clear_out )
out.clear();
// !! we should not use such characters here: '/' '#'
for(size_t i=0 ; i<in.size() ; ++i)
{
if( (in[i] >= 'a' && in[i] <= 'z') ||
(in[i] >= 'A' && in[i] <= 'Z') ||
(in[i] >= '0' && in[i] <= '9') ||
in[i] == '.' || in[i] == ',' || in[i] == '/' || in[i] == ':' || in[i] == '#' ||
in[i] == '-' || in[i] == '_' || in[i] == '(' || in[i] == ')' )
{
out += in[i];
}
else
{
Toa(static_cast<unsigned char>(in[i]), buffer, 10, 16);
out += '%';
if( buffer[1] == 0 )
out += '0'; // there is only one character in the buffer
out += buffer;
}
}
for(size_t i=0 ; in[i] != 0 ; ++i)
UrlEncode(in[i], out, false);
}
void UrlEncode(const std::wstring & in, std::string & out, bool clear_out)
void UrlEncode(const std::string & in, std::string & out, bool clear_out)
{
UrlEncode(in.c_str(), out, clear_out);
}
void UrlEncode(const wchar_t * in, std::string & out, bool clear_out)
{
static std::string ain;
PT::WideToUTF8(in, ain);
UrlEncode(ain, out, clear_out);
if( clear_out )
out.clear();
for(size_t i=0 ; i < ain.size() ; ++i)
UrlEncode(ain[i], out, false);
}
void UrlEncode(const std::wstring & in, std::string & out, bool clear_out)
{
UrlEncode(in.c_str(), out, clear_out);
}
void UrlEncode(const wchar_t * in, std::wstring & out, bool clear_out)
{
static std::string ain;
PT::WideToUTF8(in, ain);
if( clear_out )
out.clear();
for(size_t i=0 ; i < ain.size() ; ++i)
UrlEncode(ain[i], out, false);
}
void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out)
{
UrlEncode(in.c_str(), out, clear_out);
}
void QEncodeAddChar(wchar_t c, std::string & out)
{

View File

@ -630,12 +630,46 @@ time_t Time(const tm * par);
tm Time(time_t par);
template<class StringType>
void UrlEncode(char c, StringType & out, bool clear_out = true)
{
char buffer[10];
size_t buflen = sizeof(buffer)/sizeof(char);
if( clear_out )
out.clear();
if( (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '.' || c == ',' || c == '-' || c == '_' || c == '(' || c == ')' )
{
out += c;
}
else
{
Toa(static_cast<unsigned char>(c), buffer, buflen, 16);
out += '%';
if( buffer[1] == 0 )
out += '0'; // there is only one character in the buffer
for(size_t i=0 ; buffer[i] != 0 ; ++i)
out += buffer[i];
}
}
void UrlEncode(const char * in, std::string & out, bool clear_out = true);
void UrlEncode(const std::string & in, std::string & out, bool clear_out = true);
void UrlEncode(const wchar_t * in, std::string & out, bool clear_out = true);
void UrlEncode(const std::wstring & in, std::string & out, bool clear_out = true);
void UrlEncode(const wchar_t * in, std::wstring & out, bool clear_out = true);
void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out = true);
void QEncode(const std::wstring & in, std::string & out, bool clear = true);
void RemovePostFileTmp(PostFileTab & post_file_tab);

View File

@ -78,7 +78,6 @@ void Request::Clear()
browser_msie = false;
redirect_to.clear();
redirect_url_encoded = false;
redirect_type = 303;
x_sendfile.clear();
send_as_attachment = false;

View File

@ -105,13 +105,10 @@ struct Request
std::vector<Item> item_tab;
// if not empty means an address for redirecting to
// it should be url-encoded
std::wstring redirect_to;
std::string aredirect_to;
// if false then we should use url encode on 'redirect_to' before sending the header
// default: false
bool redirect_url_encoded;
// a redirect type
// following redirect types are supported:
// 300 Multiple Choices

View File

@ -112,8 +112,12 @@ bool ssl = false;
}
// !! IMPROVE ME
// !! mozna zrobic jakas obsluge kiedy nie mozemy sie redirectnac, np gdy wystapil blad
// !! moze zwracac jakas wartosc?
/*
postfix will not be UrlEncoded
*/
void System::RedirectTo(const Item & item, const wchar_t * postfix)
{
PutUrlProto(config->use_ssl, cur->request->redirect_to);
@ -137,6 +141,9 @@ void System::RedirectTo(const Item & item, const wchar_t * postfix)
/*
postfix will not be UrlEncoded
*/
void System::RedirectTo(long item_id, const wchar_t * postfix)
{
PutUrlProto(config->use_ssl, cur->request->redirect_to);
@ -174,12 +181,16 @@ void System::RedirectTo(long item_id, const wchar_t * postfix)
}
void System::RedirectTo(const std::wstring & url)
/*
url will not be UrlEncoded
*/
void System::RedirectTo(const wchar_t * url)
{
PutUrlProto(config->use_ssl, cur->request->redirect_to);
cur->request->redirect_to += config->base_url;
if( !url.empty() && url[0] == '/' )
if( url[0] == '/' )
{
// absolute path
cur->request->redirect_to += url;
@ -201,6 +212,19 @@ void System::RedirectTo(const std::wstring & url)
}
/*
url will not be UrlEncoded
*/
void System::RedirectTo(const std::wstring & url)
{
RedirectTo(url.c_str());
}
/*
params will be UrlEncoded
*/
void System::AddParams(const ParamTab & param_tab, std::wstring & str, bool clear_str)
{
if( clear_str )
@ -209,19 +233,22 @@ void System::AddParams(const ParamTab & param_tab, std::wstring & str, bool clea
for(size_t i=0 ; i<param_tab.size() ; ++i)
{
str += '/';
str += param_tab[i].name;
UrlEncode(param_tab[i].name, str, false);
if( !param_tab[i].value.empty() )
{
str += ':';
str += param_tab[i].value;
UrlEncode(param_tab[i].value, str, false);
}
}
}
void System::RedirectWithFunctionAndParamsTo(const std::wstring & url)
/*
url will not be UrlEncoded
params will be UrlEncoded
*/
void System::RedirectWithFunctionAndParamsTo(const wchar_t * url)
{
RedirectTo(url);
@ -230,11 +257,20 @@ void System::RedirectWithFunctionAndParamsTo(const std::wstring & url)
cur->request->redirect_to += '/';
cur->request->redirect_to += cur->request->function->fun.url;
AddParams(cur->request->param_tab, cur->request->redirect_to, false);
}
/*
url will not be UrlEncoded
params will be UrlEncoded
*/
void System::RedirectWithFunctionAndParamsTo(const std::wstring & url)
{
RedirectWithFunctionAndParamsTo(url.c_str());
}
void System::RedirectToLastDir()
{
if( !cur->request->dir_tab.empty() )

View File

@ -80,7 +80,9 @@ public:
void PutUrlProto(bool can_use_ssl, std::wstring & str, bool clear_str = true);
void RedirectTo(const Item & item, const wchar_t * postfix = 0);
void RedirectTo(long item_id, const wchar_t * postfix = 0);
void RedirectTo(const wchar_t * url);
void RedirectTo(const std::wstring & url);
void RedirectWithFunctionAndParamsTo(const wchar_t * url);
void RedirectWithFunctionAndParamsTo(const std::wstring & url);
void RedirectToLastDir();
void RedirectToLastItem(); // redirect to an item if exists or to the last directory