You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.1 KiB
C
59 lines
1.1 KiB
C
#include "escape.h"
|
|
|
|
#include <strings.h>
|
|
|
|
const char* http_escape( const char* str, char* out, unsigned int limit, const char* allowed )
|
|
{
|
|
const char* result = out;
|
|
|
|
// Based on RFC 3986 Uniform Resource Identifier (URI): Generic Syntax
|
|
for(; *str; ++str ) {
|
|
switch( *str ) {
|
|
// Reserved characters (sometimes encoded)
|
|
case ':':
|
|
case '/':
|
|
case '?':
|
|
case '#':
|
|
case '@':
|
|
case '[':
|
|
case ']':
|
|
case '+':
|
|
case ',':
|
|
case '(':
|
|
case ')':
|
|
case '!':
|
|
case '*':
|
|
if( !index(allowed, *str) ) {
|
|
goto escape;
|
|
}
|
|
// Unreserved characters (Never encoded)
|
|
case 'a' ... 'z':
|
|
case 'A' ... 'Z':
|
|
case '0' ... '9':
|
|
case '~':
|
|
case '.':
|
|
case '-':
|
|
case '_':
|
|
*out = *str;
|
|
if( limit ) { --limit; ++out; }
|
|
break;
|
|
default:
|
|
{
|
|
escape:
|
|
*out = '%';
|
|
if( limit ) { --limit; ++out; }
|
|
|
|
*out = "0123456789ABCDEF"[ (*str >> 4) % 16 ];
|
|
if( limit ) { --limit; ++out; }
|
|
|
|
*out = "0123456789ABCDEF"[ (*str >> 0) % 16 ];
|
|
if( limit ) { --limit; ++out; }
|
|
}
|
|
}
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
return result;
|
|
}
|