Back to Blog

VuGen: HTML/URL/Text Conversion

The web_convert_param function either converts HTML text to plain text or URL, or converts plain text to URL.

HTML format uses codes for some non-alphanumerical characters, such as & for ampersand. URLs do not support non alpha-numerical characters and use the escape sequence %number, to represent them. To use an HTML value in a URL, you must therefore convert it into an escape sequence or plain text. For example, an ampersand is represented in a URL as %26. When you use this function to convert the HTML to plain text, it converts it to the way the character would appear in a browser.

So if you had captured the a variable called pScottString with a web_reg_save_param:

<Loadtester>&

You could simply put a line of code in that looks like this:

web_convert_param("pScottString", "SourceEncoding=HTML", "TargetEncoding=PLAIN", LAST);

and you would end up with the following:

<loadtester>&

Play around with the SourceEncoding and TargetEncoding going from HTML to URL to PLAIN and mix them up.

——-

Chris Jeans added this:
This function decodes URL encoded strings by changing the %HH characters to the ascii character in a string.

e.g. %2F -> /

int URL_decode(char* string)
{
    int i;
    char code;
    char stringNew[256];
    char* start = string;

    if (string == NULL) /* don't try to deref NULL ptrs */
    return -1;

    /* walk the source string */
    for (i = 0; *string; string++, i++)
    {
        if( *string == '+' )
        /* '+' changed back to space */
        stringNew[i] = ' ';
        else if ( *string == '%' )
        {
            /* hex-encoded (ASCII) chars restored */
            if (sscanf(string+1, "%2x", &code) != 1)
            code = '?'; /* failed to scan, make it something */
            stringNew[i] = code;
            string +=2; /* hop over the scan chars */
        }
        else
        stringNew[i] = *string;
    }

    stringNew[i] = '\0'; /* make sure new string is terminated */

    sprintf( start, "%s", stringNew );

    return 0;
}

// Usage example
Action1()
{
    int rc;
    char buffer[128];

    // 1098227619000%2F1098278560720%2Foa-inoa-254551
    //sprintf( buffer, "%s", "123%2d123%2d123%2d123" );
    sprintf( buffer, "%s", "1098227619000%2F1098278560720%2Foa-inoa-254551" );

    if( ( rc = URL_decode(buffer) ) != 0 )
    {
        lr_error_message("URL_decode(): rc = %d", rc );
        return rc;
    }

    lr_output_message("buffer: '%s'", buffer );
    return 0;
}
Back to Blog