UrlEncode() methods from core/misc.h are now thread safe

This commit is contained in:
Tomasz Sowa 2021-06-23 17:04:07 +02:00
parent 99df807095
commit 2c5062ba22
3 changed files with 104 additions and 76 deletions

View File

@ -1264,8 +1264,15 @@ void UrlEncode(const char * in, std::string & out, bool clear_out)
if( clear_out ) if( clear_out )
out.clear(); out.clear();
pt::TextStream stream;
for(size_t i=0 ; in[i] != 0 ; ++i) for(size_t i=0 ; in[i] != 0 ; ++i)
UrlEncode(in[i], out, false); {
UrlEncode(in[i], stream, true);
for(size_t s=0 ; s < stream.size() ; ++s)
out += stream[s];
}
} }
@ -1275,39 +1282,44 @@ void UrlEncode(const std::string & in, std::string & out, bool clear_out)
} }
//void UrlEncode(const wchar_t * in, std::string & out, bool clear_out)
void UrlEncode(const wchar_t * in, std::string & out, bool clear_out) //{
{ // if( clear_out )
static std::string ain; // out.clear();
//
pt::wide_to_utf8(in, ain); // pt::TextStream stream;
//
if( clear_out ) // for(size_t i=0 ; in[i] != 0 ; ++i)
out.clear(); // {
// UrlEncode(in[i], stream, true);
for(size_t i=0 ; i < ain.size() ; ++i) //
UrlEncode(ain[i], out, false); // for(size_t s=0 ; s < stream.size() ++i)
} // out += stream[s];
// }
//}
void UrlEncode(const std::wstring & in, std::string & out, bool clear_out) //void UrlEncode(const std::wstring & in, std::string & out, bool clear_out)
{ //{
UrlEncode(in.c_str(), out, clear_out); // UrlEncode(in.c_str(), out, clear_out);
} //}
void UrlEncode(const wchar_t * in, std::wstring & out, bool clear_out) void UrlEncode(const wchar_t * in, std::wstring & out, bool clear_out)
{ {
static std::string ain;
pt::wide_to_utf8(in, ain);
if( clear_out ) if( clear_out )
out.clear(); out.clear();
for(size_t i=0 ; i < ain.size() ; ++i) pt::WTextStream stream;
UrlEncode(ain[i], out, false);
for(size_t i=0 ; in[i] != 0 ; ++i)
{
UrlEncode(in[i], stream, true);
for(size_t s=0 ; s < stream.size() ; ++s)
out += stream[s];
}
} }
@ -1318,6 +1330,27 @@ void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out)
void UrlEncode(const pt::TextStream & in, pt::TextStream & out, bool clear_out)
{
if( clear_out )
out.clear();
for(size_t i=0 ; i < in.size() ; ++i)
UrlEncode(in.get_char(i), out, false);
}
void UrlEncode(const pt::WTextStream & in, pt::WTextStream & out, bool clear_out)
{
if( clear_out )
out.clear();
for(size_t i=0 ; i < in.size() ; ++i)
UrlEncode(in.get_wchar(i), out, false);
}
bool UrlDecodeFromHex(int c, int & out) bool UrlDecodeFromHex(int c, int & out)
{ {

View File

@ -678,14 +678,11 @@ int SelectFileType(const std::wstring & file_name);
// thread safe template<typename StreamType>
template<typename char_type, size_t stack_size, size_t heap_block_size> void UrlEncode(char c, StreamType & out, bool clear_out = true)
void UrlEncode(char c,
pt::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{ {
char buffer[10]; char buffer[10];
size_t buflen = sizeof(buffer)/sizeof(char); size_t buflen = sizeof(buffer)/sizeof(char);
if( clear_out ) if( clear_out )
out.clear(); out.clear();
@ -710,11 +707,33 @@ size_t buflen = sizeof(buffer)/sizeof(char);
} }
// thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size> template<typename StreamType>
void UrlEncode(const char * in, void UrlEncode(wchar_t c, StreamType & out, bool clear_out = true)
pt::TextStreamBase<char_type, stack_size, heap_block_size> & out, {
bool clear_out = true) char buffer[10];
size_t buflen = sizeof(buffer)/sizeof(char);
if( clear_out )
out.clear();
// this conversion is not using surrogate pairs if wchar_t is 2 bytes long
size_t utf8_len = pt::int_to_utf8(static_cast<int>(c), buffer, buflen);
if( utf8_len > 0 )
{
for(size_t i=0 ; i < utf8_len ; ++i)
{
UrlEncode(buffer[i], out, false);
}
}
}
template<typename StreamType>
void UrlEncode(const char * in, StreamType & out, bool clear_out = true)
{ {
if( clear_out ) if( clear_out )
out.clear(); out.clear();
@ -724,67 +743,44 @@ void UrlEncode(const char * in,
} }
// thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size> template<typename StreamType>
void UrlEncode(const std::string & in, void UrlEncode(const std::string & in, StreamType & out, bool clear_out = true)
pt::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{ {
UrlEncode(in.c_str(), out, clear_out); UrlEncode(in.c_str(), out, clear_out);
} }
// not thread safe template<typename StreamType>
template<typename char_type, size_t stack_size, size_t heap_block_size> void UrlEncode(const wchar_t * in, StreamType & out, bool clear_out = true)
void UrlEncode(const wchar_t * in,
pt::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{ {
static std::string ain;
pt::wide_to_utf8(in, ain);
if( clear_out ) if( clear_out )
out.clear(); out.clear();
for(size_t i=0 ; i < ain.size() ; ++i) for(size_t i=0 ; in[i] != 0 ; ++i)
UrlEncode(ain[i], out, false); UrlEncode(in[i], out, false);
} }
// not thread safe template<typename StreamType>
template<typename char_type, size_t stack_size, size_t heap_block_size> void UrlEncode(const std::wstring & in, StreamType & out, bool clear_out = true)
void UrlEncode(const std::wstring & in,
pt::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{ {
UrlEncode(in.c_str(), out, clear_out); UrlEncode(in.c_str(), out, clear_out);
} }
// no thread safe
template<class StringType>
void UrlEncode(char c, StringType & out, bool clear_out = true)
{
static pt::TextStream tmp;
UrlEncode(c, tmp);
tmp.to_str(out, clear_out);
}
// !! IMROVE ME we need some UrlEncode methods with pt::TextBuffer instead of std::string
void UrlEncode(const char * in, std::string & out, bool clear_out = true); 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 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 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 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 wchar_t * in, std::wstring & out, bool clear_out = true);
void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out = true); void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out = true);
void UrlEncode(const pt::TextStream & in, pt::TextStream & out, bool clear_out = true);
void UrlEncode(const pt::WTextStream & in, pt::WTextStream & out, bool clear_out = true);
/* /*

View File

@ -41,14 +41,13 @@ namespace Winix
namespace TemplatesFunctions namespace TemplatesFunctions
{ {
static std::string urlencode_tmp; // not thread safe
static std::string qencode_tmp; static std::string qencode_tmp;
void fil_urlencode(Info & i) void fil_urlencode(Info & i)
{ {
UrlEncode(i.in.Str(), urlencode_tmp); UrlEncode(i.in.Str(), i.out);
i.out << R(urlencode_tmp);
} }