Skip to content

Commit

Permalink
Update uri.cpp
Browse files Browse the repository at this point in the history
fix Corvusoft#384 bug add url_encode method
  • Loading branch information
zzl-010 authored Jul 1, 2019
1 parent f74f932 commit c07e8c9
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions source/corvusoft/restbed/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ namespace restbed
return Uri( value );
}

string Uri::url_encode(const string& input)
{
string output = String::empty;
string hex = "0123456789abcdef";
for(string::size_type i=0;i<input.size();++i) {
if((('a' <= input[i]) && (input[i] <= 'z')) || (('A' <= input[i]) && (input[i] <= 'Z')) || \
(('0' <= input[i]) && (input[i] <= '9')) || (input[i] == '-') || (input[i] == '_') || \
(input[i] == '.') || (input[i] == '~') || (input[i] == '/') || (input[i] == '*')) {
output.push_back(input[i]);

} else if (input[i] == ' ') {
output.push_back('+'); // Or is it "%20"?

} else {
output.push_back('%');
output.push_back(hex[input[i] >> 4]);
output.push_back(hex[input[i] & 0x0f]);

}

}
return output; // Better free() this up after use.

}

string Uri::decode( const Bytes& value )
{
return decode( string( value.begin( ), value.end( ) ) );
Expand All @@ -102,20 +127,21 @@ namespace restbed
{
string result = String::empty;

for ( string::size_type index = 0; index not_eq value.length( ); index++ )
string valuedecode = url_encode(value);
for ( string::size_type index = 0; index not_eq valuedecode.length( ); index++ )
{
if ( value[ index ] == '%' )
if ( valuedecode[ index ] == '%' )
{
char hexidecimal[ 3 ] = { 0 };
hexidecimal[ 0 ] = value[ ++index ];
hexidecimal[ 1 ] = value[ ++index ];
hexidecimal[ 0 ] = valuedecode[ ++index ];
hexidecimal[ 1 ] = valuedecode[ ++index ];

char byte = static_cast< char >( strtol( hexidecimal, nullptr, 16 ) );
result.push_back( byte );
}
else
{
result.push_back( value[ index ] );
result.push_back( valuedecode[ index ] );
}
}

Expand Down

0 comments on commit c07e8c9

Please sign in to comment.