⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sflhttp.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 5 页
字号:
}


/*  ---------------------------------------------------------------------[<]-
    Function: http_unescape_hex

    Synopsis: Removes HTTP hex escaping from a URL string, by expanding any
    sequences of characters %xx.
    ---------------------------------------------------------------------[>]-*/

char *
http_unescape_hex (
    char *string,
    char *result)
{
    char
        *target;                        /*  Where we store the result        */

    ASSERT (string);
    if (!result)                        /*  If result string is null,        */
        result = string;                /*    modify in place                */
    target = result;

    while (*string)
      {
        if (*string == '%'              /*  Unescape %xx sequence            */
        &&   string [1] && string [2])
          {
            string++;
            *target = decode_hex ((const char **) &string, 2);
            target++;             
          }
        else
          {
            *target++ = *string;        /*  Otherwise just copy              */
            string++;
          }
      }
    *target = '\0';                     /*  Terminate target string          */
    return (result);
}


/*  ---------------------------------------------------------------------[<]-
    Function: http_query2strt

    Synopsis: Parses a HTTP query string, building an array of strings of
    the format "name=value".  The query string is assumed to be in escaped
    format, so http_unescape() is always applied to the query string.
    Within the query string, field=value pairs are delimited by & or ;.
    Returns a pointer to the array.  The array is allocated dynamically.
    The array ends with a NULL string.  To free the table, call strtfree().
    If there was not enough memory to allocate the table, returns NULL.
    ---------------------------------------------------------------------[>]-*/

char **
http_query2strt (
    const char *original_query)
{
    char
        *query,                         /*  Local copy of query string       */
        *query_ptr,                     /*  Pointer into query string        */
        *query_next,                    /*  Pointer to next query chunk      */
        **strings;                      /*  Returned string array            */
    int
        char_nbr,                       /*  Index into query string          */
        string_count,                   /*  Size of string table             */
        string_nbr;                     /*  Index into string table          */

    ASSERT (original_query);

    if (*original_query == '&')         /*  Skip leading & if present        */
        original_query++;

    if ((query = mem_strdup (original_query)) == NULL)
        return (NULL);                  /*  Could not allocate memory        */

    /*  Break query string at & and ; delimiters and count strt size         */
    string_count = 1;                   /*  Last string has no delimiter     */
    for (char_nbr = 0; original_query [char_nbr]; char_nbr++)
        if (query [char_nbr] == '&' || query [char_nbr] == ';')
          {
            query [char_nbr] = '\0';
            string_count++;
          }

    /*  Allocate the array of pointers with one slot for the final NULL      */
    if ((strings = mem_alloc (sizeof (char *) * (string_count + 1))) == NULL)
      {
        mem_free (query);
        return (NULL);                  /*  Could not allocate memory        */
      }

    /*  Query string now consists of a series of substrings, each ending in
     *  a null character.  We have to unescape each substring, which we do
     *  in-place: the unescaped string is never larger than the original
     *  string.
     */
    query_ptr = query;
    for (string_nbr = 0; string_nbr < string_count; string_nbr++)
      {
        /*  Unescape next query string component                             */
        query_next = query_ptr + strlen (query_ptr) + 1;
        http_unescape (query_ptr, NULL);

        /*  Allocate space for "name=value" plus final null char             */
        strings [string_nbr] = mem_strdup (query_ptr);
        query_ptr = query_next;
      }
    strings [string_nbr] = NULL;        /*  Store final null pointer         */
    mem_free (query);                   /*  Release temporary memory         */
    return (strings);
}


/*  ---------------------------------------------------------------------[<]-
    Function: http_query2symb

    Synopsis: Parses a HTTP query string, and populates a symbol table with
    the resulting field values.  The query string is assumed to be escaped,
    so http_unescape() is always applied to the query string.  Within the
    query string, field=value pairs are delimited by & or ;.  Returns a
    SYMTAB pointer to the new table.  If there was not enough memory to
    allocate the table, returns NULL.
    ---------------------------------------------------------------------[>]-*/

SYMTAB *
http_query2symb (
    const char *query)
{
    char
        **strings;                      /*  Formatted string array           */
    SYMTAB
        *symtab;                        /*  Returned symbol table            */

    strings = http_query2strt (query);
    if (strings)
      {
        symtab = strt2symb (strings);
        strtfree (strings);
        return (symtab);
      }
    else
        return (NULL);                  /*  Couldn't create string table     */
}


/*  ---------------------------------------------------------------------[<]-
    Function: http_query2descr

    Synopsis: Parses a HTTP query string, and returns the values as a DESCR
    block, composed of null-delimited strings with an empty string at the
    end.  See strt2descr() and http_query2symb() for more details.  Returns
    the address of the allocated descriptor, or NULL if there was not
    enough memory.
    ---------------------------------------------------------------------[>]-*/

DESCR *
http_query2descr (
    const char *query)
{
    char
        **strings;                      /*  Formatted string array           */
    DESCR
        *descr;                         /*  Returned descriptor              */

    strings = http_query2strt (query);
    if (strings)
      {
        descr = strt2descr (strings);
        strtfree (strings);
        return (descr);
      }
    else
        return (NULL);                  /*  Couldn't create string table     */
}


/*  ---------------------------------------------------------------------[<]-
    Function: http_encode_meta

    Synopsis: Translates special characters into HTML/SGML metacharacters.
    The input buffer is not modified; you supply an output buffer and specify
    the maximum size of this buffer.  The input buffer must end in a null.
    Aftern calling, the input pointer is set to the character after the last
    encoded character.  Returns the final size of the translated data 
    excluding the final null byte.  If the resulting output data would be
    too long, translations stops.  If html is TRUE then the metacharacters
    amp, lt, gt and quot are not translated - this allows you to encode
    markup characters within HTML; otherwise they are translated and the
    output doesn't look like HTML.
    ---------------------------------------------------------------------[>]-*/

size_t
http_encode_meta (
    char    *output,
    char    **input,
    size_t  outmax,
    Bool    html)
{
    size_t
        space_left,                     /*  Space left in destination        */
        length;
    char
        *dest;                          /*  Pointer to result string         */

    ASSERT (input);
    ASSERT (*input);
    ASSERT (output);

    if (outmax == 0)                    /*  Special case for zero space      */
        return (0);

    space_left = outmax - 1;            /*  Allow for final null byte        */
    dest = output;
    while (**input && space_left > 0)
      {
        length = encode_meta_char (dest, **input, space_left, html);
        if (length)
          {
            space_left -= length;
            dest += length;
            (*input) ++;
          }
        else
            break;
      }
    *dest = '\0';
    return ((size_t) (dest - output));
}


/*  ---------------------------------------------------------------------[<]-
    Function: encode_meta_char

    Synopsis: Translates one character into HTML/SGML metacharacters.  You
    supply an output buffer and specify the maximum size of this buffer.
    Returns the final size of the translated data excluding the final null
    byte.  If the resulting data is too long, translation does not occur
    and the returned value is zero.  If html is TRUE then the metacharacters
    cr, amp, lt, gt and quot are not translated - this allows you to encode
    accented characters within HTML; otherwise they are translated and the
    output doesn't look like HTML.
    ---------------------------------------------------------------------[>]-*/

size_t
encode_meta_char (
    char  *output,
    char   code,
    size_t outmax,
    Bool   html)
{
    static char
        *meta [256];                    /*  Metacharacter translation table  */
    static Bool
        first_time = TRUE;              /*  First time flag                  */
    size_t
        length;                         /*  Length of translation            */
    char
        *meta_char,                     /*  Pointer through metachar string  */
        buffer [10];                    /*  Buffer for conversions           */

    /*  Initialise translation table first time through                      */
    if (first_time)
      {
        first_time = FALSE;
        memset (meta, 0, sizeof (meta));

#if (defined (__UNIX__) || defined (__WINDOWS__))
        /*  UNIX and Windows generally use ISO-8859-1 (Latin-1)              */
        meta [0x91] = "lsquo";
        meta [0x92] = "rsquo";
        meta [0xA1] = "iexcl";
        meta [0xA2] = "cent";
        meta [0xA3] = "pound";
        meta [0xA4] = "curren";
        meta [0xA5] = "yen";
        meta [0xA6] = "brvbar";
        meta [0xA7] = "sect";
        meta [0xA8] = "uml";
        meta [0xA9] = "copy";
        meta [0xAA] = "ordf";
        meta [0xAB] = "laquo";
        meta [0xAC] = "not";
        meta [0xAD] = "shy";
        meta [0xAE] = "reg";
        meta [0xAF] = "macr";
        meta [0xB0] = "deg";
        meta [0xB1] = "plusmn";
        meta [0xB2] = "sup2";
        meta [0xB3] = "sup3";
        meta [0xB4] = "acute";
        meta [0xB5] = "micro";
        meta [0xB6] = "para";
        meta [0xB7] = "middot";
        meta [0xB8] = "cedil";
        meta [0xB9] = "sup1";
        meta [0xBA] = "ordm";
        meta [0xBB] = "raquo";
        meta [0xBC] = "frac14";
        meta [0xBD] = "frac12";
        meta [0xBE] = "frac34";
        meta [0xBF] = "iquest";
        meta [0xC0] = "Agrave";
        meta [0xC1] = "Aacute";
        meta [0xC2] = "Acirc";
        meta [0xC3] = "Atilde";
        meta [0xC4] = "Auml";
        meta [0xC5] = "Aring";
        meta [0xC6] = "AElig";
        meta [0xC7] = "Ccedil";
        meta [0xC8] = "Egrave";
        meta [0xC9] = "Eacute";
        meta [0xCA] = "Ecirc";
        meta [0xCB] = "Euml";
        meta [0xCC] = "Igrave";
        meta [0xCD] = "Iacute";
        meta [0xCE] = "Icirc";
        meta [0xCF] = "Iuml";
        meta [0xD0] = "ETH";
        meta [0xD1] = "Ntilde";
        meta [0xD2] = "Ograve";
        meta [0xD3] = "Oacute";
        meta [0xD4] = "Ocirc";
        meta [0xD5] = "Otilde";
        meta [0xD6] = "Ouml";
        meta [0xD7] = "times";
        meta [0xD8] = "Oslash";
        meta [0xD9] = "Ugrave";
        meta [0xDA] = "Uacute";
        meta [0xDB] = "Ucirc";
        meta [0xDC] = "Uuml";
        meta [0xDD] = "Yacute";
        meta [0xDE] = "THORN";
        meta [0xDF] = "szlig";
        meta [0xE0] = "agrave";
        meta [0xE1] = "aacute";
        meta [0xE2] = "acirc";
        meta [0xE3] = "atilde";
        meta [0xE4] = "auml";
        meta [0xE5] = "aring";
        meta [0xE6] = "aelig";
        meta [0xE7] = "ccedil";
        meta [0xE8] = "egrave";
        meta [0xE9] = "eacute";
        meta [0xEA] = "ecirc";
        meta [0xEB] = "euml";
        meta [0xEC] = "igrave";
        meta [0xED] = "iacute";
        meta [0xEE] = "icirc";
        meta [0xEF] = "iuml";
        meta [0xF0] = "eth";
        meta [0xF1] = "ntilde";
        meta [0xF2] = "ograve";
        meta [0xF3] = "oacute";
        meta [0xF4] = "ocirc";
        meta [0xF5] = "otilde";
        meta [0xF6] = "ouml";
        meta [0xF7] = "divide";
        meta [0xF8] = "oslash";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -