Reuse, not rework
Home

License Awareness



Highly Reusable Software

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


NAME

librock_HTTP_astrcatUrlEncode - Encode special characters to create a safe URL string, per RFC1630
#License - #Source code - #Example Use -

SYNOPSIS

#include <librock/html.h>

char *
librock_HTTP_astrcatUrlEncode(
    char **ppasz,  /* librock_astring */
    const char *pString,int cbString)

DESCRIPTION

Perform encoding of characters so that "unsafe" characters as per RFC 1630 can appear within an HTTP request This passes alphanumerics, encodes ' ' as '+' and '%-encodes' everything else.

If you want '/' to appear literally, then handle it separately as in the example use below.

Returns *ppasz.

Typical use is

#ifdef librock_TYPICAL_USE_HTTP_astrcatUrlEncode
    char *asz = 0;
    librock_astrcat(&asz,"/");
    librock_HTTP_astrcatUrlEncode(&asz,"path",4);
    librock_astrcat(&asz,"/");
    librock_HTTP_astrcatUrlEncode(&asz,"to resource",11);

    printf("GET %s HTTP/1.0\n",asz);
    librock_astrfree(&asz);
#endif
To construct a URL encoded Query String, see librock_HTTP_SetFieldUrlEncoded, or use code similar to the following:
#ifdef librock_TYPICAL_USE_2_HTTP_astrcatUrlEncode
    char *asz = 0;
    librock_HTTP_astrcatUrlEncode(&asz,"field name",10);
    librock_astrcat(&asz,"=");
    librock_HTTP_astrcatUrlEncode(&asz,"/field value/",13);

    librock_astrcat(&asz,"&");
    librock_HTTP_astrcatUrlEncode(&asz,"field name 2",12);
    librock_astrcat(&asz,"=");
    librock_HTTP_astrcatUrlEncode(&asz,"/field value 2/",15);

    printf("GET /query.cgi?%s HTTP/1.0\n",asz);
    librock_astrfree(&asz);
#endif
If you want to construct a URL-encoded Query String for placing inside an HREF attribute, then build up the query string, then use librock_astrcatValueEncode. (If you have difficulty thinking of the order, remember that any encoding must be done in the reverse order it will be decoded. The browser will decode the value first, so that means value encoding must be done last.)

Code is similar to the following:

#ifdef librock_TYPICAL_USE_3_HTTP_astrcatUrlEncode
    char *asz = 0;
    char *asz2 = 0;
    librock_HTTP_astrcatUrlEncode(&asz,"field",5);
    librock_astrcat(&asz,"=");
    librock_HTTP_astrcatUrlEncode(&asz,"value",5);

    librock_astrcat(&asz,"&");
    librock_HTTP_astrcatUrlEncode(&asz,"field",5);
    librock_astrcat(&asz,"=");
    librock_HTTP_astrcatUrlEncode(&asz,"value",5);

    librock_HTML_astrcatValueEncode(&asz2,asz,strlen(asz));
    printf("<A HREF=\"%s\">\n",asz2);

    librock_astrfree(&asz);
    librock_astrfree(&asz2);
#endif
See also librock_HTTP_astrSetFieldUrlEncode(), if field names are unique in the query string.
 */

USES

    isalnum()
librock_chisel _usesrc "text/astring.c"
    librock_astrn0cat()


LICENSE

  Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com
  Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block.
  License text in <librock/license/librock.txt> librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45

Source Code

./spec/infotool/www/html.c (implementation, plus source of this manual page)

Tests and Supported Platform Types

This is a representative sample. Librock code is highly portable. For a particular platform not reported here, request paid support

librock_HTTP_astrcatUrlEncode passed tests in thtml (Unix/Linux/BSD: 2002/08/08 sys=FreeBSD using gcc)
librock_HTTP_astrcatUrlEncode passed tests in thtml (WIN32: 2002/08/08 sys=NT 4.0 using MSVC)


This software is part of Librock

Rapid reuse, without rework. Details
This page copyright (C) 2002-2003 Forrest J. Cavalier III, d-b-a Mib Software, Saylorsburg PA 18353, USA

Verbatim copying and distribution of this generated page is permitted in any medium provided that no changes are made.
(The source of this manual page may be covered by a more permissive license which allows modifications.)

Want to help? We welcome comments, patches. -- Need help? Request paid support.