diff --git a/core/app.cpp b/core/app.cpp index fb57ad9..f3493ff 100755 --- a/core/app.cpp +++ b/core/app.cpp @@ -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); - + 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; } diff --git a/core/misc.cpp b/core/misc.cpp index 3457753..5d24aa5 100755 --- a/core/misc.cpp +++ b/core/misc.cpp @@ -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= '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(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) { diff --git a/core/misc.h b/core/misc.h index 5a26aa9..01acd56 100755 --- a/core/misc.h +++ b/core/misc.h @@ -630,12 +630,46 @@ time_t Time(const tm * par); tm Time(time_t par); -void UrlEncode(const std::string & in, std::string & out, bool clear_out = true); + +template +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(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); diff --git a/core/request.cpp b/core/request.cpp index f063706..3bc5ffd 100755 --- a/core/request.cpp +++ b/core/request.cpp @@ -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; diff --git a/core/request.h b/core/request.h index 5e7c459..75b1c55 100755 --- a/core/request.h +++ b/core/request.h @@ -105,13 +105,10 @@ struct Request std::vector 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 diff --git a/core/system.cpp b/core/system.cpp index 6b26b59..4a5d33c 100755 --- a/core/system.cpp +++ b/core/system.cpp @@ -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 ; irequest->redirect_to += '/'; - cur->request->redirect_to += cur->request->function->fun.url; - + 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() ) diff --git a/core/system.h b/core/system.h index c4fcf60..3e0a4b3 100755 --- a/core/system.h +++ b/core/system.h @@ -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