| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 1.91

 

http_escape

#include "sflhttp.h"
char *
http_escape (
    const char *string,
    char *result)

Synopsis

Performs HTTP escaping on a string. This works as follows: all characters except alphanumerics and spaces are converted into the 3-byte sequence "%xx" where xx is the character's hexadecimal value; spaces are replaced by '+'. Line breaks are stored as "%0D%0A", where a 'line break' is any one of: "\n", "\r", "\n\r", or "\r\n". If the result buffer is NULL, calculates the required size, allocates a block of memory, and returns that. Otherwise, returns result, which must be large enough for the escaping operation (see http escape size()). When you all http escape() with a null target block, you must free the returned block using mem_free(). Returns NULL if it could not allocate a target block as required.

Source Code - (sflhttp.c)

{
    static char
        hex_char [] = "0123456789ABCDEF";
    char
        *target;                        /*  Where we store the result        */

    ASSERT (string);
    if (result == NULL)
        if ((result = mem_alloc (http escape size (string))) == NULL)
            return (NULL);              /*  Could not allocate a block       */

    target = result;
    while (*string)
      {
        if (isalnum (*string))          /*  Don't escape letters or digits   */
            *target++ = *string;
        else
        if (*string == ' ')             /*  Spaces are replaced by '+'       */
            *target++ = '+';
        else
        if (*string == '\n' || *string == '\r')
          {
            if ((string [1] == '\n' || string [1] == '\r')
            &&  (string [1] != *string))
                string++;
            *target++ = '%';            /*  New line becomes %0A%0D          */
            *target++ = '0';
            *target++ = 'A';
            *target++ = '%';
            *target++ = '0';
            *target++ = 'D';
          }
        else
          {
            *target++ = '%';            /*  Some other escaped character     */
            *target++ = hex_char [(byte) *string >> 4];
            *target++ = hex_char [(byte) *string & 15];
          }
        string++;
      }
    *target = '\0';                     /*  Terminate target string          */
    return (result);
}

| << | < | > | >> iMatix Copyright © 1996-98 iMatix